when (event) has occurred

This construct enables a rule to listen for a specific event and process this event after a specified delay.

Purpose

Use the clause when <event> has occurred to monitor the arrival of events, and allow some delay in the processing of an event. For example, you can write rules that check for the absence of events. An action is taken if no other events happened between the arrival of the first event and the end of the specified duration. For example, if a purchase was not returned in the week after a purchase event, then the purchase can be confirmed.

Syntax

To delay the processing of an event by a calendar duration or an absolute duration, use the following syntax:
when <event> [, called <varname>,] has occurred 
   [where <condition>], 
   after a delay of <calendar|absolute duration>, 
	

Alternative syntax

To delay the processing of an event by a calendar duration, you can also use the following syntax, which is shorter:
when <event> has occurred <calendar duration> ago [, called <varname>]
	[where <condition>]

Description

The construct when <event> has occurred specifies the event to process after a specified duration. This duration is expressed in units of time, for example 1 hour, 2 weeks, 30 minutes.

The expressions after a delay of <calendar|absolute duration> and <calendar duration> ago impose a condition on the parameter now, namely that now is at or after the specified duration after the event that occurred. The rule instance is applicable only once, when the condition that is expressed by the duration is satisfied. The duration can use the value of an attribute of type duration.

For more information about the parameter now and the processing of events, see Event processing in agents.

An implicit variable is created that you can use in the rest of the rule, for example this bag not loaded event. Optionally you can create a local variable to reference the event, for example 'the event'. To create a local variable, use the construct called <varname>.

Examples

The following rule checks that a purchase event occurred a week before now and that the purchase was not returned during this week.

when a purchase event, called 'recent purchase', has occurred,
after a delay of 1 week, 
if
    the number of return events during the period of 1 week after 'recent purchase' is 0
then 
	print "The purchase has not been returned."

The following rule checks that no purchase was made before the trial period of the customer is over. If no purchase event occurred after this duration has passed, the rule executes.

when a create account event has occurred, 
after a delay of the trial period of this create account event,
if 
    the number of purchases equals 0 
then 
    print "The trial period of " + the name of 'the customer' +"is over and no purchase was made."

The following rule checks if the pizza order that was made 30 minutes before now is not delivered. If the order is not delivered, the order is late.

when a pizza order has occurred 30 minutes ago
if
    the order status of 'the order' is not DELIVERED
then
    make it true that 'the order' is late;