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 query might be null, for example if there is no event within the aggregate's horizon. If the time argument is null, the current time is used. In simple conditions such as the ones defined in the examples below, the rule does not execute if the aggregate value is null.
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>
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;
Getting values from shared aggregates with a grouped by expression
A shared aggregate can contain a grouped by expression:
the total price of a cart is aggregated from purchase events,
where this cart comes from the cart of each purchase event
as the total price of all purchase events
grouped by the city of each purchase event
available for 1 year.
To query a value from this aggregate, you must specify the grouped by parameter, that is, the city of each purchase event. The list of available cities is defined in the business model definitions:
a city can be one of : London, Paris, Tokyo.
The following rule queries the aggregate value for the purchase events that occurred in London:
when a purchase event occurs
if
the total price of the cart of this purchase event for London is at least 500
then
print "Expensive cart in London." ;
If the type of the grouped by parameter is not defined in the business model definitions, its type is string by default, and you need to use quotation marks to specify it in the rule: the total price of the cart of this purchase event for "London"
In the following example, the grouped by expression is a complex expression:
the number of flights of a departure airport is aggregated from flight events,
where this departure airport comes from the departure airport of each flight event
as the number of flight events
grouped by the duration between the departure time of each flight event and the arrival time of each flight event in hours
available for 1 year.
The following rule queries the aggregate value for flight events that have a duration of 2 hours:
when a flight event occurs
if
the number of flights of 'the departure airport' for 2 is more than 20
then
print "More than 20 medium-haul flights from this airport." ;