KieSession 除了提供丰富的API外, 还提供三个event listener, 方便对于规则执行状况的监控.
AgendaEventListener
该接口可以监控 rule 被fire, 所以这个listener最常用, 对于规则的计算, 我们很可能需要做logging, 常见的做法是在drl的RHS主动做logging, 但这并不是很好的方式, 一来RHS代码会变得冗长, 二来也可能会漏写logging.
使用方法:
Drools 提供了一个缺省的 DefaultAgendaEventListener
类, 我们可以实例化一个这个类, 并重载一些我们关注的方法, beforeMatchFired() 方法是在规则执行之前被调用;afterMatchFired() 方法是在规则执行之后被调用; matchCreated() 方法是规则被放到agenda时被调用;
RuleRuntimeEventListener
可以监听哪些fact 被加到working memory中了, 不管这些fact是通过rule还是java端做的insert()/update()/delete() 都能触发 RuleRuntimeEventListener.
使用方法:
Drools 提供了一个缺省的 DefaultRuleRuntimeEventListener
类, 我们可以实例化一个这个类.
ProcessEventListener
这个监听的事件仅和jBPM相关, 所以不需要关心.
示例代码
- drl 规则文件:
package com.sample.rules
import com.sample.Order;
rule "not_worry_loop"
when
$order:Order(originalPrice>0)
then
$order.setAmount(100);
update($order) ; //trigger DefaultRuleRuntimeEventListener
end
- java 测试文件:
package com.sample;
import java.util.List;
import org.drools.core.event.DefaultAgendaEventListener;
import org.junit.Test;
import org.kie.api.KieServices;
import org.kie.api.event.rule.AfterMatchFiredEvent;
import org.kie.api.event.rule.DefaultRuleRuntimeEventListener;
import org.kie.api.event.rule.ObjectDeletedEvent;
import org.kie.api.event.rule.ObjectUpdatedEvent;
import org.kie.api.runtime.KieContainer;
import org.kie.api.runtime.KieSession;
public class UnitTest {
@Test
public void test() {
// load up the knowledge base
KieServices ks = KieServices.Factory.get();
KieContainer kContainer = ks.getKieClasspathContainer();
KieSession kSession = kContainer.newKieSession("ksession-rules");
kSession.addEventListener(new DefaultAgendaEventListener() {
@Override
public void afterMatchFired(AfterMatchFiredEvent event) {
// TODO Auto-generated method stub
super.afterMatchFired(event);
System.out.println("=============");
List<Object> orders = event.getMatch().getObjects();
for (Object order : orders) {
System.out.println(order);
}
}
@Override
public void matchCreated(MatchCreatedEvent event) {
super.matchCreated(event);
System.out.println("The rule " + event.getMatch().getRule().getName() + " can be fired in agenda");
}
@Override
public void matchCancelled(MatchCancelledEvent event) {
super.matchCancelled(event);
System.out.println("The rule " + event.getMatch().getRule().getName()+ " cannot b in agenda");
}
});
kSession.addEventListener(new DefaultRuleRuntimeEventListener() {
@Override
public void objectDeleted(ObjectDeletedEvent event) {
// TODO Auto-generated method stub
super.objectDeleted(event);
System.out.println("deleted:" + event.getOldObject());
}
@Override
public void objectInserted(ObjectInsertedEvent event) {
// TODO Auto-generated method stub
super.objectInserted(event);
System.out.println("inserted:" + event.getObject());
}
@Override
public void objectUpdated(ObjectUpdatedEvent event) {
// TODO Auto-generated method stub
super.objectUpdated(event);
System.out.println("update:" + event.getObject());
}
});
Order order = null;
order = new Order();
order.setAmount(2);
order.setOriginalPrice(0.2D);
kSession.insert(order);
order = new Order();
order.setAmount(20);
order.setOriginalPrice(2.0D);
kSession.insert(order);
kSession.fireAllRules();
kSession.dispose();
}
}