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); }
private void shareTokenSets(final NodeToken newToken, final NodeToken origToken) { for (NodeTokenSetMember setMember : origToken.getTokenSetMemberships()) { TokenSet tokenSet = setMember.getTokenSet(); NodeTokenSetMember newSetMember = engine.getFactory().newNodeTokenSetMember(tokenSet, newToken, setMember.getMemberIndex()); newToken.getTokenSetMemberships().add(newSetMember); tokenSet.getActiveNodeTokens(engine).add(newToken); tokenSets.add(tokenSet); } }
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)); } }
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 visit(final NodeToken token) { if (!token.getNode().isBacktrackable(engine, token)) { throw new SarasvatiException( "Can not backtrack node name: " + token.getNode().getName() + "id: " + token.getNode().getId()); } if (token.getChildTokens().isEmpty()) { queue.add(token); } visited.add(token); }
private ArcToken generateArcToken( final GraphProcess process, final Arc arc, final NodeToken token) { ArcToken arcToken = getFactory().newArcToken(process, arc, ExecutionType.Forward, token); token.getChildTokens().add(arcToken); for (NodeTokenSetMember setMember : token.getTokenSetMemberships()) { TokenSet tokenSet = setMember.getTokenSet(); if (!tokenSet.isComplete()) { ArcTokenSetMember newSetMember = getFactory().newArcTokenSetMember(tokenSet, arcToken, setMember.getMemberIndex()); tokenSet.getActiveArcTokens(this).add(arcToken); arcToken.getTokenSetMemberships().add(newSetMember); } } return arcToken; }
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 NodeToken backtrackCompletedToken( final NodeToken token, final ExecutionType executionType) { token.markBacktracked(); NodeTokenEvent.fireBacktrackedEvent(engine, token); List<ArcToken> parents = new ArrayList<ArcToken>(token.getChildTokens().size()); for (ArcToken childToken : token.getChildTokens()) { ArcToken parent = arcTokenMap.get(childToken); if (parent == null) { throw new RuntimeException("No backtrack found for: " + childToken); } parents.add(parent); } NodeToken backtrackToken = engine .getFactory() .newNodeToken(token.getProcess(), token.getNode(), executionType, parents, token); NodeTokenEvent.fireCreatedEvent(engine, backtrackToken); token.getProcess().addNodeToken(backtrackToken); shareTokenSets(backtrackToken, token); for (ArcToken parent : parents) { token.getProcess().removeActiveArcToken(parent); parent.markBacktracked(); ArcTokenEvent.fireBacktrackedEvent(engine, parent); parent.markComplete(backtrackToken); ArcTokenEvent.fireCompletedEvent(engine, parent); } return backtrackToken; }
public NodeToken backtrack() { backtrackLeafArcTokens(); NodeToken resultToken = null; while (!queue.isEmpty()) { NodeToken token = queue.first(); queue.remove(token); if (token.getExecutionType().isBacktracked()) { continue; } token.getNode().backtrack(engine, token); boolean isDestination = token.equals(destinationToken); if (isDestination) { resultToken = backtrackCompletedToken(token, ExecutionType.Forward); } else { NodeToken backtrackToken = backtrackToken(token); if (backtrackToken != token) { backtrackToken.markBacktracked(); NodeTokenEvent.fireBacktrackedEvent(engine, backtrackToken); backtrackToken.markComplete(); NodeTokenEvent.fireCompletedEvent(engine, backtrackToken, null); } } } reactivateTokenSets(); return resultToken; }
@Override public void complete(NodeToken token, String arcName) { GraphProcess process = token.getProcess(); completeNodeExecution(token, arcName, false); if (process.isExecuting() && !arcExecutionStarted) { executeQueuedArcTokens(process); } }
@Override public HibNodeToken newNodeToken( final GraphProcess process, final Node node, final ExecutionType executionType, final List<ArcToken> parents, final NodeToken envSource) { // Here we setup the token attributes for the new node // If the node has no predecessors, it will have no attributes // If it has only one processor (or only one processor with attributes) // it will inherit the attributes of that one node // Otherwise, the attributes of all predecessor nodes will get merged into // a single set. List<ArcToken> envParents = envSource == null ? parents : envSource.getParentTokens(); HibNodeToken attrSetToken = null; Map<String, String> attrMap = new HashMap<String, String>(); Map<String, Object> transientAttributes = new HashMap<String, Object>(); boolean isMerge = false; for (ArcToken arcToken : envParents) { HibNodeToken parent = (HibNodeToken) arcToken.getParentToken(); if (parent.getAttrSetToken() == null) { continue; } if (attrSetToken == null) { attrSetToken = parent.getAttrSetToken(); } else if (!isMerge) { attrMap.putAll(attrSetToken.getAttrMap()); isMerge = true; } if (isMerge) { attrMap.putAll(parent.getAttrSetToken().getAttrMap()); } Env mergeEnv = parent.getEnv(); for (String name : mergeEnv.getTransientAttributeNames()) { transientAttributes.put(name, mergeEnv.getTransientAttribute(name)); } } HibNodeToken token = new HibNodeToken( (HibGraphProcess) process, (HibNodeRef) node, attrSetToken, executionType, attrMap, parents, transientAttributes); session.save(token); return token; }
/** * @see * com.googlecode.sarasvati.test.framework.TokenOnNodePredicate#matches(com.googlecode.sarasvati.NodeToken) */ @Override public boolean matches(final NodeToken token) { if (super.matches(token)) { for (final ArcToken parent : token.getParentTokens()) { if (parent.getParentToken().getTokenSetMember(tokenSetName) != null) { return true; } } } return false; }
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; } }
public NodeToken backtrackDeadEnd(final NodeToken token) { token.markBacktracked(); NodeTokenEvent.fireBacktrackedEvent(engine, token); List<ArcToken> parents = new ArrayList<ArcToken>(token.getParentTokens().size()); for (ArcToken parent : token.getParentTokens()) { parent.markBacktracked(); ArcTokenEvent.fireBacktrackedEvent(engine, parent); ArcToken backtrackArcToken = engine .getFactory() .newArcToken(token.getProcess(), parent.getArc(), ExecutionType.UTurn, token); ArcTokenEvent.fireCreatedEvent(engine, backtrackArcToken); token.getChildTokens().add(backtrackArcToken); parents.add(backtrackArcToken); shareTokenSets(backtrackArcToken, parent); } NodeToken backtrackToken = engine .getFactory() .newNodeToken( token.getProcess(), token.getNode(), ExecutionType.Forward, parents, token); NodeTokenEvent.fireCreatedEvent(engine, backtrackToken); token.getProcess().addNodeToken(backtrackToken); shareTokenSets(backtrackToken, token); for (ArcToken parent : parents) { parent.markProcessed(); ArcTokenEvent.fireProcessedEvent(engine, parent); parent.markComplete(backtrackToken); ArcTokenEvent.fireCompletedEvent(engine, parent); } reactivateTokenSets(); return backtrackToken; }
@Override public void backtrack(final NodeToken token) { if (!token.isComplete()) { throw new SarasvatiException("Cannot backtrack to a node token which isn't completed."); } if (token.getExecutionType().isBacktracked()) { throw new SarasvatiException("Cannot backtrack to a node token which has been backtracked."); } NodeToken resultToken = null; BacktrackTokenVisitor visitor = new BacktrackTokenVisitor(this, token); if (token.getChildTokens().isEmpty()) { resultToken = visitor.backtrackDeadEnd(token); } else { TokenTraversals.createOrderTraversal(token, visitor); resultToken = visitor.backtrack(); } executeNode(resultToken.getProcess(), resultToken); executeQueuedArcTokens(token.getProcess()); }
public boolean isCompletedNodeToken() { return token != null && token.isComplete(); }
public boolean isStartTokenNode() { return token != null && token.getParentTokens().isEmpty() && token.getNode().isStart(); }
private NodeToken backtrackToken(final NodeToken token) { NodeToken backtrackToken = token; if (!token.isComplete()) { token.markComplete(); NodeTokenEvent.fireCompletedEvent(engine, backtrackToken, null); token.markBacktracked(); NodeTokenEvent.fireBacktrackedEvent(engine, token); token.getProcess().removeActiveNodeToken(token); } else if (!token.getExecutionType().isBacktracked()) { if (token.getChildTokens().isEmpty()) { token.markBacktracked(); NodeTokenEvent.fireBacktrackedEvent(engine, token); } else { backtrackToken = backtrackCompletedToken(token, ExecutionType.Backtracked); backtrackToken.setGuardAction(GuardAction.SkipNode); NodeTokenEvent.fireSkippedEvent(engine, backtrackToken, null); } } for (ArcToken parent : getParents(token)) { boolean backtrackParent = visited.contains(parent.getParentToken()); token.getProcess().removeActiveArcToken(parent); parent.markBacktracked(); ArcTokenEvent.fireBacktrackedEvent(engine, parent); ArcToken backtrackArcToken = engine .getFactory() .newArcToken( token.getProcess(), parent.getArc(), backtrackParent ? ExecutionType.Backtracked : ExecutionType.UTurn, backtrackToken); ArcTokenEvent.fireCreatedEvent(engine, backtrackArcToken); backtrackToken.getChildTokens().add(backtrackArcToken); shareTokenSets(backtrackArcToken, parent); if (backtrackParent && parent.getExecutionType() != ExecutionType.Forward) { ArcToken mirror = backtrackMirror.getMirror(parent); arcTokenMap.put(mirror, backtrackArcToken); finishArcTokenBacktrack(backtrackArcToken, mirror); } else { finishArcTokenBacktrack(backtrackArcToken, parent); } } return backtrackToken; }
public Node getNode() { return token == null ? node : token.getNode(); }