Friday, June 17, 2016

Gremlin BFS enumeration including root

Leave a Comment

I want to include the root node in the output for the following Gremlin expression:

gremlin> g = TinkerGraphFactory.createTinkerGraph() ==>tinkergraph[vertices:6 edges:6] gremlin> g.v(1).as('x').out.loop('x'){true}{true} ==>v[2] ==>v[4] ==>v[3] ==>v[5] ==>v[3] 

So that the output includes v[1] as well. How can I achieve that?

1 Answers

Answers 1

Here are a couple of possible solutions in TinkerPop 2.x First one that uses store():

gremlin> x=[];g.v(1).store(x).as('x').out.loop('x'){true}{true}.store(x).iterate();x         ==>v[1] ==>v[3] ==>v[2] ==>v[4] ==>v[3] ==>v[5] 

Here's a second that doesn't explicitly force creation of an external variable that uses transform and a closure:

gremlin> g.v(1).transform{[it] + (it._().as('x').out.loop('x'){true}{true}.toList())}.scatter()     ==>v[1] ==>v[3] ==>v[2] ==>v[4] ==>v[3] ==>v[5] 

For those using TinkerPop 3.x, simply place the emit() in front of the repeat():

gremlin> g.V(1).emit().repeat(out()) ==>v[1] ==>v[3] ==>v[2] ==>v[4] ==>v[5] ==>v[3] 
If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment