2
votes

I am trying to make Gremlin show me the shortest path (regarding the cost, not the number vertices traveled) with meaningful information. There is a similar example in [Gremlin's Recipes]http://tinkerpop.apache.org/docs/3.2.1-SNAPSHOT/recipes/#shortest-path about how one can get all the paths and their respective costs from one vertex to another, but I can not find a way to get Gremlin to display meaningful information like names or age of vertices and weight of edges. For example one can not know who v[1} is from the result below.

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]]]

I know Gremlin supports a by()-step modulator for such task as in here:

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

, but I could not figure out how to combine the two solutions. Ideally the result I am looking for should be like this:

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

Any suggestions? Many Thanks in advance!

1

1 Answers

1
votes

You can add by modulator after the path step, and change the values into 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')

example: https://gremlify.com/2wk6e3d03fe