Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to collect optional branches in TinkerPop3?

I have a graph that looks like this: graph

Using gremlin-scala, I'm trying to traverse from A and collect these tuples:

(A, Some(A1)), (B, None), (C, Some(A2))

So essentially I want to repeatedly take α out edges and optionally branch to β, collecting those outs. I am guessing I need to inject an empty "step" if there's no β edge but I haven't been able to figure out how to do that.

I'm also a bit confused about how to rewind after traversing β now that jump has been mysteriously removed (TP 3.1+)

So far I have something like:

graph.V("A").untilWithTraverser(t => t.get.outE(α).notExists()
    ).repeat(_.out(α).as(foo).out(β).as(bar)).select((foo,bar)).toList

But this doesn't rewind back to the main traversal and fails if any nodes on the "trunk" are missing a β out edge

like image 326
Arkadiy Kukarkin Avatar asked Dec 29 '25 20:12

Arkadiy Kukarkin


1 Answers

I can't provide a gremlin-scala solution, but it should be easy for you to convert the following Groovy example:

g.V("A").until(__.not(outE("alpha"))).
           repeat(out("alpha")).path().by(union(identity(), out("beta")).fold())

This will return:

[[A, A1], [B], [C, A2]]

IMO this is sufficient. However, if you need a consistent set of 2 entries, you could do something like this:

g.V("A").until(__.not(outE("alpha"))).repeat(out("alpha")).
  path().by(union(identity(), coalesce(out("beta"), constant("N/A"))).fold())

...which will then return:

[[A, A1], [B, N/A], [C, A2]]

Complete Session:

http://gremlinbin.com/bin/view/57133bdc8ee00

like image 129
Daniel Kuppitz Avatar answered Dec 31 '25 15:12

Daniel Kuppitz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!