use of org.neo4j.graphdb.Path 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.Path in project neo4j by neo4j.
the class DijkstraTest method canGetMultiplePathsInASmallRoadNetwork.
@Test
public void canGetMultiplePathsInASmallRoadNetwork() throws Exception {
/* NODE (NAME/INDEX)
*
* --------------------- 25 -----------------------
* / \
* (A/0) - 2 - (B/1) - 2.5 - (D/3) - 3 - (E/4) - 5 - (F/5)
* | / / /
* 2.5 ---------- 7.3 ----- / /
* | / / /
* (C/2) ------------------ 5 --------- /
* \ /
* ------------------ 12 --------------------
*
*/
Node nodeA = graph.makeNode("A");
Node nodeB = graph.makeNode("B");
Node nodeC = graph.makeNode("C");
Node nodeD = graph.makeNode("D");
Node nodeE = graph.makeNode("E");
Node nodeF = graph.makeNode("F");
graph.makeEdge("A", "B", "length", 2d);
graph.makeEdge("A", "C", "length", 2.5f);
graph.makeEdge("C", "D", "length", 7.3d);
graph.makeEdge("B", "D", "length", 2.5f);
graph.makeEdge("D", "E", "length", 3L);
graph.makeEdge("C", "E", "length", 5);
graph.makeEdge("E", "F", "length", (byte) 5);
graph.makeEdge("C", "F", "length", (short) 12);
graph.makeEdge("A", "F", "length", (long) 25);
PathFinder finder = factory.dijkstra(PathExpanders.allTypesAndDirections());
// Try the search in both directions.
for (Node[] nodes : new Node[][] { { nodeA, nodeF }, { nodeF, nodeA } }) {
int found = 0;
Iterator<WeightedPath> paths = finder.findAllPaths(nodes[0], nodes[1]).iterator();
for (int i = 0; i < 2; i++) {
assertTrue("expected more paths", paths.hasNext());
Path path = paths.next();
if (path.length() != found && path.length() == 3) {
assertContains(path.nodes(), nodeA, nodeC, nodeE, nodeF);
} else if (path.length() != found && path.length() == 4) {
assertContains(path.nodes(), nodeA, nodeB, nodeD, nodeE, nodeF);
} else {
fail("unexpected path length: " + path.length());
}
found = path.length();
}
assertFalse("expected at most two paths", paths.hasNext());
}
}
use of org.neo4j.graphdb.Path in project neo4j by neo4j.
the class TestAStar method canUseBranchState.
@SuppressWarnings({ "rawtypes", "unchecked" })
@Test
public void canUseBranchState() throws Exception {
// This test doesn't use the predefined finder, which only means an unnecessary instantiation
// if such an object. And this test will be run twice (once for each finder type in data()).
/**
* <pre>
* 012345 A - B: 2
* +------>y A - B: 2
* 0|A B - C: 3
* 1| A - C: 10
* 2| B
* 3|
* 4|
* 5|
* 6|
* 7|C
* V
* x
*
* </pre>
*/
Node nodeA = graph.makeNode("A", "x", 0d, "y", 0d);
Node nodeB = graph.makeNode("B", "x", 2d, "y", 1d);
Node nodeC = graph.makeNode("C", "x", 7d, "y", 0d);
graph.makeEdge("A", "B", "length", 2d);
graph.makeEdge("A", "B", "length", 2d);
graph.makeEdge("B", "C", "length", 3d);
graph.makeEdge("A", "C", "length", 10d);
final Map<Node, Double> seenBranchStates = new HashMap<Node, Double>();
PathExpander<Double> expander = new PathExpander<Double>() {
@Override
public Iterable<Relationship> expand(Path path, BranchState<Double> state) {
double newState = state.getState();
if (path.length() > 0) {
newState += (Double) path.lastRelationship().getProperty("length");
state.setState(newState);
}
seenBranchStates.put(path.endNode(), newState);
return path.endNode().getRelationships(OUTGOING);
}
@Override
public PathExpander<Double> reverse() {
throw new UnsupportedOperationException();
}
};
double initialStateValue = 0D;
PathFinder<WeightedPath> traversalFinder = new TraversalAStar(expander, new InitialBranchState.State(initialStateValue, initialStateValue), doubleCostEvaluator("length"), ESTIMATE_EVALUATOR);
WeightedPath path = traversalFinder.findSinglePath(nodeA, nodeC);
assertEquals((Double) 5.0D, (Double) path.weight());
assertPathDef(path, "A", "B", "C");
assertEquals(MapUtil.<Node, Double>genericMap(nodeA, 0D, nodeB, 2D), seenBranchStates);
}
use of org.neo4j.graphdb.Path in project neo4j by neo4j.
the class ValuePathTest method canConstructPathWithRelationshipTraversedMultipleTimesInSameDirection.
@Test
public void canConstructPathWithRelationshipTraversedMultipleTimesInSameDirection() {
// Given A->C->B<-A->C->D
Path path = PATH_WITH_RELATIONSHIP_TRAVERSED_MULTIPLE_TIMES_IN_SAME_DIRECTION;
// Then
assertThat(path.length(), equalTo(5));
assertThat(path.startNode(), equalTo(ALICE));
assertThat(path.endNode(), equalTo(DAVE));
assertThat(nodes(path), equalTo(nodes(ALICE, CAROL, BOB, ALICE, CAROL, DAVE)));
assertThat(path.lastRelationship(), equalTo(CAROL_MARRIED_TO_DAVE));
assertThat(relationships(path), equalTo(relationships(ALICE_LIKES_CAROL, CAROL_DISLIKES_BOB, ALICE_KNOWS_BOB, ALICE_LIKES_CAROL, CAROL_MARRIED_TO_DAVE)));
}
use of org.neo4j.graphdb.Path in project neo4j by neo4j.
the class ValuePathTest method canConstructPathWithRelationshipTraversedAgainstItsDirection.
@Test
public void canConstructPathWithRelationshipTraversedAgainstItsDirection() {
// Given A->B<-C->D
Path path = PATH_WITH_RELATIONSHIP_TRAVERSED_AGAINST_ITS_DIRECTION;
// Then
assertThat(path.length(), equalTo(3));
assertThat(path.startNode(), equalTo(ALICE));
assertThat(path.endNode(), equalTo(DAVE));
assertThat(nodes(path), equalTo(nodes(ALICE, BOB, CAROL, DAVE)));
assertThat(path.lastRelationship(), equalTo(CAROL_MARRIED_TO_DAVE));
assertThat(relationships(path), equalTo(relationships(ALICE_KNOWS_BOB, CAROL_DISLIKES_BOB, CAROL_MARRIED_TO_DAVE)));
}
Aggregations