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

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

如何解决如何在嵌套的 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"))

据我所知,这是一些作用域规则的结果,该规则将嵌套的匿名遍历与作用域值分离;如何弥合差距,以便可以从 coalesce-and-where 内部引用在遍历上游部分中定义的 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)
  )

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