简体   繁体   English

匿名遍历 vs 普通遍历 gremlin

[英]Anonymous traversal vs normal traversal gremlin

I have read the documentation about anonymous traversals.我已阅读有关匿名遍历的文档。 I understand they can be started with __ and they can be used inside step modulators.我知道它们可以从__开始,并且可以在步进调制器中使用。 Although I dont understand it conceptually.虽然我从概念上看不懂。 Why cannot we use a normal traversal spawned from graph traversal source inside step modulators?为什么我们不能使用从步进调制器内的图遍历源生成的正常遍历? For example, in the following gremlin code to create an edge例如,在下面的 gremlin 代码中创建一条边

        this.g
            .V(fromId) // get vertex of id given for the source
            .as("fromVertex") // label as fromVertex to be accessed later
            .V(toId) // get  vertex of id given for destination
            .coalesce( // evaluates the provided traversals in order and returns the first traversal that emits at least one element
                inE(label) // check incoming edge of label given
                    .where( // conditional check to check if edge exists
                        outV() // get destination vertex of the edge to check
                            .as("fromVertex")), // against staged vertex
                addE(label) // add edge if not present
                    .property(T.id, id) // with given id
                    .from("fromVertex")) // from source vertexx
            .next(); // end traversal to commit to graph

why are __.inE() and __.addE() anonymous?为什么__.inE()__.addE()是匿名的? Why cannot we write this.g.inE() and this.g.addE() instead?为什么我们不能写this.g.inE()this.g.addE()呢? Either ways, the compiler is not complaining.无论哪种方式,编译器都不会抱怨。 So what special benefit does anonymous traversal gives us here?那么匿名遍历在这里给我们带来了什么特殊的好处呢?

tldr; tldr; Note that in 3.5.0 , users are prevented from utilizing a traversal spawned from a GraphTraversalSource and must use __ so it is already something you can expect to see enforced in the latest release.请注意,在3.5.0中,用户无法使用从GraphTraversalSource生成的遍历,并且必须使用__因此它已经是您可以期望在最新版本中看到强制执行的内容。

More historically speaking....更从历史上来说......

A GraphTraversalSource , your g , is meant to spawn new traversals from start steps with the configurations of the source assigned.一个GraphTraversalSource ,您的g ,旨在从分配了源配置的开始步骤产生新的遍历。 An anonymous traversal is meant to take on the internal configurations of the parent traversal it is assigned to as it is spawned "blank".匿名遍历意味着在生成“空白”时采用分配给它的父遍历的内部配置。 While a traversal spawned from g can have its internal configuration overwritten, when assigned to a parent, it's not something that is really part of the design for it to always work that way, so you take a chance in relying on that behavior.虽然从g产生的遍历可以覆盖其内部配置,但当分配给父级时,它并不是设计的一部分,它始终以这种方式工作,因此您有机会依赖这种行为。

Another point is that from the full list of Gremlin steps, only a few are actually "start steps" (ie addV() , addE() , inject() , V() , E() ) so in building your child traversals you can really only ever use those options.另一点是,从 Gremlin 步骤的完整列表中,实际上只有几个是“开始步骤”(即addV()addE()inject()V()E() ),所以在构建你的孩子遍历你真的只能使用这些选项。 As you often need access to the full list of Gremlin steps to start a child traversal argument, it is better to simply prefer __ .由于您经常需要访问完整的 Gremlin 步骤列表来启动子遍历参数,因此最好简单地选择__ By being consistent with this convention, it prevents confusion as to why child traversals "sometimes start with g and other times start with __ " if they are used interchangeably throughout a single traversal.通过与此约定保持一致,它可以防止混淆为什么子遍历“有时以g开头,而其他时间以__开头”,如果它们在单个遍历中可互换使用。

There are perhaps other technical reasons why the __ is required.需要__可能还有其他技术原因。 An easy one to see that doesn't require a ton of explanation can be demonstrated in the following Gremlin Console snippet:可以在以下 Gremlin 控制台代码段中演示一个不需要大量解释的简单易懂的方法:

gremlin> __.addV('person').steps[0].class
==>class org.apache.tinkerpop.gremlin.process.traversal.step.map.AddVertexStep
gremlin> g.addV('person').steps[0].class
==>class org.apache.tinkerpop.gremlin.process.traversal.step.map.AddVertexStartStep

The two traversals do not produce analogous steps.两次遍历不会产生类似的步骤。 If using g in replace of __ works today, it is by coincidence and not by design, which means that it could have the potential to break in the future.如果在今天使用g代替__有效,那是巧合,而不是设计,这意味着它可能在未来有破坏的潜力。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM