Question

I have the graph (see the picture below). I go with gremlin from A and need to loop all the tree up following the edges "impacts" and "similarTo".

So I need something like:

g.v(A.id).out('impacts', 'similarTo').loop(1){it.loops < 4}{true}

The problem with this query is, that the relation "similarTo" can be sometimes out edge and sometimes in edge (see graph), depends on how application stored the data at the moment. I can´t use simply "both" (g.v(A.id).both('impacts', 'similarTo')...) to retrieve edges since I need only out edge for "impacts" relations on the other hand. It´s also possible, that 'similarTo' edge is missing for some vertices (for example C), in this case loop should follow only 'impacts' relation.

graph

Finally I tried something like this, but I think i use it wrongly and the result is not appropriate:

g.v(A.id).as('x').copySplit(_().out('impacts'), _().both('similarTo')).fairMerge.loop('x'){it.loops < 4}{true}

I was also trying to filter edges by direction during traversing, but it´s not probably supported, so this also doesn´t work:

g.v(A.id).as('x').bothE.filter{((it.label=='impacts' & it.direction=='out') || it.label=='similarTo')}.bothV.loop('x'){it.loops < 4}{true}

Any suggestions?

Était-ce utile?

La solution

So finally I realized the proper way in gremlin should be:

g.v(A.id).as('x').copySplit(_().out('impacts'), _().both('similarTo')).fairMerge.loop('x'){it.loops < 4}{true}

But there is the bug in gremlin at the moment (see bug report) and copySplit doesn't work well with loop. Anyway I had to add ._() before copySplit to make it working:

g.v(A.id).as('x')._().copySplit(_().out('impacts'), _().both('similarTo')).fairMerge.loop('x'){it.loops < 4}{true}

Autres conseils

I've got no experience with Gremlin, but in Cypher you could do this as follows:

START n=node({id})
MATCH p=n-[:impacts|similarTo*3]-m
RETURN p

This query returns all paths containing unidirectional relationships of type impacts and similarTo with a depth of 3.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top