Getting notification during ruleset execution

You can get notification during ruleset execution by attaching a specific observer to the rule engine. Note that Rule Execution Server does not support observers, whichever way they are set.

Before you begin

Create a Java™ project for rules and open the main class.

About this task

After editing the main class to execute your ruleset, you can attach a notification observer to the decision engine.

In the decision engine, the execution monitoring is divided in several observer interfaces. All the observer interfaces, such as EngineObserver, RuleEngineObserver, DataObserver, and RuleflowObserver, extend the Observer tagging interface. To monitor the execution by the engine, you must implement the interfaces for the events that you want to observe. For example, if you want to observe the engine start and stop, and the rule instance execution, you can write an observer class that implements both EngineObserver and RuleEngineObserver.

Procedure

  1. Implement the observer interfaces with the following code:
    package ruledef;
    import com.ibm.rules.engine.ruledef.runtime.RuleEngine;
    import com.ibm.rules.engine.ruledef.runtime.RuleEngineObserver;
    import com.ibm.rules.engine.ruledef.runtime.RuleInstance;
    import com.ibm.rules.engine.runtime.Engine;
    import com.ibm.rules.engine.runtime.EngineInput;
    import com.ibm.rules.engine.runtime.EngineObserver;
    
    public class MyObserver implements EngineObserver, RuleEngineObserver {
    
    public void executionStarted(Observable source, EngineInput input) {
    System.out.println("Execution started");
    }
    public void executionEnded(Observable source, EngineInput input) {
    System.out.println("Execution ended");
    }
    public void engineReset(Observable source) {
    System.out.println("Engine reset");
    }
    public void engineStopped(Observable source, String message) {
    System.out.println("Engine stopped");
    }
    public void ruleExecutionStarted(Observable source,
    RuleInstance instance) {
    System.out.println("Rule " + instance.getRuleName() + " execution started");
    }
    public void ruleExecutionEnded(Observable source, 
    RuleInstance instance) {
    System.out.println("Rule " + instance.getRuleName() + " execution ended");
    }
    public void exceptionRaisedInCondition(Observable source,
    Exception exception,
    boolean internallyCatched) {
    System.out.println("Exception raised in condition :" + exception.getMessage());
    }
    public void exceptionRaisedInAction(Observable source,
    Exception exception,
    RuleInstance ruleInstance,
    boolean internallyCatched) {
    System.out.println("Exception raised in action of rule " + ruleInstance.getRuleName() + ": " + exception.getMessage());
        }
    }
  2. Pass the observer to the engine by adding the following lines:
    RuleflowEngine ruleflow = ruleflowDefinition.createEngine();
    MyObserver observer = new MyObserver();
    ruleflow.addObserver(observer);
    EngineOutput output = ruleflow.execute(engineInput);