從 Drools 統(tǒng)一行為建模平臺(tái)的視野看,Drools Fusion 是負(fù)責(zé)啟用事件處理行 為的一個(gè)模塊。
定義
支持復(fù)雜事件處理,是比簡(jiǎn)單的理解事件是什么要更多得多,cep場(chǎng)景具有幾個(gè)共同而明顯的特點(diǎn):
- 通常需要處理巨量的事件,但是只有少部分事件是真正關(guān)心的。
- 事件通常是不變的,因?yàn)樗鼈兪菭顟B(tài)改變的一條記錄。
- 通常有關(guān)事件的規(guī)則和查詢必須是運(yùn)行在被動(dòng)模式(reactive modes),即,對(duì)事件模式(patterns)的檢測(cè)作出反應(yīng)。
- 通常在相關(guān)的事件之間有強(qiáng)烈的時(shí)間關(guān)系。
- 個(gè)別事件通常是不重要的。系統(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ù)叫事件,有時(shí)間順序,云模式?jīng)]有,
流(stream)支持
大部分 CEP 用例必須處理事件流(stream)。
流的特性:
- 在流中的事件通過(guò)時(shí)間戳被排序。
- 事件的數(shù)量(volumes)總是很高的。
- 原子事件自己是很少有用的。通常根據(jù)多個(gè)事件之間的相關(guān)性或流或其他來(lái)源提取含義。
- 流可以是相似的,即包含單一類型的事件;或者是異類的,即包含多種類型的事件。
聲明流模式
在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)簽指派給該事實(shí)類行
例如:
Person 為java bean 也就是一個(gè)事實(shí)類型
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
每一個(gè)事件都要有一個(gè)關(guān)聯(lián)的時(shí)間戳指派給它。默認(rèn)時(shí),一個(gè)給定事件的時(shí)間戳是在事件被插入到工作內(nèi)存時(shí),從 Session Clock 讀取,并且分配給該事件。有些時(shí)候,事件用時(shí)間戳作為它自己的一個(gè)屬性。在這情況下,用戶可以用@timestamp 標(biāo)記用戶屬性為時(shí)間戳
例如:用Person的 createTime 屬性為時(shí)間戳
declare Person @role(event) @timestamp( createTime ) end
@expires
重要:這個(gè)標(biāo)簽只有引擎運(yùn)行在流(STREAM)模式之下才會(huì)被考慮.
該標(biāo)簽顯示定義 一個(gè)事件在什么時(shí)候應(yīng)該到期,事件到期,事件可能不再匹配和激活任何規(guī)則時(shí)。
使用如下
@expires( 1h35m )
在person 例子中假設(shè)過(guò)期時(shí)間為20S
declare Person @role(event) @timestamp( createTime ) @expires(20s) end
滑動(dòng)時(shí)間窗口
滑動(dòng)時(shí)間窗口允許用戶編寫(xiě)規(guī)則,其將僅匹配在最近的 X 時(shí)間單元內(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)的平均年齡
滑動(dòng)長(zhǎng)度窗口
和滑動(dòng)時(shí)間窗口很類似,其將僅匹配最近幾次發(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ī)則------------------------------
本文所有測(cè)試?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>
本文所有測(cè)試?yán)拥?code style="margin: 3px auto 0px; padding: 2px 4px; outline: none; font-style: inherit; font-weight: inherit; background: rgb(249, 242, 244); width: 640px; line-height: 1.5; clear: both; font-size: 12px; border: 1px solid rgb(204, 204, 204); color: rgb(199, 37, 78); border-radius: 0px; font-family: Menlo, Monaco, Consolas, "Courier New", monospace;">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)鍵字 都是用于比較兩個(gè)事件的發(fā)生時(shí)間順序,用法待以后再敘
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對(duì)服務(wù)器之家的支持。