2016-03-12 34 views
1

我最近開始使用Drools Fusion進行編程,我有一臺智能可穿戴設備,可將計步器和心率數據發送到我的筆記本電腦。然後我使用drools規則語言來處理這些數據。但假設我有多個智能可穿戴設備,每個唯一的MAC地址。我使用時間窗口,我的問題是如何更改我的規則文件,以便規則只針對具有相同macaddress的事件觸發,並基於此MAC地址採取適當的操作。 我現在的規則文件如下:不同用戶的Drools規則

import hellodrools.Steps 
import hellodrools.HeartRate 
import hellodrools.AppInfo 

declare AppInfo 
    @role(event) 
end 

declare Steps 
    @role(event) 
end 

declare HeartRate 
    @role(event)  
end 


rule "ACC STEPS RULE" 
when 
    accumulate(Steps($s : steps) 
       over window:time(1h) from entry-point "entrySteps"; 
     $fst: min($s), $lst: max($s); 
     $lst - $fst < 50) 
then 
    System.out.println("STEPS RULE: get moving!"); 
    System.out.println($lst + " " + $fst); 

end 

rule "HEARTRATE RULE 1" 
when 
    $heartrate : HeartRate(heartRate >= 150) from entry-point "entryHeartRate" 
then 
    System.out.println("Heartrate is to high!"); 
end 

rule "HEARTRATE RULE 2" 
when 
    $heartrate : HeartRate(heartRate <= 50 && heartRate >= 35) from entry-   point "entryHeartRate" 
then 
    System.out.println("Heartrate is to low!"); 
end 

rule "HEARTRATE RULE 3" 
when 
    $heartrate : HeartRate(heartRate < 35 && heartRate >= 25) from entry-point "entryHeartRate" 
then 
    System.out.println("Heartrate is critical low!"); 
end 

rule "HEARTRATE RULE 4" 
when 
    $max : Double() from accumulate(
     HeartRate($heartrates : heartRate) over window:time(10s) from entry-point "entryHeartRate", 
     max($heartrates))&& 
    $min : Double() from accumulate(
     HeartRate($heartrates : heartRate) over window:time(10s) from entry-point "entryHeartRate", 
     min($heartrates))&& 
    eval(($max - $min) >= 50) 
then 
    System.out.println("Heartrate to much difference in to little time!"); 
end 

我的心率事件有以下字段:

int heartRate; 
Date timeStamp; 
String macAddress; 

我的腳步事件有以下字段:

double steps; 
Date timeStamp; 
String macAddress; 

回答

1

這很簡單:您需要定義一個事實,將其稱爲WalkerString macAddress,使用規則應處理的MAC地址創建它,然後

rule "ACC STEPS RULE" 
when 
    Walker($mac: macAddress) 
    accumulate(Steps($s : steps, macAddress == $mac) 
       over window:time(1h) from entry-point "entrySteps"; 
     $fst: min($s), $lst: max($s); 
     $lst - $fst < 50) 
    then ... end 

和其他規則類似。 - 您可以通過定義一個基本規則

rule "MAC" 
when 
    Walker($mac: macAddress) 
then end 

簡化這個(少許),寫的其他規則作爲擴展:

rule "ACC STEPS RULE" extends "MAC" ... 

,所以你不需要重複Walker模式爲每個規則。

+0

謝謝你的回答。就像我說的,我剛開始用口水,我不明白它的完整性。我創建了一個帶有字段macAddress的新類Walker。然後在我的規則文件中將其聲明爲事實:'聲明Walker @role(fact)end'並更新我的規則。然後,我做了以下事情: 'Walker walker = new Walker(macAddress); entryPointSteps.insert(walker);'但是沒有發生。你能解釋一下嗎?謝謝。 – Tim

+1

如果使用'entryPointSteps'插入,則必須在規則中使用'from entrySteps'。你是否? – laune

+0

不,這的確是問題,謝謝! – Tim