微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

匿名遍历 vs 正常遍历 gremlin

如何解决匿名遍历 vs 正常遍历 gremlin

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

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

解决方法

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

从历史上看....

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

另一点是,从 Gremlin 步骤的完整列表中,只有少数是真正的“开始步骤”(即 addV()addE()inject()V()E()) 所以在构建你的子遍历时,你真的只能使用这些选项。由于您经常需要访问 Gremlin 步骤的完整列表来启动子遍历参数,因此最好简单地选择 __。通过与此约定保持一致,如果子遍历在整个单次遍历中可互换使用,则可以避免混淆为什么子遍历“有时以 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

两次遍历不会产生类似的步骤。如果今天使用 g 代替 __ 行得通,那是巧合而不是设计,这意味着它在未来可能会被破坏。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。