Defining global event aggregates

You define a global event aggregate by specifying an aggregate expression, an optional time filter, and an optional default value.

Before you begin

Create a global aggregate definition file (.agg).

About this task

A global event aggregate can find the total, minimum, maximum, or average value of a collection of events, or the number of events. For example, if your events are flight departures, you can determine the average number of delayed flight departures over the past month or another time period.

Global event aggregates run continuously as part of a deployed solution. A global event aggregate is calculated every time one of the events occurs.

You define a global event aggregate by composing an expression in the .agg file that has the following syntax:
define '<same_name_as_file>' as <expr>

If events have occurred for too short a time, a global event aggregate might not have a meaningful value. Therefore, you can specify a default value and the condition for when the default applies. The default value applies unless enough event history exists for the aggregate calculations, and any rules that are based on them, to have meaningful results. You might need to test and experiment with the default value and the condition to obtain optimal results for your particular solution.

The event history is the elapsed time after the first event of any type occurs in a deployed solution. If you change the definition of the global event aggregate and deploy a new version of the solution, the elapsed time is reset to zero and restarts after the first event of any type occurs.

In the editor, you can type Space or Ctrl+Space to display the available constructs, and then double-click a construct to select it.

Procedure

  1. Specify the name of the variable. You can overwrite the default, which is the same name as the .agg file.
  2. Replace <expr> with an aggregate expression:
    1. Specify an aggregation operator or the number of construct.
    2. Specify an attribute of an event.
    3. Optional: Specify a time filter to restrict the events to a specific time period, and specify a where clause to restrict the events with a condition. The time filter might be further restricted by time horizon attributes that are set for your solution.
    4. Optional: Specify a default value phrase and condition.
  3. Click Save.

Results

The global aggregate is verbalized and you can use its variable name in rules and Java™ agents in your solution. In a Java agent and in the REST API, you must prefix defvar to the variable name.

Attention: You can view a summary of the global aggregates in the globalQueries.var variable set file, which is in the aggregates folder of the BOM project. Do not modify the globalQueries.var file. If you change the name of the variable, the global aggregate cannot be used in business rules.

Examples

Consider the following definition of a global event aggregate:
define 'average_delay' as the average delay of all flight_delay events 

This statement defines the global aggregate variable average_delay, which can be used in the rules in the solution. When the solution is deployed and running, partitions in the data grid receive flight_delay events, and a local average is calculated within each partition, relating to the delays at the particular entity, such as an airport. The partial results are aggregated to obtain the overall average delay.

The following statement adds a restrictive condition to the definition:
define 'average_delay' as the average delay of all flight_delay events, where the delay of each flight_delay event is more than 10

The condition restricts the calculation to only the delays that are more than 10 minutes.

The following statement is equivalent but uses simplified language:
define 'average_delay' as the average delay of all flight_delay events, where the delay is more than 10 
The following definition of a global event aggregate includes a default value and condition:
define 'average_delay' as the average delay of all flight_delay events, defaulting to 5 if there is less than 2 hours of event history