diff --git a/service/src/main/java/li/strolch/execution/policy/DurationExecution.java b/service/src/main/java/li/strolch/execution/policy/DurationExecution.java index 275759158..be324ff65 100644 --- a/service/src/main/java/li/strolch/execution/policy/DurationExecution.java +++ b/service/src/main/java/li/strolch/execution/policy/DurationExecution.java @@ -2,35 +2,44 @@ package li.strolch.execution.policy; import static li.strolch.model.StrolchModelConstants.PolicyConstants.PARAM_DURATION; +import li.strolch.model.State; import li.strolch.model.activity.Action; import li.strolch.persistence.api.StrolchTransaction; +import java.util.concurrent.TimeUnit; + /** *

* Simple Execution Policy which starts the execution immediately, i.e. set state to in execution and completes after * the {@link Action Action's} duration has passed. *

* + *

Sub classes can by pass the delaying of completion by directly calling + * {@link #setActionStateWithValueChange(Action, State, double)}. This is useful when calling any of the + * {@link #delayRandom(long, TimeUnit, Runnable)} methods

+ * * @author Robert von Burg */ public class DurationExecution extends SimpleExecution { - protected boolean delayToExecuted = true; - public DurationExecution(StrolchTransaction tx) { super(tx); } + /** + * Delays completion of the given action. + * + *

Sub classes can by pass the delaying of completion by directly calling + * {@link #setActionStateWithValueChange(Action, State, double)}

+ * + * @param action the action to start execution for + */ @Override public void toExecution(Action action) { - if (this.delayToExecuted) { - super.toExecution(action); - if (action.findObjectivesParam(PARAM_DURATION, true).isEmpty()) - toExecuted(action); - else - delayToExecuted(action); - } else { - super.toExecution(action); - } + super.toExecution(action); + if (action.findObjectivesParam(PARAM_DURATION, true).isEmpty()) + toExecuted(action); + else + delayToExecuted(action); } } diff --git a/service/src/main/java/li/strolch/execution/policy/SimpleExecution.java b/service/src/main/java/li/strolch/execution/policy/SimpleExecution.java index 288259bab..19a3689e1 100644 --- a/service/src/main/java/li/strolch/execution/policy/SimpleExecution.java +++ b/service/src/main/java/li/strolch/execution/policy/SimpleExecution.java @@ -39,8 +39,8 @@ public class SimpleExecution extends ExecutionPolicy { protected void startWarningTask(PeriodDuration duration, Action action, Supplier handler) { if (this.warningTask != null) { - logger.warn("There is already a warning task registered, for action " + action.getLocator() - + ". Cancelling and creating a new task..."); + logger.warn("There is already a warning task registered, for action " + action.getLocator() + + ". Cancelling and creating a new task..."); this.warningTask.cancel(true); this.warningTask = null; } @@ -68,10 +68,7 @@ public class SimpleExecution extends ExecutionPolicy { @Override public void toExecution(Action action) { - setActionState(action, State.EXECUTION); - - FloatValue value = new FloatValue(1.0D); - action.addChange(new ValueChange<>(System.currentTimeMillis(), value, "")); + setActionStateWithValueChange(action, State.EXECUTION, 1.0D); } @Override @@ -83,40 +80,33 @@ public class SimpleExecution extends ExecutionPolicy { public void toExecuted(Action action) { cancelWarningTask(); stop(); - - setActionState(action, State.EXECUTED); - - FloatValue value = new FloatValue(0.0D); - action.addChange(new ValueChange<>(System.currentTimeMillis(), value, "")); + setActionStateWithValueChange(action, State.EXECUTED, 0.0D); } @Override public void toStopped(Action action) { cancelWarningTask(); stop(); - - setActionState(action, State.STOPPED); - - FloatValue value = new FloatValue(0.0D); - action.addChange(new ValueChange<>(System.currentTimeMillis(), value, "")); + setActionStateWithValueChange(action, State.STOPPED, 0.0D); } @Override public void toError(Action action) { cancelWarningTask(); stop(); + setActionStateWithValueChange(action, State.ERROR, 0.0D); + } - setActionState(action, State.ERROR); - - FloatValue value = new FloatValue(0.0D); - action.addChange(new ValueChange<>(System.currentTimeMillis(), value, "")); + protected void setActionStateWithValueChange(Action action, State execution, double value) { + setActionState(action, execution); + action.addChange(new ValueChange<>(System.currentTimeMillis(), new FloatValue(value), "")); } protected void addMessage(LogMessage message) { switch (message.getSeverity()) { - case Info, Notification -> logger.info(message.getMessage(Locale.getDefault())); - case Warning -> logger.warn(message.getMessage(Locale.getDefault())); - case Error, Exception -> logger.error(message.getMessage(Locale.getDefault())); + case Info, Notification -> logger.info(message.getMessage(Locale.getDefault())); + case Warning -> logger.warn(message.getMessage(Locale.getDefault())); + case Error, Exception -> logger.error(message.getMessage(Locale.getDefault())); } if (getContainer().hasComponent(OperationsLog.class)) { OperationsLog operationsLog = getContainer().getComponent(OperationsLog.class);