ルール・アクション

ルール・アクションは、ルールの if 部分が true の場合に実行される 1 つ以上のアクション語句で構成されます。ルールの if 部分が false の場合に実行されるアクション語句を含めることもできます。

アクションは、ルールの then 部分と else 部分で定義します。

ルールの then 部分では、1 つ以上のアクションを定義する必要があります。 then 部分または else 部分に複数のアクションが含まれる場合、ルールは、リストされている順序でアクションを実行します。

then 部分の例

以下のルールは、ショッピング・カートの値が $100 を超えている場合、クーポン・イベントを送信します。それ以外の場合、ルールは何のアクションも適用しません。

when a purchase event occurs
if 
    the value of 'the shopping cart' is more than 100 
then 
    emit a new coupon event where
       the amount is 5,
       the customer is the customer of this purchase event,
       the message is "Thank you for your purchase. Please accept this coupon for $5.";

else 部分の例

次のルールは、条件が満たされた場合に (then)、ある変更を適用し、条件が満たされなかった場合には (else)、別の変更を適用します。

if 
    the status of 'the loan report' is approved 
    and the loan grade of 'the loan report' is one of { "A", "B", "C" } 
then 
    print "Your loan has been approved"; 
else 
    print "We are sorry. Your loan has not been approved";

リスト内の各項目に対するルール・アクション

リストの各項目に対してルールが実行するアクションを定義できます。演算子 for each <item> in <list> を使用して、リスト内の項目にアクションを適用します。

次のアクション・ステートメントのグループは、「expensive purchases」のリストにあるすべての項目に適用されます。

definitions
    set 'expensive purchases' to all purchases in the purchases of 'the customer'
        where the price of each purchase is more than 100;
then 
    for each item in the items of 'expensive purchases':
    - print "A 5% discount has been applied";

then 部分と else 部分があるルール

then 部分と else 部分を含むルールは、同じエンティティーに一致し、お互いの否定である条件を持つ 2 つのルールの圧縮形式です。

場合によっては、then 部分の実行により、エンティティーの状態が変更され、else 部分が適用可能になります。その結果、then 部分と else 部分の両方が適用される可能性があります。

以下の例では、アカウントが NULL の場合、then 部分が適用され、アカウント・エンティティーを作成します。このアクションにより、エンティティーの状態が変更され、条件が false になり、else 部分が適用可能になります。

when a new account event occurs, called 'the event'
if
    'the account' is null
then
    set 'the account' to a new account where
        the customer is the customer of 'the event',
        the opening date is 'the event',
        the status is NEW; 
else 
    print "The account already exists";

エンティティーの状態を作成または変更するには、else 部分があるルールは適切でなく、混乱する結果をもたらす可能性があります。代わりに、お互いを否定する条件を持つ、2 つの別々のルールを作成することができます。例えば、上記のルールを以下のルールに置き換えることができます。

以下のルールは、アカウントが存在するかどうかをチェックします。

when a new account event occurs, called 'the event'
if
    'the account' is null
then
    set 'the account' to a new account where
        the customer is the customer of 'the event',
        the opening date is 'the event',
        the status is NEW; 

以下のルールは、アカウントが存在しないかどうかをチェックします。

when a new account event occurs, called 'the event'
if
    'the account' is not null
then
    print "The account already exists";

イベントが到着した時にアカウント・エンティティーが NULL の値を持っている場合、1 番目のルールが適用可能になり、ルール・アクションは新規アカウント・エンティティーを作成します。

ルールが再度評価される時は、このアカウントの値はもう NULL ではなく、このルール・インスタンスはもう適用可能ではありません。2 番目のルールが適用可能になり、一致したルールのリストに追加されます。2 番目のルールが実行されると、アクションは「The account already exists (このアカウントは既に存在します)」というメッセージを表示します。ルールのマッチングおよび選択プロセスについて詳しくは、ルールの振る舞いを参照してください。