use of org.neo4j.graphalgo.BasicEvaluationContext in project neo4j by neo4j.
the class TestShortestPath method testCrossedCircle.
@Test
void testCrossedCircle() {
// (t)
try (Transaction transaction = graphDb.beginTx()) {
graph.makeEdge(transaction, "s", "1");
graph.makeEdge(transaction, "s", "3");
graph.makeEdge(transaction, "1", "2");
graph.makeEdge(transaction, "1", "4");
graph.makeEdge(transaction, "3", "2");
graph.makeEdge(transaction, "3", "4");
graph.makeEdge(transaction, "2", "t");
graph.makeEdge(transaction, "4", "t");
var context = new BasicEvaluationContext(transaction, graphDb);
testShortestPathFinder(context, finder -> assertPaths(finder.findAllPaths(graph.getNode(transaction, "s"), graph.getNode(transaction, "t")), "s,1,2,t", "s,1,4,t", "s,3,2,t", "s,3,4,t"), forTypeAndDirection(R1, BOTH), 3);
transaction.commit();
}
}
use of org.neo4j.graphalgo.BasicEvaluationContext in project neo4j by neo4j.
the class TestShortestPath method withFilters.
@Test
void withFilters() {
//
try (Transaction transaction = graphDb.beginTx()) {
graph.makeEdgeChain(transaction, "a,b,c,d");
graph.makeEdgeChain(transaction, "a,g,h,d");
final Node a = graph.getNode(transaction, "a");
final Node d = graph.getNode(transaction, "d");
final Node b = graph.getNode(transaction, "b");
b.setProperty("skip", true);
var context = new BasicEvaluationContext(transaction, graphDb);
final Predicate<Node> filter = item -> {
final boolean skip = (Boolean) item.getProperty("skip", false);
return !skip;
};
testShortestPathFinder(context, finder -> assertPaths(finder.findAllPaths(a, d), "a,g,h,d"), ((StandardExpander) allTypesAndDirections()).addNodeFilter(filter), 10);
transaction.commit();
}
}
use of org.neo4j.graphalgo.BasicEvaluationContext in project neo4j by neo4j.
the class TestAStar method testSimplest.
/**
* <pre>
* 01234567
* +-------->x A - C: 10
* 0|A C A - B: 2 (x2)
* 1| B B - C: 3
* V
* y
* </pre>
*/
@ParameterizedTest
@MethodSource("params")
void testSimplest(Function<EvaluationContext, PathFinder<WeightedPath>> finderFactory) {
try (Transaction transaction = graphDb.beginTx()) {
Node nodeA = graph.makeNode(transaction, "A", "x", 0d, "y", 0d);
Node nodeB = graph.makeNode(transaction, "B", "x", 2d, "y", 1d);
Node nodeC = graph.makeNode(transaction, "C", "x", 7d, "y", 0d);
Relationship relAB = graph.makeEdge(transaction, "A", "B", "length", 2d);
Relationship relAB2 = graph.makeEdge(transaction, "A", "B", "length", 2);
Relationship relBC = graph.makeEdge(transaction, "B", "C", "length", 3f);
Relationship relAC = graph.makeEdge(transaction, "A", "C", "length", (short) 10);
int counter = 0;
var context = new BasicEvaluationContext(transaction, graphDb);
Iterable<WeightedPath> allPaths = finderFactory.apply(context).findAllPaths(nodeA, nodeC);
for (WeightedPath path : allPaths) {
assertEquals((Double) 5d, (Double) path.weight());
assertPath(path, nodeA, nodeB, nodeC);
counter++;
}
assertEquals(1, counter);
transaction.commit();
}
}
use of org.neo4j.graphalgo.BasicEvaluationContext in project neo4j by neo4j.
the class TestAStar method pathToSelfReturnsZero.
@ParameterizedTest
@MethodSource("params")
void pathToSelfReturnsZero(Function<EvaluationContext, PathFinder<WeightedPath>> finderFactory) {
// GIVEN
try (Transaction transaction = graphDb.beginTx()) {
Node start = graph.makeNode(transaction, "start", "x", 0d, "y", 0d);
// WHEN
var context = new BasicEvaluationContext(transaction, graphDb);
WeightedPath path = finderFactory.apply(context).findSinglePath(start, start);
// THEN
assertNotNull(path);
assertEquals(start, path.startNode());
assertEquals(start, path.endNode());
assertEquals(0, path.length());
transaction.commit();
}
}
use of org.neo4j.graphalgo.BasicEvaluationContext in project neo4j by neo4j.
the class TestAStar method wikipediaExample.
@ParameterizedTest
@MethodSource("params")
void wikipediaExample(Function<EvaluationContext, PathFinder<WeightedPath>> finderFactory) {
/* GIVEN
*
* (start)---2--->(d)
* \ \
* 1.5 .\
* v 3
* (a)-\ v
* -2-\ (e)
* ->(b) \
* / \
* /-- 2
* /-3- v
* v --4------->(end)
* (c)------/
*/
try (Transaction transaction = graphDb.beginTx()) {
Node start = graph.makeNode(transaction, "start", "x", 0d, "y", 0d);
graph.makeNode(transaction, "a", "x", 0.3d, "y", 1d);
graph.makeNode(transaction, "b", "x", 2d, "y", 2d);
graph.makeNode(transaction, "c", "x", 0d, "y", 3d);
graph.makeNode(transaction, "d", "x", 2d, "y", 0d);
graph.makeNode(transaction, "e", "x", 3d, "y", 1.5d);
Node end = graph.makeNode(transaction, "end", "x", 3.3d, "y", 2.8d);
graph.makeEdge(transaction, "start", "a", "length", 1.5d);
graph.makeEdge(transaction, "a", "b", "length", 2f);
graph.makeEdge(transaction, "b", "c", "length", 3);
graph.makeEdge(transaction, "c", "end", "length", 4L);
graph.makeEdge(transaction, "start", "d", "length", (short) 2);
graph.makeEdge(transaction, "d", "e", "length", (byte) 3);
graph.makeEdge(transaction, "e", "end", "length", 2);
// WHEN
var context = new BasicEvaluationContext(transaction, graphDb);
WeightedPath path = finderFactory.apply(context).findSinglePath(start, end);
// THEN
assertPathDef(path, "start", "d", "e", "end");
transaction.commit();
}
}
Aggregations