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;
}
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");
}
}
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());
}
}
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;
}
Aggregations