Types of rule variables

A rule variable can represent a constant, an expression, an object, or a collection of objects.

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 an attribute of an entity or event, for example, the age of 'the customer' or the customer of this purchase event. 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 value of a rule variable can be a constant. 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.

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

when a purchase event occurs, called 'the purchase'
definitions
	set 'loyal customer' to the customer of 'the purchase'
		where the category of this customer is Gold; 
if
	the value of the shopping cart of 'loyal customer' is more than 200 
then
	print "Apply super discount";

Rules that refer to multiple objects of same type

If you have a rule that requires you to refer to different flights, you must define the other occurrences in the definitions part of the rule, for example:

definitions
    set 'flight1' to a flight in the flights of 'the customer'; 
    set 'flight2' to a flight in the flights of 'the customer' 
        where the scheduled departure time of this flight is after the scheduled arrival time of 'flight1';
if 
    the scheduled departure time of 'flight2' is before 1 hour after the scheduled arrival time of 'flight1'
then
    print "less than 1 hour of transit time";
Note: When a rule refers to multiple objects of same type, the rule might be applied to different permutations of the same objects. In the example, there are two flights, so the rule fires two times. If the rule referred to three customers, the rule would fire six times.

Rule variables that refer to past events

You can use rule variables to refer to events that were processed by the agent and that are stored in the working memory.

For example, this can be useful to compare events over time.

when a return event occurs, called 'the return'
definitions 
	set 'the purchase' to a purchase event;
if 
	'the return' is in the same calendar week as 'the purchase'
then...

Rule variables that retrieve all the instances of an object

You can use the operator all <...> to create a variable that retrieves a list of all the instances of an object, for example:

definitions
    set 'gold customers' to all customers in the boarded passengers of 'the flight'
        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.