else

This construct introduces actions that are executed if the conditions of a rule are not satisfied.

Purpose

You use this construct to declare actions that are executed if the if part of a rule is not satisfied. These actions are ignored if all local variables are not correctly initialized due to preconditions in the definitions part failing to be satisfied.

Syntax

else
    <action>;*

Description

The else part of a rule is optional and allows you to specify one or more actions to perform if the conditions in the if part of the rule are not met.

The actions in the else part of a rule are executed in the order they appear. Start each action statement on a new line and end each action statement with a semicolon (;).

If one or more variables with preconditions are defined in the definitions part of a rule, the rule will only be processed if these preconditions are satisfied and all local variables are correctly initialized. This means that if these preconditions are not satisfied, the rule is not be processed at all, and the actions in the else part of the rule are never executed, whether or not the conditions in the if part of the rule are met.

Adding preconditions in the definitions part of a rule rather than in the if part of a rule can provide a small performance improvement, since it potentially reduces the number of conditions to be checked. However, since the logic of the rule is not the same in each case, the method to use depends on the result you want to obtain.

Example

In the following basic example, a customer gets a 10% discount if the amount of the purchase is more than 100. Otherwise, the customer gets a 5% discount.

when a purchase event occurs, called 'the purchase'
if
   the amount of 'the purchase' is more than 100
then
   set the discount of 'the customer' to 10;
else
   set the discount of 'the customer' to 5;

In the following example, a second condition has been added in the if part of the rule. Now, a customer receives a 10% discount only if the amount of the purchase is more than 100 and the customer is in the Gold category. All other customers receive a 5% discount.

when a purchase event occurs, called 'the purchase'
if
   the amount of 'the purchase' is more than 100 and the category of 'the customer' is Gold
then
   set the discount of 'the customer' to 10;
else
   set the discount of 'the customer' to 5;

Now consider a third example. In the following rule, a minimum age requirement is added as a precondition in the definitions part of the rule. This means that a customer must be more than 20 and have the category Gold. Otherwise, the ‘gold customer’ variable is not initialized and the rest of the rule is not executed. With this rule, a customer under 20 and in another category receives no discount at all. A customer who is over 20 and in the Gold category receives a 10% discount. The else part of the rule is executed and a 5% discount applied only if the customer is over 20 but not in the Gold category.

when a purchase event occurs, called 'the purchase'
definitions
    set 'gold customer' to a customer from 'the customer' where the category of this customer is Gold and the age of this customer is more than 20;
if 
    the amount of 'the purchase' is more than 100 
then
    set the discount of 'the customer' to 10;
else
    set the discount of 'the customer' to 5;