了解议程组的活动锁定
understanding lock-on-active with agenda-group
我尝试了一个示例来了解活动锁定的工作原理。当我在不使用议程组的情况下触发规则时,一切似乎都很好。但是当我在下面的代码中取消注释议程组并将焦点设置到组 "Group B" 时,不会触发任何规则。
规则
rule "Additional Rs.1 tax for books above Rs.10"
//agenda-group "Group B"
lock-on-active true
when
$o: Product(name=="Book",amount>10)
then
System.out.print($o.getAmount()+"-->");
modify ($o) {
setAmount($o.getAmount()+1);
}
System.out.println($o.getAmount());
end
rule "Additional Rs.2 tax for books above Rs.20"
//agenda-group "Group B"
lock-on-active true
when
$o: Product(name=="Book",amount>20)
then
System.out.print($o.getAmount()+"-->");
modify ($o) {
setAmount($o.getAmount()+1);
}
System.out.println($o.getAmount());
end
用于触发规则的代码
KieServices kieServices=KieServices.Factory.get();
KieContainer kieContainer=kieServices.getKieClasspathContainer();
KieSession kieSession=kieContainer.newKieSession("ksession-lockOnActive");
Product product=new Product();
product.setName("Book");
product.setAmount(11);
Product product2=new Product();
product2.setName("Book");
product2.setAmount(21);
kieSession.getAgenda().getAgendaGroup("Group B").setFocus();
kieSession.insert(product);
kieSession.insert(product2);
kieSession.fireAllRules();
没有议程组的输出
21-->22
11-->12
22-->23
我使用的是旧版本的 Drools(6.2.0 Final)。当我将其更改为 7.4.1 时。代码有效
我尝试了一个示例来了解活动锁定的工作原理。当我在不使用议程组的情况下触发规则时,一切似乎都很好。但是当我在下面的代码中取消注释议程组并将焦点设置到组 "Group B" 时,不会触发任何规则。
规则
rule "Additional Rs.1 tax for books above Rs.10"
//agenda-group "Group B"
lock-on-active true
when
$o: Product(name=="Book",amount>10)
then
System.out.print($o.getAmount()+"-->");
modify ($o) {
setAmount($o.getAmount()+1);
}
System.out.println($o.getAmount());
end
rule "Additional Rs.2 tax for books above Rs.20"
//agenda-group "Group B"
lock-on-active true
when
$o: Product(name=="Book",amount>20)
then
System.out.print($o.getAmount()+"-->");
modify ($o) {
setAmount($o.getAmount()+1);
}
System.out.println($o.getAmount());
end
用于触发规则的代码
KieServices kieServices=KieServices.Factory.get();
KieContainer kieContainer=kieServices.getKieClasspathContainer();
KieSession kieSession=kieContainer.newKieSession("ksession-lockOnActive");
Product product=new Product();
product.setName("Book");
product.setAmount(11);
Product product2=new Product();
product2.setName("Book");
product2.setAmount(21);
kieSession.getAgenda().getAgendaGroup("Group B").setFocus();
kieSession.insert(product);
kieSession.insert(product2);
kieSession.fireAllRules();
没有议程组的输出
21-->22
11-->12
22-->23
我使用的是旧版本的 Drools(6.2.0 Final)。当我将其更改为 7.4.1 时。代码有效