This commit is contained in:
Mark Wolters
2024-02-06 07:32:25 -04:00
parent 35c8538206
commit 9176d4f9e6

View File

@@ -33,70 +33,69 @@ import org.apache.logging.log4j.Logger;
import java.io.PrintWriter; import java.io.PrintWriter;
import java.io.Reader; import java.io.Reader;
import java.util.HashSet;
import java.util.List;
import java.util.Optional; import java.util.Optional;
@Service(value = NBBaseCommand.class,selector = "reset") @Service(value = NBBaseCommand.class,selector = "reset")
public class CMD_reset extends NBBaseCommand { public class CMD_reset extends NBBaseCommand {
public final static Logger logger = LogManager.getLogger("reset"); public final static Logger logger = LogManager.getLogger("reset");
public static final String DEFAULT_RATE = "100"; private static final HashSet<String> IGNORABLE = new HashSet<>(List.of("activity"));
public static final String DEFAULT_THREADS = "10";
public CMD_reset(NBBufferedContainer parentComponent, String scenarioName, String context) { public CMD_reset(NBBufferedContainer parentComponent, String scenarioName, String context) {
super(parentComponent, scenarioName, context); super(parentComponent, scenarioName, context);
} }
/**
* This command is used to restart the initial step activity in a scenario after optimization results have
* been determined by the previous steps. Any parameters that should be modified from the initially specified
* parameters for the activity should be specified on the command line, typically using bindings to reference
* the outputs of the previous steps, although this is not mandatory.
* @param params
* @param stdout
* @param stderr
* @param stdin
* @param controller
* @return null
*/
@Override @Override
public Object invoke(NBCommandParams params, PrintWriter stdout, PrintWriter stderr, Reader stdin, ContainerActivitiesController controller) { public Object invoke(NBCommandParams params, PrintWriter stdout, PrintWriter stderr, Reader stdin, ContainerActivitiesController controller) {
Optional<Activity> optionalActivity = Optional.ofNullable(params.get("activity")).flatMap(controller::getActivity); Optional<Activity> optionalActivity = Optional.ofNullable(params.get("activity")).flatMap(controller::getActivity);
if (params.get("activity")!=null && optionalActivity.isEmpty()) { if (params.get("activity")!=null && optionalActivity.isEmpty()) {
throw new RuntimeException("you specified activity '" + params.get("activity") + "' but it was not found."); throw new RuntimeException("you specified activity '" + params.get("activity") + "' but it was not found.");
} }
Activity flywheel = optionalActivity.or(controller::getSoloActivity) try (Activity flywheel = optionalActivity.or(controller::getSoloActivity)
.orElseThrow(() -> new RuntimeException("You didn't provide the name of an activity to attach to, nor was there a solo activity available in this context")); .orElseThrow(() -> new RuntimeException("You didn't provide the name of an activity to attach to, nor was there a solo activity available in this context"))) {
/* params.forEach((key, value) -> {
Set the CycleRateSpec. This should be found in params.get("rate") if the value from a previous step is specified. switch (key) {
If no value from a previous step is used, the original can be found in the flywheel activity definition. case "rate" -> {
If a value is passed in on the command line as opposed to specified in the yaml file it will override other values, so don't do this. logger.debug("Resetting rate to " + value + " cycles per second");
flywheel.onEvent(new ParamChange<>(new CycleRateSpec(Double.parseDouble(value), 1.1d, SimRateSpec.Verb.restart)));
}
case "threads" -> {
logger.debug("Resetting threads to " + value + " threads");
flywheel.onEvent(ParamChange.of(new SetThreads((int) Math.round(Double.parseDouble(value)))));
}
default -> {
if (!IGNORABLE.contains(key)) {
logger.debug("Resetting parameter: " + key + " to " + value);
flywheel.getActivityDef().getParams().put(key, value);
}
}
}
});
cli rate| yaml rate| reset rate| params rate| flywheel params rate // Get the original cycle count and re-apply it
null | 1 | 50 | 50 | 1 long cycles = Long.parseLong((String) flywheel.getActivityDef().getParams().get("cycles"));
null | null | 50 | 50 | null logger.debug("Resetting cycle count to " + cycles + " cycles");
null | 1 | null | null | 1 flywheel.getActivityDef().setEndCycle(cycles);
1 | 5 | 50 | 1 | 1
1 | null | 50 | 1 | 1
*/
String rateStr = params.hasMember("rate") ? params.get("rate") :
flywheel.getActivityDef().getParams().getOptionalString("rate").orElse(DEFAULT_RATE);
logger.debug("Resetting rate to " + rateStr + " cycles per second");
flywheel.onEvent(new ParamChange<>(new CycleRateSpec(Double.parseDouble(rateStr), 1.1d, SimRateSpec.Verb.restart)));
// Get the original cycle count and re-apply it //TODO: This needs to be reworked, but simply calling controller.start on the flywheel results in 2
long cycles = Long.parseLong((String) flywheel.getActivityDef().getParams().get("cycles")); // copies of the activity running simultaneously. This is a temporary workaround.
logger.debug("Resetting cycle count to " + cycles + " cycles"); SimFrameUtils.awaitActivity(flywheel);
flywheel.getActivityDef().setEndCycle(cycles); flywheel.getMotorDispenserDelegate().getMotor(flywheel.getActivityDef(), 0).run();
}
/*
Set the thread count. This should be found in params and the flywheel if the value from a previous step is specified.
If no value from a previous step is used, the original can be found in the flywheel activity definition.
If a value is passed in on the command line as opposed to specified in the yaml file it will be found in params
and the correct optimo thread count can be found in the flywheel, so in this case we go to the flywheel first
cli threads| yaml threads| reset threads| params threads| flywheel params threads
null | 1 | 50 | 50 | 50
null | null | 50 | 50 | 50
null | 1 | null | null | 1
1 | 5 | 50 | 1 | 50
1 | null | 50 | 1 | 50
*/
String threadStr = flywheel.getActivityDef().getParams().getOptionalString("threads")
.orElse(params.hasMember("threads") ? params.get("threads") : DEFAULT_THREADS);
logger.debug("Resetting threads to " + threadStr);
flywheel.onEvent(ParamChange.of(new SetThreads((int)Math.round(Double.parseDouble(threadStr)))));
SimFrameUtils.awaitActivity(flywheel);
flywheel.getMotorDispenserDelegate().getMotor(flywheel.getActivityDef(), 0).run();
return null; return null;
} }