Understanding the structure of business rules

The structure of the rule depends on the type of rule that you want to write. There are two ways to build rules in the rule language.

if/then/else statements

The parts must be defined in the following order:

  1. for each definitions part
  2. definitions part
  3. if (conditions) part
  4. then (actions) part
  5. else (optional actions) part

The syntax of if/then/else statements should be as follows:

[<for-each definitions>]
[<definitions>]
[if
    <condition>]
[then]
    (<action>;)+
[else 
    (<action>;)+] 
for-each definitions

This construct is used at the very beginning of the rule, and before the definitions part, to apply the rest of the rule to every object in a collection.

definitions

This construct introduces the part of the rule where you define variables.

Variables allow you to write more concise rules by using a short, convenient identifier to represent a value or the result of an expression.

Variables defined in the definitions part of a rule are local to the rule in which they are defined. A local variable can be used anywhere within this same rule, but is not available in other rules.

if

This construct introduces the part of the rule in which you define conditions.

If the conditions in the if part are met, the actions in the then part of the rule are executed. The if part of a rule is optional. If there is no if part, all actions in the then part of the rule are performed each time the rule is executed.

If the conditions in the if part of the rule are not met, the actions in the else part of the rule are executed. If the conditions are not met and the rule does not have an else part, the rule does nothing.

then

This construct introduces the part of the rule that defines the actions that are executed if the condition part of a rule is satisfied.

Every rule must have an action part. If a rule has no definition part and no condition part, the then keyword is optional and the actions in this part of the rule are always executed whenever the rule is executed.

The actions in the action 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 semi-colon (;).

else

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

The else part of a rule is optional and allows you to specify one or more actions to perform if the conditions in the condition 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 semi-colon (;).

If one or more variables with preconditions are defined in the definition part of a rule, the rule is only 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 processed at all, and the actions in the else part of the rule are never executed, whether or not the conditions in the condition part of the rule are met.

Action statements

The parts must be defined in the following order:

  1. for each definitions part
  2. actions part

The syntax of action statements should be as follows:

[<for-each definitions>]
    (<action>;)+
for-each definitions

This construct is used to apply one or more actions to all objects in a collection.

It should be immediately followed by a colon (:) and a list of one or more actions, each on a separate line, each preceded by a dash.