Reputation: 39
I want to traverse a graph and return all paths that link 2 nodes where the first relationship is outgoing and the second is incoming. For example if the relationship is Voted and i want to see all possible paths from node 25 to node 86 i have MATCH p=((n {id:"25"}) -[*1..2]-> (m {id:"86"})) RETURN p;
Then I want to check if in the returned path i have the same kind of property in the outgoing and incoming relationship (if they have the same vote).
I try to accomplish that with the graph traversal api in java but all i get back is a single path how can I get all possible paths in order to check them?
{it is basically a checking relationships with all common neighbors problem}
int common = 0;
int diff = 0;
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))
// .evaluator(Evaluators.excludeStartPosition())
.traverse(node1))
{
Iterable<Relationship> myRels = position.reverseRelationships();
for (Relationship temp : myRels) {
System.out.println((temp.getStartNode()).getProperty("id") + " with " + temp.getProperty("with") + " :" + (temp.getEndNode()).getProperty("id"));
}
String with = "";
int i = 0;
for (Relationship temp : myRels) {
if (i == 0) {
with = (String) temp.getProperty("with");
i++;
}
if (i == 1) {
if (((String) temp.getProperty("with")).equals(with)) {
common++;
} else {
diff++;
}
}
}
}
return (double) common * 100 / (common + diff);
Upvotes: 1
Views: 654
Reputation: 18002
Traversals have uniqueness rules. Read that link, it talks about how the traverser works, and how you can configure it. By default, the uniqueness rules are set to NODE_GLOBAL
meaning that a certain node cannot be traversed more than once.
I suspect this might be your problem; if you're looking for one target node, but you want all paths to that node, you should use RELATIONSHIP_GLOBAL
instead, or one of the other options outlined by the documentation. Your traverser is looking for one end node, and by default you can only traverse that node once.
So to try this fix, add a different uniqueness to your traversal description:
graphDb.traversalDescription()
.relationships(Wikilections_user.RelTypes.Voted, Direction.OUTGOING)
.relationships(Wikilections_user.RelTypes.Voted, Direction.INCOMING)
.uniqueness( Uniqueness.RELATIONSHIP_GLOBAL );
Upvotes: 2