Structure of rules

A rule defines the specific actions to take when certain conditions are met. A rule is composed of several parts that define the conditions and actions. The structure of the rule depends on the type of rule that you want to write.

A basic rule associates a condition with an action. The rule states what action to perform when a condition is true.

The vocabulary available in the rules comes from the elements that are defined in the business model and the agent descriptor. You write a rule in a formal rule language. A rule can be expressed by using business terms, variables, operators, and values.

Screen capture of a rule

  1. a purchase event: An event that is specified in the agent descriptor file.
  2. the amount of: An attribute of the purchase event.
  3. this purchase event: An implicit variable that refers to the purchase event.
  4. is more than: An operator on numeric values.
  5. 100: A numeric value.
  6. set the loyalty status of ... to ...: An action that modifies the value of the attribute of an object.
  7. 'the customer': A variable for the bound entity that is specified in the agent descriptor.
  8. Gold: A value of an enumeration.

A rule can comprise some or all of the following parts. The parts must be defined in the following order.

when
The when part defines the processing of an event.
definitions
The definitions part defines variables that can be used within the rule.
if
The if part defines the condition upon which an action is executed.
where
The where part defines the condition upon which an action is executed. You cannot use a where part in the when part of a rule that contains a definitions part. In this case, use an if part instead.
then
The then part defines the action to be taken if the condition is true.
else
The else part defines the action to be taken if the condition is false.
Some parts are optional depending on the type of rule that you want to write. A rule contains a when part or an if part, followed by a then part. Rules that contain a when part react differently to rules that use an if part:

Rules with a when part

Rules to process events start with the when part.

The rule has the following structure. The when and then parts are mandatory, and the other parts are optional.

when 
[definitions]
[if]
then
[else]

Rules with a when part are executed upon the reception of the event that is specified in the when part, or at a specified time after the reception of this event.

A rule can process an event immediately when it arrives in the agent, or later. These two types of processing are defined by the clauses when <event> occurs and when <event> has occurred.

If the conditions of a rule contain only events, each instance of the rule is executed at most once. If the rule contains a condition on the entity, an instance of the rule might be executed more than once.

The following business rule is created in a rule agent that references an entity that is named flight. When a flight late departure event is received, the flight state is updated.

when a flight late departure event occurs, called 'the event'
then
	set the state of 'the flight' to Delayed;

The following rule checks whether there was a purchase event in the 3 hours after the arrival of the flight.

when a flight arrival event has occurred 1 day ago
if
    there is at least one purchase event
        where this purchase event is during the period of 3 hours after this flight arrival event,
then
    print "At least one purchase 3 hours after the arrival of the flight.";

Rules without a when part

The rule has the following structure. The then part is mandatory, and the other parts are optional.

[definitions]
[if] 
then
[else]

In most cases, rules without a when part are evaluated when new events arrive or by an internal scheduling mechanism. An instance of a rule might be applied several times. For more information, Rule behavior.

Rules without a when part can be executed at regular intervals. For more information, see Scheduling the execution of a rule.

Warning: If a rule emits an event, this event is emitted every time that the rule is applied. Therefore, you must make sure that the rule does not emit events unconditionally.

In a rule with conditions (definitions or if parts) that bind events and contain no reference to the bound entity, each instance of this rule is executed at most once.

The following rule is applied when both a create event and a checkout event are present in the working memory of the agent. The rule instance is applied once because it binds only events.

definitions
set 'create event' to a create cart event;
set 'checkout event' to a checkout event 
	where this checkout event is after 'create event';
then
    print "The checkout happened after the cart was created.";

The following rule runs a test on the state of the customer entity to check whether the country of the customer is France. The rule is evaluated whenever its agent receives an event. If another agent changes the country of the customer to France, the rule is not applied until the agent that contains this rule receives an event.

if
    the country of the address of 'the customer' contains "France"
then
    print "Write messages in French.";