Search in sources :

Example 1 with Traverser

use of org.neo4j.graphdb.Traverser in project graphdb by neo4j-attic.

the class TestTraversal method testStopOnDepth.

// Verifies that the stop evaluator can stop based on the current depth
@Test
public void testStopOnDepth() throws Exception {
    // Build ise tree
    Node root = this.buildIseTreePopulation();
    // Traverse only ISE relationships
    RelationshipType[] traversableRels = new RelationshipType[] { MyRelTypes.TEST };
    // Construct a stop evaluator that stops on depth 2
    StopEvaluator stopEvaluator = new StopEvaluator() {

        public boolean isStopNode(TraversalPosition position) {
            return position.depth() >= 2;
        }
    };
    // Create a traverser
    Traverser traverser = root.traverse(BREADTH_FIRST, stopEvaluator, ReturnableEvaluator.ALL, traversableRels[0], Direction.BOTH);
    try {
        this.assertNextNodeId(traverser, "1");
        this.assertNextNodeId(traverser, "2");
        this.assertNextNodeId(traverser, "3");
        this.assertNextNodeId(traverser, "4");
        this.assertNextNodeId(traverser, "5");
        this.assertNextNodeId(traverser, "6");
        this.assertNextNodeId(traverser, "7");
        assertTrue("Too many nodes returned from traversal", traverser.iterator().hasNext() == false);
    } catch (java.util.NoSuchElementException nsee) {
        fail("Too few nodes returned from traversal");
    } finally {
        // Delete ise tree and commmit work
        this.deleteNodeTreeRecursively(root, 0);
    }
}
Also used : StopEvaluator(org.neo4j.graphdb.StopEvaluator) Node(org.neo4j.graphdb.Node) Traverser(org.neo4j.graphdb.Traverser) RelationshipType(org.neo4j.graphdb.RelationshipType) TraversalPosition(org.neo4j.graphdb.TraversalPosition) Test(org.junit.Test)

Example 2 with Traverser

use of org.neo4j.graphdb.Traverser in project graphdb by neo4j-attic.

the class TestTraversal method testStopOnLastRelationship.

// Verifies that the stop evaluator can stop based on the last
// traversed relationship
@Test
public void testStopOnLastRelationship() throws Exception {
    // Build ise tree
    Node root = this.buildIseTreePopulation();
    // Traverse only ISE relationships
    RelationshipType[] traversableRels = new RelationshipType[] { MyRelTypes.TEST, MyRelTypes.TEST_TRAVERSAL };
    // Construct stop- and returnable evaluators that return 5 nodes
    StopEvaluator stopEvaluator = new StopEvaluator() {

        public boolean isStopNode(TraversalPosition position) {
            // Stop when we got here by traversing a clone relationship
            Relationship rel = position.lastRelationshipTraversed();
            return rel != null && rel.isType(MyRelTypes.TEST_TRAVERSAL);
        }
    };
    // Create a traverser
    Traverser traverser = root.traverse(BREADTH_FIRST, stopEvaluator, ReturnableEvaluator.ALL, traversableRels[0], Direction.BOTH, traversableRels[1], Direction.BOTH);
    try {
        this.assertLevelsOfNodes(traverser, new String[][] { new String[] { "1" }, new String[] { "2", "3", "4" }, new String[] { "5", "6", "7", "8", "9" }, new String[] { "10", "11", "12", "13" } });
        assertTrue("Too many nodes returned from traversal", traverser.iterator().hasNext() == false);
    } catch (java.util.NoSuchElementException nsee) {
        fail("Too few nodes returned from traversal");
    } finally {
        // Delete ise tree and commmit work
        this.deleteNodeTreeRecursively(root, 0);
    }
}
Also used : StopEvaluator(org.neo4j.graphdb.StopEvaluator) Node(org.neo4j.graphdb.Node) Relationship(org.neo4j.graphdb.Relationship) Traverser(org.neo4j.graphdb.Traverser) RelationshipType(org.neo4j.graphdb.RelationshipType) TraversalPosition(org.neo4j.graphdb.TraversalPosition) Test(org.junit.Test)

Example 3 with Traverser

use of org.neo4j.graphdb.Traverser in project graphdb by neo4j-attic.

the class TestTraversal method testDirectedBreadthTraversal.

// Traverses the test "ise-tree-like" population breadth first,
// starting in the middle of the tree and traversing only in the
// "forward" direction
@Test
public void testDirectedBreadthTraversal() throws Exception {
    // Build test population
    Node root = this.buildIseTreePopulation();
    Node startNode = null;
    // Get a node in the middle of the tree:
    try {
        // a) Construct a returnable evaluator that returns node 2
        ReturnableEvaluator returnEvaluator = new ReturnableEvaluator() {

            public boolean isReturnableNode(TraversalPosition pos) {
                try {
                    Node node = pos.currentNode();
                    String key = "node.test.id";
                    String nodeId = (String) node.getProperty(key);
                    return nodeId.equals("2");
                } catch (Exception e) {
                    return false;
                }
            }
        };
        // b) create a traverser
        Traverser toTheMiddleTraverser = root.traverse(BREADTH_FIRST, StopEvaluator.END_OF_GRAPH, returnEvaluator, MyRelTypes.TEST, Direction.BOTH);
        // c) get the first node it returns
        startNode = toTheMiddleTraverser.iterator().next();
        assertEquals("2", startNode.getProperty("node.test.id"));
    } catch (Exception e) {
        e.printStackTrace();
        fail("Something went wrong when trying to get a start node " + "in the middle of the tree: " + e);
    }
    // Construct the real traverser
    Traverser traverser = startNode.traverse(BREADTH_FIRST, StopEvaluator.END_OF_GRAPH, ReturnableEvaluator.ALL, MyRelTypes.TEST, Direction.OUTGOING);
    try {
        this.assertNextNodeId(traverser, "2");
        this.assertNextNodeId(traverser, "5");
        this.assertNextNodeId(traverser, "6");
        this.assertNextNodeId(traverser, "10");
        this.assertNextNodeId(traverser, "11");
        this.assertNextNodeId(traverser, "12");
        this.assertNextNodeId(traverser, "13");
        assertTrue("Too many nodes returned from traversal", traverser.iterator().hasNext() == false);
    } catch (java.util.NoSuchElementException nsee) {
        nsee.printStackTrace();
        fail("Too few nodes returned from traversal");
    } finally {
        this.deleteNodeTreeRecursively(root, 0);
    }
}
Also used : ReturnableEvaluator(org.neo4j.graphdb.ReturnableEvaluator) Node(org.neo4j.graphdb.Node) Traverser(org.neo4j.graphdb.Traverser) TraversalPosition(org.neo4j.graphdb.TraversalPosition) NotFoundException(org.neo4j.graphdb.NotFoundException) Test(org.junit.Test)

Example 4 with Traverser

use of org.neo4j.graphdb.Traverser in project graphdb by neo4j-attic.

the class TestTraversal method testStopOnCurrentNode.

// Verifies that the stop evaluator can stop based on the current node
@Test
public void testStopOnCurrentNode() throws Exception {
    // Build ise tree
    Node root = this.buildIseTreePopulation();
    // Traverse only ISE relationships
    RelationshipType[] traversableRels = new RelationshipType[] { MyRelTypes.TEST };
    // Construct a stop evaluator that stops on nodes 5, 6, 3 and 4
    StopEvaluator stopEvaluator = new StopEvaluator() {

        public boolean isStopNode(TraversalPosition position) {
            try {
                Node node = position.currentNode();
                String nodeId = (String) node.getProperty("node.test.id");
                return nodeId.equals("5") || nodeId.equals("6") || nodeId.equals("3") || nodeId.equals("4");
            } catch (Exception e) {
                return false;
            }
        }
    };
    // Create a traverser
    Traverser traverser = root.traverse(BREADTH_FIRST, stopEvaluator, ReturnableEvaluator.ALL, traversableRels[0], Direction.BOTH);
    try {
        this.assertNextNodeId(traverser, "1");
        this.assertNextNodeId(traverser, "2");
        this.assertNextNodeId(traverser, "3");
        this.assertNextNodeId(traverser, "4");
        this.assertNextNodeId(traverser, "5");
        this.assertNextNodeId(traverser, "6");
        assertTrue("Too many nodes returned from traversal", traverser.iterator().hasNext() == false);
    } catch (java.util.NoSuchElementException nsee) {
        fail("Too few nodes returned from traversal");
    } finally {
        this.deleteNodeTreeRecursively(root, 0);
    }
}
Also used : StopEvaluator(org.neo4j.graphdb.StopEvaluator) Node(org.neo4j.graphdb.Node) Traverser(org.neo4j.graphdb.Traverser) RelationshipType(org.neo4j.graphdb.RelationshipType) TraversalPosition(org.neo4j.graphdb.TraversalPosition) NotFoundException(org.neo4j.graphdb.NotFoundException) Test(org.junit.Test)

Example 5 with Traverser

use of org.neo4j.graphdb.Traverser in project graphdb by neo4j-attic.

the class TestTraversal method testStopOnReturnedNodes.

// Verifies that the stop evaluator can stop based on the amount of
// returned nodes
@Test
public void testStopOnReturnedNodes() throws Exception {
    // Build ise tree
    Node root = this.buildIseTreePopulation();
    // Traverse only ISE relationships
    RelationshipType[] traversableRels = new RelationshipType[] { MyRelTypes.TEST };
    // Construct stop- and returnable evaluators that return 5 nodes
    StopEvaluator stopEvaluator = new StopEvaluator() {

        public boolean isStopNode(TraversalPosition position) {
            // Stop traversing when we've returned 5 nodes
            return position.returnedNodesCount() >= 5;
        }
    };
    ReturnableEvaluator returnEvaluator = new ReturnableEvaluator() {

        public boolean isReturnableNode(TraversalPosition position) {
            // Return nodes until we've reached 5 nodes or end of graph
            return position.returnedNodesCount() < 5;
        }
    };
    // Create a traverser
    Traverser traverser = root.traverse(BREADTH_FIRST, stopEvaluator, returnEvaluator, traversableRels[0], Direction.BOTH);
    try {
        this.assertLevelsOfNodes(traverser, new String[][] { new String[] { "1" }, new String[] { "2", "3", "4" }, new String[] { "5" } });
        assertTrue("Too many nodes returned from traversal", traverser.iterator().hasNext() == false);
    } catch (java.util.NoSuchElementException nsee) {
        fail("Too few nodes returned from traversal");
    } finally {
        // Delete ise tree and commmit work
        this.deleteNodeTreeRecursively(root, 0);
    }
}
Also used : ReturnableEvaluator(org.neo4j.graphdb.ReturnableEvaluator) StopEvaluator(org.neo4j.graphdb.StopEvaluator) Node(org.neo4j.graphdb.Node) Traverser(org.neo4j.graphdb.Traverser) RelationshipType(org.neo4j.graphdb.RelationshipType) TraversalPosition(org.neo4j.graphdb.TraversalPosition) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)11 Node (org.neo4j.graphdb.Node)11 Traverser (org.neo4j.graphdb.Traverser)11 RelationshipType (org.neo4j.graphdb.RelationshipType)10 TraversalPosition (org.neo4j.graphdb.TraversalPosition)7 StopEvaluator (org.neo4j.graphdb.StopEvaluator)6 NotFoundException (org.neo4j.graphdb.NotFoundException)3 ReturnableEvaluator (org.neo4j.graphdb.ReturnableEvaluator)3 Relationship (org.neo4j.graphdb.Relationship)2 DynamicRelationshipType (org.neo4j.graphdb.DynamicRelationshipType)1 Transaction (org.neo4j.graphdb.Transaction)1