use of org.cristalise.kernel.lifecycle.instance.stateMachine.Transition in project kernel by cristal-ise.
the class Activity method calculateJobsBase.
private ArrayList<Job> calculateJobsBase(AgentPath agent, ItemPath itemPath, boolean includeInactive) throws ObjectNotFoundException, InvalidDataException, InvalidAgentPathException {
Logger.msg(7, "Activity.calculateJobsBase() - act:" + getPath());
ArrayList<Job> jobs = new ArrayList<Job>();
Map<Transition, String> transitions;
if ((includeInactive || getActive()) && !getName().equals("domain")) {
transitions = getStateMachine().getPossibleTransitions(this, agent);
Logger.msg(7, "Activity.calculateJobsBase() - Got " + transitions.size() + " transitions.");
for (Transition transition : transitions.keySet()) {
Logger.msg(7, "Activity.calculateJobsBase() - Creating Job object for transition " + transition.getName());
jobs.add(new Job(this, itemPath, transition, agent, null, transitions.get(transition)));
}
}
return jobs;
}
use of org.cristalise.kernel.lifecycle.instance.stateMachine.Transition in project kernel by cristal-ise.
the class CompositeActivity method runNext.
@Override
public void runNext(AgentPath agent, ItemPath itemPath, Object locker) throws InvalidDataException {
if (!getStateMachine().getState(state).isFinished()) {
Transition trans = null;
try {
for (Transition possTran : getStateMachine().getPossibleTransitions(this, agent).keySet()) {
if (trans == null || (trans.isFinishing() && !possTran.isFinishing())) {
trans = possTran;
} else if (trans.isFinishing() == possTran.isFinishing()) {
Logger.warning("Unclear choice of transition possible from current state for Composite Activity '" + getName() + "'. Cannot automatically proceed.");
setActive(true);
return;
}
}
} catch (ObjectNotFoundException e) {
Logger.error(e);
throw new InvalidDataException("Problem calculating possible transitions for agent " + agent.toString());
}
if (trans == null) {
// current agent can't proceed
Logger.msg(3, "Not possible for the current agent to proceed with the Composite Activity '" + getName() + "'.");
setActive(true);
return;
} else {
// automatically execute the next outcome if it doesn't require an outcome.
if (trans.hasOutcome(getProperties()) || trans.hasScript(getProperties())) {
Logger.msg(3, "Composite activity '" + getName() + "' has script or schema defined. Cannot proceed automatically.");
setActive(true);
return;
}
try {
request(agent, null, itemPath, trans.getId(), null, locker);
if (// don't run next if we didn't finish
!trans.isFinishing())
return;
} catch (Exception e) {
Logger.error(e);
setActive(true);
throw new InvalidDataException("Problem completing composite activity: " + e.getMessage());
}
}
}
super.runNext(agent, itemPath, locker);
}
use of org.cristalise.kernel.lifecycle.instance.stateMachine.Transition in project kernel by cristal-ise.
the class CompositeActivity method run.
@Override
public void run(AgentPath agent, ItemPath itemPath, Object locker) throws InvalidDataException {
Logger.msg(8, "CompositeActivity.run() path:" + getPath() + " state:" + getState());
super.run(agent, itemPath, locker);
Transition autoStart = getAutoStart(agent, getStateMachine().getState(state));
if (autoStart != null) {
try {
request(agent, null, itemPath, autoStart.getId(), null, locker);
} catch (RuntimeException e) {
throw e;
} catch (AccessRightsException e) {
Logger.warning("Agent:" + agent + " didn't have permission to start the activity:" + getPath() + ", so leave it waiting");
return;
} catch (Exception e) {
Logger.error(e);
throw new InvalidDataException("Problem initializing composite activity: " + e.getMessage());
}
}
}
use of org.cristalise.kernel.lifecycle.instance.stateMachine.Transition in project kernel by cristal-ise.
the class Activity method request.
public String request(AgentPath agent, AgentPath delegate, ItemPath itemPath, int transitionID, String requestData, Object locker) throws AccessRightsException, InvalidTransitionException, InvalidDataException, ObjectNotFoundException, PersistencyException, ObjectAlreadyExistsException, ObjectCannotBeUpdated, CannotManageException, InvalidCollectionModification {
// Find requested transition
Transition transition = getStateMachine().getTransition(transitionID);
// Check if the transition is possible
String usedRole = transition.getPerformingRole(this, agent);
// Verify outcome
boolean storeOutcome = false;
if (transition.hasOutcome(getProperties())) {
if (StringUtils.isNotBlank(requestData))
storeOutcome = true;
else if (transition.getOutcome().isRequired())
throw new InvalidDataException("Transition requires outcome data, but none was given");
}
// Get new state
State oldState = getStateMachine().getState(this.state);
State newState = getStateMachine().traverse(this, transition, agent);
// Run extra logic in predefined steps here
String outcome = runActivityLogic(agent, itemPath, transitionID, requestData, locker);
// set new state and reservation
setState(newState.getId());
setBuiltInProperty(AGENT_NAME, transition.getReservation(this, agent));
try {
History hist = getWf().getHistory(locker);
if (storeOutcome) {
Schema schema = transition.getSchema(getProperties());
Outcome newOutcome = new Outcome(-1, outcome, schema);
// TODO: if we were ever going to validate outcomes on storage, it would be here.
// newOutcome.validateAndCheck();
String viewpoint = resolveViewpointName(newOutcome);
int eventID = hist.addEvent(agent, delegate, usedRole, getName(), getPath(), getType(), schema, getStateMachine(), transitionID, viewpoint).getID();
newOutcome.setID(eventID);
Gateway.getStorage().put(itemPath, newOutcome, locker);
// update specific view if defined
if (!viewpoint.equals("last")) {
Gateway.getStorage().put(itemPath, new Viewpoint(itemPath, schema, viewpoint, eventID), locker);
}
// update the default "last" view
Gateway.getStorage().put(itemPath, new Viewpoint(itemPath, schema, "last", eventID), locker);
updateItemProperties(itemPath, newOutcome, locker);
} else {
hist.addEvent(agent, delegate, usedRole, getName(), getPath(), getType(), getStateMachine(), transitionID);
}
} catch (PersistencyException ex) {
Logger.error(ex);
throw ex;
}
if (newState.isFinished() && !(getBuiltInProperty(BREAKPOINT).equals(Boolean.TRUE) && !oldState.isFinished())) {
runNext(agent, itemPath, locker);
}
DateUtility.setToNow(mStateDate);
pushJobsToAgents(itemPath);
return outcome;
}
use of org.cristalise.kernel.lifecycle.instance.stateMachine.Transition in project kernel by cristal-ise.
the class Activity method pushJobsToAgents.
/**
* Collects all Role names which are associated with this Activity and the Transitions of the current State,
* and ....
*
* @param itemPath
*/
protected void pushJobsToAgents(ItemPath itemPath) {
// Shall contain a set of unique role names
Set<String> roleNames = new TreeSet<String>();
String role = getCurrentAgentRole();
if (StringUtils.isNotBlank(role)) {
for (String r : role.split(",")) roleNames.add(r);
}
try {
for (Transition trans : getStateMachine().getState(getState()).getPossibleTransitions().values()) {
role = trans.getRoleOverride(getProperties());
if (StringUtils.isNotBlank(role))
roleNames.add(role);
}
Logger.msg(7, "Activity.pushJobsToAgents() - Pushing jobs to " + roleNames.size() + " roles");
for (String roleName : roleNames) {
pushJobsToAgents(itemPath, Gateway.getLookup().getRolePath(roleName));
}
} catch (InvalidDataException ex) {
Logger.warning("Activity.pushJobsToAgents() - " + ex.getMessage());
} catch (ObjectNotFoundException e) {
Logger.warning("Activity.pushJobsToAgents() - Activity role '" + role + "' not found.");
}
}
Aggregations