Search in sources :

Example 1 with Transition

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;
}
Also used : ArrayList(java.util.ArrayList) Transition(org.cristalise.kernel.lifecycle.instance.stateMachine.Transition) Job(org.cristalise.kernel.entity.agent.Job)

Example 2 with Transition

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);
}
Also used : ObjectNotFoundException(org.cristalise.kernel.common.ObjectNotFoundException) Transition(org.cristalise.kernel.lifecycle.instance.stateMachine.Transition) InvalidDataException(org.cristalise.kernel.common.InvalidDataException) ObjectAlreadyExistsException(org.cristalise.kernel.common.ObjectAlreadyExistsException) InvalidDataException(org.cristalise.kernel.common.InvalidDataException) PersistencyException(org.cristalise.kernel.common.PersistencyException) ObjectNotFoundException(org.cristalise.kernel.common.ObjectNotFoundException) CannotManageException(org.cristalise.kernel.common.CannotManageException) AccessRightsException(org.cristalise.kernel.common.AccessRightsException) InvalidAgentPathException(org.cristalise.kernel.lookup.InvalidAgentPathException) InvalidTransitionException(org.cristalise.kernel.common.InvalidTransitionException)

Example 3 with Transition

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());
        }
    }
}
Also used : AccessRightsException(org.cristalise.kernel.common.AccessRightsException) Transition(org.cristalise.kernel.lifecycle.instance.stateMachine.Transition) InvalidDataException(org.cristalise.kernel.common.InvalidDataException) ObjectAlreadyExistsException(org.cristalise.kernel.common.ObjectAlreadyExistsException) InvalidDataException(org.cristalise.kernel.common.InvalidDataException) PersistencyException(org.cristalise.kernel.common.PersistencyException) ObjectNotFoundException(org.cristalise.kernel.common.ObjectNotFoundException) CannotManageException(org.cristalise.kernel.common.CannotManageException) AccessRightsException(org.cristalise.kernel.common.AccessRightsException) InvalidAgentPathException(org.cristalise.kernel.lookup.InvalidAgentPathException) InvalidTransitionException(org.cristalise.kernel.common.InvalidTransitionException)

Example 4 with Transition

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;
}
Also used : State(org.cristalise.kernel.lifecycle.instance.stateMachine.State) Outcome(org.cristalise.kernel.persistency.outcome.Outcome) Viewpoint(org.cristalise.kernel.persistency.outcome.Viewpoint) Schema(org.cristalise.kernel.persistency.outcome.Schema) Transition(org.cristalise.kernel.lifecycle.instance.stateMachine.Transition) InvalidDataException(org.cristalise.kernel.common.InvalidDataException) PersistencyException(org.cristalise.kernel.common.PersistencyException) History(org.cristalise.kernel.events.History) Viewpoint(org.cristalise.kernel.persistency.outcome.Viewpoint)

Example 5 with Transition

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.");
    }
}
Also used : TreeSet(java.util.TreeSet) ObjectNotFoundException(org.cristalise.kernel.common.ObjectNotFoundException) Transition(org.cristalise.kernel.lifecycle.instance.stateMachine.Transition) InvalidDataException(org.cristalise.kernel.common.InvalidDataException)

Aggregations

Transition (org.cristalise.kernel.lifecycle.instance.stateMachine.Transition)6 InvalidDataException (org.cristalise.kernel.common.InvalidDataException)4 InvalidTransitionException (org.cristalise.kernel.common.InvalidTransitionException)3 ObjectNotFoundException (org.cristalise.kernel.common.ObjectNotFoundException)3 PersistencyException (org.cristalise.kernel.common.PersistencyException)3 AccessRightsException (org.cristalise.kernel.common.AccessRightsException)2 CannotManageException (org.cristalise.kernel.common.CannotManageException)2 ObjectAlreadyExistsException (org.cristalise.kernel.common.ObjectAlreadyExistsException)2 InvalidAgentPathException (org.cristalise.kernel.lookup.InvalidAgentPathException)2 ArrayList (java.util.ArrayList)1 TreeSet (java.util.TreeSet)1 Job (org.cristalise.kernel.entity.agent.Job)1 History (org.cristalise.kernel.events.History)1 GraphPoint (org.cristalise.kernel.graph.model.GraphPoint)1 State (org.cristalise.kernel.lifecycle.instance.stateMachine.State)1 Outcome (org.cristalise.kernel.persistency.outcome.Outcome)1 Schema (org.cristalise.kernel.persistency.outcome.Schema)1 Viewpoint (org.cristalise.kernel.persistency.outcome.Viewpoint)1