2016-03-12 36 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