priority

The priority keyword controls the sequence of rule execution.

Purpose

This keyword is a rule header property that is used to control the order in which rules are executed depending on their priority.

Context

Rule properties

Syntax

priority = {expression};

Description

A rule header can contain a property priority. The priority is an integer expression. If you do not specify any value, the default priority value is 0.

The priority of a rule determines its position in the agenda. The priority can be static or dynamic.

Example

The first example shows a static priority defined as an integer. The second example shows a dynamic priority based on a variable.

Example 1
rule StaticExample {
   priority = 1,000,000;
   when ...
   then ...
}

The StaticExample rule has a static priority value of 1,000,000. Therefore, it will be listed in the agenda before the rules with a priority lower than 1,000,000.

Example 2
rule ActiveStockByChange{ 
   priority = ?c;
   when { 
      ?s:Stock(?p:currentPrice; ?l:lastClosingPrice;
                  ?c = (?p-?l)/?l);
   }
   then {
   System.out.println(?s.symbol +" : price: "+ ?p + 
                           " change: "+ ?c);
}

The ActiveStockByChange rule has a dynamic priority equal to the percent change of the stock price. For all the Stock objects of this class, the order of rule execution will depend on the change in price. The larger the positive change, the earlier the rule will be executed. The larger the negative change, the later the rule will be executed.