gpt4 book ai didi

java - Drools针对不同用户的规则

转载 作者:行者123 更新时间:2023-11-30 07:32:05 25 4
gpt4 key购买 nike

我最近开始使用 Drools Fusion 进行编程,我有一个智能可穿戴设备,可以将计步器和心率数据发送到我的笔记本电脑。然后我使用 drools 规则语言处理这些数据。但假设我有多个智能可穿戴设备,每个设备都有一个唯一的 MAC 地址。我使用时间窗口,我的问题是如何更改我的规则文件,以便规则仅针对具有相同 mac 地址和的事件触发根据此 MAC 地址采取适当的操作。我当前的规则文件如下:

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;

最佳答案

这很简单:您需要定义一个事实,将其称为 Walker 并使用 String 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 模式。

关于java - Drools针对不同用户的规则,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35963662/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com