groovy - What is the syntax to sequence statements in Gremlin? -


within gremlin language (a groovy child) i'm little confused behavior , uses of ;, &&.

for example, in gremlinclient repl (running titan 1.0 here) might desire add few nodes @ once:

gremlin> graph = titanfactory.open("../conf/gremlin-server/titan-cassandra-server.properties") ==>standardtitangraph[cassandra:[localhost]] gremlin> g = graph.traversal(standard()) ==>graphtraversalsource[standardtitangraph[cassandra:[localhost]], standard] gremlin> g.v().count() ==>0 gremlin> g.addv(label, 'subject', 'ident', '1') ; g.addv(label, 'subject', 'ident', '2') ==>v[4304] gremlin> g.v().count() ==>1 

ok, ; didn't work. how &&? doesn't quite have semantics want work through it:

gremlin> g.v().count() ==>0 gremlin> g.addv(label, 'subject', 'ident', '1') && g.addv(label, 'subject', 'ident', '2') ==>true gremlin> g.v().count() ==>2 

well... that's sort of ok. need able bind variables part of sequence of commands. oddly, semicolon works here:

gremlin> g.v().count() ==>0 gremlin> g.e().count() ==>0 gremlin> g.addv(label, 'subject', 'ident', '1') && g.addv(label, 'subject', 'ident', '2') ==>true gremlin> node3 = graph.addvertex(label, 'subject', 'ident', '3') ; g.v().has('ident', '1').next().addedge('dc:ispartof', node3) ; node3.addedge('dc:ispartof', g.v().has('ident','2').next()) ==>e[35z-6d4-2l91-9n4][8248-dc:ispartof->12496] gremlin> g.v().count() ==>3 gremlin> g.e().count() ==>2 

now here's trick. how can perform both these operations in 1 line (one websocket request, in practice)? can't seem duplicate above node3 = ... line via ; , proper functionality.

unless you're talking gremlin language variants, gremlin pure groovy since gremlin dsl , not language per se. technically, valid groovy valid gremlin.

regarding examples provided, believe need iterate traversal via .iterate() (.next() work since you're adding 1 element).

g.addv(label, 'subject', 'ident', '1').iterate() ; g.addv(label, 'subject', 'ident', '2') 

the gremlin console automatically iterates last traversals only, explicit .iterate() second statement not required. strictly equivalent to:

g.addv(label, 'subject', 'ident', '1').iterate() ; g.addv(label, 'subject', 'ident', '2').iterate() 

if want execute multiple operations via websocket, can send multiline scripts. following should work:

node3 = graph.addvertex(label, 'subject', 'ident', '3') g.v().has('ident', '1').next().addedge('dc:ispartof', node3) node3.addedge('dc:ispartof', g.v().has('ident','2').next()) 

note more recent version of tinkerpop (i recall v3.1+), can chain .addv() steps , add multiple vertices within same traversal.

g.addv(label, 'subject', 'ident', '1').addv(label, 'subject', 'ident', '2') 

this not yet available in titan since latest stable release (v1.0.0) uses tinkerpop v3.0.1.


Comments