Gremlin-如何找到仅通过G的从A到Z的路径 [英] Gremlin- how to find paths from A to Z that went through G only

查看:327
本文介绍了Gremlin-如何找到仅通过G的从A到Z的路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Gremlin方面需要一些帮助:如果我知道起始顶点和终止顶点,并且起始和终止BUT之间有多个路径,则在此过程中会有几个顶点。如何根据已有的数据找到正确的路径?

Need some help with Gremlin: If I know the start vertex and the end vertex and there are multiple paths between start and end BUT I have a couple of vertexes along the way. How can I find the correct path based on the data I have?

例如,在这里我必须找到从学院到雀科的路径

For instance here I what I have to find the paths from 'college' to 'finch'

g.V().has('station','college').
       repeat(out().simplePath())
        .until(has('station','finch'))
        .path().by('station')

结果

==>[college, wellesley, bloor-yonge, rosedale, summerhill, st. clair, davisville, eglinton, lawrence, york mills, sheppard-yonge, north york centre, finch]
==>[college, dundas, queen, king, union, st. andrew, osgoode, st. patrick, queenspark, museum, st. george, bay, bloor-yonge, rosedale, summerhill, st. clair, davisville, eglinton, lawrence, york mills, sheppard-yonge, north york centre, finch]

但是,例如,如何获取经过 dundas的正确路径?

But how to i get the correct path that went THROUGH 'dundas' for example?

推荐答案

您可以使用路径绑定计数器仅当您在路径上找到某个元素时,您才递增:

You can use a path-bound counter that you only increment if you find a certain element along the path:

g.withSack(0).V().has('station','college').
  repeat(out().simplePath().
         choose(has('station','dundas'),
                  sack(sum).by(constant(1)))).
    until(has('station','finch')).
  filter(sack().is(1)).
  path().
    by('station')

添加更多必要的点(例如经过的过滤器路径) G H P 很容易

Adding more necessary points (e.g. filter paths that go through G, H and P) is easy with this approach.

但是,如果只有一个顶点是路径的一部分,那么sel-fish的答案是另一个有效的选择(不知道为什么它被否决了) )。

However, if it's only one vertex that has to be part of the path, then sel-fish's answer is another valid option (don't know why it got downvoted).

这篇关于Gremlin-如何找到仅通过G的从A到Z的路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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