//包: 每一个规则开始包的名称。包充当规则命名空间。包内的规则名称必须是唯一的。在规则中包类似于Java包packagecom.kittlen.cloud.drools.rules//Import语句: 要应用规则的任何fact,这些fact都需要导入importcom.kittlen.cloud.drools.Peopleimportorg.slf4j.LoggerFactoryimportorg.slf4j.Logger//定义一个全局变量,代码中使用kieSession.setGlobal(key,value)来设置其值;
global String a;//rule, when, then, 以及 end. 在上述的例子中, rule 规则名 字符串//when 部分是两个规则中的条件,then 部分是后果.在规则的术语, when 部分也被称为LHS(左手侧)以及 then 部分作为规则的RHS(右手边)//如果有多个规则都匹配,则所有规则都执行//salience 的值越大,越优先执行//rule "hello"// then// Logger log=LoggerFactory.getLogger("test");// log.info("hello,drools");// end
rule "helloMan"
salience 11
when
$p:People(sex =="1")//使用 == 不要用equals 存在null情况
then
System.out.println("你好"+$p.getName()+"先生"+);
end
rule "helloWoman"
salience 12
when
$p:People(sex =="2")//当kieSession.insert(obj);传入的对象时People并且perple的sex属性=="2"时执行该规则
then
System.out.println("你好"+$p.getName()+"女士");
end
packagecom.kittlen.cloud.drools.rulesimportcom.kittlen.cloud.drools.Tax
rule "-1"
salience 0
when $t:Tax()
then
System.out.println($t.getIncome());
end
rule "0"//duration 设置该属性,规则将指定的时间之后在另外一个线程里触发//经测试在springboot中使用,第一次调用该规则时不执行,会在下一次调用kieSession.fireAllRules()的时候执行// duration 100
salience 1//salience 的值越大,越优先执行// no-loop 设置一次fireAllRules当前规则只执行一次
when $t:Tax(income <=3500)
then
System.out.println("0");
$t.setNeedPayTax(100);
end
rule "1"//当系统时间<=date-expires后才会触发,即设置规则过期时间// date-expires "2021-08-31" //Wrong date-expires value: Invalid date input format: [2021-08-31] it should follow: [dd-MMM-yyyy] : [Rule name='1']
date-expires "2021-08-31"//设置系统时间格式System.setProperty("drools.dateformat", "yyyy-MM-dd");可以这样使用//当系统时间>=date-effective后才会触发,即设置规则生效时间
date-effective "2021-08-01"
salience 2// no-loop
when $t:Tax(income <=3500)
then
System.out.println("1");
$t.setNeedPayTax(100);
end
rule "2"
salience 2
no-loop
when $t:Tax(income <=5000&& income >3500)
then
System.out.println("2");
$t.setNeedPayTax(($t.getIncome()-3500)*0.3);
end
packagecom.kittlen.cloud.drools.rulesimportcom.kittlen.cloud.drools.Group
rule "byGroup"
activation-group "group1"//分组 同一个分组中,如果多个匹配,则仅匹配一个,默认按顺序排列的第一个,如果配置了salience,则按照salience的第一个
when
$g:Group()
then
System.out.println("by1"+$g.getName());
end
rule "byGroup2"
salience 1
activation-group "group1"
when
$g:Group()
then
System.out.println("by2"+$g.getName());
end