【问题标题】:Gremlin - Showing the shortest (lowest cost) path in with meaningful informationGremlin - 用有意义的信息显示最短(最低成本)的路径
【发布时间】:2020-08-16 15:28:04
【问题描述】:

我试图让 Gremlin 向我展示包含有意义信息的最短路径(关于成本,而不是经过的顶点数)。 [Gremlin's Recipes]http://tinkerpop.apache.org/docs/3.2.1-SNAPSHOT/recipes/#shortest-path 中有一个类似的例子,关于如何获取从一个顶点到另一个顶点的所有路径及其各自的成本,但我无法找到一种方法让 Gremlin 显示有意义的信息,如姓名或年龄边的顶点和权重。例如,从下面的结果中无法知道v[1} 是谁。

gremlin> g.V(1).repeat(outE().inV().simplePath()).until(hasId(5)).
           path().as('p').
           map(unfold().coalesce(values('weight'),
                                 constant(0.0)).sum()).as('cost').
           select('cost','p') //(4)
==>[cost:3.00, p:[v[1], e[0][1-knows->2], v[2], e[1][2-knows->4], v[4], e[2][4-knows->5], v[5]]]
==>[cost:2.00, p:[v[1], e[0][1-knows->2], v[2], e[3][2-knows->3], v[3], e[4][3-knows->4], v[4], e[2][4-knows->5], v[5]]]

我知道 Gremlin 支持一个 by()-step 调制器来完成这样的任务:

gremlin> g.V().out().out().path().by('name').by('age')
==>[marko,32,ripple]
==>[marko,32,lop]

,但我不知道如何结合这两种解决方案。理想情况下,我正在寻找的结果应该是这样的:

==>[duration:2, path:[Chicago, supertrain, New York]]

有什么建议吗?非常感谢!

【问题讨论】:

    标签: gremlin


    【解决方案1】:

    您可以在path 步骤之后添加by 调制器,并将values 更改为select

    g.V().hasLabel('A').repeat(outE().inV().
        simplePath()).
      until(hasLabel('C')).path().
        by(valueMap().with(WithOptions.tokens)).as('p').
      map(unfold().
        coalesce(
          select('distance'),
          constant(0.0)
        ).sum()).
        as('cost').
        select('cost', 'p')
    

    示例:https://gremlify.com/2wk6e3d03fe

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多