ルール変数のタイプ

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

ルール変数は、定数、式、ビジネス用語、またはビジネス用語のコレクションを表すことができます。 ルール変数の定義は、名前と値を設定することで行います。 名前を選択し、値は、テキスト、数値、または演算式のいずれでも可能です。 値は、ルールに既に存在する定義済みのビジネス用語 (例えば、 customer) にすることもできます。 変数を設定すると、その変数を宣言するルールの任意の部分でその変数を使用できます。

定数を定義するルール変数

ルール変数のもっとも単純な使用法は、定数値の宣言です。

例えば、変数 maxAmount により、以下のルールはわかりやすくなり、ルールの if 部分と then 部分が確実に同じ値を使用するようになります。

定義
	set maxAmount to 1000000;
 の場合
	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 変数がゴールド・カテゴリーの顧客に制限されます。

定義
	set 'loyal customer' to a customer 
		where the category of this customer is Gold; 
の場合
	the value of the shopping cart of 'loyal customer' is more than $200 
then
	apply the super discount;

次のルールでは、senior Gold customer 変数を、ゴールド・カテゴリーの 65 歳以上の顧客として宣言します。

定義
	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 部分でその他の出現を定義する必要があります。以下に例を示します。

定義
	set applicant to a customer; 
	set 'loyal customer' to a customer; 
の場合
	all of the following conditions are true: 
		- applicant is married to 'loyal customer' 
		- 'loyal customer' is insured 
then
	upgrade applicant's rating; 

ルール内の変数は、2 つ以上の同じタイプのものの間の関係を参照する場合に便利です。

例えば、次の条件には、2 人の顧客が含まれています。

の場合
	’customer 1’ is married to ’customer 2’ 

条件は 2 人の異なる顧客を識別し命名します: customer 1customer 2。 ビジネス用語「customer」に変化形ができるので、2 つの customer 変数を定義して、次のようなルールを作成できます。

定義
	set ’customer 1’ to a customer; 
	set ’customer 2’ to a customer; 
の場合
	’customer 1’ is married to ’customer 2’ 
	and ’customer 2’ is insured 
then
	upgrade ’customer 1’’s rating;
注: ルールにビジネス用語の複数のオカレンスが含まれている場合、そのルールは、すべての順列をカバーするために複数回実行されます。 この例では、2 人の顧客がいるため、ルールは 2 回実行されます。 そのルールに 3 人の顧客が含まれる場合は、そのルールは 6 回実行されます。

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

BAL 演算子の all <...> を使用して、次の例に示すような、ビジネス用語のすべての出現のリストを取得する変数を作成できます。

定義 
	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 歳以上のゴールド顧客のリストに含まれる顧客。