Michał
Michał

Reputation: 636

How to create multiple edges from one vertex in single traversal

I've got function which has this signature:

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

where:

from is a unique value the graph should be asked about

relations is a collection of pair, where first element from pair is edge label and the second is similar to from but it designates the second side of the relation. Eg ( assuming that andy, dog are unique ):

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

I would like to add these edges between these vertices ( they are already existing in graph, i would like to query them and create edges between them ). The requirement I've got is to prepare this in one traversal.

Thanks!

Upvotes: 0

Views: 279

Answers (1)

stephen mallette
stephen mallette

Reputation: 46206

Here is one way to do it:

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]

Don't be distracted by the Groovy syntax too much and just focus on on the Gremlin and structure. markoRelations and joshRelations are just lists of list where the inner list is basically you Pair. The ;[] you see at the end of lines is not relevant to the answer - it just helps control the output that the Gremlin Console will show.

Upvotes: 1

Related Questions