Drools Fusion(CEP)定義及使用方法講解
從 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)。
流的特性:
- 在流中的事件通過時(shí)間戳被排序。
- 事件的數(shù)量(volumes)總是很高的。
- 原子事件自己是很少有用的。通常根據(jù)多個(gè)事件之間的相關(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)簽指派給該事實(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è)過期時(shí)間為20S
declare Person @role(event) @timestamp( createTime ) @expires(20s) end
滑動(dòng)時(shí)間窗口
滑動(dòng)時(shí)間窗口允許用戶編寫規(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)長度窗口
和滑動(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>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é)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對(duì)腳本之家的支持。如果你想了解更多相關(guān)內(nèi)容請(qǐng)查看下面相關(guān)鏈接
- Spring Boot+Drools規(guī)則引擎整合詳解
- SpringBoot整合Elasticsearch7.2.0的實(shí)現(xiàn)方法
- SpringBoot2.0整合jackson配置日期格式化和反序列化的實(shí)現(xiàn)
- SpringBoot2.0整合SpringCloud Finchley @hystrixcommand注解找不到解決方案
- springboot2.0整合dubbo的示例代碼
- Spring Boot整合Swagger2的完整步驟詳解
- springboot整合H2內(nèi)存數(shù)據(jù)庫實(shí)現(xiàn)單元測(cè)試與數(shù)據(jù)庫無關(guān)性
- SpringBoot2整合Drools規(guī)則引擎及案例詳解
相關(guān)文章
10張圖總結(jié)出并發(fā)編程最佳學(xué)習(xí)路線
這篇文章主要介紹了并發(fā)編程的最佳學(xué)習(xí)路線,文中通過圖片介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-08-08
Java自動(dòng)化工具Ant的基礎(chǔ)使用教程
這篇文章主要介紹了Java自動(dòng)化工具Ant的基礎(chǔ)使用教程,例子在Windows系統(tǒng)下操作演示,講解了Ant基本的文件操作和屬性,需要的朋友可以參考下2016-02-02
SSH框架網(wǎng)上商城項(xiàng)目第13戰(zhàn)之Struts2文件上傳功能
這篇文章主要為大家詳細(xì)介紹了SSH框架網(wǎng)上商城項(xiàng)目第13戰(zhàn)之Struts2文件上傳功能的相關(guān)資料,感興趣的小伙伴們可以參考一下2016-06-06
Java中獲取時(shí)間戳的三種方式對(duì)比實(shí)現(xiàn)
這篇文章主要介紹了Java中獲取時(shí)間戳的三種方式對(duì)比實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01
SpringBoot解決jar包沖突的問題,簡(jiǎn)單有效
這篇文章主要介紹了SpringBoot解決jar包沖突的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-09-09
Intellij IDEA連接Navicat數(shù)據(jù)庫的方法
這篇文章主要介紹了Intellij IDEA連接Navicat數(shù)據(jù)庫的方法,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借價(jià)值,需要的朋友可以參考下2021-03-03

