5

I have noticed double underscore being used in some step functions in Tinkerpop Gremlin 3.3. Could someone please tell why we use this double underscore with an example ? I could not find enough information about this in the documentation.

livetolearn
  • 221
  • 3
  • 11

2 Answers2

11

__. allows you to define an anonymous Traversal, ie. a Traversal which is not bound to a specific TraversalSource.

In the Gremlin console, all Gremlin steps are statically imported so you never need to prefix anonymous traversals with __. unless that anonymous traversal starts with a reserved keyword in the target language. In Groovy, which is the default Gremlin flavor, this is the case with in() and as() steps: because these are reserved keywords, these two steps must be prefixed with __.

In Java, you can avoid __. prefix by statically importing all steps in your program:

import static org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.__

See the small Note section in the documentation: http://tinkerpop.apache.org/docs/3.3.0/reference/#graph-traversal-steps

jbmusso
  • 3,436
  • 1
  • 25
  • 36
3

__ is used to refer to the incoming traversal (vertex or edge) in Java API. For example: in gremlin shell, one can write something like this

graph.traversal().V().repeat(out("edgeType")).until(hasLabel("label")).toList()

But in java one needs an anonymous traversal to call the functions "out" and "hasLabel" within repeat and until (or any other function like by, choose etc.). The above traversal in Java will look like:

graph.traversal().V().repeat(__.out("edgeType")).until(__.hasLabel("label")).toList()
Him
  • 1,609
  • 12
  • 20
  • A minor nit to your answer - that first traversal won't work in the Gremlin Console. `in` is a reserved word in Groovy so you'll actually get an error. If you change your example to use `out` that would be better. – stephen mallette Nov 16 '17 at 11:32
  • 4
    I think "__ is used to refer to the incoming traversal (vertex or edge) " is a bit misleading. It doesn't really refer to a vertex or an edge, it's just a way to define an anonymous traversal. Typically, such anonymous traversal is passed as an argument to another traversal step and it then dictates the behavior of the current traverser (ie. filtering, projecting, branching, etc.) when iterating the traversal. – jbmusso Nov 16 '17 at 14:07