Create a Custom Gremlin Step in Any JVM Language?

119 views
Skip to first unread message

James Thornton

unread,
Aug 20, 2011, 4:45:41 AM8/20/11
to gremli...@googlegroups.com
During my experimentation in creating Gremlin user-defined steps (https://github.com/tinkerpop/gremlin/wiki/User-Defined-Steps), I had a thought -- what if you could create a Gremlin step using any JVM language?

To create a step in Gremlin you write a closure in Groovy and include it as the third argument to the Gremlin.defineStep() method:

  Gremlin.defineStep("myCustomStep", [Vertex,Pipe], myCustomClosure)  

A few weeks ago Marko refactored Gremlin by pushing much of the heavy lifting down into Pipes, and this opens up the doors for Gremlin implementations in other JVM languages (https://groups.google.com/d/msg/gremlin-users/5DiHC9dWSAA/cWDZFnBl9l4J). 

But what if instead of rewriting all of Gremlin, you were able to just write your custom steps in whatever JVM language you want?

I did a few Google queries, and it looks like you can create Groovy closures in other JVM languages. Here's a blog about someone creating a Groovy closure in Java (http://www.jroller.com/melix/entry/coding_a_groovy_closure_in). 

I am not a JVM expert so I don't yet know if you can do this for every language, but it would be way cool if you could.

Hopefully some of the JVM gurus around here can provide more insight.

- James

James Thornton

unread,
Aug 20, 2011, 4:56:29 AM8/20/11
to gremli...@googlegroups.com
Here's the key line and the docs to Groovy's MethodClosure function...

  MethodClosure cl = new MethodClosure(MyJavaClass.class, "staticMethod");

Daniel Quest

unread,
Aug 20, 2011, 2:15:16 PM8/20/11
to gremli...@googlegroups.com
Yep would be awesome

Sent from my iPod

James Thornton

unread,
Aug 21, 2011, 8:14:56 AM8/21/11
to gremli...@googlegroups.com
But what if instead of rewriting all of Gremlin, you were able to just write your custom steps in whatever JVM language you want?

Well after some hacking, I got this to work. Evidently it is possible to write Jython methods that compile down to Gremlin closures -- I presume this will for other JVM languages too.

I'll post a tutorial soon.

- James  

James Thornton

unread,
Aug 22, 2011, 11:03:14 AM8/22/11
to gremli...@googlegroups.com
Ok, I created a library that provides an example for how to write custom, user-defined Gremlin steps in Jython. 

The library is called mogwai, and it's on Github (https://github.com/espeed/mogwai).

You should be able to adapt it for any JVM language. 

Here's the example code:

Jython module:

Java Interface:

Java Factory you can reuse for each Jython module:

Groovy script that create the Gremlin steps:

Instructions on how to make this work:

- James


Pierre De Wilde

unread,
Aug 22, 2011, 11:18:02 AM8/22/11
to gremli...@googlegroups.com
Cool. Thanks for sharing.

Pierre
Reply all
Reply to author
Forward
0 new messages