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.graphdb.Path;
22 import org.neo4j.graphdb.Relationship;
23 import org.neo4j.graphdb.Node;
24 import org.neo4j.graphdb.traversal.PruneEvaluator;
25 import org.neo4j.graphdb.traversal.TraversalDescription;
26 import org.neo4j.helpers.Predicate;
27 import org.neo4j.kernel.Traversal;
28 import org.neo4j.kernel.Uniqueness;
29 import org.neo4j.server.plugins.Description;
30 import org.neo4j.server.plugins.PluginTarget;
31 import org.neo4j.server.plugins.ServerPlugin;
32 import org.neo4j.server.plugins.Source;
33
34
35
36
37
38
39 @Description( "Performs a depth two traversal along all relationship types." )
40 public class
41 DepthTwo extends ServerPlugin
42 {
43 @Description( "Traverse depth two and return the end nodes" )
44 @PluginTarget( Node.class )
45 public Iterable<Node> nodesOnDepthTwo( @Source Node node )
46 {
47 return traversal.traverse( node ).nodes();
48 }
49
50 @Description( "Traverse depth two and return the last relationships" )
51 @PluginTarget( Node.class )
52 public Iterable<Relationship> relationshipsOnDepthTwo( @Source Node node )
53 {
54 return traversal.traverse( node ).relationships();
55 }
56
57 @Description( "Traverse depth two and return the paths" )
58 @PluginTarget( Node.class )
59 public Iterable<Path> pathsOnDepthTwo( @Source Node node )
60 {
61 return traversal.traverse( node );
62 }
63
64 private static final TraversalDescription traversal = Traversal.description().uniqueness(
65 Uniqueness.RELATIONSHIP_PATH ).prune( new PruneEvaluator()
66 {
67 public boolean pruneAfter( Path position )
68 {
69 return position.length() >= 2;
70 }
71 } ).filter( new Predicate<Path>()
72 {
73 public boolean accept( Path item )
74 {
75 return item.length() == 2;
76 }
77 } );
78 }