简体   繁体   English

流口水 一段时间内没有达到规则

[英]Drools Absence of hitting a rule over a perios of time

I am new to Drools.我是 Drools 的新手。 I need to build an application with spring-boot & Drools(7.40.0.Final) where springboot application streams an external source that will continuously trigger data packets (tracking data related to movement of an entity) to my application.我需要使用 spring-boot & Drools(7.40.0.Final) 构建一个应用程序,其中 springboot 应用程序流式传输外部源,该源将不断触发数据包(与实体移动相关的跟踪数据)到我的应用程序。 I need to use evaluate all these streaming data.我需要使用评估所有这些流数据。

I am using a "geofence_rule.drl" file to keep the rules related to the geographic locations.我正在使用“geofence_rule.drl”文件来保存与地理位置相关的规则。

rule "Rule for Tag position in room 1"
when
    model : ComponentModel(positionValue <= 50)
then
    model.setRoomId(1);
end

rule "Rule for Tag position in room 2"
when
    model : ComponentModel(positionValue > 50)))
then
    model.setRoomId(2);
end

Model class is as below. Model class 如下。

import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
public class ComponentModel{

    private long tagId;
    private long roomId;
    private int positionValue;

}

I may get position related data of "n" number of tags say tag1, tag2 tag3 and so on.我可能会得到 position 的“n”个标签的相关数据,例如 tag1、tag2 tag3 等等。 I need to calculate if tag1 is absent from Room-1 (which means tag-1's data does not hit "Rule for Tag position in room 1") for Last 5 minutes.需要计算 tag1 是否在 Room-1 中不存在(这意味着 tag-1 的数据在最后 5 分钟内未达到“房间 1 中标签 position 的规则”)。 Is there any support in Drools for such calculation? Drools 是否支持这种计算?

I have seen "not" keyword in drools documentation.我在 drools 文档中看到了“not”关键字。 But it just negating the condition of Rule.但它只是否定了规则的条件。 I need to check rule hiting nature for last few minutes, this time limit is configurable in application.我需要检查最后几分钟的规则命中性质,这个时间限制可以在应用程序中配置。

You're looking for stream mode and negative patterns .您正在寻找stream 模式负模式 The links are to the official Drools documentation.这些链接指向官方 Drools 文档。

Stream mode is one of two event modes in Drools. Stream 模式是 Drools 中的两种事件模式之一。 The default is "cloud" mode, where you have all of the facts up front and it makes decisions automatically.默认是“云”模式,在这种模式下,您预先掌握了所有事实,它会自动做出决定。 The other mode, "stream" mode, is intended for processing temporal streams, which sounds a lot like your "pinging" application.另一种模式“流”模式用于处理时间流,这听起来很像您的“ping”应用程序。 In stream mode, Drools evaluates each fact as it comes in, and is aware of time -- namely when other facts were in.在 stream 模式下,Drools 会在每个事实进入时对其进行评估,并且知道时间——即其他事实何时进入。

Negative patterns in stream mode are the logical temporal extension of the not keyword. stream 模式中的否定模式是not关键字的逻辑时间扩展。 As you correctly pointed out, in cloud mode it simply negates a condition (eg. "there is no condition in working memory that matches this criteria.") In stream mode, however, you can update these patterns to take effect over a period of time.正如您正确指出的那样,在云模式下,它只是否定一个条件(例如,“工作 memory 中没有符合此条件的条件。”)但是,在 stream 模式下,您可以更新这些模式以在一段时间内生效时间。

The Drools documentation provides this example: Drools 文档提供了这个例子:

rule "Sound the alarm"
when
  $h: Heartbeat() from entry-point "MonitoringStream"
  not(Heartbeat(this != $h, this after[0s,10s] $h) from entry-point "MonitoringStream")
then
  // Sound the alarm.
end

The first line in the 'when' clause identifies an instance of a heartbeat ( $h ). 'when' 子句中的第一行标识心跳 ( $h ) 的实例。 The second identifies the situation when a heartbeat is not received within 10 seconds.第二个标识在 10 秒内收到心跳的情况。 If both conditions are true, the rule is executed -- in this case an alarm is triggered.如果两个条件都为真,则执行规则——在这种情况下会触发警报。

This is the same pattern you'd apply for your rule.这与您为规则应用的模式相同。

rule "Tag has not been in Room 1 for 5 minutes"
when
  // Tag with ID 1, present in Room 1 -- First instance
  $tag: ComponentModel( tagId == 1, roomId == 1 ) from entry-point "TrackingStream"

  // Trigger if this condition hasn't been met a second time within 5 minutes
  not( ComponentModel( 
    this != $tag, 
    tagId == 1,
    roomId == 1,
    this after[0s, 5m] $tag ) from entry-point "TrackingStream")
then
  // Do whatever it is you need to do for this condition
end

In this case, I'm leveraging the after temporal operator (link to Drools documentation.)在这种情况下,我利用了after时间运算符(链接到 Drools 文档。)

Basically this is how it works--基本上这就是它的工作原理——

$tag: ComponentModel( tagId == 1, roomId == 1 ) from entry-point "TrackingStream"

The first condition identifies the scenario, in this case ID 1 is present in Room 1. It identifies the current instance of that situation we're tracking.第一个条件标识场景,在本例中 ID 1 出现在房间 1 中。它标识我们正在跟踪的该情况的当前实例 Since this is a temporal stream, it's easy to think of it as "Tagged entity (1) has just entered Room 1."由于这是时间 stream,因此很容易将其视为“标记实体 (1) 刚刚进入房间 1”。

not( ComponentModel( 
  this != $tag, 
  tagId == 1,
  roomId == 1,
  this after[0s, 5m] $tag 
) from entry-point "TrackingStream")

This is where the magic happens, and the syntax takes a bit getting used to.这就是魔法发生的地方,语法需要一点时间来适应。 This second condition is waiting for the next temporal and then checking the conditions.第二个条件是等待下一个时间,然后检查条件。 The conditions checked are:检查的条件是:

  • TagID is 1,标记 ID 为 1,
  • RoomId is 5 RoomId 为 5

The temporal constraint ( this after[0s, 5m] $tag ) says to wait to check this condition.时间约束( this after[0s, 5m] $tag )表示等待检查此条件。 If a second ComponentModel is received within this time-frame after $tag , then the rule will not trip, and the scenario will repeat waiting for up to another 5 minutes.如果在$tag之后的这个时间范围内收到第二个 ComponentModel,则规则不会失效,并且该场景将重复等待最多 5 分钟。 Since the time range [0s, 5m] starts checking immediately, we need to exclude $tag explicitly from the matching in the not(...) clause ( this != $tag .)由于时间范围[0s, 5m]立即开始检查,我们需要从not(...)子句( this != $tag )的匹配中显式排除$tag tag 。


To illustrate, this is how it might execute (simplified):为了说明,这是它可能执行的方式(简化):

  • 0m:00s - Event A ~ received (id = 1, room = 1). 0m:00s - 事件 A ~ 收到(id = 1,房间 = 1)。 $tag = Event A. We begin checking incoming streams for the second condition. $tag = 事件 A。我们开始检查第二个条件的传入流。
  • 0m:30s - Event B ~ received (id = 2, room = 1). 0 分钟:30 秒 - 事件 B ~ 收到(id = 2,房间 = 1)。 ID mismatch;身份证不匹配; ignored.忽略。
  • 0m:45s - Event C ~ received (id = 1, room = 1). 0 分钟:45 秒 - 事件 C ~ 收到(id = 1,房间 = 1)。 Event A rule match 'cancelled'.事件 A 规则匹配“已取消”。 Now checking for $tag = Event C.现在检查$tag = Event C。
  • 5m:45s - No matching events received in 5 minute window, rule for Event C triggers right hand side. 5m:45s - 5 分钟内未收到匹配事件 window,事件 C 的规则触发右侧。

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

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