Drools Fusion(CEP)定義及使用方法講解
從 Drools 統(tǒng)一行為建模平臺的視野看,Drools Fusion 是負責啟用事件處理行 為的一個模塊。
定義
支持復雜事件處理,是比簡單的理解事件是什么要更多得多,cep場景具有幾個共同而明顯的特點:
- 通常需要處理巨量的事件,但是只有少部分事件是真正關心的。
- 事件通常是不變的,因為它們是狀態(tài)改變的一條記錄。
- 通常有關事件的規(guī)則和查詢必須是運行在被動模式(reactive modes),即,對事件模式(patterns)的檢測作出反應。
- 通常在相關的事件之間有強烈的時間關系。
- 個別事件通常是不重要的。系統(tǒng)關心相關事件的模式(patterns)和它們的關系
- 通常,要求系統(tǒng)執(zhí)行組合和聚合的事件。
用fusion,要把插入drools的數(shù)據(jù)聲明為事件。
drools處理數(shù)據(jù)有兩種方式,云模式和流模式,默認是云模式,用fusion,需要設置為流模式。流模式,插入的數(shù)據(jù)叫事件,有時間順序,云模式?jīng)]有,
流(stream)支持
大部分 CEP 用例必須處理事件流(stream)。
流的特性:
- 在流中的事件通過時間戳被排序。
- 事件的數(shù)量(volumes)總是很高的。
- 原子事件自己是很少有用的。通常根據(jù)多個事件之間的相關性或流或其他來源提取含義。
- 流可以是相似的,即包含單一類型的事件;或者是異類的,即包含多種類型的事件。
聲明流模式
在kmodule.xml 中添加配置 eventProcessingMode=“stream” 為流模式
<kbase name="fusionAge" eventProcessingMode="stream" packages="com.us.fusion"> <ksession name="fusionAgeKS" type="stateful"/> </kbase>
事件聲明
用fusion,要把插入drools的數(shù)據(jù)聲明為事件,聲明事件使用@role標簽
@role
把@role元數(shù)據(jù)標簽指派給該事實類行
例如:
Person 為java bean 也就是一個事實類型
declare Person @role(event) end
Person 的屬性如下:
public class Person { private String name; private Integer age; private String like; private String sex; private String desc; private String address; private Date createTime; // getter setter 省略
@timestamp
每一個事件都要有一個關聯(lián)的時間戳指派給它。默認時,一個給定事件的時間戳是在事件被插入到工作內存時,從 Session Clock 讀取,并且分配給該事件。有些時候,事件用時間戳作為它自己的一個屬性。在這情況下,用戶可以用@timestamp 標記用戶屬性為時間戳
例如:用Person的 createTime 屬性為時間戳
declare Person @role(event) @timestamp( createTime ) end
@expires
重要:這個標簽只有引擎運行在流(STREAM)模式之下才會被考慮.
該標簽顯示定義 一個事件在什么時候應該到期,事件到期,事件可能不再匹配和激活任何規(guī)則時。
使用如下
@expires( 1h35m )
在person 例子中假設過期時間為20S
declare Person @role(event) @timestamp( createTime ) @expires(20s) end
滑動時間窗口
滑動時間窗口允許用戶編寫規(guī)則,其將僅匹配在最近的 X 時間單元內發(fā)生的事件
rule "boy" when $p : Person(age < 25) over window:time(3s) then $p.setDesc("少年"); retract($p); end
例如:只匹配最近3秒內,年齡小于25的人
調用代碼如下:
package com.us.fusion; import com.us.model.Person; import org.kie.api.KieServices; import org.kie.api.runtime.KieContainer; import org.kie.api.runtime.KieSession; import java.util.Date; /** * Created by yangyibo on 17/1/3. * @author yangyibo */ public class Application { private static KieSession getSession() { KieServices ks = KieServices.Factory.get(); KieContainer kc = ks.getKieClasspathContainer(); return kc.newKieSession("fusionAgeKS"); } public static void run() { KieSession ks = getSession(); Person p1 = new Person("白展堂", 2,new Date()); Person p2 = new Person("佟湘玉", 7,new Date()); try { Thread.sleep(4000); } catch (InterruptedException e) { System.out.println(e); } Person p3 = new Person("李大嘴", 16,new Date()); ks.insert(p1); ks.insert(p2); ks.insert(p3); int count = ks.fireAllRules(); System.out.println("總執(zhí)行了" + count + "條規(guī)則------------------------------"); // ks.dispose(); } public static void main(String[] args) { run(); } }
規(guī)則代碼如下:
package com.us.fusion7 import com.us.model.Person function void printName(String streamName,String name,int age,String desc) { System.out.println("streamName:"+streamName+" name:"+name+" age:"+age+" desc:"+ desc); } declare Person @role(event) @timestamp( createTime ) @expires(20s) end rule "boy" when $p : Person(age > 0) over window:time(3s) then $p.setDesc("少年"); retract($p); printName("boy",$p.getName(),$p.getAge(),$p.getDesc()); end
由于Thread.sleep(4000);所以最近3秒內只有李大嘴一條記錄所以
結果如下:
streamName:boy name:李大嘴 age:16 desc:少年
總執(zhí)行了1條規(guī)則------------------------------
范例2 10S 內的平均年齡
滑動長度窗口
和滑動時間窗口很類似,其將僅匹配最近幾次發(fā)生的事件,用法如圖,只匹配最近1次發(fā)生的事件。
rule "old" when $p : Person(age > 49) over window:length(2) then $p.setDesc("老年"); retract($p); end
例如年領大于49歲的最近兩條記錄
調用代碼:
public class Application { private static KieSession getSession() { KieServices ks = KieServices.Factory.get(); KieContainer kc = ks.getKieClasspathContainer(); return kc.newKieSession("fusionAgeKS"); } public static void run() { KieSession ks = getSession(); Person p1 = new Person("白展堂", 52,new Date()); Person p2 = new Person("佟湘玉", 57,new Date()); try { Thread.sleep(4000); } catch (InterruptedException e) { System.out.println(e); } Person p3 = new Person("李大嘴", 56,new Date()); ks.insert(p1); ks.insert(p2); ks.insert(p3); int count = ks.fireAllRules(); System.out.println("總執(zhí)行了" + count + "條規(guī)則------------------------------"); ks.dispose(); } public static void main(String[] args) { run(); } }
規(guī)則代碼
package com.us.fusion7 import com.us.model.Person function void printName(String streamName,String name,int age,String desc) { System.out.println("streamName:"+streamName+" name:"+name+" age:"+age+" desc:"+ desc); } declare Person @role(event) @timestamp( createTime ) @expires(20s) end rule "old" when $p : Person(age > 49) over window:length(2) then $p.setDesc("老年"); retract($p); printName("boy",$p.getName(),$p.getAge(),$p.getDesc()); end
只匹配符合規(guī)則的最近的兩條記錄,所以舍棄“白展堂記錄”
執(zhí)行結果
streamName:boy name:李大嘴 age:56 desc:老年
streamName:boy name:佟湘玉 age:57 desc:老年
總執(zhí)行了2條規(guī)則------------------------------
本文所有測試例子的pom 依賴
<dependency> <groupId>org.kie</groupId> <artifactId>kie-api</artifactId> <version>6.5.0.Final</version> </dependency> <dependency> <groupId>org.drools</groupId> <artifactId>drools-core</artifactId> <version>6.5.0.Final</version> </dependency> <dependency> <groupId>org.drools</groupId> <artifactId>drools-compiler</artifactId> <version>6.5.0.Final</version> </dependency> <dependency> <groupId>org.drools</groupId> <artifactId>drools-decisiontables</artifactId> <version>6.5.0.Final</version> </dependency> <dependency> <groupId>org.drools</groupId> <artifactId>drools-templates</artifactId> <version>6.5.0.Final</version> </dependency>
本文所有測試例子的kmodule.xml
配置
<kbase name="fusionAge" eventProcessingMode="stream" packages="com.us.fusion"> <ksession name="fusionAgeKS" type="stateful"/> </kbase>
其他關鍵字: After, Before, During, Meet 等關鍵字 都是用于比較兩個事件的發(fā)生時間順序,用法待以后再敘
總結
以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,謝謝大家對腳本之家的支持。如果你想了解更多相關內容請查看下面相關鏈接
- Spring Boot+Drools規(guī)則引擎整合詳解
- SpringBoot整合Elasticsearch7.2.0的實現(xiàn)方法
- SpringBoot2.0整合jackson配置日期格式化和反序列化的實現(xiàn)
- SpringBoot2.0整合SpringCloud Finchley @hystrixcommand注解找不到解決方案
- springboot2.0整合dubbo的示例代碼
- Spring Boot整合Swagger2的完整步驟詳解
- springboot整合H2內存數(shù)據(jù)庫實現(xiàn)單元測試與數(shù)據(jù)庫無關性
- SpringBoot2整合Drools規(guī)則引擎及案例詳解
相關文章
SSH框架網(wǎng)上商城項目第13戰(zhàn)之Struts2文件上傳功能
這篇文章主要為大家詳細介紹了SSH框架網(wǎng)上商城項目第13戰(zhàn)之Struts2文件上傳功能的相關資料,感興趣的小伙伴們可以參考一下2016-06-06Intellij IDEA連接Navicat數(shù)據(jù)庫的方法
這篇文章主要介紹了Intellij IDEA連接Navicat數(shù)據(jù)庫的方法,本文通過圖文并茂的形式給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借價值,需要的朋友可以參考下2021-03-03