Search in sources :

Example 1 with BidirectionalTraversalDescription

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

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

the class TestBidirectionalTraversal method collisionEvaluator.

@Test
void collisionEvaluator() {
    /*
         *           (d)-->(e)--
         *            ^     |   \
         *            |     v    v
         *           (a)-->(b)<--(f)
         *            |    ^
         *            v   /
         *           (c)-/
         */
    createGraph("a TO b", "a TO c", "c TO b", "a TO d", "d TO e", "e TO b", "e TO f", "f TO b");
    try (Transaction transaction = getGraphDb().beginTx()) {
        PathExpander<Void> expander = PathExpanders.forTypeAndDirection(to, OUTGOING);
        BidirectionalTraversalDescription traversal = transaction.bidirectionalTraversalDescription().mirroredSides(transaction.traversalDescription().uniqueness(NODE_PATH).expand(expander));
        expectPaths(traversal.collisionEvaluator(includeIfContainsAll(getNodeWithName(transaction, "e"))).traverse(getNodeWithName(transaction, "a"), getNodeWithName(transaction, "b")), "a,d,e,b", "a,d,e,f,b");
        expectPaths(traversal.collisionEvaluator(includeIfContainsAll(getNodeWithName(transaction, "e"), getNodeWithName(transaction, "f"))).traverse(getNodeWithName(transaction, "a"), getNodeWithName(transaction, "b")), "a,d,e,f,b");
    }
}
Also used : Transaction(org.neo4j.graphdb.Transaction) BidirectionalTraversalDescription(org.neo4j.graphdb.traversal.BidirectionalTraversalDescription) Test(org.junit.jupiter.api.Test)

Example 3 with BidirectionalTraversalDescription

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

the class TestPath method testBidirectionalPath.

@Test
public void testBidirectionalPath() {
    var graphDb = getGraphDb();
    BidirectionalTraversalDescription bidirectional;
    try (var transaction = graphDb.beginTx()) {
        Node a = transaction.getNodeById(this.a.getId());
        Node e = transaction.getNodeById(this.e.getId());
        TraversalDescription side = transaction.traversalDescription().uniqueness(Uniqueness.NODE_PATH);
        bidirectional = transaction.bidirectionalTraversalDescription().mirroredSides(side);
        Path bidirectionalPath = getFirstPath(bidirectional.traverse(a, e));
        assertPathIsCorrect(transaction, bidirectionalPath);
        Path path = getFirstPath(bidirectional.traverse(a, e));
        Node node = path.startNode();
        assertEquals(a, node);
        // White box testing below: relationships(), nodes(), reverseRelationships(), reverseNodes()
        // does cache the start node if not already cached, so just make sure they to it properly.
        bidirectionalPath = getFirstPath(bidirectional.traverse(a, e));
        bidirectionalPath.relationships();
        assertEquals(a, bidirectionalPath.startNode());
        bidirectionalPath = getFirstPath(bidirectional.traverse(a, e));
        bidirectionalPath.nodes();
        assertEquals(a, bidirectionalPath.startNode());
        bidirectionalPath = getFirstPath(bidirectional.traverse(a, e));
        bidirectionalPath.reverseRelationships();
        assertEquals(a, bidirectionalPath.startNode());
        bidirectionalPath = getFirstPath(bidirectional.traverse(a, e));
        bidirectionalPath.reverseNodes();
        assertEquals(a, bidirectionalPath.startNode());
        bidirectionalPath = getFirstPath(bidirectional.traverse(a, e));
        bidirectionalPath.iterator();
        assertEquals(a, bidirectionalPath.startNode());
    }
}
Also used : Path(org.neo4j.graphdb.Path) Node(org.neo4j.graphdb.Node) BidirectionalTraversalDescription(org.neo4j.graphdb.traversal.BidirectionalTraversalDescription) TraversalDescription(org.neo4j.graphdb.traversal.TraversalDescription) BidirectionalTraversalDescription(org.neo4j.graphdb.traversal.BidirectionalTraversalDescription) Test(org.junit.jupiter.api.Test)

Example 4 with BidirectionalTraversalDescription

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

the class DijkstraBidirectional method traverser.

private Traverser traverser(Node start, final Node end, PathInterest<Double> interest) {
    final MutableDouble shortestSoFar = new MutableDouble(Double.MAX_VALUE);
    final MutableDouble startSideShortest = new MutableDouble(0);
    final MutableDouble endSideShortest = new MutableDouble(0);
    PathExpander<Double> dijkstraExpander = new DijkstraBidirectionalPathExpander(expander, shortestSoFar, true, startSideShortest, endSideShortest, epsilon);
    var transaction = context.transaction();
    TraversalDescription side = transaction.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 = transaction.bidirectionalTraversalDescription().startSide(startSide).endSide(endSide).collisionEvaluator(Evaluators.all()).collisionPolicy((evaluator, pathPredicate) -> new DijkstraBranchCollisionDetector(evaluator, costEvaluator, shortestSoFar, epsilon, pathPredicate));
    lastTraverser = traversal.traverse(start, end);
    return lastTraverser;
}
Also used : MutableDouble(org.apache.commons.lang3.mutable.MutableDouble) BidirectionalTraversalDescription(org.neo4j.graphdb.traversal.BidirectionalTraversalDescription) TraversalDescription(org.neo4j.graphdb.traversal.TraversalDescription) DijkstraBranchCollisionDetector(org.neo4j.graphalgo.impl.util.DijkstraBranchCollisionDetector) DijkstraSelectorFactory(org.neo4j.graphalgo.impl.util.DijkstraSelectorFactory) MutableDouble(org.apache.commons.lang3.mutable.MutableDouble) BidirectionalTraversalDescription(org.neo4j.graphdb.traversal.BidirectionalTraversalDescription)

Aggregations

BidirectionalTraversalDescription (org.neo4j.graphdb.traversal.BidirectionalTraversalDescription)4 TraversalDescription (org.neo4j.graphdb.traversal.TraversalDescription)3 MutableDouble (org.apache.commons.lang3.mutable.MutableDouble)2 Test (org.junit.jupiter.api.Test)2 DijkstraBranchCollisionDetector (org.neo4j.graphalgo.impl.util.DijkstraBranchCollisionDetector)2 DijkstraSelectorFactory (org.neo4j.graphalgo.impl.util.DijkstraSelectorFactory)2 Path (org.neo4j.graphdb.Path)2 CostEvaluator (org.neo4j.graphalgo.CostEvaluator)1 WeightedPath (org.neo4j.graphalgo.WeightedPath)1 GraphDatabaseService (org.neo4j.graphdb.GraphDatabaseService)1 Node (org.neo4j.graphdb.Node)1 PathExpander (org.neo4j.graphdb.PathExpander)1 Transaction (org.neo4j.graphdb.Transaction)1 BranchCollisionDetector (org.neo4j.graphdb.traversal.BranchCollisionDetector)1 BranchCollisionPolicy (org.neo4j.graphdb.traversal.BranchCollisionPolicy)1 Evaluator (org.neo4j.graphdb.traversal.Evaluator)1 PathEvaluator (org.neo4j.graphdb.traversal.PathEvaluator)1