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.
Procedure
To get notification from the rule engine during
ruleset execution:
- Load the ruleset, create an engine based on the ruleset,
set ruleset parameters, and insert objects in the working memory,
as described in the first steps of Completing the ruleset execution code.
- Attach a notification observer to the rule engine, as
follows:
- Add the observer.
IlrToolAdapter engineObserver = new IlrToolAdapter() {
- Override some of the notification methods.
-
The notifyAssertObject method is called
when an object is added.
-
The notifyRetractObject method is called
when an object is retracted.
-
The notifyBeginInstance method is called
when the engine executes a rule instance using the RetePlus algorithm.
-
The notifyBeginSequentialInstance method
is called when the engine executes a rule in sequential mode.
public void notifyAssertObject(Object object) {
System.out.println(object + “ is added”);
}
public void notifyRetractObject(Object object) {
System.out.println(object + “ is retracted”);
}
public void notifyBeginInstance(IlrRuleInstance instance) {
System.out.println(instance.getRuleName());
}
public void notifyBeginSequentialInstance(
IlrRule rule,Object[] objs,int arg) {
System.out.println(rule.getName());
}
};
- Add a try-catch block.
try {
engine.connectTool(engineObserver);
} catch (IlrToolConnectionException e) {
e.printStackTrace();
}
- Run the engine, prepare the rule engine for another execution,
terminate the rule engine and free the memory, as described in steps
4 to 6 of Completing the ruleset execution code.
Results
Note: In Sequential and Fastpath modes, turn on the debug
mode when the rules are compiled for the notification mechanism (IlrTool) to be available
at run time. To turn the debug mode on, add the following task property
specification: property ilog.rules.engine.sequential.debug
= true;
For information on the classes, interfaces,
and methods available with the Rule
Designer API,
see the Java API.