我在DROOLS中有2个简单的规则来测试规则是否被触发。这是规则:
package rule2
import com.SBVR.carrental.model.Rental
rule "Your First Rule"
when
Rental( )
then
System.out.println("Rental exist");
end
rule "Your Second Rule"
when
//conditions
then
System.out.println("Second rule");
end
我还在我的kieSession中添加了eventListener,所以我知道是否触发规则,这是我用来触发规则的代码:
KieServices service = KieServices.Factory.get();
KieContainer kieContainer = service.getKieClasspathContainer();
KieSession session= kieContainer.newKieSession("ksession-rules2");
session.addEventListener(new RuleRuntimeEventListener() {
@Override
public void objectInserted(ObjectInsertedEvent event) {
System.out.println("Object inserted \n"
+ event.getObject().toString());
}
@Override
public void objectUpdated(ObjectUpdatedEvent event) {
System.out.println("Object was updated \n"
+ "new Content \n" + event.getObject().toString());
}
@Override
public void objectDeleted(ObjectDeletedEvent event) {
System.out.println("Object retracted \n"
+ event.getOldObject().toString());
}
});
session.addEventListener(new AgendaEventListener() {
@Override
public void matchCreated(MatchCreatedEvent event) {
System.out.println("The rule "
+ event.getMatch().getRule().getName()
+ " can be fired in agenda");
}
@Override
public void matchCancelled(MatchCancelledEvent event) {
System.out.println("The rule "
+ event.getMatch().getRule().getName()
+ " cannot b in agenda");
}
@Override
public void beforeMatchFired(BeforeMatchFiredEvent event) {
System.out.println("The rule "
+ event.getMatch().getRule().getName()
+ " will be fired");
}
@Override
public void afterMatchFired(AfterMatchFiredEvent event) {
System.out.println("The rule "
+ event.getMatch().getRule().getName()
+ " has be fired");
}
@Override
public void agendaGroupPopped(AgendaGroupPoppedEvent event) {
}
@Override
public void agendaGroupPushed(AgendaGroupPushedEvent event) {
}
@Override
public void beforeRuleFlowGroupActivated(RuleFlowGroupActivatedEvent event) {
}
@Override
public void afterRuleFlowGroupActivated(RuleFlowGroupActivatedEvent event) {
}
@Override
public void beforeRuleFlowGroupDeactivated(RuleFlowGroupDeactivatedEvent event) {
}
@Override
public void afterRuleFlowGroupDeactivated(RuleFlowGroupDeactivatedEvent event) {
}
});
Rental mirrorRental = new Rental();
session.insert(mirrorRental);
session.fireAllRules();
session.dispose();
这是结果:
Object inserted
com.SBVR.carrental.model.Rental@3d4ab141
The rule Your Second Rule can be fired in agenda
The rule Your Second Rule will be fired
Second rule
The rule Your Second Rule has be fired
我的问题是:第二条规则很明显,因为没有条件。第一条规则有一个简单的条件来检查租赁是否存在。通过日志,它清楚地显示Rental已插入规则引擎。为什么第一条规则甚至没有解雇?