ルール変数のタイプ
ルール変数は、定数、式、オブジェクト、またはオブジェクトのコレクションを表すことができます。
ルール変数を定義するには、変数に名前を付け、値を設定します。任意の名前を選択でき、値はテキスト、数値、または演算式にすることができます。値は、the age of 'the customer' または the customer of this purchase event などのように、エンティティーまたはイベントの属性にすることもできます。変数を設定した後は、その変数を宣言したルールの任意の部分で使用できます。
定数を定義するルール変数
ルール変数の値は、定数にすることができます。例えば、変数 maxAmount により、以下のルールはわかりやすくなり、ルールの if 部分と then 部分が確実に同じ値を使用するようになります。
definitions set maxAmount to 1000000; if the amount of 'the loan' is at least maxAmount then in 'the loan report', reject the data with the message "The loan cannot exceed " + maxAmount;
ルール変数に関する制約事項
演算子 where を使用すれば、ルールの definitions 部分で変数を更に制限することができます。
次のルールでは、where を使用して、loyal customer 変数をゴールド・カテゴリーの顧客に制限しています。
when a purchase event occurs, called 'the purchase' definitions set 'loyal customer' to the customer of 'the purchase' where the category of this customer is Gold; if the value of the shopping cart of 'loyal customer' is more than 200 then print "Apply super discount";
同じタイプの複数のオブジェクトを参照するルール
異なるフライトを参照する必要があるルールがある場合、ルールの definitions 部分に他方のオカレンスを定義する必要があります。例えば、次のようになります。
definitions
set 'flight1' to a flight in the flights of 'the customer';
set 'flight2' to a flight in the flights of 'the customer'
where the scheduled departure time of this flight is after the scheduled arrival time of 'flight1';
if
the scheduled departure time of 'flight2' is before 1 hour after the scheduled arrival time of 'flight1'
then
print "less than 1 hour of transit time";
注: ルールが同じタイプの複数のオブジェクトを参照している場合、そのルールは、同じオブジェクトの異なる順列に適用される可能性があります。この例では、2 本のフライトがあるため、ルールは 2 回実行されます。ルールが 3 人の顧客を参照する場合、ルールは 6 回実行されます。
過去のイベントを参照するルール変数
ルール変数を使用して、エージェントによって処理済みの、作業メモリーに保管されているイベントを参照することができます。
これは、例えば、長い時間にわたってイベントを比較する場合に役立ちます。
when a return event occurs, called 'the return'
definitions
set 'the purchase' to a purchase event;
if
'the return' is in the same calendar week as 'the purchase'
then...
オブジェクトのすべてのインスタンスを取得するルール変数
演算子 all <...> を使用して、オブジェクトのすべてのインスタンスのリストを取得する変数を作成できます。以下に例を示します。
definitions
set 'gold customers' to all customers in the boarded passengers of 'the flight'
where the category of each customer is Gold;
set 'junior gold customer' to a customer in 'gold customers'
where the age of this customer is at most 15;
set 'senior gold customer' to a customer in 'gold customers'
where the age of this customer is at least 65;
この例では、3 つの変数を作成します。
- gold customers: ゴールド・カテゴリーの顧客のリスト。
- junior gold customer: gold customers リスト内の、年齢が 15 歳以下の顧客。
- senior gold customer: gold customers リスト内の、年齢が 65 歳以上の顧客。