文章目录
- Drools开源业务规则引擎(三)- 事件模型(Event Model)
- 1.org.kie.api.event
- 2.RuleRuntimeEventManager
- 3.RuleRuntimeEventListener
- 接口说明
- 示例
- 规则文件
- 规则执行
- 日志输出
- 4.AgentaEventListener
- 接口说明
- 示例
- 监听器实现类MyAgendaEventListener
- 规则文件
- 规则执行
- 日志输出
Drools开源业务规则引擎(三)- 事件模型(Event Model)
1.org.kie.api.event
在org.kie.api.event
中提供了规则引擎相关的事件监听和处理机制,允许开发者在规则引擎执行过程中监听和处理事件,包括规则触发、断言对象等,并且还有利于将日志记录和应用程序的核心功能(以及规则)分离。
接口摘要
Interface | Description |
---|---|
KieRuntimeEvent | KIE运行时生成的事件。 |
KieRuntimeEventManager | KIE运行事件的管理器 |
KieRuntimeEventManager
接口由KieRuntime
实现,它提供两个接口,RuleRuntimeEventManager
和ProcessEventManager
(KieRuntimeEventManager
类图如下)。本章仅介绍RuleRuntimeEventManager
。
2.RuleRuntimeEventManager
RuleRuntimeEventManager
允许添加和删除侦听器,以便可以侦听工作内存和agenda的事件。
Drools 还提供DebugRuleRuntimeEventListener
和DebugAgentaEventListener
,通过调试打印语句实现每个方法。
示例
//添加 DebugRuleRuntimeEventListener
ksession.addEventListener( new DebugRuleRuntimeEventListener());
//添加 DebugAgentaEventListener
ksession.addEventListener( new DebugAgentaEventListener());
3.RuleRuntimeEventListener
接口说明
public interface RuleRuntimeEventListener extends EventListener {
void objectInserted(ObjectInsertedEvent event);
void objectUpdated(ObjectUpdatedEvent event);
void objectDeleted(ObjectDeletedEvent event);
}
objectInserted
:表示发生了插入事实的事件objectUpdated
:表示发生了更新事实的事件objectDeleted
:表示发生了删除事实的事件
示例
规则文件
rule "age"
enabled true
when
$in:UserIn(age > 10)
then
$in.setAge(1);
update($in);
end
rule "in_rule"
enabled true
when
$in:UserIn(name in ("jack","mary"))
then
output.setAgeStages("命中了in条件");
delete($in);
end
规则执行
KieBase kieBase = kieHelper.build(kieBaseConfiguration);
// 创建会话
KieSession kieSession = kieBase.newKieSession();
// 创建规则输出对象
UserOut output = new UserOut();
kieSession.setGlobal("output", output);
// 创建规则输入对象
UserIn input = new UserIn();
input.setAge(19);
input.setName("jack");
// 添加事件监听
kieSession.addEventListener(new DebugRuleRuntimeEventListener());
// 执行规则
kieSession.insert(input);
kieSession.fireAllRules();
日志输出
4.AgentaEventListener
接口说明
public interface AgendaEventListener
extends
EventListener {
void matchCreated(MatchCreatedEvent event);
void matchCancelled(MatchCancelledEvent event);
void beforeMatchFired(BeforeMatchFiredEvent event);
void afterMatchFired(AfterMatchFiredEvent event);
void agendaGroupPopped(AgendaGroupPoppedEvent event);
void agendaGroupPushed(AgendaGroupPushedEvent event);
void beforeRuleFlowGroupActivated(RuleFlowGroupActivatedEvent event);
void afterRuleFlowGroupActivated(RuleFlowGroupActivatedEvent event);
void beforeRuleFlowGroupDeactivated(RuleFlowGroupDeactivatedEvent event);
void afterRuleFlowGroupDeactivated(RuleFlowGroupDeactivatedEvent event);
}
matchCreated
:在规则匹配创建时触发,当规则条件满足时,创建一个匹配项。matchCancelled
:在规则匹配取消时触发,当规则条件不再满足时,取消一个匹配项。beforeMatchFired
:在规则执行前触发,可以用于在规则执行前进行一些前置处理。afterMatchFired
:在规则执行后触发,可以用于在规则执行后进行一些后置处理。agendaGroupPopped
:当一个AgendaGroup
被弹出Popped
时,将触发此方法。AgendaGroup
是drl
中的一个逻辑分组,它可以包含多个规则Rule
。当某个AgendaGroup
不再是活动状态时,即规则引擎不再对其进行处理时,该AgendaGroup
将被弹出。agendaGroupPushed
:当一个AgendaGroup
被推入Pushed
规则引擎时,将触发此方法。。当某个AgendaGroup
变为活动状态时,即规则引擎开始对其进行处理时,该AgendaGroup
将被推入。beforeRuleFlowGroupActivated
:当一个规则流组RuleFlowGroup
被激活之前,将触发此方法。afterRuleFlowGroupActivated
:当一个规则流组RuleFlowGroup
被激活之后,将触发此方法。beforeRuleFlowGroupDeactivated
:当一个规则流组RuleFlowGroup
被停用之前,将触发此方法afterRuleFlowGroupDeactivated
:当一个规则流组RuleFlowGroup
被停用之后,将触发此方法
示例
监听器实现类MyAgendaEventListener
创建一个监听器实现类MyAgendaEventListener
,继承AgentaEventListener
监听器接口默认的实现类DefaultAgendaEventListener
。
@Slf4j
public class MyAgendaEventListener extends DefaultAgendaEventListener {
@Override
public void matchCreated(MatchCreatedEvent event) {
log.info("规则匹配创建:{}",event.getMatch().getRule().getName());
}
@Override
public void matchCancelled(MatchCancelledEvent event) {
log.info("规则匹配取消:{}",event.getMatch().getRule().getName());
}
@Override
public void beforeMatchFired(BeforeMatchFiredEvent event) {
log.info("规则执行前:{}",event.getMatch().getRule().getName());
}
@Override
public void afterMatchFired(AfterMatchFiredEvent event) {
log.info("规则执行后:{}",event.getMatch().getRule().getName());
}
@Override
public void agendaGroupPopped(AgendaGroupPoppedEvent event) {
log.info("AgendaGroup弹出:{}",event.getAgendaGroup().getName());
}
@Override
public void agendaGroupPushed(AgendaGroupPushedEvent event) {
log.info("AgendaGroup推入:{}",event.getAgendaGroup().getName());
}
@Override
public void beforeRuleFlowGroupActivated(RuleFlowGroupActivatedEvent event) {
log.info("RuleFlowGroup激活前:{}",event.getRuleFlowGroup().getName());
}
@Override
public void afterRuleFlowGroupActivated(RuleFlowGroupActivatedEvent event) {
log.info("RuleFlowGroup激活后:{}",event.getRuleFlowGroup().getName());
}
@Override
public void beforeRuleFlowGroupDeactivated(RuleFlowGroupDeactivatedEvent event) {
log.info("RuleFlowGroup停用前:{}",event.getRuleFlowGroup().getName());
}
@Override
public void afterRuleFlowGroupDeactivated(RuleFlowGroupDeactivatedEvent event) {
log.info("RuleFlowGroup停用后:{}",event.getRuleFlowGroup().getName());
}
}
规则文件
rule "age"
enabled true
// 自版本6.x起,agenda-group和ruleflow-group两个概念已经合并
// ruleflow-group "flow" 相当于 agenda-group "flow"
ruleflow-group "flow"
when
$in:UserIn(age > 10)
then
output.setAgeStages("大于10");
end
rule "in_rule"
enabled true
ruleflow-group "flow"
when
$in:UserIn(age <= 10)
then
output.setAgeStages("小于等于10");
end
规则执行
KieBase kieBase = kieHelper.build(kieBaseConfiguration);
// 创建会话
KieSession kieSession = kieBase.newKieSession();
// 创建规则输出对象
UserOut output = new UserOut();
kieSession.setGlobal("output", output);
// 创建规则输入对象
UserIn input = new UserIn();
input.setAge(19);
input.setName("jack");
// 设置焦点
kieSession.getAgenda().getAgendaGroup("flow").setFocus();
// 添加事件监听
kieSession.addEventListener(new MyAgendaEventListener());
// 执行规则
kieSession.insert(input);
kieSession.fireAllRules();