Notifying the rule engine of object changes
You can notify the rule engine of the availability of new objects, or when an object is updated or retracted.
About this task
Notification of object changes depends on the execution mode:
- When the rule engine runs in RetePlus mode, it instantly recomputes the matching rules and reschedules an internal agenda.
- When the rule engine runs in sequential or Fastpath mode, it uses the new status of the objects when you enter a new rule task.
Procedure
Results
The following code presents the global pattern to notify the rule engine of object changes:
insert object;
insert ClassName(<constructor_arguments>)
[{statement1; ... statement}] ;
update object;
retract object;
Example
In the then action part of this example, the RetePlus algorithm is notified that the new object penalty is available, and that the account object is no longer available. As a result, rules can run for penalty objects, but not for account objects anymore. In the else action part, the RetePlus algorithm is notified that the rules that depend on the account object must be reevaluated. As a result, some of these rules stop running and others start running.
rule financial.rules.CheckBalance {
when {
account: Account();
evaluate (account.getBalance() < 0 );
}
then { // compute a penalty
Penalty penalty = new Penalty(account);
insert penalty;
retract account;
}
else { // compute a bonus
account.computeBonus();
update account;
}
};