anu
anu

Reputation: 39

traverser Relationship direction order neo4j java

I want to traverse a graph with the same relationship but firstly the outgoing and secondly the incoming (in this specific order). I have found

    traverse(Traverser.Order traversalOrder, 
StopEvaluator stopEvaluator,ReturnableEvaluator returnableEvaluator, 
RelationshipType firstRelationshipType,Direction firstDirection, 
RelationshipType secondRelationshipType,Direction secondDirection)

I havent found any examples on how to fill the fields traversalOrder stopEvaluator, returnableEvaluator

My example code is :

for (Path position : graphDb.traversalDescription()
            .relationships(Wikilections_user.RelTypes.Voted, Direction.OUTGOING)
            .relationships(Wikilections_user.RelTypes.Voted, Direction.INCOMING)
            .evaluator(Evaluators.fromDepth(1))
            .evaluator(Evaluators.toDepth(2))
            .evaluator(Evaluators.includeWhereEndNodeIs(node2))
            .uniqueness(Uniqueness.RELATIONSHIP_PATH)
            //.evaluator(Evaluators.excludeStartPosition())
            .traverse(node1)) {

I want to change the part .traverse(node1)) in order to return only paths where firstly i encounter outgoing and secondly i encounter incoming relationship. How is that possible?

Upvotes: 1

Views: 126

Answers (1)

Stefan Armbruster
Stefan Armbruster

Reputation: 39925

For more complex behaviour you can supply a custom PathExpander using TraversalDescription.expand(). A pseudo implementation could look like:

class MyPathExpander implements PathExpander {
   Iterable<Relationship> expand(Path path, BranchState state) {
      switch (path.length()) {
         case 0:
          return path.endNode().getRelationships(Wikilections_user.RelTypes.Voted, Direction.OUTGOING);
         case 1:
          return path.endNode().getRelationships(Wikilections_user.RelTypes.Voted, Direction.INCOMING)
         default: 
             return Iterables.empty();

      }
   }
}

Upvotes: 1

Related Questions