Rule actions

A rule action consists of at least one action phrase that the rule executes when the if part of the rule is true. It can also contain action phrases to execute when the if part of the rule is false.

You define actions in the then and else parts of a rule.

The then part of a rule must define at least one action. If the then or else part contains multiple actions, the rule executes the actions in their listed order.

Example of the then part

The following rule sends a coupon event when the value of the shopping cart is more than $100, otherwise the rule applies no action.

when a purchase event occurs
if 
    the value of 'the shopping cart' is more than 100 
then 
    emit a new coupon event where
       the amount is 5,
       the customer is the customer of this purchase event,
       the message is "Thank you for your purchase. Please accept this coupon for $5.";

Example of the else part

The following rule applies a change when the conditions are met (then), or a different change when the conditions are not met (else):

if 
    the status of 'the loan report' is approved 
    and the loan grade of 'the loan report' is one of { "A", "B", "C" } 
then 
    print "Your loan has been approved"; 
else 
    print "We are sorry. Your loan has not been approved";

Rule actions for each item in a list

You can define actions that a rule executes for each item in a list. You use the operator for each <item> in <list> to apply actions to items in a list.

The following group of action statements applies to every item in the list of 'expensive purchases'.

definitions
    set 'expensive purchases' to all purchases in the purchases of 'the customer'
        where the price of each purchase is more than 100;
then 
    for each item in the items of 'expensive purchases':
    - print "A 5% discount has been applied";

Rules with a then and else part

A rule that contains a then and an else part is a condensed form of two rules that match the same entity and have conditions that are negations of each other.

In some cases, the execution of the then part changes the state of the entity and makes the else part applicable. As a result both the then and else parts might be applied.

In the following example, when the account is null, the then part is applied and creates an account entity. The action changes the state of the entity, makes the condition false, and makes the else part applicable.

when a new account event occurs, called 'the event'
if
    'the account' is null
then
    set 'the account' to a new account where
        the customer is the customer of 'the event',
        the opening date is 'the event',
        the status is NEW; 
else 
    print "The account already exists";

To create or modify the state of an entity, rules with an else part are not appropriate and might lead to confusing results. Instead, you can create two separate rules with conditions that negate each other. For example, you can replace the rule above with the following rules.

The following rule checks if the account exists.

when a new account event occurs, called 'the event'
if
    'the account' is null
then
    set 'the account' to a new account where
        the customer is the customer of 'the event',
        the opening date is 'the event',
        the status is NEW; 

The following rule checks if the account does not exist.

when a new account event occurs, called 'the event'
if
    'the account' is not null
then
    print "The account already exists";

If the account entity has a null value when the event arrives, the first rule is applicable, and the rule action creates a new account entity.

When the rules are evaluated again, the value of the account is no longer null, and this rule instance is no longer be applicable. The second rule becomes applicable, and is added to the list of matched rules. When the second rule is executed, the action prints the message: "The account already exists". For more information on the rule matching and selection process, see Rule behavior.