-
Notifications
You must be signed in to change notification settings - Fork 239
Using Gremlin through Groovy
<dependency>
<groupId>com.tinkerpop.gremlin</groupId>
<artifactId>gremlin-groovy</artifactId>
<version>??</version>
</dependency>
Gremlin Groovy works by using the metaprogramming facilities provided by Groovy. Groovy is used to compile Gremlin syntax down to raw Java Pipes.
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 (i.e. method invocations). There is a Groovy class called Gremlin.groovy
. To use Gremlin while in Groovy, simply invoke Gremlin.load()
.
class SimpleExample {
static {
Gremlin.load()
}
public List exampleMethod() {
Graph g = TinkerGraphFactory.createTinkerGraph()
def results = []
g.v(1).out('knows').fill(results)
return results
}
}
Here is a typical used pattern when mixing Gremlin/Groovy and Java classes. In this example, the Java class calls the methods of the Groovy class as if it were a Java class. This is one of the benefits of Groovy—it works seamlessly within a larger Java project while providing useful language features and, of course, Gremlin.
// 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}.iterate()
return m
}
}
// a Java class
public class GraphFramework {
public static void main(String[] args) {
System.out.println(GraphAlgorithms.eigenvectorRank(new Neo4jGraph("/tmp/graphdata"));
}
}
Finally, 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>??</version>
</dependency>
<!-- PROJECT BUILDING WITH GROOVY/JAVA -->
<dependency>
<groupId>org.codehaus.groovy.maven</groupId>
<artifactId>gmaven-plugin</artifactId>
<version>1.3</version>
</dependency>
<plugin>
<groupId>org.codehaus.groovy.maven</groupId>
<artifactId>gmaven-plugin</artifactId>
<version>1.3</version>
<executions>
<execution>
<goals>
<goal>generateStubs</goal>
<goal>compile</goal>
<goal>generateTestStubs</goal>
<goal>testCompile</goal>
</goals>
<configuration>
<providerSelection>1.7</providerSelection>
</configuration>
</execution>
</executions>
</plugin>
Gremlin shell (gremlin.sh
) is basically the Groovy shell wrapped to have the Gremlin look and feel and some other small tweaks. For example, do \h
at the gremlin>
prompt. Its recommended that you use gremlin.sh
for terminal work. However, this subsection is provided for those that use Groovy shell exclusively.
marko:~$ groovysh
Groovy Shell (1.7.2, JVM: 1.6.0_22)
Type 'help' or '\h' for help.
-------------------------------------------------------------------------------
groovy:000> com.tinkerpop.gremlin.groovy.Gremlin.load()