Search in sources :

Example 1 with Description

use of org.neo4j.server.plugins.Description 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;
}
Also used : Path(org.neo4j.graphdb.Path) Transaction(org.neo4j.graphdb.Transaction) ArrayList(java.util.ArrayList) PathExpanderBuilder(org.neo4j.graphdb.PathExpanderBuilder) PluginTarget(org.neo4j.server.plugins.PluginTarget) Description(org.neo4j.server.plugins.Description)

Aggregations

ArrayList (java.util.ArrayList)1 Path (org.neo4j.graphdb.Path)1 PathExpanderBuilder (org.neo4j.graphdb.PathExpanderBuilder)1 Transaction (org.neo4j.graphdb.Transaction)1 Description (org.neo4j.server.plugins.Description)1 PluginTarget (org.neo4j.server.plugins.PluginTarget)1