public class Goods {
private int discount = 100;
private int type;
}
kmodule.xml
<kbase name="noLoop-kbase" packages="com.kq.noloop" >
<ksession name="noLoopSession"/>
</kbase>
noloop.drl
package com.kq.noloop
import com.kq.drools.entity.Goods
rule "updateDistcount"
// 设置为 true
no-loop true
when
productObj:Goods(discount > 0);
then
// 符合就将Discount自增1
productObj.setDiscount(productObj.getDiscount() + 1);
System.out.println("discount="+productObj.getDiscount());
update(productObj); //这个会导致死循环 要加no-loop true
end
NoLoopTest
public class NoLoopTest {
public static void main(String[] args) {
// 构建KieServices
KieServices ks = KieServices.Factory.get();
KieContainer kieContainer = ks.getKieClasspathContainer();
// 获取kmodule.xml中配置中名称为ksession-rule的session,默认为有状态的。
KieSession kSession = kieContainer.newKieSession("noLoopSession");
Goods product = new Goods();
product.setType(1);
product.setDiscount(10);
kSession.insert(product);
int count = kSession.fireAllRules();
System.out.println("命中了" + count + "条规则!");
System.out.println("商品" +product.getType() + "的商品折扣为" + product.getDiscount() + "%。");
kSession.dispose();
}
}