简体   繁体   English

针对不同用户的 Drools 规则

[英]Drools rules for different users

I recently started programming with Drools Fusion and I have a smart wearable that sends pedometer and heart rate data to my laptop.我最近开始使用 Drools Fusion 进行编程,我有一个智能可穿戴设备,可以将计步器和心率数据发送到我的笔记本电脑。 I then process this data with using the drools rule language.然后我使用 drools 规则语言处理这些数据。 But suppose I have multiple smart wearables with each an unique MAC address.但是假设我有多个智能可穿戴设备,每个都有一个唯一的 MAC 地址。 I use time windows and my question is how can I change my rule file so that the rules only fire for events with the same macaddress and take appropiate action based on this MAC address.我使用时间窗口,我的问题是如何更改我的规则文件,以便规则仅针对具有相同 macaddress 的事件触发,并根据此 MAC 地址采取适当的操作。 My current rule file is the following:我当前的规则文件如下:

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

My HeartRate events have the following fields:我的心率事件具有以下字段:

int heartRate;
Date timeStamp;
String macAddress;

My Steps events have the following fields:我的步骤事件具有以下字段:

double steps;
Date timeStamp;
String macAddress;

This is simple: you need to define a fact, call it Walker with String macAddress , create it with the MAC address the rules should handle, and then这很简单:您需要定义一个事实,使用String macAddress将其命名为Walker ,使用规则应处理的 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

and similarly with the other rules.其他规则也类似。 - You may simplify this (a little) by defining a basic rule - 你可以通过定义一个基本规则来简化这个(一点点)

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

and write the other rules as extensions:并将其他规则写为扩展:

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

so you don't need to repeat the Walker pattern for each rule.所以你不需要为每个规则重复Walker模式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM