Skip to content

Event Implementation

joshsh edited this page Oct 20, 2012 · 5 revisions
<dependency>
   <groupId>com.tinkerpop.blueprints</groupId>
   <artifactId>blueprints-core</artifactId>
   <version>??</version>
</dependency>

EventGraph and EventIndexableGraph wrap any Graph or IndexableGraph, respectively. The purpose of an EventGraph is to raise events to one or more GraphChangedListener as changes to the underlying Graph occur. The obvious limitation is that events will only be raised to listeners if the changes to the Graph occur within the same process.

EventTransactionalGraph and EventTransactionalIndexableGraph wrap any TransactionalGraph. These wrappers behave in the same fashion as the aforementioned EventGraph and EventIndexableGraph, but respect the concept of transactions, such that the events that are triggered during a transaction are queued until the transaction is successfully committed. Once committed, the events will fire in the order that they were queued. If the transaction is rolled back the event queue is reset.

The following events are raised:

  • New vertex
  • New edge
  • Vertex property changed
  • Edge property changed
  • Vertex property removed
  • Edge property removed
  • Vertex removed
  • Edge removed

To start processing events from a Graph first implement the GraphChangedListener interface. An example of this implementation is the ConsoleGraphChangedListener which writes output to the console for each event.

To add a listener to the EventGraph:

EventGraph graph = new EventGraph(TinkerGraphFactory.createTinkerGraph());
graph.addListener(new ConsoleGraphChangedListener(graph));

Vertex v = graph.addVertex(100);
v.setProperty("name", "noname");

for (Edge edge : graph.getEdges()) {
    edge.removeProperty("weight");
}

The following output would appear in the console:

Vertex [v[100]] added to graph [eventgraph[tinkergraph[vertices:6 edges:6]]]
Vertex [v[4]] property [name] set to value of [noname] in graph [eventgraph[tinkergraph[vertices:6 edges:6]]]
Edge [e[10][4-created->5]] property [weight] with value of [1.0] removed in graph [eventgraph[tinkergraph[vertices:6 edges:6]]]
Edge [e[7][1-knows->2]] property [weight] with value of [0.5] removed in graph [eventgraph[tinkergraph[vertices:6 edges:6]]]
Edge [e[9][1-created->3]] property [weight] with value of [0.4] removed in graph [eventgraph[tinkergraph[vertices:6 edges:6]]]
Edge [e[8][1-knows->4]] property [weight] with value of [1.0] removed in graph [eventgraph[tinkergraph[vertices:6 edges:6]]]
Edge [e[11][4-created->3]] property [weight] with value of [0.4] removed in graph [eventgraph[tinkergraph[vertices:6 edges:6]]]
Edge [e[12][6-created->3]] property [weight] with value of [0.2] removed in graph [eventgraph[tinkergraph[vertices:6 edges:6]]]