Operating over past events

You can write rules that run tests on several events of the same type. For example, you can test that several event instances happened over a certain period.

Use temporal and aggregation operators, such as the number of, and during, over a collection of events.

The following rule checks if at least three purchases happened in the last seven days. The purchase event that is processed in this rule is not included in the number of events returned. The condition of the rule becomes true when the fourth event is received.

when a purchase event occurs
if
    the number of purchase events during the last period of 7 days is at least 3
then
    print "At least 3 purchases in a week";
Warning: Expressions such as the last period of refer implicitly to the time now. You must be aware that now might not refer to the time stamp of the event in the when <event> occurs clause. For more information, see Event processing in agents.

The following rule calculates the amount of all the purchase events. The total amount is calculated for the events that are within the event time horizon for this agent.

when a purchase event occurs
if
    the total amount of all purchase events is more than 300
then
    print "Apply 10% discount on this purchase"; 
    set the loyalty status of 'the customer' to Gold; 

The following rule identifies the last and previous events among events of the same type that occurred over a specified period of time. In the definitions part, the rule defines a variable that corresponds to all the zone enter events that occurred in the last 20 minutes. Each event in this group of events is then processed as a previous zone enter event. When there is no event in the collection of events that is after a previous event, the most recent event is identified. The rule then prints the time of the current event and the time of the previous event.

when a zone enter occurs , called LAST
definitions
    set 'recent zone enters' to all zone enters during the last period of 20 minutes ;
    set 'previous zone enter' to a zone enter in 'recent zone enters';
if
    there is no zone enter in 'recent zone enters'
        where this zone enter is after 'previous zone enter' ,
        and the zone id of the zone of 'previous zone enter' is "ZONE001"
then
    print "Current zone enter is: " + the timestamp of LAST ;
    print "Previous zone enter is: " + the timestamp of 'previous zone enter';