Skip to content

Frame initializers

Bryn Cooke edited this page Aug 6, 2013 · 5 revisions

Sometimes you need to be able to initialize a vertex or edge is added to the graph knowing the type of frame that it was created with.

The FrameInitializer interface allows a newly added vertex or edge to be initialized before it is returned to the user.

public interface FrameInitializer {
	/**
	 * @param kind The kind of frame. 
	 * @param framedGraph The graph.
	 * @param element The new element that is being inserted into the graph.
	 */
	public void initElement(final Class<?> kind, final FramedGraph<?> framedGraph, final Element element);
}

To use the initializer register it via a module with the FramedGraphFactory.

FrameInitializer myInitializer = new FrameInitializer() {
  public void initElement(final Class<?> kind, final FramedGraph<?> framedGraph, final Element element) {
    element.setProperty("class", kind.getName()); //For example: save the class name of the frame on the element
  }
};

FramedGraph g = new FramedGraphFactory(new AbstractModule() {
  public void doConfigure(FramedGraphConfiguration config) {
    config.addFrameInitializer(myInitializer);
  }
}).create(baseGraph);

Person p = g.addVertex(null, Person.class); //myInitializer is called.

Every registered initializer is called before returning the new element.

Some possible use cases

  • You want to save the type information of the frame to the vertex.
  • You want to set the default property values of the vertex based on the frame type.
Clone this wiki locally