首页 > 解决方案 > 如何在嵌套的 Gremlin 遍历中引用 as-variable?

问题描述

尝试编写仅在从顶点 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"))

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

标签: javagremlintinkerpop3

解决方案


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

推荐阅读