when (event) has occurred

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

Purpose

The construct when <event> has occurred <calendar duration> ago listens for the arrival of an event and delays the processing of the event by the specified calendar duration.

Use the clause when <event> has occurred <calendar duration> ago 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

when <event> has occurred <calendar duration> ago [, called <varname>]
	[where <condition>]

Description

The construct when <event> has occurred <calendar duration> ago 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 expression <calendar duration> ago imposes 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 calendar duration is satisfied.

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>. If you specify a local variable for the event, the comma before called is required.

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 has occurred 1 week ago, called 'recent purchase'
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 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;