Null values handling in rules

Null values in rule conditions can prevent some rules from being fired. You can add tests in your rules to identify null values.

An expression can result in a null value for the following reasons:

  • A value in an event is missing.
  • A value in an entity is not defined.
  • A relationship points to an entity that does not exist. For example, the target entity is not created or it is deleted.

When the missing or undefined value is a number, it results in 0, and it is not considered as a null value.

Expressions that result in a null value are treated as unknown values and are handled by the rule engine. When a rule condition that determines whether the rule is matched or not contains a boolean expression that results in an unknown value, the rule is not fired.

The following diagram describes the three valued logic that applies for all combinations of boolean expressions that can result in an unknown value.

Three valued logic for resolving combinations of boolean expressions that result in unknown values

Examples of rules that do not test for null values

Because null values are handled by the rule engine as unknown values, you do not need to include an explicit condition to check that an entity is null.

For example, in the following rule, when the value of the name of the customer is null, the result of the condition is unknown, and the rule is not fired.

if
    the name of the customer is 'Paul'
then
    reject the loan;

In the following rule, when the value of the name of the customer is null, and the age of the customer is 20, the first condition cannot be resolved, and the second condition is met. Because the rule uses the logical or operator, the rule action to reject the loan is taken. If the age of the customer is not 20, the first condition cannot be resolved, and the second condition is not met. In this case, the rule is not fired, and the loan is not rejected.

if
    the name of the customer is 'Paul'
    or the age of the customer is 20
then
    reject the loan;

In the following rule, the two conditions are combined by using the and operator. When the value of the name of the customer is null, and the age of the customer is 20, the then and else parts are ignored. The first condition is equal to unknown and the second condition is true, which results in a combined value of unknown. Because unknown is neither true or false, no action can be taken. If the age of the customer is not 20, the result of the entire condition part is always false. In this case, the else part of the rule is fired, and the loan is granted.

if
    the name of the customer is 'Paul'
    and the age of the customer is 20
then
    reject the loan;
else
    grant the loan;

In the following example, when the value of the name of the customer is null, the result of the condition is unknown, and the rule is not fired.

if
    it is not true that the name of the customer is 'Paul'
then
    reject the loan;

Examples of rules that test for null values

You can include a condition to check that an entity is null, or not null, in your rules. For example, the following rule checks if the relationship to the customer entity does not exist.

when a purchase event occurs , called 'the purchase'
if
    the customer of 'the purchase' is null
then
    print "There is no customer!";

A rule can also use the condition negation to check whether the relationship to the customer entity exists:

when a purchase event occurs , called 'the purchase'
if
    the customer of 'the purchase' is not null
then
    print "The purchase is from a registered customer!";

The following rule checks if the relationship to the customer entity exists in the when part.

when a purchase event occurs , called 'the purchase'
   where the customer of 'the purchase' is not null
   and the country of the address of the customer of 'the purchase'
is not null
   and the country of the address of the customer of 'the purchase'
starts with "United" 
if
   the amount of 'the purchase' is more than 100
then
   print "High value purchase";

It is also possible to write the same rule by setting variables in the definitions part instead of checking that a value equals null.

when a purchase event occurs, called 'the purchase'
definitions
   set 'the customer' to the customer of 'the purchase';
   set 'the country' to the country of the address of 'the customer';
if
   'the country' starts with "United"
   and the amount of 'the purchase' is more than 100
then
   print "High value purchase";

The actions in the then part of the rule are not taken if the values of the variables are null.