0

I really like Gremlin but I think it's sometimes really hard to convert the Code of the Console to Java-Code For Example this:

g.E().project('EDGE','IN','OUT','PROP')
     .by(id())
     .by(inV().union(id()).fold())
     .by(outV().union(id()).fold())
     .by(properties().fold())

Works fine in the Console but not in Java. Can someone help me with this code or maybe give me a good addvice for the future ?

lucas
  • 197
  • 4
  • 14

1 Answers1

2

The Gremlin Console automatically has a host of static imports in place so that you can save keystrokes and make Gremlin look less verbose. When you do:

g.E().project('EDGE','IN','OUT','PROP')
     .by(id())
     .by(inV().union(id()).fold())
     .by(outV().union(id()).fold())
     .by(properties().fold())

What you're really doing is:

g.E().project('EDGE','IN','OUT','PROP')
     .by(__.id())
     .by(__.inV().union(__.id()).fold())
     .by(__.outV().union(__.id()).fold())
     .by(__.properties().fold())

In your Java application you merely need to include an import statement like:

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

and the original syntax from the Groovy console will paste perfectly into a Java application. Or if you prefer the more verbose syntax use a standard import of the __ class and then explicitly use that to spawn your child traversals as demonstrated in teh second example above. Please see the full list of suggested imports in the Reference Documentation.

stephen mallette
  • 45,298
  • 5
  • 67
  • 135
  • 2
    note that when possible you should use `T.id` or just `id` (again - static import), rather than `id()` step - https://tinkerpop.apache.org/docs/current/recipes/#steps-instead-of-tokens – stephen mallette Apr 09 '20 at 19:45