Getting values from shared aggregates in rules

Use the values of shared aggregates in your rules to evaluate conditions and make decisions.

You can access the value of a shared aggregate at a specific time point or over a specific time period, and use it in your rules logic. For example, you can use shared aggregates in condition statements and calculations to determine a course of action or whether an event should be emitted.

Each value that is retrieved for an aggregate depends on the time point or the time period that is used in the rule. For time point queries, values are calculated over past events up to and including events that occur at the specified time point. If you don't specify a time point, the value of now of the rule agent is used. For time period queries, values are calculated over events that occur within the specified time period.

The value of the aggregate might be null. In simple conditions such as the ones defined in the examples below, the rule does not execute if the aggregate value is null.

For example, the following rule refers to the aggregate value of the purchase count attribute at the implicit time of now.
if 
   the purchase count of 'the customer' is more than 100
then 
   set the status of 'the customer' to EXCELLENT;

Getting values from shared aggregates at a time point

In rules, you can specify the time point at which a value is aggregated with the following construct:

the <aggregate> at <time point>

In the following example, a rule refers to the aggregate value of the purchase count attribute as of 05/15/2015.
if
   the purchase count of 'the customer' at the time of 5/15/2015 is less than 10
then
   print "The customer made" + the purchase count of 'the customer' + "purchases before 05/16/2015."

Getting values from shared aggregates over a time period

In rules, you can specify the time period over which a value is aggregated with the following construct:

the <aggregate> over <time period>

Restriction: You cannot get the value of a shared aggregate over a time period if this aggregate explicitly or implicitly refers to now in its business model statement.

When you specify the time period in the rule rather than in the aggregate definition, you can reuse the aggregate definition across rules to aggregate the same attribute values over different time periods. The period during which events are available in the business model statement of the aggregate must be large enough to contain the largest time period that is defined in the rules. Otherwise, the query returns null.

In the following example, three rules refer to the same aggregated attribute purchase count defined in the business model statements as follows:

the purchase count of a customer is aggregated from purchase events, 
   where this customer comes from the customer of each purchase event 
   as the number of purchase events
   available for 30 days.

The first rule refers to the purchase count attribute during the last week:

if
   the purchase count of 'the customer' over the last period of 1 week is less than 10
then
   print "The customer made" + the purchase count of 'the customer' + "purchases last week."

The second rule refers to the purchase count attribute over the calendar month of a discount event:

when a discount event occurs
definitions
   set 'purchase month' to the calendar month of this discount event;
if
   the purchase count of 'the customer' over 'purchase month' is less than 10
then
   print "The customer made" + the purchase count of 'the customer' + "purchases last week."

The third rule compares the aggregated values of the purchase count attribute:

when a discount event occurs
definitions
   set 'discount week' to the period between 1 week before this discount event and this discount event;
   set 'recent large purchases' to the purchase count of 'the customer' over 'discount week';
   set 'earlier large purchases' to the purchase count of 'the customer' over the period of 1 week before 'discount week';
if 'recent large purchases' is more than 'earlier large purchases'
then emit a new coupon;