2024年4月28日发(作者:)

drools fusion(3)

2010-12-02 23:07

五、事件处理模式(Event Processing Modes)

Drools支持2种事件处理模式:云模式(Cloud Mode)和流模式(Stream Mode)

1.云模式(Cloud Mode)

云(Cloud)处理模式是默认的处理方式。

在云模式下,不会区分事实和事件,都看成是事实。

用。

(2)无序的事件云。由于事件无序,没有自动的生命周期管理,需要像正常的事实一样显示的删除事件。

云模式虽然是默认的执行模式,我们也可以配置它:

KnowledgeBaseConfiguration config = wledgeBaseConfiguration();

ion( );

等同系统属性配置:

rocessingMode = cloud

2.流模式(Stream Mode)

当处理事件流的时候需要选择流处理模式。

在流模式下:

(1) 插入到引擎里的事件必须是时间顺序的。

(2) 引擎强制性的和使用的会话时钟session clock同步。

配置流模式:

KnowledgeBaseConfiguration config = wledgeBaseConfiguration();

ion( );

等同配置系统属性:

rocessingMode = stream

使用流(STREAM)模式,引擎有时间流和"现在"的概念(通过读取Session Clock的时间戳),

提供了以下3种支持:

(1) 滑动窗的支持

(2) 自动的时间生命周期管理

(3) 使用消极模式(Negative Patterns)自动的规则延迟

3.会话时钟(Session Clock)在流模式(Stream mode)中的作用

在云模式下,会话时钟只有一个作用,就是给插入到working momery 的事件赋予时间戳的值(如果规则没有定义时间戳属性)

4.流模式(in Stream Mode)中的消极模式(Negative Patterns)

(1)没有时间的概念。尽管事件在插入引擎被赋予了时间戳,也不能判断该事件“多大了”,因为没有“现在”的概念。滑动窗(slid

在流模式下,会话时钟负责维护当前时间戳,基于当前的时间戳,引擎根据事件的年龄计算所有时间运算,从多种源同步流,安排

消极模式在流模式和云模式意义是不同的。

在云模式下,所有的事实和事件都是预先知道的,消极模式立即计算执行

//a rule that activates immediately upon matching

rule "Sound the alarm"

when

$f : FireDetected( )

not( SprinklerActivated( ) )

then

// sound the alarm

end

在流模式下,带有时间约束的消极模式可以要求引擎等待一段时间后激活规则。

//a rule that automatically delays activation due to temporal constraints

rule "Sound the alarm"

when

$f : FireDetected( )

not( SprinklerActivated( this after[0s,10s] $f ) )

then

// sound the alarm

end

六. 滑动窗(Sliding Windows)

滑动窗是一种选择事件范围的方式。

滑动窗有2种实现方式:滑动时间窗(Sliding Time Windows),滑动长度窗(Sliding Length Windows)。

注意:滑动窗只有在引擎为(STREAM)流模式下才是可用的

1.滑动时间窗(Sliding Time Windows)

滑动时间窗(允许我们的规则)仅仅匹配发生在最近X时间单元的事件。

例如,如果只关心最近2分钟的股票行情,模式(pattern)可以这样写:

StockTick() over window:time( 2m )

更复杂的例子:

如果最近10分钟从传感器读来的温度高于最大的临界值,发出警报。

rule "Sound the alarm in case temperature rises above threshold"

when

TemperatureThreshold( $max : max )

Number( doubleValue > $max ) from accumulate(

SensorReading( $temp : temperature ) over window:time( 10m ),

average( $temp ) )

then

// sound the alarm

end