Search in sources :

Example 6 with TraversalPosition

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

the class TestTraversal method testStopOnPreviousNode.

// Verifies that the stop evaluator can stop based on the previous node
@Test
public void testStopOnPreviousNode() 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 2, 3, and 4
    // (ie root's children)
    StopEvaluator stopEvaluator = new StopEvaluator() {

        public boolean isStopNode(TraversalPosition position) {
            try {
                Node node = position.previousNode();
                String nodeId = (String) node.getProperty("node.test.id");
                return nodeId.equals("1");
            } 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");
        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) NotFoundException(org.neo4j.graphdb.NotFoundException) Test(org.junit.Test)

Example 7 with TraversalPosition

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

the class CircularGraphTest method testCircularBug.

@Test
public void testCircularBug() {
    final long timestamp = 3;
    Transaction tx = beginTx();
    getNodeWithName("2").setProperty("timestamp", 1L);
    getNodeWithName("3").setProperty("timestamp", 2L);
    tx.success();
    tx.finish();
    final RelationshipType type = DynamicRelationshipType.withName("TO");
    Traverser t = referenceNode().traverse(Order.DEPTH_FIRST, new StopEvaluator() {

        public boolean isStopNode(TraversalPosition position) {
            Relationship last = position.lastRelationshipTraversed();
            if (last != null && last.isType(type)) {
                Node node = position.currentNode();
                long currentTime = (Long) node.getProperty("timestamp");
                return currentTime >= timestamp;
            }
            return false;
        }
    }, new ReturnableEvaluator() {

        public boolean isReturnableNode(TraversalPosition position) {
            Relationship last = position.lastRelationshipTraversed();
            if (last != null && last.isType(type)) {
                return true;
            }
            return false;
        }
    }, type, Direction.OUTGOING);
    Iterator<Node> nodes = t.iterator();
    assertEquals("2", nodes.next().getProperty("name"));
    assertEquals("3", nodes.next().getProperty("name"));
    assertFalse(nodes.hasNext());
}
Also used : ReturnableEvaluator(org.neo4j.graphdb.ReturnableEvaluator) Transaction(org.neo4j.graphdb.Transaction) StopEvaluator(org.neo4j.graphdb.StopEvaluator) Traverser(org.neo4j.graphdb.Traverser) Relationship(org.neo4j.graphdb.Relationship) Node(org.neo4j.graphdb.Node) RelationshipType(org.neo4j.graphdb.RelationshipType) DynamicRelationshipType(org.neo4j.graphdb.DynamicRelationshipType) TraversalPosition(org.neo4j.graphdb.TraversalPosition) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)7 Node (org.neo4j.graphdb.Node)7 TraversalPosition (org.neo4j.graphdb.TraversalPosition)7 Traverser (org.neo4j.graphdb.Traverser)7 RelationshipType (org.neo4j.graphdb.RelationshipType)6 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