1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.neo4j.examples.server.plugins;
20
21 import org.neo4j.graphalgo.GraphAlgoFactory;
22 import org.neo4j.graphalgo.PathFinder;
23 import org.neo4j.graphdb.DynamicRelationshipType;
24 import org.neo4j.graphdb.Expander;
25 import org.neo4j.graphdb.Node;
26 import org.neo4j.graphdb.Path;
27 import org.neo4j.kernel.Traversal;
28 import org.neo4j.server.plugins.Description;
29 import org.neo4j.server.plugins.Parameter;
30 import org.neo4j.server.plugins.PluginTarget;
31 import org.neo4j.server.plugins.ServerPlugin;
32 import org.neo4j.server.plugins.Source;
33
34 public class ShortestPath extends ServerPlugin
35 {
36 @Description( "Find the shortest path between two nodes." )
37 @PluginTarget( Node.class )
38 public Iterable<Path> shortestPath(
39 @Source Node source,
40 @Description( "The node to find the shortest path to." ) @Parameter( name = "target" ) Node target,
41 @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,
42 @Description( "The maximum path length to search for, default value (if omitted) is 4." ) @Parameter( name = "depth", optional = true ) Integer depth )
43 {
44 Expander expander;
45 if ( types == null )
46 {
47 expander = Traversal.expanderForAllTypes();
48 }
49 else
50 {
51 expander = Traversal.emptyExpander();
52 for ( int i = 0; i < types.length; i++ )
53 {
54 expander = expander.add( DynamicRelationshipType.withName( types[i] ) );
55 }
56 }
57 PathFinder<Path> shortestPath = GraphAlgoFactory.shortestPath( expander, depth == null ? 4
58 : depth.intValue() );
59 return shortestPath.findAllPaths( source, target );
60 }
61 }