Drools Ecosystem
Saumitra Srivastav
@_saumitra_
1
Components
1. Drools Expert
2. Drools Guvnor
3. Drools Fusion
4. KIE Workbench
5. jBPM
6. Optaplanner
2
Drools Expert
3
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
RHS
end
4
Custom Types
• Create new Types and use them
declare Person
name : String
dateOfBirth : java.util.Date
address : Address
end
rule "Using a declared Type"
when
$p : Person( name == "Bob" )
then
Person mark = new Person();
mark.setName("Mark");
insert( mark );
end
5
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") 6
Drools Guvnor
7
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 control
2. Version control
3. Provide GUI for non-programmers to edit rules
8
Drools Fusion
9
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
10
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
11
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
12
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 alarm
end
rule "Sound the alarm"
when
$f : FireDetected( )
not( SprinklerActivated( this after[0s,10s] $f ) )
then
// sound the alarm
end
13
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 alarm
end
14
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 alarm
end
rule "Sound the alarm in US"
when
$f : FireDetected( ) from “US-Office”
not( SprinklerActivated())
then
// sound the alarm
end
WorkingMemoryEntryPoint entryPoint1 =
session.getWorkingMemoryEntryPoint(“US-office");
entryPoint1.insert(fact);
15
Sliding Windows
• Supports 2 sliding window implementations:
• Sliding time windows
• Sliding length windows
• Time - Fire rules on events which occurred in last 2 mins
when
Event() 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 alarm
end
16
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
when
Event() 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 alarm
end
17
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
when
Event(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 alarm
end
18
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 something
end
19
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 )
20
jBPM
21
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)
22
KIE Workbench Demo
23
Thanks
• http://www.jboss.org/drools.html
• http://docs.jboss.org/drools/release/6.0.1.Final/drools-
docs/html/index.html
Questions?
24

Drools Ecosystem

  • 1.
  • 2.
    Components 1. Drools Expert 2.Drools Guvnor 3. Drools Fusion 4. KIE Workbench 5. jBPM 6. Optaplanner 2
  • 3.
  • 4.
    Drools Expert • Providesa 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 RHS end 4
  • 5.
    Custom Types • Createnew Types and use them declare Person name : String dateOfBirth : java.util.Date address : Address end rule "Using a declared Type" when $p : Person( name == "Bob" ) then Person mark = new Person(); mark.setName("Mark"); insert( mark ); end 5
  • 6.
    Defining DSL • Supportsdefining 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") 6
  • 7.
  • 8.
    Drools Guvnor • Centralizedrepository for Drools Knowledge Bases • Provide GUI tools to aid in management of large number of rules. • Guvnor helps in: 1. Access control 2. Version control 3. Provide GUI for non-programmers to edit rules 8
  • 9.
  • 10.
    Drools Fusion • Addsevent 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 10
  • 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 11
  • 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 12
  • 13.
    Negative Patterns inStream 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 alarm end rule "Sound the alarm" when $f : FireDetected( ) not( SprinklerActivated( this after[0s,10s] $f ) ) then // sound the alarm end 13
  • 14.
    Negative Patterns inStream 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 alarm end 14
  • 15.
    Entry points • Entrypoints 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 alarm end rule "Sound the alarm in US" when $f : FireDetected( ) from “US-Office” not( SprinklerActivated()) then // sound the alarm end WorkingMemoryEntryPoint entryPoint1 = session.getWorkingMemoryEntryPoint(“US-office"); entryPoint1.insert(fact); 15
  • 16.
    Sliding Windows • Supports2 sliding window implementations: • Sliding time windows • Sliding length windows • Time - Fire rules on events which occurred in last 2 mins when Event() 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 alarm end 16
  • 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 when Event() 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 alarm end 17
  • 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 when Event(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 alarm end 18
  • 19.
    Memory Management forEvents 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 something end 19
  • 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 ) 20
  • 21.
  • 22.
    jBPM • a flexibleBusiness 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) 22
  • 23.
  • 24.