-
Notifications
You must be signed in to change notification settings - Fork 239
Using Gremlin through Groovy
Gremlin works by using the metaprogramming facilities provided by Groovy. Groovy is used to compile Gremlin syntax down to raw Java Pipes.
Gremlin shell (gremlin.sh
) is basically the Groovy shell| wrapped to have the Gremlin look and feel. For example, do \h
at the gremlin>
prompt.
marko:~$ groovysh
Groovy Shell (1.7.2, JVM: 1.6.0_22)
Type 'help' or '\h' for help.
-------------------------------------------------------------------------------
groovy:000> com.tinkerpop.gremlin.Gremlin.load()
If you are using Groovy classes, its trivial to access Gremlin. In this way, your Java code and your Gremlin/Groovy code seamless interact through standard class mechanisms. There is a Groovy class called Gremlin.groovy
. To use Gremlin while in Groovy, simply invoke Gremlin.load()
(be sure Gremlin is in the classpath).
class SimpleExample {
public List exampleMethod() {
Gremlin.load()
Graph g = TinkerGraphFactory.createTinkerGraph()
def results = []
g.v(1).outE.inV >> results
return results;
}
}
Here is a typical pattern when mixing Gremlin/Groovy and Java classes.
// a Groovy class
class GraphAlgorithms {
static {
Gremlin.load();
}
public static Map<Vertex, Integer> eigenvectorRank(Graph g) {
Map<Vertex,Integer> m = [:]; int c = 0;
g.V.outE.inV.groupCount(m).loop(3) {c++ < 1000} >> -1;
return m;
}
}
// a Java class
public class GraphFramework {
public static void main(String[] args) {
System.out.println(GraphAlgorithms.eigenvectorRank(new Neo4jGraph("/tmp/graphdata"));
}
}
If you build with Maven2, here are some useful snippets that you can add to your pom.xml
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy</artifactId>
<version>1.7.5</version>
</dependency>
<!-- PROJECT BUILDING WITH GROOVY/JAVA -->
<dependency>
<groupId>org.codehaus.groovy.maven</groupId>
<artifactId>gmaven-plugin</artifactId>
<version>1.0</version>
</dependency>
<plugin>
<groupId>org.codehaus.groovy.maven</groupId>
<artifactId>gmaven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>generateStubs</goal>
<goal>compile</goal>
<goal>generateTestStubs</goal>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
</plugin>