Drools规则属性

属性No-loop

作用:防止规则通过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

属性lock-on-active

作用: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属性,规则体的执行顺序为由上到下,否者,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

作用:指规则是否可以被执行,若规则体设置为 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

属性dialect

作用:用来定义规则中要使用的语言类型,支持MvelJava两种类型的语言,默认情况下由包指定。

属性date-effective

作用:只有当前系统时间大于等于设置的时间或者日期,规则才会被激活。
例如:

rule "date-effective"
	date-effective "07-August-2018"
  when 
    //这里如果为空,则表示eval(true)
  then
    System.out.println("hello word");
end

属性date-expires

作用:与date-effective相反,只有当前系统时间小于设置的时间或者日期,规则才会被激活。

属性duration

作用:表示定时器,如果当前规则LHS部分为True,规则继续执行。如果该属性已经被弃用,那么通过新的属性timer来控制。

属性activation-group

作用: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-group1activation-group4

属性agenda-group

作用: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 属性为自动获取焦点,即当前规则是否被激活,如果一个规则被执行,那么认为auto-focustrue;如果单独设置,一般结合agenda-group。当一个agenda-group没有获取焦点时,可以用auto-focus来控制。

属性timer

作用:用来控制规则的执行时间。
用法:
①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

你可能感兴趣的:(Drools,Drools规则属性)