Rule variables
A rule variable has a name to which you assign a value and is used to refer to a population of objects rather than a particular object. You define the variable in the definitions part of a rule, and you can use it only in the condition and action parts of the rule that declares the variable.
You define variables so that you can use the name independently of the value it represents. Variables can make your business rules less ambiguous and easier to understand.
A variable can have one of the following value types:
| Variable value type | Example |
|---|---|
An expression |
|
An event type |
An event type that the agent processes, for example, a purchase event or all purchase events. |
A constant |
|
A collection of values of the types already listed |
For example, a list of numbers { 1, 3, 5 }, a list of objects { CHINA, FRANCE, UK, USA } |
Making rules easier to understand
Rule variables can make your action rules easier to build and understand by simplifying terms.
You might find parts of a rule difficult to understand. For example, the following rule uses the term of to chain together business terms:
if the value of the shopping cart of 'the customer' is less than $100 then...
You can shorten this chain of words by using a rule variable. For example, you can define a variable that is named the cart to represent the business terms shopping cart and customer:
definitions
set 'the cart' to the shopping cart of 'the customer';
if
the value of 'the cart' is less than $100
then...
One rule per rule variable
The scope of a variable is the rule that declares the variable. The name of the variable must be unique in the rule. After you define a variable, it can be used in any part of the rule.
In the following rule, the definitions part defines the variable Smith, which is then used in the if and then parts of the rule.
definitions
set customerName to the customer of 'the purchase';
if
the category of customerName is Gold
then
print "10% discount to the shopping cart of " + customerName;
Impact of variables on rule instances
Rule variables might multiply the number of rule instances. When a variable is used in the condition part of a rule but not in the action part, you can define the variable directly in the condition part. In the following rule example, the number of rule instances that are executed corresponds to the number of items that cost more than 10000 in the purchase, that is, it depends on the number of values that the variable some item can take. If there are 10 items, where 8 items cost more than 10000, then 8 instances of the rule are executed.
definitions
set 'some purchase' to a purchase;
set 'some item' to an item in the items of 'some purchase';
if
the price of 'some item' is more than 10000
then
print "purchase with expensive item";
In this scenario, you can avoid the multiplication of rule instances by using the there is at least one construct. In the following example, only one instance of the rule is executed, independently from the number of items:
definitions
set 'some purchase' to a purchase;
if
if there is at least one item in the items of 'some purchase'
where the price of this item is more than 10000
then
print "purchase with expensive item";
The two examples are different because the action part is not applied the same number of times. In most cases, it is sufficient to execute only one instance of the rule, which results in better performance and a more logical result.
When rule variables do not match data
A rule with a definitions part and an if part expresses two types of conditions. The if part defines an explicit condition, and the definitions part defines an implicit condition that the rule variable must match existing data.
For example, you might define a variable that is named 'low risk borrowers' for all the borrowers whose credit score is at least 200. However, when you run the rule against data from an application, the rule might not find any borrowers who match the rule variable. In this case, the rule variable behaves as a condition, and the rule cannot apply the actions in the then and else parts of the rule. For more information, see Dependency of rule actions on rule variables.
Restrictions on rule variable names
If the name of a rule variable contains one or more spaces, you must enclose the name in single quotation marks when you define or use the variable. If a variable name contains only one word, quotation marks are optional.
Do not use the following characters in the names of your variables:
| Character | Description |
|---|---|
| TAB | Tabulation |
| \n | Line break |
| ' | Single quotation mark |
| " | Double quotation mark |
| () | Opening and closing parentheses |
| / | Slash |
| , | Comma |
| ; | Semicolon |