作用:防止规则通过update
之类的函数修改了Fact
对象时,可能使规则再次被激活,从而导致死循环。
什么时候用?
当被修改的事实对象与规则LHS
部分的约束条件为包含关系时。例如
rule No-loop1
no-loop true
when
$p:Person(name=="张三", age==30);
then
$p.setAge(50);
update($p);
System.out.println("设置no-loop时的效果");
end
否则,设置了no-loop为true也会出现死循环,例如
rule No-loop2
no-loop true
when
$p:Person(name=="张三");
then
$p.setAge(50);
update($p);
System.out.println("设置no-loop时的效果");
end
作用:no-loop的升级版,一个更强大的解决死循环的属性。当规则提设置该属性为True
时,则当前只会被触发一次。无论如何更新规则事实对象,当前规则也只能被触发一次。
例如
rule lock-on-active
lock-on-active true
when
$p:Person(name=="张三");
then
$p.setAge(50);
update($p);
System.out.println("设置no-loop时的效果");
end
作用:如果不设置salience
属性,规则体的执行顺序为由上到下,否者,salience
值越大,执行顺序越高。
例如:
rule "salience1"
salience 10
when
//这里如果为空,则表示eval(true)
then
System.out.println("hello word");
end
rule "salience2"
salience 5
when
//这里如果为空,则表示eval(true)
then
System.out.println("hello word");
end
作用:指规则是否可以被执行,若规则体设置为 enabled false
,即将规则体视为永不激活。
例如:
rule "enabled1"
enabled true
when
//这里如果为空,则表示eval(true)
then
System.out.println("hello word");
end
rule "enabled2"
enabled false
when
//这里如果为空,则表示eval(true)
then
System.out.println("hello word");
end
作用:用来定义规则中要使用的语言类型,支持Mvel
和Java
两种类型的语言,默认情况下由包指定。
作用:只有当前系统时间大于等于设置的时间或者日期,规则才会被激活。
例如:
rule "date-effective"
date-effective "07-August-2018"
when
//这里如果为空,则表示eval(true)
then
System.out.println("hello word");
end
作用:与date-effective
相反,只有当前系统时间小于设置的时间或者日期,规则才会被激活。
作用:表示定时器,如果当前规则LHS
部分为True
,规则继续执行。如果该属性已经被弃用,那么通过新的属性timer
来控制。
作用:activation-group
指激活分组,通过字符串定义分组名称,具有相同组名名称的规则体有且只有一个规则被激活,其他规则体的LHS
部分仍然为true
也不会被执行。该属性受salience
属性的影响,如当前规则文件中的其他规则未设计该属性,则视为规则处于被激活状态,并不受该属性的影响。
例如:
rule "activation-group1"
salience 10
activation-group "testAgs"
when
//这里如果为空,则表示eval(true)
then
System.out.println("hello word");
end
rule "activation-group2"
salience 10
activation-group "testAgs"
when
//这里如果为空,则表示eval(true)
then
System.out.println("hello word");
end
rule "activation-group3"
activation-group "testAgs"
when
//这里如果为空,则表示eval(true)
then
System.out.println("hello word");
end
rule "activation-group4"
activation-group "testAgs"
when
//这里如果为空,则表示eval(true)
then
System.out.println("hello word");
end
会优先执行 activation-group2
,其次是activation-group3
,但不会执行activation-group1
和 activation-group4
作用:agenda-group
是议程分组,属于另一种可控的规则执行方式,是指用户可以通过配置agenda-group
的参数来控制规则的执行,而且只有获取焦点的规则才会被激活。
例如:ks.getAgenda().getAgendaGroup("ag1").setFocus();
rule "agenda-group1"
agenda-group "ag1"
when
//这里如果为空,则表示eval(true)
then
System.out.println("hello word");
end
rule "agenda-group2"
agenda-group "ag2"
when
//这里如果为空,则表示eval(true)
then
System.out.println("hello word");
end
如果有两个规则体的agenda-group
属性相同,都可以被激活。
agenda-group | activation-group | 结果说明 | |
是否同一Focus |
是 | 是 | 只会执行其中一个规则。执行顺序根据优化级控制,默认为从上到下 |
否 | 是 | 只有获取焦点的规则才会被激活 | |
是 | 否 | 会执行多个规则,但只有获取Focus的规则才会被激活 | |
否 | 否 | 只有获取焦点的规则才会被激活 |
作用:auto-focus
属性为自动获取焦点,即当前规则是否被激活,如果一个规则被执行,那么认为auto-focus
为true
;如果单独设置,一般结合agenda-group
。当一个agenda-group
没有获取焦点时,可以用auto-focus
来控制。
作用:用来控制规则的执行时间。
用法:
①timer(int:30s) timer(int:30s 5m)
②timer(cron:0/1****?)
③timer(int:30s 10s; start=3-JAN-2018,end=5-JAN-2018)
示例:
rule "timer1"
timer(int:3s) //每三秒执行一次
when
//这里如果为空,则表示eval(true)
then
System.out.println("hello word");
end
rule "timer2"
timer(cron:0/1****?) //每一秒执行一次
when
//这里如果为空,则表示eval(true)
then
System.out.println("hello word");
end