Breadth First enumeration in Gremlin

后端 未结 1 1945
被撕碎了的回忆
被撕碎了的回忆 2021-01-07 07:10

I\'m trying to get breadth first enumeration working with Gremlin, however I\'m having trouble finding a way to output all the steps observed during the enumeration. I can o

相关标签:
1条回答
  • 2021-01-07 07:38

    You don't provide any significant code that shows how you are using loop, but I think with the right arguments you can get it to do what you want:

    gremlin> g = TinkerGraphFactory.createTinkerGraph()
    ==>tinkergraph[vertices:6 edges:6]
    gremlin> g.v(1).as('x').out.gather.scatter.loop('x'){true}{true}
    ==>v[2]
    ==>v[4]
    ==>v[3]
    ==>v[5]
    ==>v[3]
    

    I'm assuming that you understand the code through gather/scatter and the first part of the loop that points back to x. So, with that assumption in mind, I'll focus on the two closures passed to loop.

    The first closure passed to loop tells Gremlin when to break out of the loop. By simply returning true, you're saying to exhaust the loop. Depending on the structure of your graph, that may not be advisable as you could be waiting for a very long time for a result to come back. At a minimum you should consider setting it to something impossibly high so that if you do hit some cycle in the graph, your traversal breaks.

    The second closure is known as the "emit closure". You can read more about it here, but basically it determines if intermediate objects in the pipe (not just the ones at the end of the loop) should be returned or not. In this case, you can see I simply set that value to true so that it would emit all objects at all steps of the loop.

    0 讨论(0)
提交回复
热议问题