簡體   English   中英

無法遍歷Gremlin中具有獨特屬性的頂點

[英]Can't traverse to vertices with unique properties in Gremlin

我正在尋找從一個頂點到另一個頂點的路徑,避免使用具有已經在該路徑上匹配的屬性的頂點。

考慮以下示例:

    Graph graph = TinkerGraph.open();

    GraphTraversalSource t = graph.traversal();

    Vertex A = t.addV().property("age", 19).next();
    Vertex B = t.addV().property("age", 21).next();
    Vertex C = t.addV().property("age", 20).next();
    Vertex D = t.addV().property("age", 21).next();
    Vertex E = t.addV().property("age", 22).next();

    t.V(A).addE("knows").to(B).iterate();
    t.V(B).addE("knows").to(C).iterate();
    t.V(C).addE("knows").to(D).iterate();
    t.V(D).addE("knows").to(E).iterate();
    t.V(C).addE("knows").to(E).iterate();

    List<Path> paths = t.V(A)
            .repeat(
                    out()
            ).times(5).emit()
            .has("age", 22).path().toList();

    Assert.assertEquals(1, paths.size());

我正在尋找從A到E的方法。有兩種方法:

A-> B-> C-> D-> E A-> B-> C-> E

我要尋找的只是第二個,因為在第一個路徑中,B和D具有相同的年齡。

我嘗試使用as和by進行過濾,但未能將其縮放到整個路徑。 例如,通過執行以下操作,我可以檢查頂點不匹配第一個頂點的屬性:

    List<Path> paths = t.V(A).as("first")
            .repeat(
                    out()
                    .where(P.neq("first")).by("age")
            ).times(5).emit()
            .has("age", 22).path().toList();

    Assert.assertEquals(1, paths.size());

但您可以想象,它不會過濾路徑中間的碰撞。 我覺得應該有一種更簡單的方法來實現此目的,而我卻不這么想。 是否有類似as()的語句,但是不是替換先前的賦值,而是將它們保留在數組中? 我該如何實現?

謝謝

您需要將當前比較ageall先前看到的age秒。 如果有任何匹配,讓遍歷者死亡:

t.V(A).as("a").
  repeat(filter(loops().is(lt(5))).out().
         not(values("age").as("current").         /* current age   */
               select(all, "a").unfold().         /* previous ages */
               values("age").
                 where(eq("current"))).as("a")).  /* not() negates the match */
    until(has("age", 22)).
  path()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM