use of org.neo4j.graphalgo.BasicEvaluationContext in project neo4j by neo4j.
the class TestShortestPath method testSimplestGraph.
@Test
void testSimplestGraph() {
// \__/
try (Transaction transaction = graphDb.beginTx()) {
graph.makeEdge(transaction, "s", "t");
graph.makeEdge(transaction, "s", "t");
var context = new BasicEvaluationContext(transaction, graphDb);
testShortestPathFinder(context, finder -> {
final Iterable<Path> paths = finder.findAllPaths(graph.getNode(transaction, "s"), graph.getNode(transaction, "t"));
assertPaths(paths, "s,t", "s,t");
assertPaths(asList(finder.findSinglePath(graph.getNode(transaction, "s"), graph.getNode(transaction, "t"))), "s,t");
}, forTypeAndDirection(R1, BOTH), 1);
transaction.commit();
}
}
use of org.neo4j.graphalgo.BasicEvaluationContext in project neo4j by neo4j.
the class TestShortestPath method makeSureShortestPathsReturnsNoLoops.
@Test
void makeSureShortestPathsReturnsNoLoops() {
//
try (Transaction transaction = graphDb.beginTx()) {
graph.makeEdgeChain(transaction, "a,b,c,d,b,c,e");
var context = new BasicEvaluationContext(transaction, graphDb);
testShortestPathFinder(context, finder -> {
final Node a = graph.getNode(transaction, "a");
final Node e = graph.getNode(transaction, "e");
assertPaths(finder.findAllPaths(a, e), "a,b,c,e", "a,b,c,e");
}, forTypeAndDirection(R1, BOTH), 6);
transaction.commit();
}
}
use of org.neo4j.graphalgo.BasicEvaluationContext in project neo4j by neo4j.
the class TestShortestPath method makeSureRelationshipNotConnectedIssueNotThere.
@Test
void makeSureRelationshipNotConnectedIssueNotThere() {
/*
* (g)
* / ^
* v \
* (a)<--(b)<--(c)<--(d)<--(e)<--(f) (i)
* ^ /
* \ v
* (h)
*/
try (Transaction transaction = graphDb.beginTx()) {
graph.makeEdgeChain(transaction, "i,g,f,e,d,c,b,a");
graph.makeEdgeChain(transaction, "i,h,f");
var context = new BasicEvaluationContext(transaction, graphDb);
testShortestPathFinder(context, finder -> {
final Node start = graph.getNode(transaction, "a");
final Node end = graph.getNode(transaction, "i");
assertPaths(finder.findAllPaths(start, end), "a,b,c,d,e,f,g,i", "a,b,c,d,e,f,h,i");
}, forTypeAndDirection(R1, INCOMING), 10);
transaction.commit();
}
}
use of org.neo4j.graphalgo.BasicEvaluationContext in project neo4j by neo4j.
the class TestShortestPath method makeSureDescentStopsWhenPathIsFound.
@Test
void makeSureDescentStopsWhenPathIsFound() {
/*
* (a)==>(b)==>(c)==>(d)==>(e)
* \
* v
* (f)-->(g)-->(h)-->(i)
*/
try (Transaction transaction = graphDb.beginTx()) {
graph.makeEdgeChain(transaction, "a,b,c,d,e");
graph.makeEdgeChain(transaction, "a,b,c,d,e");
graph.makeEdgeChain(transaction, "a,f,g,h,i");
final Node a = graph.getNode(transaction, "a");
final Node b = graph.getNode(transaction, "b");
final Node c = graph.getNode(transaction, "c");
final Set<Node> allowedNodes = new HashSet<>(asList(a, b, c));
var context = new BasicEvaluationContext(transaction, graphDb);
final PathFinder<Path> finder = new ShortestPath(context, 100, PathExpanders.forDirection(OUTGOING)) {
@Override
protected Node filterNextLevelNodes(Node nextNode) {
if (!allowedNodes.contains(nextNode)) {
return null;
}
return nextNode;
}
};
Iterator<Path> paths = finder.findAllPaths(a, c).iterator();
for (int i = 0; i < 4; i++) {
Path aToBToC = paths.next();
assertPath(aToBToC, a, b, c);
}
assertFalse(paths.hasNext(), "should only have contained four paths");
transaction.commit();
}
}
use of org.neo4j.graphalgo.BasicEvaluationContext in project neo4j by neo4j.
the class TestShortestPath method makeSureShortestPathCanBeFetchedEvenIfANodeHasLoops.
@Test
void makeSureShortestPathCanBeFetchedEvenIfANodeHasLoops() {
// (p)
try (Transaction transaction = graphDb.beginTx()) {
graph.makeEdgeChain(transaction, "m,s,n,p");
graph.makeEdgeChain(transaction, "m,o,n");
graph.makeEdge(transaction, "o", "o");
graph.makeEdge(transaction, "n", "n");
var context = new BasicEvaluationContext(transaction, graphDb);
testShortestPathFinder(context, finder -> assertPaths(finder.findAllPaths(graph.getNode(transaction, "m"), graph.getNode(transaction, "p")), "m,s,n,p", "m,o,n,p"), forTypeAndDirection(R1, BOTH), 3);
transaction.commit();
}
}
Aggregations