Rule behavior

Rules are matched and selected for execution by a condition controlled mechanism. The actions of a rule can influence which rules are matched after the rule is executed.

A rule agent has a working memory that contains the current event and previously received events, if these events are within the event time horizon. A rule agent also has a parameter that provides access to its bound entity.

When a rule agent receives a new event, the agent matches and selects the rules that are applicable according to the when, definitions, and if clauses in the rules. The process of matching and selecting rules is repeated until there are no rules applicable.
Match
The match step creates applicable rule instances if the conditions of the rule are satisfied. A rule instance is created by binding the variables in each rule to objects, such as entities or events. For example, the variable 'the customer' is matched to an entity named "John Doe". Each rule instance is added to a list of matched rules.

Rules that contain only if clauses are matched if they have conditions that use an entity or the event history. If an entity is changed by another agent, conditions that depend on the entity or an entity attribute are reevaluated every time that the engine returns to match rules. These types of rules are also sensitive to events and the variable now if they contain conditions on them.

Rules that contain when clauses are matched if they have conditions that match the single event that is asserted into the history of events. These rules can include the following clauses:

  • when <event> occurs and a condition that matches the event
  • when <event> has occurred and a condition and time match
  • if conditions that depend on an entity attribute
Select
The select step selects one of the rule instances from the list. The selection is based on certain criteria, such as the rule priority, how recent the instances are, and the exclusion of false positives. Each rule instance in the list of matched rules is selected one time only. After a rule instance that includes only if clauses is selected the condition of that rule must become false and then true again for the same rule to be matched and selected again.

Rules that contain when clauses are safeguarded to execute only for a single event. If these rules contain if conditions the order in which they are selected from the list might not match exactly the rule priority order.

Act
The act step applies the action of the selected rule instance. The execution can modify the state of the entity and therefore might affect the result of the following match step.

The following two rules show an example of when one rule can affect another.

when a purchase occurs
if the expenses of the customer is more than 10000
then print "stop spending";

when a purchase occurs
if the expenses of the customer is at most 10000
then set the expenses of the customer to the expenses of the customer + the amount of this purchase;

When a purchase of $2 is received and the customer already spent $9,999, the second rule is matched. The actions of this rule increase the expenses to $10,001, and as a result the first rule is matched.

Rule processing mechanism