Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,10 @@ public ParallelGroupBuilder until(BooleanSupplier condition) {
* @return The built group
*/
public ParallelGroup named(String name) {
return named(name, false);
}

private ParallelGroup named(String name, boolean useAutomaticName) {
requireNonNullParam(name, "name", "ParallelGroupBuilder.named");

var group = new ParallelGroup(name, m_requiredCommands, m_optionalCommands);
Expand All @@ -109,9 +113,10 @@ public ParallelGroup named(String name) {
}

// We have a custom end condition, so we need to wrap the group in a race
return new ParallelGroupBuilder()
.optional(group, Command.waitUntil(m_endCondition).named("Until Condition"))
.named(name);
var out =
new ParallelGroupBuilder()
.optional(group, Command.waitUntil(m_endCondition).named("Until Condition"));
return useAutomaticName ? out.withAutomaticName() : out.named(name);
}

/**
Expand All @@ -130,15 +135,15 @@ public ParallelGroup withAutomaticName() {

if (m_requiredCommands.isEmpty()) {
// No required commands, pure race
return named(optional);
return named(optional, true);
} else if (m_optionalCommands.isEmpty()) {
// Everything required
return named(required);
return named(required, true);
} else {
// Some form of deadline
// eg "[(CommandA & CommandB) * (CommandX | CommandY | ...)]"
String name = "[%s * %s]".formatted(required, optional);
return named(name);
return named(name, true);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -82,16 +82,21 @@ public SequentialGroupBuilder until(BooleanSupplier endCondition) {
* @return The built command
*/
public Command named(String name) {
return named(name, false);
}

private Command named(String name, boolean useAutomaticName) {
var seq = new SequentialGroup(name, m_steps);
if (m_endCondition == null) {
// No custom end condition, return the group as is
return seq;
}

var out =
new ParallelGroupBuilder()
.optional(seq, Command.waitUntil(m_endCondition).named("Until Condition"));
// We have a custom end condition, so we need to wrap the group in a race
return new ParallelGroupBuilder()
.optional(seq, Command.waitUntil(m_endCondition).named("Until Condition"))
.named(name);
return useAutomaticName ? out.withAutomaticName() : out.named(name);
}

/**
Expand All @@ -101,6 +106,6 @@ public Command named(String name) {
* @return The built command
*/
public Command withAutomaticName() {
return named(m_steps.stream().map(Command::name).collect(Collectors.joining(" -> ")));
return named(m_steps.stream().map(Command::name).collect(Collectors.joining(" -> ")), true);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,13 @@ void namedWithNull() {
var builder = new ParallelGroupBuilder();
assertThrows(NullPointerException.class, () -> builder.named(null));
}

@Test
void automaticNameWithUntil() {
var a = Command.noRequirements(Coroutine::park).named("A");
var b = Command.noRequirements(Coroutine::park).named("B");

var group = new ParallelGroupBuilder().requiring(a, b).until(() -> false).withAutomaticName();
assertEquals("((A & B) | Until Condition)", group.name());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -105,4 +105,13 @@ void automaticNameWithMultipleCommands() {
var sequence = builder.andThen(c1, c2).withAutomaticName();
assertEquals("C1 -> C2", sequence.name());
}

@Test
void automaticNameWithUntilCondition() {
var c1 = Command.noRequirements(Coroutine::park).named("C1");
var c2 = Command.noRequirements(Coroutine::park).named("C2");
var builder = new SequentialGroupBuilder();
var sequence = builder.andThen(c1, c2).until(() -> true).withAutomaticName();
assertEquals("(C1 -> C2 | Until Condition)", sequence.name());
}
}
Loading