イベントの発行
ルール・エージェントからイベントを発行することができます。ルールのアクション部分で emit <an event> キーワードを使用して、条件が適用される場合に、エージェントがトリガーされる際に送信されるイベントを作成します。発行されたイベントは、このイベント・タイプを listen しているエージェントによって処理することができます。
where キーワードを使用して、作成する新規イベント・インスタンスの値を指定します。これらの値は、イベント・タイプに定義されている属性から取得します。
新規イベントのタイム・スタンプを指定する必要はありません。新規イベントのタイム・スタンプは、パラメーター now の値に設定されます。
以下の例で、駐輪場照会 (bike station query) イベントが発生すると、応答イベントが駐輪場 (bike station) エンティティーからの情報と共に発行されます。
when a bike station query occurs
then
print "BikeStation query rule received query for station [" + the name of 'the bike station' + "]";
emit a new bike station query response where
the available bike stands is the available bike stands of 'the bike station',
the available bikes is the available bikes of 'the bike station',
the last update is the last update of 'the bike station',
the rider is the rider of this bike station query;
以下のビジネス・ルールは、エンティティー customer を参照するルール・エージェント内に作成されます。顧客 (customer) イベントを受信すると、新規クーポン・オブジェクトがこの顧客 (customer) エンティティーに対して作成され、新規イベントが送信されます。
when a customer event occurs, called 'the event'
then
add a new coupon where
the amount is 100,
the long description is the description of 'the event',
the short description is "New coupon" to the coupons of 'the customer';
emit a new coupon sent external where
the customer is 'the customer';
以下のルールは、ピザの注文の処理を延期します。注文の状況が 1 時間半後もまだ「PRICED」の場合は、ピザがまだ配達されていないことを意味しています。その場合、ルールは遅延の補償として無料注文イベントを発行します。
when a pizza order has occurred 1 hour 30 minutes ago
if
the order status of ‘the order’ is PRICED
then
emit a new free order where
the amount is the price of 'the order',
the customer is the customer of this pizza order,
the order is 'the order',
the message is "We apologize for the lateness of your order. It is on us!";
以下のルールはピザの注文を処理します。そのお客様の状況が「GOLD」である場合、イベントが作成および発行され、お客様のお気に入りのおまけが各ピザのトッピングに追加されます。この例ではコレクションを持つイベントを発行する方法を示します (ここでは、ピザによって表されます)。
when a pizza event occurs
if
the status of the customer of this pizza event is GOLD
then
define 'bonus' as a new pizza bonus event ;
for each pizza in the pizzas of the order of this pizza event :
- add the favorite supplement of the customer of this pizza event to the toppings of this pizza
- add this pizza to the pizzas of bonus ;
emit bonus ;
リモート・エンティティーを更新するイベントの発行
リモート・エンティティーの更新が必要な場合は、あるエージェントから別のエージェントに通知イベントを発行することができます。
エージェントは、そのエージェントのバインド・エンティティーのみを更新できます。エージェントが関係を使用して参照するリモート・エンティティーは、読み取りはできますが、更新はできません。エージェントは、リモート・エンティティーの更新が必要なことを検出し、イベントを発行して別のエージェントに通知することができます。
以下のルールは、脅迫的な顧客 (abusive customer) イベントを listen し、その脅迫的な顧客をブラックリストに追加する必要があることを通知する新規イベントを発行します。
when an abusive customer occurs
then
emit a new add blacklist event where
the customer is the customer of this abusive customer;
イベントは、エンティティーが初期化される前に出力されるため、この同じイベントによって初期化されるリモート・エンティティーを更新する目的でイベントを出力することはできません。イベントとエンティティーの間のルーティングは、エンティティーの初期化前に作成され、その時点でまだエンティティーは存在しないため、解決できません。
以下の例で、blacklist event には customer id 属性があります。初期化ステートメントでは、この属性を使用して blacklist event の customer エンティティーを初期化します。
--- business model definitions ---
a customer is a business entity identified by an id.
a customer has a name.
an abusive customer is a business event.
an abusive customer has a customer id.
a blacklist event is a business event.
a blacklist event has a customer id.
--- initialization statement ---
a customer is initialized from a blacklist event , where this customer comes from the customer id of this blacklist event .
これで、blacklist event を出力して、abusive customer イベントの customer id から customer id リモート・エンティティーを更新できます。 blacklist event の customer エンティティーを、この customer id から初期化および更新できます。
when an abusive customer occurs
then
emit a new add blacklist event where
the customer id is the customer id of this abusive customer;