JAVA規則引擎 -- Drools介紹(轉)


1.salience
功能:設置規制執行的優先級
值:數字(數字越大執行優先級越高)
示例:

rule "rule1" 
salience 1 
when 
  eval(true) --eval(true):是一個默認的api,true 無條件執行,類似於 while(true)
then 
  System.out.println("rule1");
end 

2.no-loop

功能:控制已經執行的規則條件再次滿足是否再次執行
值:true/false
示例:    

rule "rule1" 
no-loop true 
when 
  $customer:Customer(name=="張三") 
then 
  update($customer); 
  System.out.println("customer name:"+$customer.getName()); 
End

 

3.activation-group
功能:若干個規則划分成一個組
值:分組名稱

示例: 

rule "rule2"
 activation-group "test"
 salience 10 
 when
    eval(true)
  then
    System.out.println("rule2 execute");
 end
 
 rule "rule1"
 activation-group "test"
 salience 9
 when
   eval(true)
 then
   System.out.println("rule1 execute");
end 

note:

如果同一組規則,誰的salience高就執行誰,沒有則按順序執行最后同組最后那個規則


4.declare
作用:
Drools除了可以接受用戶在外部向 WorkingMemory當中插入現成的
Fact對象,還允許用戶在規則文件當中定義一個新的 Fact對象。

語法:
declare Address
熟悉名 : 類型
end

示例:

package com.demo.fact

declare Address
city : String
addressName : String
end

rule "rule1"
salience 2
when
eval(true);
then 
Address add = new Address();
add.setCity("中國上海");
add.setAddressName("中國上海松江區");
insert(add);
end

5.date-expires

功能:當系統時間<=date-expires后才會觸發
值:日期默認格式為dd-MMM-yyyy
可以設置其它時間格式如yyyy-MM-dd,需在代碼設置系統時間格式System.setProperty("drools.dateformat", "yyyy-MM-dd");

示例:

rule "rule1" 
date-expires "2009-09-27" 
when 
  eval(true); 
then 
  System.out.println("rule1 is execution!"); 
end

6.lock-on-active true

通過這個標簽,可以控制當前的規則只會被執行一次,因為一個規則的重復執行不一定是本身觸發的,也可能是其他規則觸發的,所以這個是no-loop的加強版。。

7、date-effective

設置規則的生效時間

8、duration

規則定時,duration 3000 3秒后執行規則

 

9、Drools提供了十二中類型比較操作符:

> >= < <= == != contains / not contains / memberOf / not memberOf /matches/ not matches

contains:對比是否包含操作,操作的被包含目標可以是一個復雜對象也可以是一個簡單的值。

not contains:與contains相反。

memberOf:判斷某個Fact屬性值是否在某個集合中,與contains不同的是他被比較的對象是一個集合,而contains被比較的對象是單個值或者對象。

not memberOf:正好相反。

matches:正則表達式匹配,與java不同的是,不用考慮'/'的轉義問題

not matches:正好相反。

10、結果部分也有drools提供的方法:

insert:往當前workingMemory中插入一個新的Fact對象,會觸發規則的再次執行,除非使用no-loop限定;

update:更新

modify:修改,與update語法不同,結果都是更新操作

retract:刪除


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM