private void checkForCompletion(final GraphProcess process) { if (!process.hasActiveTokens() && process.isArcTokenQueueEmpty() && asyncQueue.isEmpty()) { process.setState(ProcessState.PendingCompletion); fireEvent(ProcessEvent.newCompletedEvent(this, process)); finalizeComplete(process); } }
private void completeExecuteArc(GraphProcess process, Node targetNode, List<ArcToken> tokens) { NodeToken nodeToken = getFactory().newNodeToken(process, targetNode, tokens); process.addNodeToken(nodeToken); // Add new node token to add the token sets which its generating arc tokens are members of Set<TokenSet> tokenSets = new HashSet<TokenSet>(); for (ArcToken token : tokens) { for (ArcTokenSetMember setMember : token.getTokenSetMemberships()) { TokenSet tokenSet = setMember.getTokenSet(); if (!tokenSet.isComplete() && !tokenSets.contains(tokenSet)) { tokenSets.add(tokenSet); NodeTokenSetMember newSetMember = getFactory().newNodeTokenSetMember(tokenSet, nodeToken, setMember.getMemberIndex()); tokenSet.getActiveNodeTokens(this).add(nodeToken); nodeToken.getTokenSetMemberships().add(newSetMember); } } } fireEvent(NodeTokenEvent.newCreatedEvent(this, nodeToken)); for (ArcToken token : tokens) { process.removeActiveArcToken(token); if (token.isPending()) { token.markProcessed(this); } token.markComplete(this, nodeToken); fireEvent(ArcTokenEvent.newCompletedEvent(this, token)); } executeNode(process, nodeToken); }
@Override public void finalizeComplete(GraphProcess process) { process.setState(ProcessState.Completed); NodeToken parentToken = process.getParentToken(); if (parentToken != null) { Engine engine = getParentEngine() == null ? newEngine(false) : getParentEngine(); engine.complete(parentToken, Arc.DEFAULT_ARC); } }
@Override public void complete(NodeToken token, String arcName) { GraphProcess process = token.getProcess(); completeNodeExecution(token, arcName, false); if (process.isExecuting() && !arcExecutionStarted) { executeQueuedArcTokens(process); } }
@Override public void executeQueuedArcTokens(final GraphProcess process) { arcExecutionStarted = true; try { while (!process.isArcTokenQueueEmpty()) { executeArc(process, process.dequeueArcTokenForExecution()); } checkForCompletion(process); } finally { arcExecutionStarted = false; drainAsyncQueue(process); } }
protected void completeNodeExecution( final NodeToken token, final String arcName, final boolean asynchronous) { GraphProcess process = token.getProcess(); if (!process.isExecuting() || token.isComplete()) { return; } completeNodeToken(process, token, arcName); for (Arc arc : process.getGraph().getOutputArcs(token.getNode(), arcName)) { ArcToken arcToken = generateArcToken(process, arc, token); finishNewArcTokenProcessing(process, arcToken, asynchronous); } }
@Override public void removeExecutionListener( final GraphProcess process, final ExecutionListener listener, final ExecutionEventType... eventTypes) { process.getEventQueue().removeListener(this, listener, eventTypes); }
@Override public void removeExecutionListener( GraphProcess process, ExecutionListener listener, ExecutionEventType... eventTypes) { process.getEventQueue().removeListener(this, listener, eventTypes); List<ExecutionEventType> types = eventTypes == null ? null : Arrays.asList(eventTypes); for (HibProcessListener hibListener : ((HibGraphProcess) process).getListeners()) { if (process.equals(hibListener.getProcess()) && (eventTypes == null || eventTypes.length == 0 || types.contains(hibListener.getEventType()))) { session.delete(hibListener); } } }
public void completeWithNewTokenSet( final NodeToken token, final String arcName, final String tokenSetName, final int numberOfTokens, final boolean asynchronous, final Env initialEnv, final Map<String, List<?>> initialMemberEnv) { GraphProcess process = token.getProcess(); if (!process.isExecuting() || token.isComplete()) { return; } completeNodeToken(process, token, arcName); List<? extends Arc> outArcs = process.getGraph().getOutputArcs(token.getNode(), arcName); if (!outArcs.isEmpty()) { TokenSet tokenSet = getFactory().newTokenSet(process, tokenSetName, numberOfTokens); if (initialEnv != null) { tokenSet.getEnv().importEnv(initialEnv); } if (initialMemberEnv != null) { TokenSetMemberEnv memberEnv = tokenSet.getMemberEnv(); for (Map.Entry<String, List<?>> entry : initialMemberEnv.entrySet()) { memberEnv.setAttribute(entry.getKey(), entry.getValue()); } } for (int memberIndex = 0; memberIndex < numberOfTokens; memberIndex++) { for (Arc arc : outArcs) { ArcToken arcToken = generateArcToken(process, arc, token); ArcTokenSetMember setMember = getFactory().newArcTokenSetMember(tokenSet, arcToken, memberIndex); tokenSet.getActiveArcTokens(this).add(arcToken); arcToken.getTokenSetMemberships().add(setMember); finishNewArcTokenProcessing(process, arcToken, asynchronous); } } } }
private void finishNewArcTokenProcessing( final GraphProcess process, final ArcToken arcToken, final boolean asynchronous) { fireEvent(ArcTokenEvent.newCreatedEvent(this, arcToken)); if (asynchronous && arcExecutionStarted) { asyncQueue.add(arcToken); } else { process.enqueueArcTokenForExecution(arcToken); } }
private void completeNodeToken( final GraphProcess process, final NodeToken token, final String arcName) { process.removeActiveNodeToken(token); token.markComplete(this); // If the node was skipped, we already sent a 'skipped' event and don't want to // send another 'completed' event. if (token.getGuardAction() != GuardAction.SkipNode) { fireEvent(NodeTokenEvent.newCompletedEvent(this, token, arcName)); } }
private void executeArc(GraphProcess process, ArcToken token) { if (token.isPending()) { token.markProcessed(this); process.addActiveArcToken(token); Node targetNode = token.getArc().getEndNode(); JoinResult result = targetNode.getJoinStrategy().performJoin(this, process, token); if (result.isJoinComplete()) { completeExecuteArc(process, targetNode, result.getArcTokensCompletingJoin()); } } }
protected void executeNode(GraphProcess process, NodeToken token) { GuardResponse response = token.getNode().guard(this, token); token.recordGuardAction(this, response.getGuardAction()); switch (response.getGuardAction()) { case AcceptToken: process.addActiveNodeToken(token); fireEvent(NodeTokenEvent.newAcceptedEvent(this, token)); token.getNode().execute(this, token); break; case DiscardToken: token.markComplete(this); fireEvent(NodeTokenEvent.newDiscardedEvent(this, token)); break; case SkipNode: process.addActiveNodeToken(token); fireEvent(NodeTokenEvent.newSkippedEvent(this, token, response.getExitArcForSkip())); complete(token, response.getExitArcForSkip()); break; } }
@Override public void startProcess(GraphProcess process) { process.setState(ProcessState.Executing); fireEvent(ProcessEvent.newStartedEvent(this, process)); arcExecutionStarted = true; try { for (Node startNode : process.getGraph().getStartNodes()) { NodeToken startToken = getFactory().newNodeToken(process, startNode, new ArrayList<ArcToken>(0)); process.addNodeToken(startToken); executeNode(process, startToken); } executeQueuedArcTokens(process); } finally { arcExecutionStarted = false; drainAsyncQueue(process); } if (process.isExecuting()) { checkForCompletion(process); } }
@Override public void addExecutionListener( GraphProcess process, ExecutionListener listener, ExecutionEventType... eventTypes) { if (eventTypes == null || listener == null) { return; } for (ExecutionEventType eventType : eventTypes) { if (eventType != null) { HibProcessListener hibListener = new HibProcessListener(listener.getClass().getName(), eventType, process); session.save(hibListener); } } process.getEventQueue().addListener(this, listener, eventTypes); listenerCache.ensureContainsListenerType(listener); }
@Override public void cancelProcess(GraphProcess process) { process.setState(ProcessState.PendingCancel); fireEvent(ProcessEvent.newCanceledEvent(this, process)); finalizeCancel(process); }
@Override public void finalizeCancel(GraphProcess process) { process.setState(ProcessState.Canceled); }
private void drainAsyncQueue(final GraphProcess process) { while (!asyncQueue.isEmpty()) { process.enqueueArcTokenForExecution(asyncQueue.remove(0)); } }