Filip Drools Fusion

From Training Material
Revision as of 08:17, 23 August 2016 by Filip Stachecki (talk | contribs) (→‎CEP example)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

What is Drools Fusion

  • module responsible for enabling event processing behaviour

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.

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

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 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

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)