Gremlin-显示最短成本最低的路径并提供有意义的信息

问题描述

我正在尝试使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,e[3][2-kNows->3],v[3],e[4][3-kNows->4],v[5]]]

我知道Gremlin支持by()步调制器来执行以下任务:

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

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

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

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

解决方法

您可以在by步骤之后添加path调制器,并将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