如何从 tinkerpop 中的重复步骤访问存储的变量

How to access stored variable from repeat step in tinkerpop

我刚接触 gremlin 两天。我有一组顶点和彩色边。我想找到从 S2 到 D2 的路径。如果我通过绿色边缘 (G1 -B1) 进入黑色顶点,那么我只能通过绿色边缘 (B2-G2) 出来。我应该不会出红边吧

下面的查询有效,但我不能对颜色进行硬编码(has('color',within("green") 在第 3 行)。

 g.V().hasLabel("S2").repeat(outE("tx").choose(values("type")).
      option("multiplex",aggregate(local,"colors").by("color").inV()).
      option("demultiplex",has('color',within("green")).inV()).
      option(none,__.inV()).
      simplePath()).until(hasLabel("D2")).path().by(label())

所以我尝试了下面的查询它没有给出任何路径。如果我的边缘有标签“multiplex”,那么我存储颜色。如果我的边缘有标签“demultiplex”,那么我从商店读取颜色。

 g.V().hasLabel("S2").repeat(outE("tx").choose(values("type")).
      option("multiplex",aggregate("colors").by("color").inV()).
      option("demultiplex",has("color",within(select("colors").unfold())).inV()).
      option(none,__.inV()).
      simplePath()).until(hasLabel("D2")).path().by(label())

下面的代码填充图表

 Vertex s1 = g.addV("S1").next();
    Vertex s2 = g.addV("S2").next();
    Vertex d1 = g.addV("D1").next();
    Vertex d2 = g.addV("D2").next();
    Vertex r1 = g.addV("R1").next();
    Vertex r2 = g.addV("R2").next();
    Vertex r3 = g.addV("R3").next();
    Vertex r4 = g.addV("R4").next();
    Vertex g1 = g.addV("G1").next();
    Vertex g2 = g.addV("G2").next();
    Vertex g3 = g.addV("G3").next();
    Vertex g4 = g.addV("G4").next();
    Vertex b1 = g.addV("B1").next();
    Vertex b2 = g.addV("B2").next();
    Vertex b3 = g.addV("B3").next();
    Vertex b4 = g.addV("B4").next();


    g.V(s1).addE("tx").to(r1).property("type","straight").next();
    g.V(r1).addE("tx").to(b1).property("color","red").property("type","multiplex").next();

    g.V(s2).addE("tx").to(g1).property("type","straight").next();
    g.V(g1).addE("tx").to(b1).property("color","green").property("type","multiplex").next();

    g.V(b1).addE("tx").to(b2).property("type","straight").next();


    g.V(b2).addE("tx").to(r2).property("color","red").property("type","demultiplex").next();

    g.V(b2).addE("tx").to(g2).property("color","green").property("type","demultiplex").next();

    g.V(r2).addE("tx").to(r3).property("type","straight").next();
    g.V(g2).addE("tx").to(g3).property("type","straight").next();

    g.V(r3).addE("tx").to(b3).property("color","red").property("type","multiplex").next();

    g.V(g3).addE("tx").to(b3).property("color","green").property("type","multiplex").next();


    g.V(b3).addE("tx").to(b4).property("type","straight").next();


    g.V(b4).addE("tx").to(g4).property("color","green").property("type","demultiplex").next();

    g.V(g4).addE("tx").to(d2).property("type","straight").next();

    g.V(b4).addE("tx").to(r4).property("color","red").property("type","demultiplex").next();
    g.V(r4).addE("tx").to(d1).property("type","straight").next();

你们非常接近。这种语法总是很诱人:

has("color",within(select("colors").unfold())

但它并不像您发现的那样工作。 P 语法不采用 Traversal 那种方式。当您需要引用副作用时(即 "colors"),您需要使用 where() 的形式。

gremlin> g.V().hasLabel("S2").
......1>   repeat(outE("tx").
......2>          choose(values("type")).
......3>            option("multiplex",aggregate(local,"colors").by("color").inV()).
......4>            option("demultiplex", filter(values('color').as('c').
......5>                                         where('c',eq('colors')).
......6>                                           by().
......7>                                           by(unfold().tail())).inV()).
......8>            option(none,__.inV()).
......9>          simplePath()).
.....10>     until(hasLabel("D2")).
.....11>   path().by(label)
==>[S2,tx,G1,tx,B1,tx,B2,tx,G2,tx,G3,tx,B3,tx,B4,tx,G4,tx,D2]