Search in sources :

Example 1 with Evaluator

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

the class DijkstraBidirectional method traverser.

private Traverser traverser(Node start, final Node end, PathInterest interest) {
    final MutableDouble shortestSoFar = new MutableDouble(Double.MAX_VALUE);
    final MutableDouble startSideShortest = new MutableDouble(0);
    final MutableDouble endSideShortest = new MutableDouble(0);
    PathExpander dijkstraExpander = new DijkstraBidirectionalPathExpander(expander, shortestSoFar, true, startSideShortest, endSideShortest, epsilon);
    GraphDatabaseService db = start.getGraphDatabase();
    TraversalDescription side = db.traversalDescription().expand(dijkstraExpander, stateFactory).order(new DijkstraSelectorFactory(interest, costEvaluator)).evaluator(new DijkstraBidirectionalEvaluator(costEvaluator)).uniqueness(Uniqueness.NODE_PATH);
    TraversalDescription startSide = side;
    TraversalDescription endSide = side.reverse();
    BidirectionalTraversalDescription traversal = db.bidirectionalTraversalDescription().startSide(startSide).endSide(endSide).collisionEvaluator(Evaluators.all()).collisionPolicy(new BranchCollisionPolicy() {

        @Override
        public BranchCollisionDetector create(Evaluator evaluator, Predicate<Path> pathPredicate) {
            return new DijkstraBranchCollisionDetector(evaluator, costEvaluator, shortestSoFar, epsilon, pathPredicate);
        }
    });
    lastTraverser = traversal.traverse(start, end);
    return lastTraverser;
}
Also used : WeightedPath(org.neo4j.graphalgo.WeightedPath) Path(org.neo4j.graphdb.Path) GraphDatabaseService(org.neo4j.graphdb.GraphDatabaseService) BranchCollisionPolicy(org.neo4j.graphdb.traversal.BranchCollisionPolicy) MutableDouble(org.apache.commons.lang3.mutable.MutableDouble) PathExpander(org.neo4j.graphdb.PathExpander) DijkstraSelectorFactory(org.neo4j.graphalgo.impl.util.DijkstraSelectorFactory) PathEvaluator(org.neo4j.graphdb.traversal.PathEvaluator) Evaluator(org.neo4j.graphdb.traversal.Evaluator) CostEvaluator(org.neo4j.graphalgo.CostEvaluator) BidirectionalTraversalDescription(org.neo4j.graphdb.traversal.BidirectionalTraversalDescription) BranchCollisionDetector(org.neo4j.graphdb.traversal.BranchCollisionDetector) DijkstraBranchCollisionDetector(org.neo4j.graphalgo.impl.util.DijkstraBranchCollisionDetector) BidirectionalTraversalDescription(org.neo4j.graphdb.traversal.BidirectionalTraversalDescription) TraversalDescription(org.neo4j.graphdb.traversal.TraversalDescription) DijkstraBranchCollisionDetector(org.neo4j.graphalgo.impl.util.DijkstraBranchCollisionDetector)

Example 2 with Evaluator

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

the class TestTraversalWithLoops method traverseThroughNodeWithLoop.

@Test
public void traverseThroughNodeWithLoop() throws Exception {
    /*
         * (a)-->(b)-->(c)-->(d)-->(e)
         *             /  \ /  \
         *             \__/ \__/
         */
    createGraph("a TO b", "b TO c", "c TO c", "c TO d", "d TO d", "d TO e");
    try (Transaction tx = beginTx()) {
        Node a = getNodeWithName("a");
        final Node e = getNodeWithName("e");
        Evaluator onlyEndNode = new Evaluator() {

            @Override
            public Evaluation evaluate(Path path) {
                return Evaluation.ofIncludes(path.endNode().equals(e));
            }
        };
        TraversalDescription basicTraverser = getGraphDb().traversalDescription().evaluator(onlyEndNode);
        expectPaths(basicTraverser.traverse(a), "a,b,c,d,e");
        expectPaths(basicTraverser.uniqueness(Uniqueness.RELATIONSHIP_PATH).traverse(a), "a,b,c,d,e", "a,b,c,c,d,e", "a,b,c,d,d,e", "a,b,c,c,d,d,e");
        tx.success();
    }
}
Also used : Path(org.neo4j.graphdb.Path) Transaction(org.neo4j.graphdb.Transaction) Node(org.neo4j.graphdb.Node) TraversalDescription(org.neo4j.graphdb.traversal.TraversalDescription) Evaluator(org.neo4j.graphdb.traversal.Evaluator) Test(org.junit.Test)

Example 3 with Evaluator

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

the class TestBidirectionalTraversal method mirroredTraversalReversesInitialState.

@Test
public void mirroredTraversalReversesInitialState() throws Exception {
    /*
         * (a)-->(b)-->(c)-->(d)
         */
    createGraph("a TO b", "b TO c", "c TO d");
    BranchCollisionPolicy collisionPolicy = new BranchCollisionPolicy() {

        @Override
        public BranchCollisionDetector create(Evaluator evaluator, Predicate<Path> pathPredicate) {
            return new StandardBranchCollisionDetector(null, null) {

                @Override
                protected boolean includePath(Path path, TraversalBranch startPath, TraversalBranch endPath) {
                    assertEquals(0, startPath.state());
                    assertEquals(10, endPath.state());
                    return true;
                }
            };
        }
    };
    Iterables.count(getGraphDb().bidirectionalTraversalDescription().mirroredSides(getGraphDb().traversalDescription().uniqueness(NODE_PATH).expand(PathExpanders.<Integer>forType(to), new InitialBranchState.State<>(0, 10))).collisionPolicy(collisionPolicy).traverse(getNodeWithName("a"), getNodeWithName("d")));
}
Also used : Path(org.neo4j.graphdb.Path) BranchCollisionPolicy(org.neo4j.graphdb.traversal.BranchCollisionPolicy) InitialBranchState(org.neo4j.graphdb.traversal.InitialBranchState) StandardBranchCollisionDetector(org.neo4j.graphdb.impl.traversal.StandardBranchCollisionDetector) Evaluator(org.neo4j.graphdb.traversal.Evaluator) TraversalBranch(org.neo4j.graphdb.traversal.TraversalBranch) Predicate(java.util.function.Predicate) Test(org.junit.Test)

Example 4 with Evaluator

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

the class TestMultipleFilters method testNarrowingFilters.

@Test
public void testNarrowingFilters() {
    Evaluator mustBeConnectedToK = new MustBeConnectedToNodeFilter(getNodeWithName("k"));
    Evaluator mustNotHaveMoreThanTwoOutRels = path -> Evaluation.ofIncludes(Iterables.count(path.endNode().getRelationships(Direction.OUTGOING)) <= 2);
    TraversalDescription description = getGraphDb().traversalDescription().evaluator(mustBeConnectedToK);
    expectNodes(description.traverse(node("a")), "b", "c");
    expectNodes(description.evaluator(mustNotHaveMoreThanTwoOutRels).traverse(node("a")), "c");
}
Also used : Evaluator(org.neo4j.graphdb.traversal.Evaluator) Predicate(java.util.function.Predicate) Direction(org.neo4j.graphdb.Direction) Test(org.junit.Test) Evaluators.includeIfAcceptedByAny(org.neo4j.graphdb.traversal.Evaluators.includeIfAcceptedByAny) Node(org.neo4j.graphdb.Node) Evaluation(org.neo4j.graphdb.traversal.Evaluation) Path(org.neo4j.graphdb.Path) TraversalDescription(org.neo4j.graphdb.traversal.TraversalDescription) Relationship(org.neo4j.graphdb.Relationship) Iterables(org.neo4j.helpers.collection.Iterables) After(org.junit.After) Transaction(org.neo4j.graphdb.Transaction) Before(org.junit.Before) TraversalDescription(org.neo4j.graphdb.traversal.TraversalDescription) Evaluator(org.neo4j.graphdb.traversal.Evaluator) Test(org.junit.Test)

Example 5 with Evaluator

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

the class TestMultiPruneEvaluators method testMaxDepthAndCustomPruneEvaluatorCombined.

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

        public Evaluation evaluate(Path path) {
            return IteratorUtil.count(path.endNode().getRelationships(Direction.OUTGOING).iterator()) < 3 ? Evaluation.INCLUDE_AND_PRUNE : Evaluation.INCLUDE_AND_CONTINUE;
        }
    };
    TraversalDescription description = Traversal.description().evaluator(Evaluators.all()).evaluator(Evaluators.toDepth(1)).evaluator(lessThanThreeRels);
    Set<String> expectedNodes = new HashSet<String>(Arrays.asList("a", "b", "c", "d", "e"));
    for (Path position : description.traverse(referenceNode())) {
        String name = (String) position.endNode().getProperty("name");
        assertTrue(name + " shouldn't have been returned", expectedNodes.remove(name));
    }
    assertTrue(expectedNodes.isEmpty());
}
Also used : Path(org.neo4j.graphdb.Path) TraversalDescription(org.neo4j.graphdb.traversal.TraversalDescription) Evaluator(org.neo4j.graphdb.traversal.Evaluator) PruneEvaluator(org.neo4j.graphdb.traversal.PruneEvaluator) 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