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 loan is at least maxAmount 
then
	set decision to "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.

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

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

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 in customers
		where all of 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.

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 in customers; 
	set 'loyal customer' to a customer in customers; 
if
	all of the following conditions are true: 
		- applicant is married 
		- 'loyal customer' is insured
		- the address of 'loyal customer' is the address of applicant
then
	set decision to "high 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
	the address of 'customer 1' is the address of '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 in customers; 
	set 'customer 2' to a customer in customers; 
if
	the address of 'customer 1' is the address of 'customer 2' 
	and 'customer 2' is insured 
then
	set rating to the rating of 'customer 2';
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 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 in customers
		where the category of each 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.