匿名遍历与正常遍历 gremlin

CHI*_*HID 5 gremlin tinkerpop tinkerpop3 janusgraph amazon-neptune

我已阅读有关匿名遍历的文档。我知道它们可以开始使用__,并且可以在步进调制器内使用。虽然我从概念上不理解它。为什么我们不能使用从步骤调制器内的图遍历源生成的正常遍历?例如,在下面的 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
Run Code Online (Sandbox Code Playgroud)

为什么是匿名__.inE()__.addE()?为什么我们不能写this.g.inE()andthis.g.addE()呢?无论哪种方式,编译器都不会抱怨。那么匿名遍历在这里给我们带来了什么特殊的好处呢?

ste*_*tte 5

太棒了;请注意,在3.5.0中,用户无法使用从 a 生成的遍历GraphTraversalSource,并且必须使用__它,因此您可以期望在最新版本中看到强制执行的内容。

更从历史角度来说......

A GraphTraversalSource,您的g,旨在使用分配的源配置从开始步骤生成新的遍历。匿名遍历意味着在它产生“空白”时采用其分配给的父遍历的内部配置。虽然从中生成的遍历g可以覆盖其内部配置,但当分配给父级时,它并不是真正设计的一部分,因此它始终以这种方式工作,因此您有机会依赖该行为。

另一点是,从 Gremlin 步骤的完整列表中,只有少数实际上是“开始步骤”(即addV()addE()inject()V()E()),因此在构建子遍历时,您实际上只能使用这些选项。由于您经常需要访问 Gremlin 步骤的完整列表来启动子遍历参数,因此最好简单地使用 Preferred __。通过与此约定保持一致,如果子遍历在单个遍历中可互换使用,则可以防止混淆为什么子遍历“有时开始于g,有时开始于”。__

也许还有其他技术原因__需要这样做。下面的 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
Run Code Online (Sandbox Code Playgroud)

这两次遍历不会产生类似的步骤。如果今天用它g来代替__作品,那是巧合而不是有意为之,这意味着它在未来可能会出现故障。