Gremlin:如何合并遍历路径上遇到的两个对象的选定属性

Gremlin: how do I merge selected attributes of two objects encountered on a traversal path

让我们假设我有来自不同农场的树的苹果。所以树结苹果,农场有树。我想要一个苹果列表,其中还包含对它们来自的农场的引用。

g = new TinkerGraph();

// apples
a1 = g.addVertex("a1");
a1.setProperty("type", "apple");

a2 = g.addVertex("a2");
a2.setProperty("type", "apple");

a3 = g.addVertex("a3");
a3.setProperty("type", "apple");


// trees
t1 = g.addVertex("t1");
t1.setProperty("type", "tree");

t2 = g.addVertex("t2");
t2.setProperty("type", "tree");


// farms
f1 = g.addVertex("f1");
f1.setProperty("type", "farm");
f1.setProperty("uid", "f1");

f2 = g.addVertex("f2");
f2.setProperty("type", "farm");
f2.setProperty("uid", "f2");

g.addEdge(t1, a1, "bears");
g.addEdge(t1, a2, "bears");
g.addEdge(t2, a3, "bears");

g.addEdge(f1, t1, "has");
g.addEdge(f2, t2, "has");

我想遍历和图表来报告每个苹果,并在其中包含农场 ID。我试过这样的事情:

g.V.has("type", "apple").copySplit(_().in("bears").in("has").map("uid"),
                                  _().map()).fairMerge

我得到的输出是:

==>{uid=f1}
==>{type=apple}
==>{uid=f1}
==>{type=apple}
==>{uid=f2}
==>{type=apple}

我想要的是:

==>{uid=f1, type=apple}
==>{uid=f1, type=apple}
==>{uid=f2, type=apple}

假设 TinkerPop 2.x,您可以使用 transform:

gremlin> g.V.has("type", "apple").transform{
gremlin>   m = [:] 
gremlin>   m<<it.in("bears").in("has").map("uid").next(); 
gremlin>   m<<it.map() }
==>{uid=f1, type=apple}
==>{uid=f1, type=apple}
==>{uid=f2, type=apple}

<< 语法的行为类似于 "m" 的 Map 上的 putAll()

TinkerPop 3.x 中(除非你有很好的理由,否则你应该使用它),粗略的近似是:

graph = TinkerGraph.open()
a1 = graph.addVertex(label, 'apple')
a2 = graph.addVertex(label, 'apple')
a3 = graph.addVertex(label, 'apple')
t1 = graph.addVertex(label, 'tree')
t2 = graph.addVertex(label, 'tree')
f1 = graph.addVertex(label, 'farm', 'uid', 'f1')
f2 = graph.addVertex(label, 'farm', 'uid', 'f2')
t1.addEdge('bears',a1)
t1.addEdge('bears',a2)
t2.addEdge('bears',a3)
f1.addEdge('has',t1)
f2.addEdge('has',t2)

与:

gremlin> g = graph.traversal()
==>graphtraversalsource[tinkergraph[vertices:7 edges:5], standard]
gremlin> g.V().hasLabel("apple").as('type').in('bears').in('has').as('uid').select('type','uid').by(label()).by('uid')
==>[type:apple, uid:f1]
==>[type:apple, uid:f1]
==>[type:apple, uid:f2]