li.strolch.execution.policy.ReservationExection Maven / Gradle / Ivy
package li.strolch.execution.policy;
import li.strolch.agent.api.ComponentContainer;
import li.strolch.exception.StrolchModelException;
import li.strolch.model.Locator;
import li.strolch.model.Resource;
import li.strolch.model.State;
import li.strolch.model.activity.Action;
import li.strolch.model.parameter.BooleanParameter;
import li.strolch.model.timevalue.impl.FloatValue;
import li.strolch.model.timevalue.impl.ValueChange;
import li.strolch.persistence.api.StrolchTransaction;
/**
*
* This extension of the {@link DurationExecution} overrides the {@link #isExecutable(Action)} method and validates that
* the {@link Resource} to which the {@link Action} is attached, has a {@link BooleanParameter} reserved
* and only allows execution if the value is false, in which case the {@link #toExecution(Action)} method sets the value
* to true, and the {@link #toExecuted(Action)} method returns the value to false.
*
*
*
* Note: the reservation is done for {@link Action} of type {@link #TYPE_RESERVE} and releasing is done for
* {@link Action} of type {@link #TYPE_RELEASE}
*
*
* @author Robert von Burg
*/
public class ReservationExection extends DurationExecution {
public static final String PARAM_RESERVED = "reserved";
public static final String BAG_PARAMETERS = "parameters";
public static final String TYPE_RESERVE = "Reserve";
public static final String TYPE_RELEASE = "Release";
public ReservationExection(ComponentContainer container, StrolchTransaction tx) {
super(container, tx);
}
@Override
public boolean isExecutable(Action action) {
tx().lock(getResource(action));
// only check if reserve
if (!action.getType().equals(TYPE_RESERVE) && !action.getType().equals(TYPE_RELEASE)) {
// otherwise delegate to super class
return super.isExecutable(action);
}
if (action.getType().equals(TYPE_RESERVE))
return !isReserved(action);
else
return isReserved(action);
}
protected boolean isReserved(Action action) {
// get resource
Resource resource = getResource(action);
if (!resource.hasParameter(BAG_PARAMETERS, PARAM_RESERVED))
throw new StrolchModelException(
"Parameter " + PARAM_RESERVED + " on bag " + BAG_PARAMETERS + " missing on " + resource
.getLocator());
BooleanParameter reservedP = resource.getParameter(BAG_PARAMETERS, PARAM_RESERVED);
return reservedP.getValue();
}
@Override
public void toExecution(Action action) {
tx().lock(getResource(action));
// only do if reserve or release
boolean isReserve = action.getType().equals(TYPE_RESERVE);
boolean isRelease = action.getType().equals(TYPE_RELEASE);
if (!isReserve && !isRelease) {
// otherwise delegate to super class
super.toExecution(action);
return;
}
setReservation(action);
String realmName = tx().getRealmName();
Locator locator = action.getLocator();
getDelayedExecutionTimer().execute(realmName, getContainer(), locator, 0L);
setActionState(action, State.EXECUTION);
}
@Override
public void toExecuted(Action action) {
tx().lock(getResource(action));
// only do if reserve or release
boolean isReserve = action.getType().equals(TYPE_RESERVE);
boolean isRelease = action.getType().equals(TYPE_RELEASE);
if (!isReserve && !isRelease) {
// otherwise delegate to super class
super.toExecuted(action);
return;
}
setReservation(action);
setActionState(action, State.EXECUTED);
FloatValue value = new FloatValue(isReserve ? 1.0D : 0.0D);
action.addChange(new ValueChange<>(System.currentTimeMillis(), value, ""));
}
public void setReservation(Action action) {
Resource resource = getResource(action);
boolean reserved = action.getType().equals(TYPE_RESERVE);
// release the resource
BooleanParameter reservedP = resource.getParameter(BAG_PARAMETERS, PARAM_RESERVED);
reservedP.setValue(reserved);
// save changes
tx().update(resource);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy