-
Notifications
You must be signed in to change notification settings - Fork 38
Java Handler
Bryn Cooke edited this page Jul 5, 2013
·
13 revisions
You can use Java to handle your frames methods. This is useful you want to embed some logic in your model.
To use @JavaHandler you must include the module when creating the graph factory:
FramedGraph framedGraph = new FramedGraphFactory(new JavaHandlerModule()).create(graph);Here we demonstrate how the getCoCreators using the @GremlinGroovy annotation can be emulated using @JavaHandler and also a generic method that uses other methods on the frame.
interface Person {
@Property("name")
public String getName();
@Property("age")
public Integer getAge();
@GremlinGroovy("it.as('x').out('created').in('created').except('x')")
public Iterable<Person> getCoCreators();
@JavaHandler
public Iterable<Person> getCoCreatorsJava();
@JavaHandler
public String getNameAndAge();
}Create a nested abstract class in your frame called ‘Impl’.
Note that this class must implement the original interface and may optionally implement
JavaHandlerContext with either Vertex or Edge as a parameter.
interface Person {
//Interface methods excluded for brevity.
abstract class Impl implements JavaHandlerContext<Vertex>, Person {
public Iterable<Person> getCoCreatorsJava() {
return frameVertices(gremlin().as("x").out("created").in("created").except("x"));
}
public String getNameAndAge() {
return getName() + " (" + getAge() + ")"; //Call other methods that are handled by other annotations.
}
}
}By implementing JavaHandlerContext your implementation has access to the following:
FramedGraph<?> g(); //The graph
C it(); //The element being framed
GremlinPipeline<C, E> gremlin(); //Start a gremlin pipeline from the framed element
GremlinPipeline<C, E> gremlin(Object starts); //Start a gremlin pipeline from an element
//... Also all the framing methods available on FramedGraph.You can specify an alternative class for your JavaHandler implementation using the @JavaHandlerClass annotation.
@JavaHandlerClass(MyPersonImpl.class)
interface Person {
}In this case
MyPersonImpl will be used in preference to the nested Impl class.
If you would rather be in control of the creating the handlers for your methods then you can specify a factory on the module.
JavaHandlerFactory handlerFactory = new JavaHandlerFactory() {...};
FramedGraphFactory factory = new FramedGraphFactory(new JavaHandlerModule().withFactory(handlerFactory))
FramedGraph framedGraph = factory.create(graph);