簡體   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