Previous topic  Top  Next topic  Print this Topic

Datamodel Listener


The OntoBroker API provides a convenient mechanism following the observer pattern for notifying client code about changes in an ontology. A listener can only be registered for selected ontologies and particular event types if desired.

Demo program output:

Initial average temperature: 12,000

New avg temperature: 13,100

New avg temperature: 14,400

New avg temperature: 15,600

Implementing a DatamodelListener

In order to monitor ontology changes, the DatamodelListener interface must be implemented. The interface is intuitive containing a single method handleEvent, which receives one argument of the generic type DatamodelEvent. Concrete events can be differentiated based on the particular dynamic type of this argument (see the javadoc for the possible subtypes). For example, when axioms are added or deleted from an ontology, events of type AxiomsUpdatedEvent are raised.

The following code snippets show a simple DatamodelListener implementation which prints the string representation of an added axiom.

class AddListener implements DatamodelListener {

  Formatting form = new Formatting(OntologyLanguage.OBJECTLOGIC);


  public void handleEvent(DatamodelEvent event) {

    if (event instanceof AxiomsUpdatedEvent) {

        for (OntologyChangeEvent chEvent : ((AxiomsUpdatedEvent) event)

              .getChanges()) {

          if(chEvent.getChangeAction() == ChangeType.ADD) {

              Axiom ax = chEvent.getAxiom();

              System.out.println("Axiom added: " + ax.toString(form);






Registering a Listener

The listener can be registered at an OntologyManager by calling the addDatamodelListener method. Besides a DatamodelListener instance, one additional argument of type DatamodelEventFilter is required. The filter controls which events of which type, occurring in which ontologies, are passed to a listener. The following instantiates a filter for the AXIOMS_UPDATED event in one ontology:

ArrayList<Integer> ontoIds = new ArrayList<Integer>(1);


DatamodelEventFilter filter = new DatamodelEventFilter(ontoIds, EnumSet

    .of(DatamodelEvent.EventType.AXIOMS_UPDATED), true);


manager.addDatamodelListener(new AddListener(), filter);

Controlling a Listener Lifecycle

Upon registering, events start being immediately passed to the listener. Calling the OntologyManager method removeListener will deregister the listener again. The method updateDatamodelListenerFilter allows you to adjust the filter applied.