void addNode(Node node) {
    setNodeReachable(node);
    EdgeList list = node.incoming;
    Edge e;
    for (int i = 0; i < list.size(); i++) {
      e = list.getEdge(i);
      if (!isNodeReachable(e.source)) {
        if (!isCandidate(e)) {
          setCandidate(e);
          candidates.add(e);
        }
      } else candidates.remove(e);
    }

    list = node.outgoing;
    for (int i = 0; i < list.size(); i++) {
      e = list.getEdge(i);
      if (!isNodeReachable(e.target)) {
        if (!isCandidate(e)) {
          setCandidate(e);
          candidates.add(e);
        }
      } else candidates.remove(e);
    }
    members.add(node);
  }
 public void test_add_tooBig() {
   NodeList<ASTNode> list = new NodeList<ASTNode>(argumentList());
   try {
     list.add(1, booleanLiteral(true));
     fail("Expected IndexOutOfBoundsException");
   } catch (IndexOutOfBoundsException exception) {
     // Expected
   }
 }
 private void findInitialSources(NodeList children, NodeList sources) {
   for (int i = 0; i < children.size(); i++) {
     Node node = children.getNode(i);
     if (isSource(node) && canBeRemoved(node)) {
       sources.add(node);
       node.flag = true;
     }
     if (node instanceof Subgraph) findInitialSources(((Subgraph) node).members, sources);
   }
 }
  /*
   * Finds all sources in graphNodes and adds them to the sL NodeList.
   */
  private void findSources(NodeList children) {
    NodeList sources = new NodeList();
    findInitialSources(children, sources);
    while (!sources.isEmpty()) {
      Node source = sources.getNode(sources.size() - 1);
      sL.add(source);
      sources.remove(source);
      removeSource(source, sources);

      // Check to see if the removal has made the parent node a source
      if (source.getParent() != null) {
        Node parent = source.getParent();
        setChildCount(parent, getChildCount(parent) - 1);
        if (isSource(parent) && canBeRemoved(parent)) {
          sources.add(parent);
          parent.flag = true;
        }
      }
    }
  }
 private void remove(Node n) {
   n.flag = true;
   if (n.getParent() != null) setChildCount(n.getParent(), getChildCount(n.getParent()) - 1);
   removeSink(n, null);
   removeSource(n, null);
   sL.add(n);
   if (n instanceof Subgraph) {
     Subgraph s = (Subgraph) n;
     isolateSubgraph(s, s);
     cycleRemove(s.members);
   }
 }
  /*
   * Finds all sinks in graphNodes and adds them to the passed NodeList
   */
  private void findSinks(NodeList children, NodeList rightList) {
    //	NodeList rightList = new NodeList();
    NodeList sinks = new NodeList();
    findInitialSinks(children, sinks);
    while (!sinks.isEmpty()) {
      Node sink = sinks.getNode(sinks.size() - 1);
      rightList.add(sink);
      sinks.remove(sink);
      removeSink(sink, sinks);

      // Check to see if the removal has made the parent node a sink
      if (sink.getParent() != null) {
        Node parent = sink.getParent();
        setChildCount(parent, getChildCount(parent) - 1);
        if (isSink(parent) && canBeRemoved(parent)) {
          sinks.add(parent);
          parent.flag = true;
        }
      }
    }
  }
  /** @see GraphVisitor#visit(org.eclipse.draw2d.graph.DirectedGraph) */
  public void visit(DirectedGraph g) {
    initializeDegrees(g);
    graphNodes = g.nodes;

    NodeList roots = new NodeList();
    for (int i = 0; i < graphNodes.size(); i++) {
      if (graphNodes.getNode(i).getParent() == null) roots.add(graphNodes.getNode(i));
    }
    buildNestingTreeIndices(roots, 0);
    removeParentChildEdges(g);
    cycleRemove(roots);
    invertEdges(g);
  }
 private void removeSink(Node sink, NodeList allSinks) {
   for (int i = 0; i < sink.incoming.size(); i++) {
     Edge e = sink.incoming.getEdge(i);
     if (!e.flag) {
       removeEdge(e);
       Node source = e.source;
       if (allSinks != null && isSink(source) && canBeRemoved(source)) {
         allSinks.add(source);
         source.flag = true;
       }
     }
   }
 }
  public void test_add() {
    ASTNode parent = argumentList();
    ASTNode firstNode = booleanLiteral(true);
    ASTNode secondNode = booleanLiteral(false);
    NodeList<ASTNode> list = new NodeList<ASTNode>(parent);
    list.add(0, secondNode);
    list.add(0, firstNode);
    assertSize(2, list);
    assertSame(firstNode, list.get(0));
    assertSame(secondNode, list.get(1));
    assertSame(parent, firstNode.getParent());
    assertSame(parent, secondNode.getParent());

    ASTNode thirdNode = booleanLiteral(false);
    list.add(1, thirdNode);
    assertSize(3, list);
    assertSame(firstNode, list.get(0));
    assertSame(thirdNode, list.get(1));
    assertSame(secondNode, list.get(2));
    assertSame(parent, firstNode.getParent());
    assertSame(parent, secondNode.getParent());
    assertSame(parent, thirdNode.getParent());
  }
  private void removeSource(Node n, NodeList allSources) {
    for (int i = 0; i < n.outgoing.size(); i++) {
      Edge e = n.outgoing.getEdge(i);
      if (!e.flag) {
        e.flag = true;
        changeInDegree(e.target, -1);
        changeOutDegree(e.source, -1);

        Node target = e.target;
        if (allSources != null && isSource(target) && canBeRemoved(target)) {
          allSources.add(target);
          target.flag = true;
        }
      }
    }
  }
  /*
   * Execution of the modified greedy cycle removal algorithm.
   */
  private void cycleRemove(NodeList children) {
    NodeList sR = new NodeList();
    do {
      findSinks(children, sR);
      findSources(children);

      // all sinks and sources added, find node with highest
      // outDegree - inDegree
      Node max = findNodeWithMaxDegree(children);
      if (max != null) {
        for (int i = 0; i < children.size(); i++) {
          Node child = (Node) children.get(i);
          if (child.flag) continue;
          if (child == max) restoreSinks(max, sR);
          else restoreSources(child);
        }
        remove(max);
      }
    } while (!allFlagged(children));
    while (!sR.isEmpty()) sL.add(sR.remove(sR.size() - 1));
  }
Exemple #12
0
  /**
   * If possible, returns a list containing a path of nodes visited. Otherwise an empty list. Each
   * edge will be visited exactly once.
   *
   * @return
   */
  public List<Integer> path() {
    NodeList path = new NodeList();
    LinkedList<Integer> empty = new LinkedList<Integer>();

    if (!precondition()) return empty;

    start = start > -1 ? start : nonzero;

    path.add(start);
    Node iterator = path.end;

    while (iterator != null) {
      int current = iterator.value;

      if (adjacencyLists[current].size() > 0) {
        // Find cycle for current, append to iterator...
        Node insert = iterator;
        int next = current;
        do {
          next = adjacencyLists[next].pollFirst();
          insert = path.insert(insert, next);
        } while (adjacencyLists[next].size() > 0);

        iterator = insert;
      } else {
        // All cycles handled, continue backwards.
        iterator = iterator.previous;
      }
    }

    if (path.size <= numberOfEdges) // composites..
    return empty;

    List<Integer> ret = new ArrayList<Integer>(path.size);

    for (int v : path.toList()) ret.add(v);

    return ret;
  }
 public void test_getEndToken_nonEmpty() {
   NodeList<ASTNode> list = new NodeList<ASTNode>(argumentList());
   ASTNode node = parenthesizedExpression(booleanLiteral(true));
   list.add(node);
   assertSame(node.getEndToken(), list.getEndToken());
 }
 @Override
 protected Node aggregateResult(Node aggregate, Node nextResult) {
   assert (aggregate instanceof NodeList);
   ((NodeList) aggregate).add(nextResult);
   return aggregate;
 }