Skip to content

Gremlin Groovy

Bryn Cooke edited this page Jul 21, 2013 · 4 revisions

Gremlin is the graph traversal language of the TinkerPop stack and provides a concise syntax for traversing a Blueprints graph data structure. Frames leverages Gremlin via annotations in order to allow methods to do more complex computations/traversals than direct incidences and adjacencies as seen with the core Frames annotations.

Frame Adjacencies

It is possible to make use of a Gremlin path expression as a means of determining vertex adjacency via the GremlinGroovyModule.

annotation method prefix arguments description example
@GremlinGroovy get value get and frame the vertices at then end of the expression @GremlinGroovy("it.out('knows')")
@GremlinGroovy get, can, is value, frame=false return the objects at then end of the expression @GremlinGroovy(value="it.out('knows').name", frame=false)
public interface Person {
    @GremlinGroovy("it.as('x').out('created').in('created').except('x')")
    public Iterable<Person> getCoCreators();
}

In the example above, the vertices at the end of the path expression are framed as Person classes.

FramedGraphFactory factory = new FramedGraphFactory(new GremlinGroovyModule()); //Use the gremlin groovy module
FramedGraph framedGraph = factory.create(graph);

Person marko = framedGraph.frame(graph.getVertex(1), Person.class);
for (Person coCreator : marko.getCoCreators()) {
  System.out.println(coCreator.getName());
}
// josh
// peter

When constructing your FramedGraphFactory ensure you include the GremlinGroovyModule.

You can also pass parameters to the gremlin expression by using @GremlinParam to specify the name of the parameter.

public interface Person {
    @GremlinGroovy("it.as('x').out('created').in('created').except('x').has('age',age)")
    public Iterable<Person> getCoCreatorsOfAge(@GremlinParam("age") int age);
}

In this example the paramater named ‘age’ is passed to the script.

Person josh = marko.getCoCreatorsOfAge(32).iterator().next();

Arbitrary Java objects

If the resultant object return type is not a Frame, then frame=false should be used in the @GremlinGroovy annotation.

@GremlinGroovy(value="'1+2'", frame=false)
public Integer getASumOfTwoNumbers();
Clone this wiki locally