Database Reference
In-Depth Information
To use the newly created expander, you specify it using the TraversalDescrip-
tion.expand(...) method
. The rest of the code is the same as before.
When you execute this traversal, the result will be exactly as expected:
Alien
Fargo
OrderedByTypeExpander gives you more power when you're designing traversals.
In some cases, however, none of Neo4j's built-in traversals provides the functionality that
the problem requires. In that case, you can implement your own custom expanders using
the Neo4j Traversal API; that's what we're going to show you in the next section.
8.2.3. Custom expanders
Neo4j has an extension point readily available for when you need to build a custom ex-
pander that meets your requirements. The extension API is defined with the Path-Ex-
pander interface, which is part of the Neo4j kernel library.
The PathExpander interface defines two methods you need to implement:
Iterable<Relationship> expand( Path path, BranchState<STATE> state );
—This method contains the logic that determines which relationships the traverser
should follow (and in which order) from the current node of the traversal. The ar-
gument path contains the full path (including nodes and relationships) that was
traversed up to the current point in the graph. You can use all available methods of
the path object as usual. For example,
Path.endNode() gets the node you're currently visiting.
Path.startNode() gets the node from which the traversal started.
Path.lastRelationship() retrieves the last relationships tra-
versed before you reached the current point in the graph.
In addition to the traversal path, you have access to another method argument,
BranchState , which represents the state associated with the current branch of
the traversal. It's optional, and typically expanders won't use it.
PathExpander<STATE> reverse(); —This method returns the expander with the
same expanding logic, but with reversed direction, and it's typically used in bid-
Search WWH ::




Custom Search