event (in rule actions)

The event keyword declares an event condition or an event object.

Purpose

This keyword is used for declaring an event condition or an event object in rule actions.

Context

Functions or rule actions

Syntax

insert event [(timeExpression)] object [{statement1;...;statementn}];

Description

Deprecated as of V7.5.

Next to a rule, when an object is inserted into the working memory as an event, a timestamp is automatically associated with it. The timeExpression argument defines an integer value which is used as the timestamp when an event is inserted. The term object must be a constructor for the class. For the constructor to apply, you must declare it as public in your Java™ code.

Following the optional timeExpression, the insert event statement can either terminate with a semicolon (;) or specify a block of executable statements. The statements are executed on the object before this object is inserted into the working memory.

You can remove an event object explicitly from the working memory by using the retract statement or the IlrContext. IlrContext#retract method.

Example

The alarmManager rule prints a message when two alarms occur on the same managed object with a time difference between 1 and 5 clock ticks.

rule alarmManager {
   when {
      ?mo: ManagedObject();
      ?operator: Operator() in ?mo.subscribers();
      ?alarm1: event Alarm(managedObject == ?mo);
      ?alarm2: event Alarm(managedObject == ?mo; ?this after[1, 5]
            ?alarm1);
   } 
   then {
      System.out.println("Seen two alarms on " + ?mo.name);
      ?operator.report(?alarm1, ?alarm2);
   }
}

The when keyword introduces four conditions:

  1. In the first condition, a variable ?mo is matched by a ManagedObject object.

  2. The second condition returns the subscribed Operator objects in the variable ?operator.

  3. The third condition is an event statement. The variable ?alarm1 points to an event object named Alarm with a single constraint: the value of the field managedObject is equal to the variable ?mo.

  4. The fourth condition is also an event statement, where the variable ?alarm2 points to an event object named Alarm with the following constraints: the value of the field managedObject is equal to the variable ?mo, and the difference between the first event object timestamp and the second event object timestamp is greater than or equal to 1 and less than or equal to 5 clock ticks.

The then keyword introduces two actions:

  1. The first action prints a message naming a managed object.

  2. The second action executes an ?operator.report method that uses the event object variables ?alarm1 and ?alarm2.

Any alarm is automatically retracted from the working memory 6 clock ticks after its insertion.