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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章