From ef4ea84cb5d5dd8eb29705383ca282d6195acc01 Mon Sep 17 00:00:00 2001 From: Starlight220 <53231611+Starlight220@users.noreply.github.com> Date: Mon, 14 Jun 2021 02:05:14 +0300 Subject: [PATCH] [commands] Change grouping decorator impl to flatten nested group structures (#3335) --- .../wpi/first/wpilibj2/command/Command.java | 29 ++++++++++++----- .../command/ParallelCommandGroup.java | 8 ++++- .../command/ParallelDeadlineGroup.java | 6 ++++ .../wpilibj2/command/ParallelRaceGroup.java | 6 ++++ .../wpilibj2/command/PerpetualCommand.java | 5 +++ .../command/SequentialCommandGroup.java | 24 ++++++++++++++ .../cpp/frc2/command/PerpetualCommand.cpp | 4 +++ .../frc2/command/SequentialCommandGroup.cpp | 32 +++++++++++++++++++ .../native/include/frc2/command/Command.h | 16 +++++----- .../include/frc2/command/PerpetualCommand.h | 2 ++ .../frc2/command/SequentialCommandGroup.h | 9 ++++++ .../wpilibj2/command/WaitCommandTest.java | 2 ++ 12 files changed, 126 insertions(+), 17 deletions(-) diff --git a/wpilibNewCommands/src/main/java/edu/wpi/first/wpilibj2/command/Command.java b/wpilibNewCommands/src/main/java/edu/wpi/first/wpilibj2/command/Command.java index b37a17054e..c5466a542e 100644 --- a/wpilibNewCommands/src/main/java/edu/wpi/first/wpilibj2/command/Command.java +++ b/wpilibNewCommands/src/main/java/edu/wpi/first/wpilibj2/command/Command.java @@ -72,7 +72,7 @@ public interface Command { * @return the command with the timeout added */ default ParallelRaceGroup withTimeout(double seconds) { - return new ParallelRaceGroup(this, new WaitCommand(seconds)); + return raceWith(new WaitCommand(seconds)); } /** @@ -91,7 +91,7 @@ public interface Command { * @return the command with the interrupt condition added */ default ParallelRaceGroup withInterrupt(BooleanSupplier condition) { - return new ParallelRaceGroup(this, new WaitUntilCommand(condition)); + return raceWith(new WaitUntilCommand(condition)); } /** @@ -108,7 +108,23 @@ public interface Command { * @return the decorated command */ default SequentialCommandGroup beforeStarting(Runnable toRun, Subsystem... requirements) { - return new SequentialCommandGroup(new InstantCommand(toRun, requirements), this); + return beforeStarting(new InstantCommand(toRun, requirements)); + } + + /** + * Decorates this command with another command to run before this command starts. + * + *
Note: This decorator works by composing this command within a CommandGroup. The command
+ * cannot be used independently after being decorated, or be re-decorated with a different
+ * decorator, unless it is manually cleared from the list of grouped commands with {@link
+ * CommandGroupBase#clearGroupedCommand(Command)}. The decorated command can, however, be further
+ * decorated without issue.
+ *
+ * @param before the command to run before this one
+ * @return the decorated command
+ */
+ default SequentialCommandGroup beforeStarting(Command before) {
+ return new SequentialCommandGroup(before, this);
}
/**
@@ -125,7 +141,7 @@ public interface Command {
* @return the decorated command
*/
default SequentialCommandGroup andThen(Runnable toRun, Subsystem... requirements) {
- return new SequentialCommandGroup(this, new InstantCommand(toRun, requirements));
+ return andThen(new InstantCommand(toRun, requirements));
}
/**
@@ -266,10 +282,7 @@ public interface Command {
}
/**
- * Whether the command requires a given subsystem. Named "hasRequirement" rather than "requires"
- * to avoid confusion with {@link
- * edu.wpi.first.wpilibj.command.Command#requires(edu.wpi.first.wpilibj.command.Subsystem)} - this
- * may be able to be changed in a few years.
+ * Whether the command requires a given subsystem.
*
* @param requirement the subsystem to inquire about
* @return whether the subsystem is required
diff --git a/wpilibNewCommands/src/main/java/edu/wpi/first/wpilibj2/command/ParallelCommandGroup.java b/wpilibNewCommands/src/main/java/edu/wpi/first/wpilibj2/command/ParallelCommandGroup.java
index 5c6fe1ee66..7195606e19 100644
--- a/wpilibNewCommands/src/main/java/edu/wpi/first/wpilibj2/command/ParallelCommandGroup.java
+++ b/wpilibNewCommands/src/main/java/edu/wpi/first/wpilibj2/command/ParallelCommandGroup.java
@@ -86,11 +86,17 @@ public class ParallelCommandGroup extends CommandGroupBase {
@Override
public boolean isFinished() {
- return !m_commands.values().contains(true);
+ return !m_commands.containsValue(true);
}
@Override
public boolean runsWhenDisabled() {
return m_runWhenDisabled;
}
+
+ @Override
+ public ParallelCommandGroup alongWith(Command... parallel) {
+ addCommands(parallel);
+ return this;
+ }
}
diff --git a/wpilibNewCommands/src/main/java/edu/wpi/first/wpilibj2/command/ParallelDeadlineGroup.java b/wpilibNewCommands/src/main/java/edu/wpi/first/wpilibj2/command/ParallelDeadlineGroup.java
index 06539f0f52..d444991742 100644
--- a/wpilibNewCommands/src/main/java/edu/wpi/first/wpilibj2/command/ParallelDeadlineGroup.java
+++ b/wpilibNewCommands/src/main/java/edu/wpi/first/wpilibj2/command/ParallelDeadlineGroup.java
@@ -117,4 +117,10 @@ public class ParallelDeadlineGroup extends CommandGroupBase {
public boolean runsWhenDisabled() {
return m_runWhenDisabled;
}
+
+ @Override
+ public ParallelDeadlineGroup deadlineWith(Command... parallel) {
+ addCommands(parallel);
+ return this;
+ }
}
diff --git a/wpilibNewCommands/src/main/java/edu/wpi/first/wpilibj2/command/ParallelRaceGroup.java b/wpilibNewCommands/src/main/java/edu/wpi/first/wpilibj2/command/ParallelRaceGroup.java
index 434ee38fcd..2d798852f5 100644
--- a/wpilibNewCommands/src/main/java/edu/wpi/first/wpilibj2/command/ParallelRaceGroup.java
+++ b/wpilibNewCommands/src/main/java/edu/wpi/first/wpilibj2/command/ParallelRaceGroup.java
@@ -86,4 +86,10 @@ public class ParallelRaceGroup extends CommandGroupBase {
public boolean runsWhenDisabled() {
return m_runWhenDisabled;
}
+
+ @Override
+ public ParallelRaceGroup raceWith(Command... parallel) {
+ addCommands(parallel);
+ return this;
+ }
}
diff --git a/wpilibNewCommands/src/main/java/edu/wpi/first/wpilibj2/command/PerpetualCommand.java b/wpilibNewCommands/src/main/java/edu/wpi/first/wpilibj2/command/PerpetualCommand.java
index 9291f64a5f..af219dc523 100644
--- a/wpilibNewCommands/src/main/java/edu/wpi/first/wpilibj2/command/PerpetualCommand.java
+++ b/wpilibNewCommands/src/main/java/edu/wpi/first/wpilibj2/command/PerpetualCommand.java
@@ -50,4 +50,9 @@ public class PerpetualCommand extends CommandBase {
public boolean runsWhenDisabled() {
return m_command.runsWhenDisabled();
}
+
+ @Override
+ public PerpetualCommand perpetually() {
+ return this;
+ }
}
diff --git a/wpilibNewCommands/src/main/java/edu/wpi/first/wpilibj2/command/SequentialCommandGroup.java b/wpilibNewCommands/src/main/java/edu/wpi/first/wpilibj2/command/SequentialCommandGroup.java
index 492e3169e1..38dff8d228 100644
--- a/wpilibNewCommands/src/main/java/edu/wpi/first/wpilibj2/command/SequentialCommandGroup.java
+++ b/wpilibNewCommands/src/main/java/edu/wpi/first/wpilibj2/command/SequentialCommandGroup.java
@@ -92,4 +92,28 @@ public class SequentialCommandGroup extends CommandGroupBase {
public boolean runsWhenDisabled() {
return m_runWhenDisabled;
}
+
+ @Override
+ public SequentialCommandGroup beforeStarting(Command before) {
+ // store all the commands
+ var commands = new ArrayList