Drools常用属性

1、no-loop循环控制

rule规则默认可以重复循环执行,如果只想让规则执行1次,则将no-loop属性设置为true

rule "test-rule"
no-loop true
    when
    then
end

2、分组属性

2.1、agenda-group

agenda-group属性用来对规则分组,如下例中两条规则都属于testrule组,当在代码中调用该组时,会匹配组中所有的规则

rule "test-rule01"
agenda-group "testrule"
    when
    then
end

rule "test-rule02"
agenda-group "testrule"
    when
    then
end

调用方式如下,在一个会话中如果多次调用,仍然只会输出一次匹配结果

kieSession.getAgenda().getAgendaGroup("testrule").setFocus();
2.2、ruleflow-group

ruleflow-groupagenda-group分组效果一样

2.3、activation-group

activation-group属性组中的规则只要有一个被执行,其它的则不再执行,具有排他性,默认值为false
下面示例中只要任意一个匹配成功,其它的则不会再执行

rule "test-rule01"
activation-group "testrule"
    when
    then
end

rule "test-rule02"
activation-group "testrule"
    when
    then
end

rule "test-rule03"
activation-group "testrule"
    when
    then
end

3、lock-on-active

lock-on-active属性用于指定某条规则,值设置为true时免于被其它规则输出的fact对象干扰。比如其它规则执行update、modify等操作后,FACT对象中的属性值被更新,而该值正好触发其它规则的判断条件,这样就会出现规则间的干扰
如下例中,规则1执行完后update($b),将income设置为10001,刚好满足规则2触发条件,如果不设置lock-on-active true,那么规则2也会触发,这里需要根据实际需求判断是否添加该属性。

rule "lock-on-active-1"
when
    $b : Borrower( income < 10000)
then
    System.out.println("lock-on-active-1触发,income=" + $b.getIncome());
    $b.setIncome(new BigDecimal(10001));
    update($b)
 end

rule "lock-on-active-2"
lock-on-active true
when
    $b : Borrower(income > 10000)
then
    System.out.println("lock-on-active-2触发,income=" + $b.getIncome());
end

4、salience执行顺序控制

通过指定salience属性值来控制规则执行顺序,其默认值为0,数值越大优先级越高,也可以为负数
下面示例执行顺序为test-rule1test-rule2test-rule3

rule "test-rule1"
salience 5
    when
    then
end

rule "test-rule2"
salience 0
    when
    then
end

rule "test-rule3"
salience -2
    when
    then
end

5、时间控制

5.1、date-effective生效时间

date-effective属性表示生效时间,可以与系统时间比对,根据条件在指定时间执行,默认格式为dd-MMM-yyyy,下面示例表示只要到了2020年7月15日才会执行该规则

rule "test-rule1"
date-effective "15-Jul-2020"
    when
    then
end

自定义时间为yyyy-MM-dd HH:mm格式,在创建KieSession会话前加一行设置代码即可

System.setProperty("drools.dateformat","yyyy-MM-dd HH:mm");
KieSession kieSession = kieContainer.newKieSession("date-effective-rules");
...

表示只要到了2020年7月15日上午9点30分才会执行该规则

rule "test-rule1"
date-effective "2020-7-15 9:30"
    when
    then
end
5.2、date-expires失效时间

date-expires属性表示失效时间,用法与date-effective一样

5.3、定时器

定时器是通过timer()函数进行设置,有两种格式可以使用
1)格式一:timer ( int: <initial delay> <repeat interval>? ),第一个参数表示初始执行时间,第二个参数表示执行间隔时间,比如timer ( int: 1m 10m )表示运行后1分钟开始执行第一次,往后每个10分钟执行一次
2)格式二:timer ( cron: <cron expression> )
cron表达式详细用法请参考Cron表达式常见用法
下面示例表示为每天凌晨 2 点执行

rule "test-rule1"
timer (cron:0 0 2 * * * )
    when
    then
end

6、规则启用停用

enabled属性设置规则是否可以,默认为true可用,设置为false则不可用

rule "test-rule1"
enabled true
    when
    then
end

rule "test-rule2"
enabled false
    when
    then
end

7、dialect语言设置

dialect默认为java,也可以指定为mvel

rule "test-rule1"
dialect "java"
    when
    then
end

rule "test-rule2"
dialect "mvel"
    when
    then
end

8、规则继承

extends关键字可以实现规则间触发条件的继承

rule "test-rule1"
    when
    then
end

rule "test-rule2" extends "test-rule1"
    when
    then
end

9、function函数定义

在rule规则中,可以使用function定义封装函数,然后在then后面调用,支持传参和返回值

rule "test-rule1"
    when
    then
        //函数调用
    	printIncome(new BigDecimal(1000));
        System.out.println(getName());
end

// 无返回值,传入参数
function void printIncome(BigDecimal income){
    System.out.println(income);
}

// 有返回值
function String getName(){
    String name = "zhangshan";
    return name;
}

10、Global 全局变量

global定义的全局变量对象,可以在规则文件中使用
先创建TestService类,并定义一个testGlobal()方法

public class TestService {

    public static void testGlobal() {
        System.out.println("testGlobal");
    }
}

静态方法注入:
直接在规则中使用globalTestService设置为全局变量,然后就可以进行调用了

global com.example.droolsdemo.TestService testService

rule "test-rule1"
    when
    then
        //函数调用
    	 testService.testGlobal();
end

实例化bean注入:

	TestService testService = new TestService();
	kieSession.setGlobal("testService" , testService);
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值