Database Reference
In-Depth Information
Finally, you add an evaluator to
TraversalDescription
. The evaluator is re-
sponsible for two decisions as part of the Neo4j Traversal API:
• It determines whether or not the current node being visited should be added to the
traversal result.
• It determines if the traversal should continue further down the current path of the
graph,orifthepathitcurrentlyevaluatesshouldbeabandoned,movingtothenext
path if possible.
The evaluators in Neo4j are defined using the
org.neo4j.graphdb.traversal.Evaluator
interface. Neo4j provides a num-
ber of convenient out-of-the-box implementations that you can use. The provided im-
plementations are accessible via static factory methods in the
org.neo4j.graphdb.traversal.Evaluators
class. In
listing 4.6
,
you use the
Evaluators.atDepth(int depth)
evaluator, which simply accepts all nodes at
the specified depth, counting from the starting node. In addition, this evaluator stops any
traversal at a depth higher than specified.
The other useful evaluator implementations provided by Neo4j are covered in
chapter 8
.
EvaluatorsareoneofthekeyconceptsoftheNeo4jTraversalAPI,andit'slikelythatyou'll
need to implement your own custom evaluators often. We'll look at a custom implementa-
tion of the
Evaluator
interface in the next section.
4.2.2. Implementing a custom evaluator
Younowneedtoimprove theprevious section'scodetoexclude movies that Johnhasseen
from the result. To do that, you need to add a new rule to the traversal description. We
mentioned earlier that Neo4j's
Evaluator
implementation defines which nodes to keep
in the result and which to discard. In addition, it defines when the traverser should stop the
traversal altogether. Based on that, you can implement an additional custom evaluator that
will exclude the movies already seen by the user.
The
Evaluator
interface defines a single method that you need to implement,
public
Evaluation evaluate(Path path)
. This method accepts a single argument of
type
org.neo4j.graphdb.Path
that represents all nodes and relationships that were
traverseduntilthecurrentnode.Thisinterface definesanumberofconvenientmethodsfor