规则文件
   
| 名称 | 描述 | 备注 | 
|---|---|---|
| package | 包,命名空间 | 包内的规则名称必须唯一 | 
| import | 引入fact | 可以是declare定义的类,也可以是java中定义的类 | 
| global | 定义一个全局变量 | 使用kieSession.setGlobal(key,value)来设置其值 | 
| rule | 规则明 | 同一个package下rule是唯一的 | 
| salience | 规则优先级 | 可以是负数,0,正数;默认为0,数字越大优先级越高 | 
| when | 规则中的条件部分 | 如果有多个规则都匹配,则所有规则都执行 | 
| then end | 规则中执行的内容 | 可以直接使用java中定义的静态方法,最后一定接end | 
//包: 每一个规则开始包的名称。包充当规则命名空间。包内的规则名称必须是唯一的。在规则中包类似于Java包
package com.kittlen.cloud.drools.rules
//Import语句: 要应用规则的任何fact,这些fact都需要导入
import com.kittlen.cloud.drools.People
import org.slf4j.LoggerFactory
import org.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
| 名称 | 描述 | 备注 | 
|---|---|---|
| duration | 规则将指定的时间之后在另外一个线程里触发 | 经测试在springboot中使用,第一次调用该规则时不执行,会在下一次调用kieSession.fireAllRules()的时候执行 | 
| date-expires | 当系统时间<=date-expires后才会触发,即设置规则过期时间 | 默认时间格式为dd-MMM-yyyy,可以通过设置系统时间格式System.setProperty(“drools.dateformat”, “yyyy-MM-dd”)来进行修改格式 | 
| date-effective | 当系统时间>=date-effective后才会触发,即设置规则生效时间 | 默认时间格式为dd-MMM-yyyy,可以通过设置系统时间格式System.setProperty(“drools.dateformat”, “yyyy-MM-dd”)来进行修改格式 | 
package com.kittlen.cloud.drools.rules
import com.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
| 名称 | 描述 | 备注 | 
|---|---|---|
| activation-group | 分组 | 同一个分组中,如果多个匹配,则仅匹配一个,默认执行按顺序排列的第一个,如果配置了salience(优先级),则执行salience最大的那个 | 
package com.kittlen.cloud.drools.rules
import com.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
    
    
    使用
   
package com.kittlen.cloud.controller;
import com.kittlen.cloud.drools.AllTax;
import com.kittlen.cloud.drools.Tax;
import entities.Result;
import org.kie.api.runtime.KieSession;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
/**
 * @author kittlen
 * @version 1.0
 * @date 2021/8/31 0031
 */
@RestController
@RequestMapping("/tax")
public class SelfTaxController {
    @Resource
    KieSession kieSession;
    @RequestMapping("/calculate")
    public Result calculate(double income) {
        Tax tax = new Tax();
        tax.setIncome(income);
        kieSession.insert(tax);
        kieSession.fireAllRules();
        return Result.ok(tax);
    }
}
    
    
    补充
   
    
    
    declare
   
    
    
    KieSession和KieBase
   
 
版权声明:本文为weixin_44728369原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
