コード例 #1
0
  /**
   * Test 0: Graph with no bifurcations. 100 nodes. G = 1 for all edges H = TargetsID - currentID
   * Expected: 1. Every node should be visited 2. ParentID = SonsID - 1
   */
  public void test_0() {
    final int nnodes = 100;
    Node[] ends = GraphTestUtil.buildNoBifurcations(builder(), nnodes);

    CountingWalker walker = new CountingWalker();

    final AStarIterator iterator = createIterator(ends[0], ends[ends.length - 1]);

    BasicGraphTraversal traversal = new BasicGraphTraversal(builder().getGraph(), walker, iterator);
    traversal.init();
    traversal.traverse();

    GraphVisitor visitor =
        new GraphVisitor() {
          public int visit(Graphable component) {
            assertTrue(component.isVisited());
            if (component.getID() == 0) assertNull(iterator.getParent((Node) component));
            else assertTrue(component.getID() == iterator.getParent((Node) component).getID() + 1);
            return 0;
          }
        };
    builder().getGraph().visitNodes(visitor);

    assertTrue(walker.getCount() == nnodes);
  }
コード例 #2
0
  /**
   * Create a graph with no bifurcations and start a traversal from start node, then suspend, and
   * resume. <br>
   * <br>
   * Expected: After suspend: 1. Nodes from 0 to suspend node should be visted, others not.
   *
   * <p>After resume: 1. Next node visited should have id suspend node id + 1 2. Every node should
   * have a parent with id node id + 1
   */
  public void test_1() {
    int nnodes = 100;
    Node[] ends = GraphTestUtil.buildNoBifurcations(builder(), nnodes);
    final int suspend = 50;

    CountingWalker walker =
        new CountingWalker() {
          int m_mode = 0;

          public int visit(Graphable element, GraphTraversal traversal) {
            super.visit(element, traversal);
            if (m_mode == 0) {
              if (element.getID() == suspend) {
                m_mode++;
                return (GraphTraversal.SUSPEND);
              }
            } else if (m_mode == 1) {
              assertTrue(element.getID() == suspend + 1);
              m_mode++;
            }
            return (GraphTraversal.CONTINUE);
          }
        };

    final AStarIterator iterator = createIterator(ends[0], ends[ends.length - 1]);

    BasicGraphTraversal traversal = new BasicGraphTraversal(builder().getGraph(), walker, iterator);
    traversal.init();
    traversal.traverse();

    GraphVisitor visitor =
        new GraphVisitor() {
          public int visit(Graphable component) {
            if (component.getID() <= suspend) assertTrue(component.isVisited());
            else assertTrue(!component.isVisited());
            return 0;
          }
        };
    builder().getGraph().visitNodes(visitor);
    assertTrue(walker.getCount() == nnodes - suspend + 1);

    // resume
    traversal.traverse();

    visitor =
        new GraphVisitor() {
          public int visit(Graphable component) {
            assertTrue(component.isVisited());
            if (component.getID() == 0) assertNull(iterator.getParent((Node) component));
            else assertTrue(iterator.getParent((Node) component).getID() == component.getID() - 1);

            return 0;
          }
        };
    builder().getGraph().visitNodes(visitor);
    assertTrue(walker.getCount() == nnodes);
  }