Search in sources :

Example 1 with BranchCollisionPolicy

use of org.neo4j.graphdb.traversal.BranchCollisionPolicy 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 BranchCollisionPolicy

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

the class TestBidirectionalTraversal method mirroredTraversalReversesInitialState.

@Test
void mirroredTraversalReversesInitialState() {
    /*
         * (a)-->(b)-->(c)-->(d)
         */
    createGraph("a TO b", "b TO c", "c TO d");
    try (Transaction transaction = getGraphDb().beginTx()) {
        BranchCollisionPolicy collisionPolicy = (evaluator, pathPredicate) -> 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(transaction.bidirectionalTraversalDescription().mirroredSides(transaction.traversalDescription().uniqueness(NODE_PATH).expand(PathExpanders.forType(to), new InitialBranchState.State<>(0, 10))).collisionPolicy(collisionPolicy).traverse(getNodeWithName(transaction, "a"), getNodeWithName(transaction, "d")));
    }
}
Also used : Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) BranchCollisionPolicy(org.neo4j.graphdb.traversal.BranchCollisionPolicy) Traverser(org.neo4j.graphdb.traversal.Traverser) OUTGOING(org.neo4j.graphdb.Direction.OUTGOING) Iterators.count(org.neo4j.internal.helpers.collection.Iterators.count) PathExpanders(org.neo4j.graphdb.PathExpanders) Node(org.neo4j.graphdb.Node) TraversalBranch(org.neo4j.graphdb.traversal.TraversalBranch) PathExpanderBuilder(org.neo4j.graphdb.PathExpanderBuilder) PathExpander(org.neo4j.graphdb.PathExpander) SideSelectorPolicies(org.neo4j.graphdb.traversal.SideSelectorPolicies) Evaluators.includeIfContainsAll(org.neo4j.graphdb.traversal.Evaluators.includeIfContainsAll) Iterables(org.neo4j.internal.helpers.collection.Iterables) Arrays.asList(java.util.Arrays.asList) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) Transaction(org.neo4j.graphdb.Transaction) NODE_PATH(org.neo4j.graphdb.traversal.Uniqueness.NODE_PATH) StandardBranchCollisionDetector(org.neo4j.graphdb.impl.traversal.StandardBranchCollisionDetector) RelationshipType.withName(org.neo4j.graphdb.RelationshipType.withName) BidirectionalTraversalDescription(org.neo4j.graphdb.traversal.BidirectionalTraversalDescription) RELATIONSHIP_PATH(org.neo4j.graphdb.traversal.Uniqueness.RELATIONSHIP_PATH) Uniqueness(org.neo4j.graphdb.traversal.Uniqueness) Test(org.junit.jupiter.api.Test) Path(org.neo4j.graphdb.Path) InitialBranchState(org.neo4j.graphdb.traversal.InitialBranchState) TraversalDescription(org.neo4j.graphdb.traversal.TraversalDescription) Relationship(org.neo4j.graphdb.Relationship) Evaluators(org.neo4j.graphdb.traversal.Evaluators) RelationshipType(org.neo4j.graphdb.RelationshipType) Path(org.neo4j.graphdb.Path) BranchCollisionPolicy(org.neo4j.graphdb.traversal.BranchCollisionPolicy) InitialBranchState(org.neo4j.graphdb.traversal.InitialBranchState) Transaction(org.neo4j.graphdb.Transaction) StandardBranchCollisionDetector(org.neo4j.graphdb.impl.traversal.StandardBranchCollisionDetector) TraversalBranch(org.neo4j.graphdb.traversal.TraversalBranch) Test(org.junit.jupiter.api.Test)

Aggregations

Path (org.neo4j.graphdb.Path)2 PathExpander (org.neo4j.graphdb.PathExpander)2 BidirectionalTraversalDescription (org.neo4j.graphdb.traversal.BidirectionalTraversalDescription)2 BranchCollisionPolicy (org.neo4j.graphdb.traversal.BranchCollisionPolicy)2 TraversalDescription (org.neo4j.graphdb.traversal.TraversalDescription)2 Arrays.asList (java.util.Arrays.asList)1 MutableDouble (org.apache.commons.lang3.mutable.MutableDouble)1 Assertions.assertEquals (org.junit.jupiter.api.Assertions.assertEquals)1 Assertions.assertThrows (org.junit.jupiter.api.Assertions.assertThrows)1 Test (org.junit.jupiter.api.Test)1 CostEvaluator (org.neo4j.graphalgo.CostEvaluator)1 WeightedPath (org.neo4j.graphalgo.WeightedPath)1 DijkstraBranchCollisionDetector (org.neo4j.graphalgo.impl.util.DijkstraBranchCollisionDetector)1 DijkstraSelectorFactory (org.neo4j.graphalgo.impl.util.DijkstraSelectorFactory)1 OUTGOING (org.neo4j.graphdb.Direction.OUTGOING)1 GraphDatabaseService (org.neo4j.graphdb.GraphDatabaseService)1 Node (org.neo4j.graphdb.Node)1 PathExpanderBuilder (org.neo4j.graphdb.PathExpanderBuilder)1 PathExpanders (org.neo4j.graphdb.PathExpanders)1 Relationship (org.neo4j.graphdb.Relationship)1