您好,登錄后才能下訂單哦!
這篇文章給大家分享的是有關(guān)Drools Fusion怎么用的內(nèi)容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。
從 Drools 統(tǒng)一行為建模平臺的視野看,Drools Fusion 是負(fù)責(zé)啟用事件處理行 為的一個模塊。
定義
支持復(fù)雜事件處理,是比簡單的理解事件是什么要更多得多,cep場景具有幾個共同而明顯的特點:
通常需要處理巨量的事件,但是只有少部分事件是真正關(guān)心的。
事件通常是不變的,因為它們是狀態(tài)改變的一條記錄。
通常有關(guān)事件的規(guī)則和查詢必須是運行在被動模式(reactive modes),即,對事件模式(patterns)的檢測作出反應(yīng)。
通常在相關(guān)的事件之間有強烈的時間關(guān)系。
個別事件通常是不重要的。系統(tǒng)關(guān)心相關(guān)事件的模式(patterns)和它們的關(guān)系
通常,要求系統(tǒng)執(zhí)行組合和聚合的事件。
用fusion,要把插入drools的數(shù)據(jù)聲明為事件。
drools處理數(shù)據(jù)有兩種方式,云模式和流模式,默認(rèn)是云模式,用fusion,需要設(shè)置為流模式。流模式,插入的數(shù)據(jù)叫事件,有時間順序,云模式?jīng)]有,
流(stream)支持
大部分 CEP 用例必須處理事件流(stream)。
流的特性:
在流中的事件通過時間戳被排序。
事件的數(shù)量(volumes)總是很高的。
原子事件自己是很少有用的。通常根據(jù)多個事件之間的相關(guān)性或流或其他來源提取含義。
流可以是相似的,即包含單一類型的事件;或者是異類的,即包含多種類型的事件。
聲明流模式
在kmodule.xml 中添加配置 eventProcessingMode=“stream” 為流模式
<kbase name="fusionAge" eventProcessingMode="stream" packages="com.us.fusion"> <ksession name="fusionAgeKS" type="stateful"/> </kbase>
事件聲明
用fusion,要把插入drools的數(shù)據(jù)聲明為事件,聲明事件使用@role標(biāo)簽
@role
把@role元數(shù)據(jù)標(biāo)簽指派給該事實類行
例如:
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
每一個事件都要有一個關(guān)聯(lián)的時間戳指派給它。默認(rèn)時,一個給定事件的時間戳是在事件被插入到工作內(nèi)存時,從 Session Clock 讀取,并且分配給該事件。有些時候,事件用時間戳作為它自己的一個屬性。在這情況下,用戶可以用@timestamp 標(biāo)記用戶屬性為時間戳
例如:用Person的 createTime 屬性為時間戳
declare Person @role(event) @timestamp( createTime ) end
@expires
重要:這個標(biāo)簽只有引擎運行在流(STREAM)模式之下才會被考慮.
該標(biāo)簽顯示定義 一個事件在什么時候應(yīng)該到期,事件到期,事件可能不再匹配和激活任何規(guī)則時。
使用如下
@expires( 1h45m )
在person 例子中假設(shè)過期時間為20S
declare Person @role(event) @timestamp( createTime ) @expires(20s) end
滑動時間窗口
滑動時間窗口允許用戶編寫規(guī)則,其將僅匹配在最近的 X 時間單元內(nèi)發(fā)生的事件
rule "boy" when $p : Person(age < 25) over window:time(3s) then $p.setDesc("少年"); retract($p); end
例如:只匹配最近3秒內(nèi),年齡小于25的人
調(diào)用代碼如下:
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秒內(nèi)只有李大嘴一條記錄所以
結(jié)果如下:
streamName:boy name:李大嘴 age:16 desc:少年
總執(zhí)行了1條規(guī)則------------------------------
范例2 10S 內(nèi)的平均年齡
滑動長度窗口
和滑動時間窗口很類似,其將僅匹配最近幾次發(fā)生的事件,用法如圖,只匹配最近1次發(fā)生的事件。
rule "old" when $p : Person(age > 49) over window:length(2) then $p.setDesc("老年"); retract($p); end
例如年領(lǐng)大于49歲的最近兩條記錄
調(diào)用代碼:
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í)行結(jié)果
streamName:boy name:李大嘴 age:56 desc:老年
streamName:boy name:佟湘玉 age:57 desc:老年
總執(zhí)行了2條規(guī)則------------------------------
本文所有測試?yán)拥膒om 依賴
<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>
本文所有測試?yán)拥?code>kmodule.xml 配置
<kbase name="fusionAge" eventProcessingMode="stream" packages="com.us.fusion"> <ksession name="fusionAgeKS" type="stateful"/> </kbase>
其他關(guān)鍵字: After, Before, During, Meet 等關(guān)鍵字 都是用于比較兩個事件的發(fā)生時間順序,用法待以后再敘
感謝各位的閱讀!關(guān)于“Drools Fusion怎么用”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,讓大家可以學(xué)到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。