Search in sources :

Example 36 with Traverser

use of org.neo4j.graphdb.traversal.Traverser in project neo4j by neo4j.

the class TreeGraphTest method nodesIteratorReturnAllNodes.

@Test
public void nodesIteratorReturnAllNodes() throws Exception {
    try (Transaction transaction = beginTx()) {
        Traverser traverser = getGraphDb().traversalDescription().traverse(node("1"));
        int count = 0;
        for (Node node : traverser.nodes()) {
            assertNotNull("returned nodes should not be null. node #" + count, node);
            count++;
        }
        assertEquals(13, count);
    }
}
Also used : Transaction(org.neo4j.graphdb.Transaction) Traverser(org.neo4j.graphdb.traversal.Traverser) Node(org.neo4j.graphdb.Node) Test(org.junit.Test)

Example 37 with Traverser

use of org.neo4j.graphdb.traversal.Traverser in project neo4j by neo4j.

the class TreeGraphTest method relationshipsIteratorReturnAllNodes.

@Test
public void relationshipsIteratorReturnAllNodes() throws Exception {
    try (Transaction transaction = beginTx()) {
        Traverser traverser = getGraphDb().traversalDescription().traverse(node("1"));
        int count = 0;
        for (Relationship relationship : traverser.relationships()) {
            assertNotNull("returned relationships should not be. relationship #" + count, relationship);
            count++;
        }
        assertEquals(12, count);
    }
}
Also used : Transaction(org.neo4j.graphdb.Transaction) Traverser(org.neo4j.graphdb.traversal.Traverser) Relationship(org.neo4j.graphdb.Relationship) Test(org.junit.Test)

Example 38 with Traverser

use of org.neo4j.graphdb.traversal.Traverser in project neo4j by neo4j.

the class TreeGraphTest method testBreadthFirst.

@Test
public void testBreadthFirst() throws Exception {
    Traverser traverser = getGraphDb().traversalDescription().breadthFirst().traverse(node("1"));
    Stack<Set<String>> levels = new Stack<>();
    levels.push(new HashSet<>(asList("5", "6", "7", "8", "9", "A", "B", "C", "D")));
    levels.push(new HashSet<>(asList("2", "3", "4")));
    levels.push(new HashSet<>(asList("1")));
    try (Transaction tx = beginTx()) {
        assertLevels(traverser, levels);
        tx.success();
    }
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) Transaction(org.neo4j.graphdb.Transaction) Traverser(org.neo4j.graphdb.traversal.Traverser) Stack(java.util.Stack) Test(org.junit.Test)

Example 39 with Traverser

use of org.neo4j.graphdb.traversal.Traverser in project neo4j by neo4j.

the class TreeGraphTest method testPostorderDepthFirstReturnsDeeperNodesFirst.

@Test
public void testPostorderDepthFirstReturnsDeeperNodesFirst() {
    Traverser traverser = getGraphDb().traversalDescription().order(POSTORDER_DEPTH_FIRST).traverse(node("1"));
    int i = 0;
    List<String> encounteredNodes = new ArrayList<>();
    try (Transaction tx = beginTx()) {
        for (Path pos : traverser) {
            encounteredNodes.add((String) pos.endNode().getProperty("name"));
            assertEquals(expectedDepth(12 - i++), pos.length());
        }
        tx.success();
    }
    assertEquals(13, i);
    assertTrue(encounteredNodes.indexOf("5") < encounteredNodes.indexOf("2"));
    assertTrue(encounteredNodes.indexOf("6") < encounteredNodes.indexOf("2"));
    assertTrue(encounteredNodes.indexOf("7") < encounteredNodes.indexOf("2"));
    assertTrue(encounteredNodes.indexOf("8") < encounteredNodes.indexOf("3"));
    assertTrue(encounteredNodes.indexOf("9") < encounteredNodes.indexOf("3"));
    assertTrue(encounteredNodes.indexOf("A") < encounteredNodes.indexOf("3"));
    assertTrue(encounteredNodes.indexOf("B") < encounteredNodes.indexOf("4"));
    assertTrue(encounteredNodes.indexOf("C") < encounteredNodes.indexOf("4"));
    assertTrue(encounteredNodes.indexOf("D") < encounteredNodes.indexOf("4"));
    assertTrue(encounteredNodes.indexOf("2") < encounteredNodes.indexOf("1"));
    assertTrue(encounteredNodes.indexOf("3") < encounteredNodes.indexOf("1"));
    assertTrue(encounteredNodes.indexOf("4") < encounteredNodes.indexOf("1"));
}
Also used : Path(org.neo4j.graphdb.Path) Transaction(org.neo4j.graphdb.Transaction) Traverser(org.neo4j.graphdb.traversal.Traverser) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 40 with Traverser

use of org.neo4j.graphdb.traversal.Traverser in project neo4j by neo4j.

the class TreeGraphTest method testDepthFirstTraversalReturnsNodesOnCorrectDepths.

@Test
public void testDepthFirstTraversalReturnsNodesOnCorrectDepths() throws Exception {
    try (Transaction transaction = beginTx()) {
        Traverser traverser = getGraphDb().traversalDescription().depthFirst().traverse(node("1"));
        int i = 0;
        for (Path pos : traverser) {
            assertEquals(expectedDepth(i++), pos.length());
        }
        assertEquals(13, i);
    }
}
Also used : Path(org.neo4j.graphdb.Path) Transaction(org.neo4j.graphdb.Transaction) Traverser(org.neo4j.graphdb.traversal.Traverser) Test(org.junit.Test)

Aggregations

Traverser (org.neo4j.graphdb.traversal.Traverser)44 Test (org.junit.Test)19 Path (org.neo4j.graphdb.Path)15 Transaction (org.neo4j.graphdb.Transaction)10 HashSet (java.util.HashSet)6 Node (org.neo4j.graphdb.Node)6 Predicate (org.neo4j.helpers.Predicate)6 Set (java.util.Set)4 Stack (java.util.Stack)4 WeightedPath (org.neo4j.graphalgo.WeightedPath)4 BestFirstSelectorFactory (org.neo4j.graphalgo.impl.util.BestFirstSelectorFactory)4 StopAfterWeightIterator (org.neo4j.graphalgo.impl.util.StopAfterWeightIterator)4 Relationship (org.neo4j.graphdb.Relationship)3 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 LiteDepthFirstSelector (org.neo4j.graphalgo.impl.util.LiteDepthFirstSelector)2 BranchOrderingPolicy (org.neo4j.graphdb.traversal.BranchOrderingPolicy)2 BranchSelector (org.neo4j.graphdb.traversal.BranchSelector)2 TraversalBranch (org.neo4j.graphdb.traversal.TraversalBranch)2 TraversalDescription (org.neo4j.graphdb.traversal.TraversalDescription)2