ルール変数のタイプ
さまざまなタイプの値をルール変数に割り当てることができます。
ルール変数は、定数、式、ビジネス用語、またはビジネス用語のコレクションを表すことができます。ルール変数を定義するには、変数に名前を付け、値を設定します。任意の名前を選択でき、値はテキスト、数値、または演算式にすることができます。値は、あらかじめルールに含まれている定義済みのビジネス用語 (例えば、customer) にすることもできます。変数を設定した後は、その変数を宣言したルールの任意の部分で使用できます。
定数を定義するルール変数
ルール変数の最も簡単な使用法は、定数値の宣言です。
例えば、変数 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 変数をゴールド・カテゴリーの顧客に制限しています。
definitions set 'loyal customer' to a customer where the category of this customer is Gold; if the value of the shopping cart of 'loyal customer' is more than $200 then apply the super discount;
例
次のルールでは、senior Gold customer 変数を、ゴールド・カテゴリーの 65 歳以上の顧客として宣言しています。
definitions set 'senior Gold customer' to a customer where all the following conditions are true: - the category of this customer is Gold - the age of this customer is at least 65;
ビジネス用語の複数の出現箇所を参照するルール変数
あるビジネス用語が同一ルール内で複数の定義を持つ場合、それぞれの定義を明示しなければなりません。
開発者がビジネス用語の自動変数を使用可能にすると、単語 the によって、その種の冠詞を使用するロケールで自動変数が識別されます。例えば、ルール内で「customer」という単語をビジネス用語として使用する場合、開発者は the customer という自動変数を定義できます。その後はこの自動変数をルール内で参照できます。
「customer」の複数のオカレンスを参照する必要のあるルールがある場合、ルールの definitions 部分で、それぞれのオカレンスを定義する必要があります。以下に例を示します。
definitions set applicant to a customer; set 'loyal customer' to a customer; if all of the following conditions are true: - applicant is married to 'loyal customer' - 'loyal customer' is insured then upgrade applicant's rating;
同じ種類の物が 2 つ以上あり、それらの関係を参照するルールでは、変数が便利です。
例えば、次の条件には、2 人の顧客が含まれています。
if ’customer 1’ is married to ’customer 2’
この条件では、2 人の顧客にそれぞれ customer 1 と customer 2 と名前を付け、識別しています。ビジネス用語「customer」に変化を持たせることで、customer の 2 つの変数を定義して、以下のようなルールを記述できます。
definitions set ’customer 1’ to a customer; set ’customer 2’ to a customer; if ’customer 1’ is married to ’customer 2’ and ’customer 2’ is insured then upgrade ’customer 1’’s rating;
ビジネス用語のすべての出現箇所を取得するルール変数
BAL 演算子 all <...> を使用して、ビジネス用語のすべてのオカレンスのリストを取得する変数を作成できます。以下に例を示します。
definitions set 'gold customers' to all customers where the category of this 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 歳以上の顧客。