use of org.neo4j.graphdb.Path in project neo4j by neo4j.
the class DepthPitfallGraphTest method testSmallestPossibleInit.
@Test
void testSmallestPossibleInit() {
int count = 0;
try (Transaction transaction = beginTx()) {
Traverser traversal = transaction.traversalDescription().traverse(transaction.getNodeById(node("1").getId()));
for (Path position : traversal) {
count++;
assertNotNull(position);
assertNotNull(position.endNode());
if (position.length() > 0) {
assertNotNull(position.lastRelationship());
}
}
assertNotEquals(0, count, "empty traversal");
transaction.commit();
}
}
use of org.neo4j.graphdb.Path in project neo4j by neo4j.
the class TestShortestPath method testAnotherSimpleGraph.
@Test
void testAnotherSimpleGraph() {
// (n)---(p)---(q)
try (Transaction transaction = graphDb.beginTx()) {
graph.makeEdge(transaction, "s", "m");
graph.makeEdge(transaction, "m", "o");
graph.makeEdge(transaction, "s", "n");
graph.makeEdge(transaction, "n", "p");
graph.makeEdge(transaction, "p", "q");
graph.makeEdge(transaction, "q", "t");
graph.makeEdge(transaction, "n", "o");
graph.makeEdge(transaction, "o", "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,m,o,t", "s,n,o,t");
}, forTypeAndDirection(R1, BOTH), 6);
transaction.commit();
}
}
use of org.neo4j.graphdb.Path 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.graphdb.Path 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.graphdb.Path in project neo4j by neo4j.
the class Neo4jJsonCodecTest method testPathWriting.
@Test
void testPathWriting() throws IOException {
// Given
Path path = mock(Path.class);
Entity entity = mock(Entity.class);
when(entity.getAllProperties()).thenThrow(RuntimeException.class);
when(path.iterator()).thenReturn(Arrays.asList(entity).listIterator());
// When
assertThrows(Exception.class, () -> jsonCodec.writeValue(jsonGenerator, path));
// Then
verify(jsonGenerator).writeEndArray();
}
Aggregations