ルール変数のタイプ

ルール変数にはさまざまなタイプの値を割り当てることができます。

ルール変数は、定数、式、ビジネス用語、またはビジネス用語のコレクションを表すことができます。ルール変数は、その名前と値を割り当てることで定義します。名前を選択し、値はテキスト、数値、または算術式とすることができます。また値は、ルールに既に存在する定義済みのビジネス用語 (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;

ビジネス用語の複数の出現箇所を参照するルール変数

ルール内に 1 つのビジネス用語の定義が複数ある場合には、異なる定義を定義する必要があります。

開発者がビジネス用語の自動変数を使用可能にすると、単語 the によって、その種の冠詞を使用するロケールで自動変数が識別されます。例えば、語 "customer" をルールのビジネス用語として使用した場合、開発者は、the 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’ 

この条件は、customer 1 と customer 2 という 2 人の異なる顧客を識別して、名前を割り当てます。 ビジネス用語「顧客」は多様であり、2 つの customer 変数を定義して、次のようなルールを書くことができます。

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;
注: ルールにビジネス用語の複数の出現が含まれている場合、そのルールは、すべての置換に対応するために複数回実行されます。この例では、顧客の数は 2 人であるため、ルールが 2 回実行されます。ルールの顧客の数が 3 人であったとすると、ルールは 6 回実行されます。

ビジネス用語のすべての出現箇所を取得するルール変数

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: 年齢が 15 歳以下のゴールド顧客リストの顧客。
  • senior gold customer: 年齢が 65 歳以上のゴールド顧客リストの顧客。