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.
- a purchase event: An event that is specified in the agent descriptor file.
- the amount of: An attribute of the purchase event.
- this purchase event: An implicit variable that refers to the purchase event.
- is more than: An operator on numeric values.
- 100: A numeric value.
- set the loyalty status of ... to ...: An action that modifies the value of the attribute of an object.
- 'the customer': A variable for the bound entity that is specified in the agent descriptor.
- 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.
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.
- In a rule that uses the when <event> occurs clause, the rule is triggered when an event occurs. For more information, see Processing events immediately by using when occurs.
- In a rule that uses the when <event> has occurred clause, the evaluation of the rule is postponed until a fixed delay has passed. For more information, see Postponing the processing of events by using when 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.
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.";