我读过关于匿名遍历的文档。我知道它们可以用__启动,也可以在step调制器中使用。虽然我不理解它的概念。为什么我们不能使用步骤调制器内的图形遍历源生成的普通遍历?例如,在下面的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()呢?无论哪种方式,编译器都不会抱怨。那么匿名遍历给我们带来了什么特殊的好处呢?
发布于 2021-05-28 11:02:53
注意,在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代替__的话,它只是巧合,而不是设计,这意味着它有可能在未来崩溃。
https://stackoverflow.com/questions/67733463
复制相似问题