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;
  }
Beispiel #2
0
  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);
    }
  }
Beispiel #3
0
  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);
        }
      }
    }
  }
Beispiel #4
0
  @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();
 }