Skip to: Site menu | Main content

Drools

Java Rules Engine

Event Model Tutorial Print

Simple Example

Drools provides the DefaultWorkingMemoryEventListener which is an empty method implementation of WorkingMemoryEventListener.:

To execute code when an event is generated we override any of the above methods. All event objects return references back to the working memory. The following example shows how we can listen to when an object is asserted into the WorkingMemory.

WorkingMemoryEventListener listener = new DefaultWorkingMemoryEventListener()
{
    public void objectAsserted(ObjectAssertedEvent event)
    {
         System.out.println("asserted " + event.getFactHandle() + ":" event.getObject());
    }
};

workingMemory.addEventListener(listener);

Debug Example

Events can be used for debugging, to help with this drools provides the DebugWorkingMemoryEventListener class:

public class DebugWorkingMemoryEventListener
    implements WorkingMemoryEventListener
{
    public DebugWorkingMemoryEventListener()
    {
        // intentionally left blank
    }

    public void objectAsserted(ObjectAssertedEvent event)
    {
        System.err.println( event );
    }
    public void objectModified(ObjectModifiedEvent event)
    {
        System.err.println( event );
    }

    public void objectRetracted(ObjectRetractedEvent event)
    {
        System.err.println( event );
    }

    public void conditionTested(ConditionTestedEvent event)
    {
        System.err.println( event );
    }

    public void activationCreated(ActivationCreatedEvent event)
    {
        System.err.println( event );
    }
    
    public void activationCancelled(ActivationCancelledEvent event)
    {
        System.err.println( event );
    }

    public void activationFired(ActivationFiredEvent event)
    {
        System.err.println( event );
    }
}

Each event object already has a toString() method to print information about itself, to use the class :

workingMemory.addEventListener(new DebugWorkingMemoryEventListener());

The HelloWorld example shows the Event Model being used to provide debug information.