Skip to content

Commit

Permalink
Improved updateRequirements on command so ConfigCommandStore should a…
Browse files Browse the repository at this point in the history
…lways have correct requirements
  • Loading branch information
amikhalev committed Apr 2, 2018
1 parent 1096103 commit f1356a5
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,14 @@ public void createCommandsFromConfig(ObjectNode json) throws ConfigException {
JsonNode config = json.get(command.getName());
configureCommand(command, config);
}
for (int i = 0; i < 10; i++) {
boolean wasUpdated;
do {
wasUpdated = false;
for (Map.Entry<String, Command> commandEntry : commandsMapCopy.entrySet()) {
Command command = commandEntry.getValue();
command.updateRequirements();
wasUpdated = wasUpdated || command.updateRequirements();
}
}
} while (wasUpdated);
}

public void configureCommand(Command command, JsonNode config) throws ConfigException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,12 @@ public boolean doesRequire(Subsystem subsystem) {
return requirements != null && requirements.contains(subsystem);
}

public void updateRequirements() {

/**
* Updates any requirements on this command to require all child commands
* @return If any requirements had to be added
*/
public boolean updateRequirements() {
return false;
}

private boolean isRequiring(Subsystem subsystem, CommandRunContext context) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,17 @@ private void updateValidStates() {
}

@Override
public void updateRequirements() {
public boolean updateRequirements() {
// A sequential command requires all subsystems required by all child commands
sequence.forEach(run -> {
run.command.updateRequirements();
if (run.command.getRequirements() != null)
boolean wasUpdated = false;
for (SequentialCommandRun run : sequence) {
wasUpdated = wasUpdated || run.command.updateRequirements();
int lengthBefore = getRequirements().size();
if (run.command.getRequirements() != null && !run.parallel)
requiresAll(run.command.getRequirements());
});
wasUpdated = wasUpdated || (lengthBefore != getRequirements().size());
}
return wasUpdated;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,9 +141,9 @@ public void configure(Config config) {
}

@Override
public void updateRequirements() {
public boolean updateRequirements() {
if (config == null) {
return;
return false;
}
List<String> commandNames = new ArrayList<>();
if (config.type == SelectorType.FIELD_CONFIGURATION) {
Expand All @@ -156,15 +156,19 @@ public void updateRequirements() {
} else {
logger.error("Invalid selector type: " + config.type);
}
boolean wasUpdated = false;
for (String commandName : commandNames) {
Command command;
try {
command = commandStore.getCommand(commandName);
} catch (IllegalArgumentException e) {
continue;
}
int lengthBefore = getRequirements().size();
requiresAll(command.getRequirements());
wasUpdated = wasUpdated || (lengthBefore != getRequirements().size());
}
return wasUpdated;
}

public enum SelectorType {
Expand Down

0 comments on commit f1356a5

Please sign in to comment.