简体   繁体   English

如何在嵌套的 Gremlin 遍历中引用 as-variable?

[英]How can I refer to an as-variable inside a nested Gremlin traversal?

In trying to write a traversal that matches only if there is not an existing edge from vertex Vo to vertex Vi (where the ID of Vi might not be known ahead of time, and so Vi has to be specified by a traversal).尝试编写仅在从顶点 Vo 到顶点 Vi 不存在边时才匹配的遍历(其中 Vi 的 ID 可能无法提前知道,因此必须通过遍历指定 Vi)。

I had this initial traversal:我有这个初始遍历:

<A> GraphTraversal<A, Edge> addEdge(
  GraphTraversal<A, Vertex> traversalToVo,
  String viSelectKey
) {
  return traversalToVo.coalesce(
    __.outE("Manages").and(
      __.inV().as("inV").where("inV", P.neq(viSelectKey))
      // more conditions
    ),
    __addE("Manages").to(select(viSelectKey))
  );
}

My problem is that I can't figure out how to make Vi available inside the nested anonymous traversal;我的问题是我不知道如何在嵌套匿名遍历中使Vi可用; everything I've thought of results in the error我想到的一切都会导致错误

Neither the sideEffects, map, nor path has a Vi-key: WherePredicateStep(inV,neq(Vi))

I've debugged the call to getScopeValue , and in fact Vi is never defined when I get there.我已经调试了对getScopeValue的调用,事实上,当我到达那里时,从未定义过Vi

Approaches I've tried to populate Vi include:我尝试填充Vi的方法包括:

// define "Vi" in the upstream part of the query
gts.addV(...).as("Vi").V(Vo).coalesce(...)

// modeled after "Long Traversals" recipe; variable not defined afterward
gt.V(Vo).sideEffect(viTraversal.asAdmin().clone().as("Vi")).coalesce(...)

// produces a Map, and I can't apply unfold() downstream inside predicate
gt.sideEffect(viTraversal.asAdmin().clone().group("Vi"))

As far as I can tell, this is the result of some scoping rule that detaches the nested anonymous traversals from the scope values;据我所知,这是一些范围规则的结果,该规则将嵌套匿名遍历与 scope 值分离; how do I bridge the gap so that an as -variable defined in the upstream part of the traversal can be referenced from inside coalesce-and-where?如何弥合差距,以便可以从内部合并和何处引用遍历上游部分中定义的as变量?

Your first version of this is very close to correct, however you didn't specify what Vi represents.您的第一个版本非常接近正确,但是您没有指定Vi代表什么。 You can do this through the use of a mid-traversal V().您可以通过使用中间遍历 V() 来做到这一点。 Here is how you would accomplish something like this on the modern graph:以下是您将如何在现代图表上完成这样的事情:

g.V(2).as('Vi').V(1).coalesce(
  __.outE("Manages").and(
    __.inV().where(P.neq("Vi"))
  ),
  __.addE("Manages").to(select("Vi"))
);

In this example, I am specifying the vertex id, but you could use other Gremlin filter steps to get the desired result.在此示例中,我指定了顶点 ID,但您可以使用其他 Gremlin 过滤器步骤来获得所需的结果。

This approach seems inefficient and icky, but it does work.这种方法似乎效率低下且令人讨厌,但确实有效。 I still don't know why I can access a group but not an as .我仍然不知道为什么我可以访问一个group但不能访问as

traversalToVo.sideEffect(traversalToVi.asAdmin().clone().group("toV").by(id))
  .coalesce(
    __.outE("rel").and(
      __.inV().id().where(P.within("toV")).by().by(Column.keys),
      // other filters
    ),
    __.addE("rel").to(traversalToVi)
  )

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

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