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;
  }
  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;
  }
Ejemplo n.º 4
0
  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);
  }
Ejemplo n.º 5
0
  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));
    }
  }
  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;
  }
Ejemplo n.º 7
0
  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;
    }
  }