Writing a rule with an else action clause

When you write a rule with an else action clause, use the ILOG® Rule Language syntax.

About this task

When the condition part of a rule finishes with an evaluation, the action part of the rule might include an else clause. The last evaluation within the condition part acts as a discriminator to choose between the then or the else execution branches. If the evaluation expression is true, the then part is executed. If it is false, the else part is executed.

If the rule ends with several evaluations, only the last one is used as the discriminator. The else clause is optional, and cannot be used when there is no evaluation at the end of the condition part.

Procedure

To write a rule with an else action clause, use the following pattern:
rule rulename {
   when {
      // conditions:
      <condition>;
      [<condition>;]*
      evaluate (<then/else discrimination test>);
   }
   then {
      // “then” actions
      [<action>;]*
   }
   else {
      // “else” actions
      [<action>;]*
   }
}

Example

In this example, if the evaluation expression is true, the rule executes the then action and charges a penalty. If the evaluation is false, the rule executes the else action and computes a bonus.

rule financial.rules.CheckBalance {
   when {
     account: Account();
   evaluate (account.getBalance() < 0 );
   }
   then {
			account.chargePenalty();
   }
   else {
			account.computeBonus();
   }
};