如何在一次遍历中从一个顶点创建多条边

How to create multiple edges from one vertex in single traversal

我有一个具有这个签名的函数:

public void doTheJob(String from, Collection<Pair<String, String>> relations)

其中:

from 是图表应该询问的唯一值

relations 是对的集合,其中对中的第一个元素是边 label,第二个元素类似于 from,但它指定关系的第二边。例如(假设 andy、dog 是唯一的):

"andy" ["has,dog", "has,cat", "is,person"]

我想在这些顶点之间添加这些边(它们已经存在于图中,我想查询它们并在它们之间创建边)。我的要求是在一次遍历中准备好这个。

谢谢!

这是一种方法:

gremlin> g = TinkerFactory.createModern().traversal()
==>graphtraversalsource[tinkergraph[vertices:6 edges:6], standard]
gremlin> g.addV().property('name','cat').
......1>   addV().property('name','dog').
......2>   addV().property('name','person').iterate()
gremlin> markoRelations = [['has','dog'],['has','cat'],['is','person']];[]
gremlin> joshRelations = [['has','dog'],['is','person']];[]
gremlin> t = g.V().has('name','marko').as('from');[]
gremlin> markoRelations.each { t = t.V().has('name',it[1]).
......1>                             addE(it[0]).from('from') };[]
gremlin> t.iterate()
gremlin> t = g.V().has('name','josh').as('from');[]
gremlin> joshRelations.each { t = t.V().has('name',it[1]).
......1>                            addE(it[0]).from('from') };[]
gremlin> t.iterate()
gremlin> g.E().hasLabel('is','has')
==>e[19][1-has->15]
==>e[20][1-has->13]
==>e[21][1-is->17]
==>e[22][4-has->15]
==>e[23][4-is->17]

不要过多地被 Groovy 语法分心,只需关注 Gremlin 和结构。 markoRelationsjoshRelations 只是列表的列表,其中内部列表基本上是你 Pair。您在行尾看到的 ;[] 与答案无关 - 它只是帮助控制 Gremlin 控制台将显示的输出。