이벤트 생성
규칙 에이전트에서 이벤트를 생성할 수 있습니다. 규칙의 조치 파트에서는 emit <an event> 키워드를 사용하여 에이전트가 트리거될 때 외부로 전송되는 이벤트를 작성할 수 있습니다(조건이 적용되는 경우). 그리고 생성된 이벤트는 이 이벤트 유형을 청취하는 에이전트에 의해 처리될 수 있습니다.
where 키워드를 사용함으로써 작성하고자 하는 새 이벤트 인스턴스의 값을 지정합니다. 이 값은 이벤트 유형에 정의된 속성으로부터 가져옵니다.
새 이벤트의 시간소인은 지정할 필요가 없습니다. 새 이벤트의 시간소인은 매개변수 now의 값으로 설정됩니다.
다음의 예에서는 자전거 대여소 조회 이벤트가 발생할 때 자전거 대여소 엔티티의 정보와 함께 응답 이벤트가 생성됩니다.
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를 참조하는 규칙 에이전트에서 작성됩니다. 고객 이벤트가 수신될 때 새 쿠폰 오브젝트가 이 고객 엔티티에 대해 작성되며 새 이벤트가 외부로 전송됩니다.
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';
다음 규칙은 피자 주문의 처리를 연기합니다. 한 시간 반 이후에 주문의 상태가 아직 "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 ;
원격 엔티티를 업데이트하는 이벤트 생성
원격 엔티티를 업데이트해야 하는 경우에는 한 에이전트에서 다른 에이전트로 알림 이벤트를 생성할 수 있습니다.
에이전트는 해당 바인드된 엔티티만 업데이트할 수 있습니다. 에이전트가 관계를 통해 참조하는 원격 엔티티를 읽을 수는 있지만 이를 업데이트할 수는 없습니다. 에이전트는 원격 엔티티에 대한 업데이트가 필요함을 발견하고 이벤트를 생성하여 다른 에이전트에게 알릴 수 있습니다.
다음 규칙은 불량 고객 이벤트를 청취하며, 불량 고객을 블랙리스트에 추가해야 함을 알리는 새 이벤트를 생성합니다.
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;