我如何将Gremlin控制台代码转换为Java代码?

huangapple go评论64阅读模式
英文:

How do I convert Gremlin-Console-Code in Java-Code?

问题

我非常喜欢 Gremlin,但我觉得有时候将控制台代码转换为 Java 代码非常困难。例如这段代码:

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

在控制台中可以正常工作,但在 Java 中却不行。有人可以帮我处理这段代码,或者也许可以给我一些建议以供将来参考吗?

英文:

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 ?

答案1

得分: 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 the second example above. Please see the full list of suggested imports in the Reference Documentation.

英文:

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.

huangapple
  • 本文由 发表于 2020年4月10日 02:56:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/61128321.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定