Drools Ecosystem

24
Drools Ecosystem Saumitra Srivastav @_saumitra_ 1

description

 

Transcript of Drools Ecosystem

Page 1: Drools Ecosystem

Drools Ecosystem

Saumitra Srivastav@_saumitra_

1

Page 2: Drools Ecosystem

2

Components

1. Drools Expert2. Drools Guvnor3. Drools Fusion4. KIE Workbench5. jBPM6. Optaplanner

Page 3: Drools Ecosystem

3

Drools Expert

Page 4: Drools Ecosystem

4

Drools Expert

• Provides a declarative, rule based, coding environment

• Rules(.drl files) can be combined in packages• allows imports, globals, functions, queries, rules

rule "name" attributes when LHS then RHSend

Page 5: Drools Ecosystem

5

Custom Types

• Create new Types and use them

declare Person name : String dateOfBirth : java.util.Date address : Addressend

rule "Using a declared Type"when $p : Person( name == "Bob" )then Person mark = new Person(); mark.setName("Mark"); insert( mark );end

Page 6: Drools Ecosystem

6

Defining DSL• Supports defining DSL • Syntax:

• Example: Using this DSL,

One can define a rule as:

[when]Something is {colour} = Something(colour=="{colour}")

[when]There is a person with name of "{name}"=Person(name=="{name}")[when]Person is at least {age} years old and lives in "{location}"= Person(age >= {age}, location=="{location}")[then]Log "{message}"=System.out.println("{message}");[when]And = and

There is a person with name of "Kitty" ==> Person(name="Kitty")Person is at least 42 years old and lives in "Atlanta" ==> Person(age >= 42, location="Atlanta")Log "boo" ==> System.out.println("boo");There is a person with name of "Bob" and Person is at least 30 years old and lives in "Utah" ==> Person(name="Bob") and Person(age >= 30, location="Utah")

Page 7: Drools Ecosystem

7

Drools Guvnor

Page 8: Drools Ecosystem

8

Drools Guvnor

• Centralized repository for Drools Knowledge Bases

• Provide GUI tools to aid in management of large number of rules.

• Guvnor helps in:1. Access control2. Version control3. Provide GUI for non-programmers to edit rules

Page 9: Drools Ecosystem

9

Drools Fusion

Page 10: Drools Ecosystem

10

Drools Fusion

• Adds event processing capabilities into the drools platform

• Provides 2 modes:• Cloud Mode• Stream Mode

• Sliding window support

• Stream support• allows defining entry points

• Memory management

• Temporal Reasoning

Page 11: Drools Ecosystem

11

Event Processing – Cloud Mode

• Its default processing mode

• when running in CLOUD mode, no notion of flow of time

• it is not possible for the engine to determine how "old" the event is, because there is no concept of "now".

• The engine looks at the events as an unordered cloud against which the engine tries to match rules.

• no automatic life-cycle management for events• application must explicitly delete events when they are no longer

necessary

Page 12: Drools Ecosystem

12

Event Processing – Stream Mode

• In this mode, engine uses a session clock to force synchronization between streams

• session clock is responsible for keeping the current timestamp, while helps in:• determining event age• do temporal calculations• synchronizes streams from multiple sources• schedules future tasks

• Two types of clocks:• Realtime

• Uses system clock• Pseudo

• Controlled by app, useful for dev

Page 13: Drools Ecosystem

13

Negative Patterns in Stream Mode

• In STREAM mode, negative patterns(with time constraints) may require the engine to wait for a time period before activating a rule.

• Without time constraints

• With time constraints

rule "Sound the alarm"when $f : FireDetected( ) not( SprinklerActivated( ) )then // sound the alarmend

rule "Sound the alarm"when $f : FireDetected( ) not( SprinklerActivated( this after[0s,10s] $f ) )then // sound the alarmend

Page 14: Drools Ecosystem

14

Negative Patterns in Stream Mode

• A rule that expects every 10 seconds at least one “Heartbeat” event, if not the rule fires.

rule "Sound the alarm" duration( 10s )when $f : FireDetected( ) not( SprinklerActivated( this after[0s,10s] $f ) )then // sound the alarmend

Page 15: Drools Ecosystem

15

Entry points

• Entry points can be used so that a rules matches only selective events

rule "Sound the alarm in Bangalore"when $f : FireDetected( ) from “Bangalore-Office” not( SprinklerActivated())then // sound the alarmend

rule "Sound the alarm in US"when $f : FireDetected( ) from “US-Office” not( SprinklerActivated())then // sound the alarmend

WorkingMemoryEntryPoint entryPoint1 = session.getWorkingMemoryEntryPoint(“US-office");entryPoint1.insert(fact);

Page 16: Drools Ecosystem

16

Sliding Windows

• Supports 2 sliding window implementations:• Sliding time windows• Sliding length windows

• Time - Fire rules on events which occurred in last 2 mins

whenEvent() over window:time( 2m )

then

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 alarmend

Page 17: Drools Ecosystem

17

Sliding Time Windows

• Fire rules on events which occurred in last 2 mins

• sound an alarm in case the average temperature over the last 10 minutes read from a sensor is above the threshold value

whenEvent() over window:time( 2m )

then

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 alarmend

Page 18: Drools Ecosystem

18

Sliding Length Windows

• Sliding length implementation consider events based on order of their insertion into the session

• sound an alarm in case the average temperature for last 100 entries from sensor is above the threshold value

whenEvent(type == “securitylogs”) over window:length( 10 )

then

rule "Sound the alarm in case temperature rises above threshold"when TemperatureThreshold( $max : max ) Number( doubleValue > $max ) from accumulate( SensorReading( $temp : temperature ) over window:length( 100 ), average( $temp ) )then // sound the alarmend

Page 19: Drools Ecosystem

19

Memory Management for Events

1. Explicit expiration offset

2. Inferred expiration offset

declare Event @expires( 30m )end

rule "correlate orders"when $bo : BuyOrderEvent( $id : id ) $ae : AckEvent( id == $id, this after[0,10s] $bo )then // do somethingend

Page 20: Drools Ecosystem

20

Temporal Reasoning operators

• Drools implements all 13 temporal operators defined in Allen’s paper

• Example:match if and only if the temporal distance between the time when, eventB finished and the time when $eventA started is between ( 3 minutes and 30 seconds ) and ( 4 minutes ).

After, before, coincides, during, finishes, finished by, includes, meets, met by, overlaps, overlapped by, starts, started by

$eventA : EventA( this after[ 3m30s, 4m ] $eventB )

Page 21: Drools Ecosystem

21

jBPM

Page 22: Drools Ecosystem

22

jBPM

• a flexible Business Process Management (BPM) Suite.

• offers process management features suited for both business users and developers

• allows you to model your business goals using flowcharts

• Domain-specific nodes can be plugged to make the processes more easily understood by business users.

• Eclipse-based and web-based editor

• History logging (for querying / monitoring / analysis)

• Optional process repository to deploy your process (and other related knowledge)

Page 23: Drools Ecosystem

23

KIE Workbench Demo