-
Notifications
You must be signed in to change notification settings - Fork 38
Java Handler
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.
public 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.
public interface Person {
//Interface methods excluded for brevity.
public 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.
To perform initialization on the frame annotate a no-args method with @Initializer
. It will be called when a frame is created via the FramedGraph.addVertex
or FramedGraph.addEdge
.
public interface Person {
//Interface methods excluded for brevity.
public abstract class Impl implements JavaHandlerContext<Vertex>, Person {
@Initializer
public void init() {
//This will be called when a new framed element is added to the graph.
setAge(23);//Set the default age
}
}
}
You can specify an alternative class for your JavaHandler
implementation using the @JavaHandlerClass
annotation.
@JavaHandlerClass(MyPersonImpl.class)
public 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);