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

  1. Use the following code phrase structure: insert object;

    This statement notifies the RetePlus algorithm that a new object is available. Rules that depend on this kind of object run when the objects meet the rules conditions.

  2. Use the following code structure: insert ClassName(<constructor_arguments>) [{statement1; ... statement}] ;
  3. If the rule engine runs in RetePlus mode, use the following code phrase structure: update object;

    This statement notifies the RetePlus algorithm that rules that depend on this object must be reevaluated. Some of them no longer run, and others start running.

  4. If the rule engine runs in RetePlus mode, use the following code phrase structure: retract object;

    This statement notifies the RetePlus algorithm that this object is no longer available. The rules that depend on this object no longer run.

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; 
   }
};