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