英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论