Drools Fusion

From Training Material
Jump to navigation Jump to search
title
Drools Fusion
author
Bernard Szlachta (NobleProg Ltd)

CEP ⌘

  • Complex Event Processing (CEP) - focuses on correlating and composing of atomic events into complex (compound) events
  • Wikipedia Definition of CEP:
"CEP is event processing that combines data from multiple sources to infer events or patterns that suggest more complicated circumstances
The goal of complex event processing is to identify meaningful events (such as opportunities or threats) and respond to them as quickly as possible."


CEP 。

  • 复杂事件处理 (CEP) - 将原子事件关联组合成为复杂(组合式的)事件
  • Wikipedia对CEP的定义:
"CEP从多个数据源整合数据,进而推导出事件或模式,这些推导出的事件提示出更加复杂情况的情况。
负责事件处理的目标是找到有意义的事件(比如机会或者危险)并快速做出响应."

Fusion Use Cases | Fusion 案例

  1. Using operators
    1. Trainer should not be assigned to teach two courses at the same time 培训师不可以同时培训两门课
  2. Aggregations (Time Window)
    1. Trainer shall not teach more than 40 hours in any 30 day window 培训师在三十天内课时不能超过40小时
  3. Streaming Mode with Moving Window
    1. When sales exceeds 3 bookings per coordinator per day, send warning to managers 如果一个培训专员每天收到的订单超过3个那么给经理发出警告
    2. If share price falls 10% in less than an hour, sell of all shares 如果股票价格在一小时之内跌过10%,那么卖出全部的股票

What is Drools Fusion ⌘

  • module responsible for enabling event processing behaviour

ClipCapIt-160802-211428.PNG

什么是 Drools Fusion 。

  • 负责启动事件处理行为的模块

ClipCapIt-160802-211428.PNG

Event ⌘

  • An event is a fact + information about time of occurence
  • Events are a record of a significant change of state in the application domain e.g. change of the price, booking form arrival
  • Rules involving events usually require the correlation of multiple events

Drools supports the declaration and usage of events with both point-in-time events and interval-based events semantics.

事件 。

  • 一个事件即事实+发生时间信息
  • 在应用领域,事件是重大的状态变化的记录,比如价格变动,预订单到达
  • 包含事件的规则通常与多个事件相关联

Drools支持声明和使用事件,语义上分为在某个时间点的事件和某段时间间隔内的事件

Declaring a fact type as an event⌘

declare StockTick
    @role( event )
    @timestamp(whenReceived)
    @duration(howLong)
    @expires(1h20m)
end
  • @role (required) defines how the engine should handle instances of that type: either as regular facts or as events
  • @timestamp (optional) defines the moment when the event took place. If not present, the timestamp of each event instance will be the moment it was inserted into the working memory.
  • @duration (optional) specifies the duration of the event
  • @expires (optional) specifies how long this type of event should be present in the working memory before automatic deletion

将一个fact type作为事件声明。

declare StockTick
    @role( event )
    @timestamp(whenReceived)
    @duration(howLong)
    @expires(1h20m)
end
  • @role (必需) 定义引擎如何处理该类型数据:是一般的facts或者是事件
  • @timestamp (可选) 定义事件发生时间. 如果没有定义,每个事件实例的timestamp即插入工作内存的时间.
  • @duration (可选) 描述事件的持续时间
  • @expires (可选) 描述这个类型的事件在工作内存的驻留时间,超过时间将被自动删除


Event Processing Modes

Stream Mode

  • The STREAM processing mode is the mode of choice when the application needs to process streams of events.
  • When using the STREAM, the engine knows the concept of flow of time and the concept of "now", i.e., the engine understands how old events are based on the current timestamp read from the Session Clock.


CEP example⌘

source: Salatino, De Maio, Aliverti, Mastering JBoss Drools 6

declare SuspiciousCustomerEvent
    @role(event)
    customerId: Long
    reason: String
end

rule "More than 10 transactions in an hour from one client"
    when
        $t1: TransactionEvent($cId: customerId)
        Number(intValue >= 10) from accumulate(
            $t2: TransactionEvent(
                this != $t1, 
                customerId == $cId, 
                this meets[1h] $t1
            ),
            count($t2)
        )
        not (SuspiciousCustomerEvent(customerId == $cId, reason == "Many transactions"))
    then
        insert(new SuspiciousCustomerEvent($cId, "Many transactions"));
end
  • The meets evaluator correlates two events and matches when the current event's end timestamp happens at the same time as the correlated event's start timestamp.
    • $eventA : EventA( this meets $eventB )
  • it means: abs( $eventB.startTimestamp - $eventA.endTimestamp ) == 0
  • The meets evaluator accepts one optional parameter. If it is defined, it determines the maximum distance between the end timestamp of current event and the start timestamp of the correlated event in order for the operator to match. Example:
    • $eventA : EventA( this meets[ 5s ] $eventB )
  • Will match if and only if: abs( $eventB.startTimestamp - $eventA.endTimestamp) <= 5s

CEP 举例。

declare SuspiciousCustomerEvent
    @role(event)
    customerId: Long
    reason: String
end

rule "More than 10 transactions in an hour from one client"
    when
        $t1: TransactionEvent($cId: customerId)
        Number(intValue >= 10) from accumulate(
            $t2: TransactionEvent(
                this != $t1, 
                customerId == $cId, 
                this meets[1h] $t1
            ),
            count($t2)
        )
        not (SuspiciousCustomerEvent(customerId == $cId, reason == "Many transactions"))
    then
        insert(new SuspiciousCustomerEvent($cId, "Many transactions"));
end
  • The meets evaluator 关联了两个事件,在当前事件结束的时间与相关联事件开始时间一致时,达成匹配
    • $eventA : EventA( this meets $eventB )
  • it means: abs( $eventB.startTimestamp - $eventA.endTimestamp ) == 0
  • The meets evaluator 接受一个可选参数. 如果有,它决定了当前事件的结束时间和相关事件的开始时间的最大时间间隔,以便操作完成匹配. 比如:
    • $eventA : EventA( this meets[ 5s ] $eventB )
  • 只在: abs( $eventB.startTimestamp - $eventA.endTimestamp) <= 5s 时匹配


Example and Exercises ⌘

  1. See examples in the code
  2. Write a code which would turn the splrnkler on 10 sec after detecting the fire
  3. Write HeartBeat implementation (i.e. if there is no signal for 10 sec, fire the rule)

举例和练习 。

  1. 见示例代码
  2. 写一段代码,在发现火情时,启动洒水器并喷洒10秒
  3. 写段代码,实现心跳(HeartBeat) (比如. 如果10秒无信号,启动规则)