Types of rule variables

You can assign different types of values to your rule variables.

A rule variable can represent a constant, an expression, a business term, or a collection of business terms. You define a rule variable by giving it a name and a value. You choose the name, and the value can be text, a number, or an arithmetic expression. The value can also be a predefined business term that is already in your rule (for example, customer). Once you set a variable, you can use it in any part of the rule that declares the variable.

Rule variables that define constants

The simplest use of a rule variable is to declare a constant value.

For example, the variable maxAmount makes the following rule easier to understand, and ensures that the if and then parts of the rule use the same value:

definitions
	set maxAmount to 1000000;
 if
	the amount of 'the loan'  is at least maxAmount 
then
	in 'the loan report', reject the data with the message "The loan cannot exceed " + maxAmount;

Restrictions on rule variables

You can further restrict a variable in the definitions part of a rule by using the operator where.

Example

In the following rule, where restricts the loyal customer variable to customers in the Gold category:

definitions
	set 'loyal customer' to a customer 
		where the category of this customer is Gold; 
if
	the value of the shopping cart of 'loyal customer' is more than $200 
then
	apply the super discount;

Example

The following rule declares the senior Gold customer variable to be a customer who is in the Gold category and at least 65 years old:

definitions
	set 'senior Gold customer' to a customer 
		where all the following conditions are true: 
			- the category of this customer is Gold 
			- the age of this customer is at least 65;

Rule variables that refer to more than one occurrence of a business term

If a business term has more than one definition in a rule, you must define the different definitions.

When developers enable the automatic variable for a business term, the word the identifies the automatic variable in locales that use this type of article. For example, if you use the word customer as a business term in your rules, developers can define an automatic variable named the customer, which you can then refer to in rules.

If you have a rule that requires you to refer to more than one occurrence of a customer, you must define the other occurrences in the definitions part of the rule, for example:

definitions
	set applicant to a customer; 
	set 'loyal customer' to a customer; 
if
	all of the following conditions are true: 
		- applicant is married to 'loyal customer' 
		- 'loyal customer' is insured 
then
	upgrade applicant's rating; 

Variables are useful in rules that refer to relationships between two or more things of the same type.

For example, the following condition involves two different customers:

if
	’customer 1’ is married to ’customer 2’ 

The condition identifies and names two different customers: customer 1 and customer 2. The business term "customer" varies, and you can define two customer variables to write a rule like the following one:

definitions
	set ’customer 1’ to a customer; 
	set ’customer 2’ to a customer; 
if
	’customer 1’ is married to ’customer 2’ 
	and ’customer 2’ is insured 
then
	upgrade ’customer 1’’s rating;
Note: When a rule contains multiple occurrences of a business term, the rule fires multiple times to cover all the permutations. In the example, there are two customers, so the rule fires two times. If the rule had three customers, the rule would fire six times.

Rule variables that retrieve all the occurrences of a business term

You can use the BAL operator all <...> to create a variable that retrieves a list of all the occurrences of a business term, for example:

definitions 
	set 'gold customers' to all customers 
		where the category of this customer is gold; 
	set 'junior gold customer' to a customer in 'gold customers' 
		where the age of this customer is at most 15; 
	set 'senior gold customer' to a customer in 'gold customers' 
		where the age of this customer is at least 65;

The example creates three variables:

  • gold customers: A list of the customers in the gold category.
  • junior gold customer: A customer from the list of gold customers whose age is at most 15 years old.
  • senior gold customer: A customer from the list of gold customers whose age is at least 65 years old.