Thomas M. DuBuisson Thomas M. DuBuisson - 3 months ago 14
Groovy Question

What is the syntax to sequence statements in Gremlin?

Within the Gremlin language (a Groovy child) I'm a little confused by the behavior and uses of

;
,
&&
.

For example, in GremlinClient REPL (running Titan 1.0 here) I might desire to add a few nodes at 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, so
;
didn't work. How about
&&
? It doesn't quite have the semantics I want but I could 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. Now I also need to be able to bind variables as part of the 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 the trick. How can I perform both these operations in one line (one Websocket request, in practice)? I can't seem to even duplicate the above
node3 = ...
line via
;
and get proper functionality.

Answer

Unless you're talking specifically about Gremlin language variants, Gremlin is pure Groovy since Gremlin is a DSL and not a language per se. Technically, any valid Groovy is valid Gremlin.

Regarding the examples your provided, I believe you need to iterate your traversal via .iterate() (.next() would work since you're only adding one element).

g.addV(label, 'Subject', 'ident', '1').iterate() ; g.addV(label, 'Subject', 'ident', '2')

The Gremlin console automatically iterates the last Traversals only, so an explicit .iterate() for that second statement is not required. This is strictly equivalent to:

g.addV(label, 'Subject', 'ident', '1').iterate() ; g.addV(label, 'Subject', 'ident', '2').iterate()

If you want to execute multiple operations via WebSocket, you can just send multiline scripts. The 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 that with a more recent version of TinkerPop (I recall v3.1+), you can chain .addV() steps and add multiple vertices within the same traversal.

g.addV(label, 'Subject', 'ident', '1').addV(label, 'Subject', 'ident', '2')

This is not yet available in Titan since the latest stable release (v1.0.0) uses TinkerPop v3.0.1.