广度首次枚举Gremlin中 [英] Breadth First enumeration in Gremlin

查看:158
本文介绍了广度首次枚举Gremlin中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用Gremlin获得第一个枚举,但是我很难找到一种方法来输出在枚举期间观察到的所有步骤。我只能打印出最后一次迭代的结果。



我的问题是,如果像这样的起始节点,我如何遵循所有路径(不知道整体深度)使用Gremlin并打印出我一路上找到的所有东西?

  study = gv('myId')

我已经尝试了散布方法,循环方法(尽管两者似乎都需要事先知道路径的实际长度,如果我理解正确的话)



非常感谢!

解决方案

您不提供任何显示您如何使用循环的重要代码,但我认为有了正确的论点,你可以做到你想做的事:

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

我假设您通过gather / scatter和循环的第一部分理解代码,该部分指向 x 。所以,考虑到这个假设,我将重点关注传递给循环的两个闭包。



传递给循环的第一个闭包告诉Gremlin什么时候跳出循环。通过简单地返回真实,你就是在说要耗尽循环。根据图表的结构,这可能不是可取的,因为您可能需要等很长时间才能找到结果。至少你应该考虑把它设置成不可能的高度,这样如果你在图中打了一些循环,你的遍历就会中断。第二个闭包被称为发出关闭。你可以阅读更多关于这里,但基本上它确定是否应该返回管道中的中间对象(不仅仅是循环结尾的中间对象)。在这种情况下,您可以看到我简单地将该值设置为 true ,以便它可以在循环的所有步骤中发出所有对象。


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 only print out the result of the last iteration.

My question would be, given a starting node like this, how can I follow all paths (without knowing the overall depth) using Gremlin and print out everything I find along the way?

study=g.v('myId')

I have tried the scatter approach, looping approach (although both seem to require knowledge about the actual length of the path beforehand if I understand correctly)

Many thanks!

解决方案

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.

这篇关于广度首次枚举Gremlin中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆