Search in sources :

Example 11 with Evaluator

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

the class CircularGraphTest method testCircularBug.

@Test
public void testCircularBug() {
    final long timestamp = 3;
    try (Transaction tx = beginTx()) {
        getNodeWithName("2").setProperty("timestamp", 1L);
        getNodeWithName("3").setProperty("timestamp", 2L);
        tx.success();
    }
    try (Transaction tx2 = beginTx()) {
        final RelationshipType type = RelationshipType.withName("TO");
        Iterator<Node> nodes = getGraphDb().traversalDescription().depthFirst().relationships(type, Direction.OUTGOING).evaluator(path -> {
            Relationship rel = path.lastRelationship();
            boolean relIsOfType = rel != null && rel.isType(type);
            boolean prune = relIsOfType && (Long) path.endNode().getProperty("timestamp") >= timestamp;
            return Evaluation.of(relIsOfType, !prune);
        }).traverse(node("1")).nodes().iterator();
        assertEquals("2", nodes.next().getProperty("name"));
        assertEquals("3", nodes.next().getProperty("name"));
        assertFalse(nodes.hasNext());
    }
}
Also used : Evaluator(org.neo4j.graphdb.traversal.Evaluator) Iterator(java.util.Iterator) Direction(org.neo4j.graphdb.Direction) Test(org.junit.Test) Node(org.neo4j.graphdb.Node) Evaluation(org.neo4j.graphdb.traversal.Evaluation) Path(org.neo4j.graphdb.Path) Relationship(org.neo4j.graphdb.Relationship) Assert.assertFalse(org.junit.Assert.assertFalse) RelationshipType(org.neo4j.graphdb.RelationshipType) Transaction(org.neo4j.graphdb.Transaction) Assert.assertEquals(org.junit.Assert.assertEquals) Before(org.junit.Before) Transaction(org.neo4j.graphdb.Transaction) Relationship(org.neo4j.graphdb.Relationship) Node(org.neo4j.graphdb.Node) RelationshipType(org.neo4j.graphdb.RelationshipType) Test(org.junit.Test)

Example 12 with Evaluator

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

the class TestMultiPruneEvaluators method testMaxDepthAndCustomPruneEvaluatorCombined.

@Test
public void testMaxDepthAndCustomPruneEvaluatorCombined() {
    Evaluator lessThanThreeRels = new Evaluator() {

        public Evaluation evaluate(Path path) {
            return count(path.endNode().getRelationships(Direction.OUTGOING).iterator()) < 3 ? Evaluation.INCLUDE_AND_PRUNE : Evaluation.INCLUDE_AND_CONTINUE;
        }
    };
    TraversalDescription description = getGraphDb().traversalDescription().evaluator(Evaluators.all()).evaluator(toDepth(1)).evaluator(lessThanThreeRels);
    Set<String> expectedNodes = new HashSet<String>(asList("a", "b", "c", "d", "e"));
    try (Transaction tx = beginTx()) {
        for (Path position : description.traverse(node("a"))) {
            String name = (String) position.endNode().getProperty("name");
            assertTrue(name + " shouldn't have been returned", expectedNodes.remove(name));
        }
        tx.success();
    }
    assertTrue(expectedNodes.isEmpty());
}
Also used : Path(org.neo4j.graphdb.Path) Transaction(org.neo4j.graphdb.Transaction) TraversalDescription(org.neo4j.graphdb.traversal.TraversalDescription) Evaluator(org.neo4j.graphdb.traversal.Evaluator) HashSet(java.util.HashSet) Test(org.junit.Test)

Aggregations

Evaluator (org.neo4j.graphdb.traversal.Evaluator)12 Path (org.neo4j.graphdb.Path)9 Test (org.junit.Test)8 TraversalDescription (org.neo4j.graphdb.traversal.TraversalDescription)6 Node (org.neo4j.graphdb.Node)4 Transaction (org.neo4j.graphdb.Transaction)4 Evaluation (org.neo4j.graphdb.traversal.Evaluation)4 HashSet (java.util.HashSet)2 Predicate (java.util.function.Predicate)2 Before (org.junit.Before)2 Direction (org.neo4j.graphdb.Direction)2 Relationship (org.neo4j.graphdb.Relationship)2 BranchCollisionPolicy (org.neo4j.graphdb.traversal.BranchCollisionPolicy)2 TraversalBranch (org.neo4j.graphdb.traversal.TraversalBranch)2 Iterator (java.util.Iterator)1 MutableDouble (org.apache.commons.lang3.mutable.MutableDouble)1 After (org.junit.After)1 Assert.assertEquals (org.junit.Assert.assertEquals)1 Assert.assertFalse (org.junit.Assert.assertFalse)1 CostEvaluator (org.neo4j.graphalgo.CostEvaluator)1