ルールでの共有集約の値の取得
ルール内で共有集約の値を使用して、条件を評価し、意思決定を行うことができます。
特定時点または特定期間における共有集約の値にアクセスして、それをルール・ロジック内で使用できます。例えば、共有集約を条件ステートメントおよび計算に使用して、実行すべき一連のアクションを決定したり、イベントを出力すべきかどうかを判定したりできます。
集約用に取得される各値は、ルール内で使用される時点または期間に依存します。時点照会では、値は指定された時点に発生したイベント (そのイベントも含む) までの過去のイベントを対象に計算されます。 時刻を指定しないと、ルール・エージェントの now の値が使用されます。期間照会では、値は指定された期間内に発生したイベントを対象に計算されます。
例えば集約の期間内にイベントがない場合などに、集約照会の値が NULL になることがあります。 時間の引数が NULL の場合、現在時刻が使用されます。以下の例で定義したような単純な条件では、集約値が NULL の場合、ルールは実行されません。
if
the purchase count of 'the customer' is more than 100
then
set the status of 'the customer' to EXCELLENT;
特定時点の共有集約の値の取得
ルールで、以下の構成体を使用して、値を集約する時点を指定できます。the <aggregate> at <time point>
以下の例の場合、ルールは、05/15/2015 時点における purchase count 属性の集約値を参照します。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."
特定期間の共有集約の値の取得
ルールで、以下の構成体を使用して、値を集約する期間を指定できます。the <aggregate> over <time period>
集約定義内ではなく、ルール内で期間を指定すると、 ルールをまたがって集約定義を再使用して、同じ属性値を異なる期間で集約することができます。 集約のビジネス・モデル・ステートメント内のイベント使用可能期間は、ルール内に定義された最大期間を含めるのに十分な大きさでなければなりません。 そうでない場合、照会はヌルを返します。
以下の例では、ビジネス・モデル・ステートメントで次のように定義された同一の集約属性 purchase count を 3 つのルールで参照します。
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.
以下の最初のルールでは、過去 1 週間の purchase count 属性を参照します。
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."
以下の 2 番目のルールでは、discount event のカレンダー月における purchase count 属性を参照します。
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."
以下の 3 番目のルールでは、purchase count 属性の集約値を比較します。
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;
grouped by 式を使用した共有集約の値の取得
共有集約は grouped by 式を含むことができます。
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.
この集約の値を照会するには、grouped by パラメーター (つまり、各 purchase event の city) を指定する必要があります。使用可能な都市のリストは、ビジネス・モデル定義で定義されています。
a city can be one of : London, Paris, Tokyo.
以下のルールは、London で発生した purchase events の集約値を照会します。
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." ;
grouped by パラメーターの型がビジネス・モデル定義で定義されていない場合、その型はデフォルトでストリングとなり、ルールでそのパラメーターを指定するときに、次のように引用符を使用する必要があります: the total price of the cart of this purchase event for "London"
以下の例で、grouped by 式は複合式です。
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.
以下のルールは、所要時間が 2 時間のフライト・イベントの集約値を照会します。
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." ;