2

Situation here, I just do not want to loop externally in java for a gremlin query. Like I want to fetch all the paths or rather the name of the nodes/ edges in between of two separate nodes. Here the first query with loop would be

(Name of the first Node).loop(1){it.loops<100}{true}.has('name', (Name of the second node)).path{it.name}

converting which in java has taken a lots of code !

but in case of the above there is a loop which looping for 100 nodes. If I try to traverse through any big number of nodes. or the count is not numerable to me then how the full traversal would be ?

here I have got one suggestion like : g.v(100).out.loop(1) { it.object != g.v(200) }.path

How will it directly work inside java ? I want to avoid groovy !

Sagiruddin Mondal
  • 5,407
  • 2
  • 29
  • 44

1 Answers1

2

If you don't know the number of steps to loop you can just do:

g.v(100).out.loop(1){true}.path

That first closure after the loop represents a function that accepts loop metadata and return a boolean that decides whether or not to continue the looping process (true) or to terminate (false). As I simply return true above, the loop will not cease until all the paths are exhausted. Of course, the problem there is that if the graph cycles, the traversal will iterate indefinitely. It is therefore only safe to do a {true} if you are certain that your graph structure is such that you won't fall into that trap.

It is far better to simply have a max loop length of some reasonable size so that the traversal terminates. Or, alternatively, you could have some other intelligent method for managing loop termination. Maybe you could terminate the loop after some number of cycles is detected.

As far as wanting a Java answer instead of a groovy answer, I suggest that you read this answer about this topic. It explains how you go about converting groovy to java. For your specific problem, here's the java:

new GremlinPipeline(g.getVertex(100)).out().loop(1, 
    new PipeFunction<LoopPipe.LoopBundle<Vertex>,Boolean>() {
        public Boolean compute(LoopPipe.LoopBundle<Vertex> argument) {
            return true;
        }
    }, closure)
Community
  • 1
  • 1
stephen mallette
  • 45,298
  • 5
  • 67
  • 135