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
RHS
end
4
5. 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
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") 6
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 control
2. Version control
3. Provide GUI for non-programmers to edit rules
8
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 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
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 alarm
end
14
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 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
• 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
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 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
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
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)
22