use of org.neo4j.graphdb.Path in project neo4j-documentation by neo4j.
the class Person method getShortestPathTo.
public Iterable<Person> getShortestPathTo(Person otherPerson, int maxDepth) {
// use graph algo to calculate a shortest path
PathFinder<Path> finder = GraphAlgoFactory.shortestPath(forTypeAndDirection(FRIEND, BOTH), maxDepth);
Path path = finder.findSinglePath(underlyingNode, otherPerson.getUnderlyingNode());
return createPersonsFromNodes(path);
}
use of org.neo4j.graphdb.Path in project neo4j-documentation by neo4j.
the class ShortestPath method shortestPath.
@Description("Find the shortest path between two nodes.")
@PluginTarget(Node.class)
public Iterable<Path> shortestPath(@Source Node source, @Description("The node to find the shortest path to.") @Parameter(name = "target") Node target, @Description("The relationship types to follow when searching for the shortest path(s). " + "Order is insignificant, if omitted all types are followed.") @Parameter(name = "types", optional = true) String[] types, @Description("The maximum path length to search for, default value (if omitted) is 4.") @Parameter(name = "depth", optional = true) Integer depth) {
PathExpander<?> expander;
List<Path> paths = new ArrayList<>();
if (types == null) {
expander = PathExpanders.allTypesAndDirections();
} else {
PathExpanderBuilder expanderBuilder = PathExpanderBuilder.empty();
for (int i = 0; i < types.length; i++) {
expanderBuilder = expanderBuilder.add(RelationshipType.withName(types[i]));
}
expander = expanderBuilder.build();
}
try (Transaction tx = source.getGraphDatabase().beginTx()) {
PathFinder<Path> shortestPath = GraphAlgoFactory.shortestPath(expander, depth == null ? 4 : depth.intValue());
for (Path path : shortestPath.findAllPaths(source, target)) {
paths.add(path);
}
tx.success();
}
return paths;
}
use of org.neo4j.graphdb.Path in project neo4j-apoc-procedures by neo4j-contrib.
the class ExpandPathTest method testLimitPlaysNiceWithMinLevel.
@Test
public void testLimitPlaysNiceWithMinLevel() {
db.execute("MATCH (c:Person) WHERE c.name in ['Clint Eastwood', 'Gene Hackman'] SET c:Western");
TestUtil.testResult(db, "MATCH (k:Person {name:'Keanu Reeves'}) " + "CALL apoc.path.expandConfig(k, {relationshipFilter:'ACTED_IN|PRODUCED|DIRECTED', labelFilter:'>Western', uniqueness: 'NODE_GLOBAL', limit:1, minLevel:3}) yield path " + "return path", result -> {
List<Map<String, Object>> maps = Iterators.asList(result);
assertEquals(1, maps.size());
Path path = (Path) maps.get(0).get("path");
assertEquals("Clint Eastwood", path.endNode().getProperty("name"));
});
}
use of org.neo4j.graphdb.Path in project neo4j-apoc-procedures by neo4j-contrib.
the class ExpandPathTest method testEndNodeFilterAsWhitelist.
@Test
public void testEndNodeFilterAsWhitelist() {
db.execute("MATCH (c:Person) WHERE c.name in ['Clint Eastwood', 'Gene Hackman'] SET c:Western");
TestUtil.testResult(db, "MATCH (k:Person {name:'Keanu Reeves'}) " + "CALL apoc.path.expandConfig(k, {relationshipFilter:'ACTED_IN|PRODUCED|DIRECTED', labelFilter:'>Western|+Movie', uniqueness: 'NODE_GLOBAL', filterStartNode:false}) yield path " + "return path", result -> {
List<Map<String, Object>> maps = Iterators.asList(result);
assertEquals(2, maps.size());
Path path = (Path) maps.get(0).get("path");
assertEquals("Gene Hackman", path.endNode().getProperty("name"));
path = (Path) maps.get(1).get("path");
assertEquals("Clint Eastwood", path.endNode().getProperty("name"));
});
}
use of org.neo4j.graphdb.Path in project neo4j-apoc-procedures by neo4j-contrib.
the class ExpandPathTest method testTerminationFilterBeforeWhitelist.
@Test
public void testTerminationFilterBeforeWhitelist() {
db.execute("MATCH (c:Person) WHERE c.name in ['Clint Eastwood', 'Gene Hackman', 'Christian Bale'] SET c:Western");
TestUtil.testResult(db, "MATCH (k:Person {name:'Keanu Reeves'}) " + "CALL apoc.path.expandConfig(k, {relationshipFilter:'ACTED_IN|PRODUCED|DIRECTED', labelFilter:'/Western|+Movie', uniqueness: 'NODE_GLOBAL', filterStartNode:false}) yield path " + "return path", result -> {
List<Map<String, Object>> maps = Iterators.asList(result);
assertEquals(1, maps.size());
Path path = (Path) maps.get(0).get("path");
assertEquals("Gene Hackman", path.endNode().getProperty("name"));
});
}
Aggregations