繁体   English   中英

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

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

尝试编写仅在从顶点 Vo 到顶点 Vi 不存在边时才匹配的遍历(其中 Vi 的 ID 可能无法提前知道,因此必须通过遍历指定 Vi)。

我有这个初始遍历:

<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))
  );
}

我的问题是我不知道如何在嵌套匿名遍历中使Vi可用; 我想到的一切都会导致错误

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

我已经调试了对getScopeValue的调用,事实上,当我到达那里时,从未定义过Vi

我尝试填充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"))

据我所知,这是一些范围规则的结果,该规则将嵌套匿名遍历与 scope 值分离; 如何弥合差距,以便可以从内部合并和何处引用遍历上游部分中定义的as变量?

您的第一个版本非常接近正确,但是您没有指定Vi代表什么。 您可以通过使用中间遍历 V() 来做到这一点。 以下是您将如何在现代图表上完成这样的事情:

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

在此示例中,我指定了顶点 ID,但您可以使用其他 Gremlin 过滤器步骤来获得所需的结果。

这种方法似乎效率低下且令人讨厌,但确实有效。 我仍然不知道为什么我可以访问一个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