user1189332
user1189332

Reputation: 1941

TinkerPop 3 Gremlin Collect Edge Properties during traversal

Using TinkerPop 3 Java APIs:

My graph looks like this:

james {risk: 'n'} --> chris {risk: 'n'} --> raj {risk: 'n'} --> joanne {risk: 'y'}

The edge label is 'travelledWith' and a property called 'pnrLocator'

Now, I want to traverse from james till a vertex where the risk is set to 'y'. Along the way, I'd want to collect the vertex and edge properties.

This is what I have which only works for vertex properties. How can I add a 'by' and collect the 'pnrLocator' too?

GraphTraversal<Vertex, ?> values =
                g.traversal()
                        .V()
                        .has("personId", "james")
                        .repeat(out("travelledWith"))
                        .until(has("risk", "y"))
                        .limit(100)
                        .path()
                        .by("personId");


        values.forEachRemaining(v -> System.out.println(v));

Upvotes: 0

Views: 936

Answers (1)

stephen mallette
stephen mallette

Reputation: 46226

The path() step is going to output any elements Gremlin traverses given the steps you provided so, using the "modern" TinkerPop toy graph:

gremlin> g.V().repeat(out()).emit().path().by("name")
==>[marko,lop]
==>[marko,vadas]
==>[marko,josh]
==>[marko,josh,ripple]
==>[marko,josh,lop]
==>[josh,ripple]
==>[josh,lop]
==>[peter,lop]

I traversed out() which returns vertices so that's the only output I see in the path output. If I change my traversal a little to explicitly traverse the edges (i.e. out() to outE().inV()) then I can do this:

gremlin> g.V().repeat(outE().inV()).emit().path().by("name").by('weight')
==>[marko,0.4,lop]
==>[marko,0.5,vadas]
==>[marko,1.0,josh]
==>[marko,1.0,josh,1.0,ripple]
==>[marko,1.0,josh,0.4,lop]
==>[josh,1.0,ripple]
==>[josh,0.4,lop]
==>[peter,0.2,lop]

Upvotes: 2

Related Questions