Drools Expert and Fusion Intro : London 2012

Embed Size (px)

Citation preview

The SkyNet funding bill is passed.

The system goes online on August 4th, 1997.

Human decisions are removed from strategic defense.

SkyNet begins to learn at a geometric rate.

It becomes self-aware at 2:14am Eastern time, August 29th

In a panic, they try to pull the plug.

And, Skynet fights back

Mark ProctorProject Lead

Drools Books

Sample Industries and Users

Investment

Millennium Investment Group (MIG)

Logistics

Fedex

Airline

Sabre

Mortgage

Franklin American

Healthcare

OSDE

Boot Camps

San Francisco 2009 (40+ attendees)

Sponsored by Third Pillar

Sun, FAMC, OSDE, Kaseya, Fedex, TU Group, Intermountain Healthcare, Gap, Sony Pictures, Lockheed Martin, Kaiser, HP, Wells Fargo, US Navy Research, FOLIOfn, Boeing .....

San Diego 2010 (80+ attendess)

Sponsored by US Navy

5 day event, with 2 days focus on the healthcare industry

OSDE, AT&T, SAIC, US Navy Research, Kaiser, Clinica, Intermountain Healthcare, GE Healthcare, VA, Boeing, Nationwide ....

Integrated Systems

Semantic
OntologiesRulesEventProcessesWorkflowsRules Workflows

EventProcesses

Semantic
Ontologies

Rules and processes

loosely coupledtightly coupledspecificgenericDecisionServicesProcessRulesSCOPECOUPLING?

Integrated Systems

Drools
Grid

Drools
Planner

Drools
Semantics

Drools
Chance Business Logic integration System

Drools
Guvnor

Drools
Fusion

JBPM5(Drools Flow)

Drools
Expert

Because Not Everyone
Is As Smart As He Is

Declarative Programming

Production Rule Systems PRD (forward chaining)

Reactive

when Alarm( status == alert )
then send( warning )

Logic Programming LP (backward chaining)

Query

descendant( mary, jane)

Functional Programming FP

Map,Fold, Filter

avg([12, 16, 4, 6])Returns single value 9.5

round([10.3, 4.7, 7.8] )Returns List [10, 5, 8]

Description Logic

Person Has Name and
LivesAt Address

Definitions

public class Applicant { private String name; private int age; private boolean valid; // getter and setter methods here}

rule "Is of valid age" when $a : Applicant( age < 18 )then modify( $a ) { valid = false };ends

Building

KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();

kbuilder.add( ResourceFactory .newClassPathResource( "licenseApplication.drl", getClass() ), ResourceType.DRL );

if ( kbuilder.hasErrors() ) { System.err.println( kbuilder.getErrors().toString() );}

kbase.addKnowledgePackages( kbuilder.getKnowledgePackages() );

Spring Configuration

Spring and CamelConfiguration

Executing

StatelessKnowledgeSession ksession = kbase.newStatelessKnowledgeSession();Applicant applicant = new Applicant( "Mr John Smith", 16 );assertTrue( applicant.isValid() );

ksession.execute( applicant );

assertFalse( applicant.isValid() );

rule "Is of valid age" when $a : Applicant( age < 18 )then modify( $a ) { valid = false };ends

Definitions

public class Room { private String name // getter and setter methods here}public class Sprinkler { private Room room; private boolean on; // getter and setter methods here}public class Fire { private Room room; // getter and setter methods here}public class Alarm {}

not Bus( color = red )

Conditional Elements

exists Bus( color = red )

forall ( $bus : Bus( floors == 2 ) Bus( this == $bus, color == red ) )

forall ( $bus : Bus( color == red ) )

Accumulate CE

rule "accumulate"when accumulate( Bus( color == "red", $t : takings ); $sum : sum( $t ), $min : min( $t ), $max : max( $t ); $min > 100 && $max < 200 && $sum > 500 )then print "sum is + $sum;end

Classes

rule increase balance for AccountPeriod Credits when ap : AccountPeriod() acc : Account( $accountNo : accountNo ) CashFlow( type == CREDIT, accountNo == $accountNo, date >= ap.start && = ap.start and cf.date = ap.start && = ap.start && = 16 )then insert(new AdultBusPass( $p ) );end

TMS and Inference

rule "Issue Child Bus Pass"when $p : Person( age < 16 )then insert(new ChildBusPass( $p ) );endrule "Issue Adult Bus Pass"when $p : Person( age >= 16 )then insert(new AdultBusPass( $p ) );end

Couples the logicWhat happens when the Child stops being 16?

TMS and Inference

Bad

Monolithic

Leaky

Brittle integrity - manual maintenance

TMS and Inference

A rule logically inserts an object

When the rule is no longer true, the object is retracted.

when $p : Person( age < 16 )then logicalInsert( new IsChild( $p ) )endwhen $p : Person( age >= 16 )then logicalInsert( new IsAdult( $p ) )end

de-couples the logicMaintains the truth by
automatically retracting

TMS and Inference

rule "Issue Child Bus Pass"when $p : Person( ) IsChild( person =$p )then logicalInsert(new ChildBusPass( $p ) );endrule "Issue Adult Bus Pass"when $p : Person( age >= 16 ) IsAdult( person =$p )then logicalInsert(new AdultBusPass( $p ) );end

The truth maintenance cascades

TMS and Inference

rule "Issue Child Bus Pass"when $p : Person( ) not( ChildBusPass( person == $p ) )then requestChildBusPass( $p );end

The truth maintenance cascades

TMS and Inference

Good

De-couple knowledge responsibilities

Encapsulate knowledge

Provide semantic abstractions for those encapsulation

Integrity robustness truth maintenance

Wumpus World

Wumpus World

Wumpus World

Cell

int row

Int col

Hero

int row

Int col

Wumpus

int row

Int col

Pitt

int row

Int col

Gold

int row

Int col

Wumpus World

Demonstration

http://www.youtube.com/watch?v=4CvjKqUOEzM

What is Complex Event Processing

1. Detect

2. Correlate

3. React

Time is Money

Adapted from a presentation by James Taylor, Sep/2011

Time

Business Value

Business Event

Reaction

Value Loss

Time Loss

For the scope of this presentation:

CEP is used as a common term meaning both CEP and ESP.

Terminology: CEP and ESP

EDA vs SOA

EDA is **not** SOA 2.0

Complementary architectures

Metaphor

In our body:

SOA is used to build our muscles and organs

EDA is used to build our sensory system

Event Driven Architectures
edBPM + EDM

Drools Fusion: Enables

Event Detection:

From an event cloud or set of streams, select all the meaningful events, and only them.

[Temporal] Event Correlation:

Ability to correlate events and facts declaring both temporal and non-temporal constraints between them.

Ability to reason over event aggregation

Event Abstraction:

Ability to compose complex events from atomic events AND reason over them

Drools Fusion

Features:

Event Semantics as First Class Citizens

Allow Detection, Correlation and Composition

Temporal Constraints

Session Clock

Stream Processing

Sliding Windows

CEP volumes (scalability)

(Re)Active Rules

Data Loaders for Input

Drools Fusion

Features:

Event Semantics as First Class Citizens

Allow Detection, Correlation and Composition

Temporal Constraints

Session Clock

Stream Processing

Sliding Windows

CEP volumes (scalability)

(Re)Active Rules

Data Loaders for Input

Event Declaration and Semantics

Event semantics:

Point-in-time and Interval

An event is a fact with a few special characteristics:

Usually immutable, but not enforced

Strong temporal relationships

Lifecycle may be managed

Allow use of sliding windows

All events are facts, but not all facts are events.

// declaring existing classimport some.package.VoiceCalldeclare VoiceCall @role( event ) @timestamp( calltime ) @duration( duration )end

// generating an event classdeclare StockTick @role( event )

symbol : String price : doubleend

Temporal Reasoning

Semantics for:

time: discrete

events: point-in-time and interval

Ability to express temporal relationships:

Allens 13 temporal operators

James F. Allen defined the 13 possible temporal relations between two events.

Eiko Yoneki and Jean Bacon defined a unified semantics for event correlation over time and space.

Stream Support (entry-points)

A scoping abstraction for stream support

Rule compiler gather all entry-point declarations and expose them through the session API

Engine manages all the scoping and synchronization behind the scenes.

rule Stock Trade Correlationwhen

$c : Customer( type == VIP )

BuyOrderEvent( customer == $c, $id : id ) from entry-point Home Broker Stream

BuyAckEvent( sourceEvent == $id ) from entry-point Stock Trader Stream

then

// take some action

end

Delaying Rules

Negative patterns may require rule firings to be delayed.

rule Order timeoutwhen $bse : BuyShares ( $id : id ) not BuySharesAck( id == $id, this after[0s,30s] $bse )then // Buy order was not acknowledged. Cancel operation // by timeout.end

Temporal Relationships

rule Shipment not picked up in timewhen Shipment( $pickupTime : scheduledPickupTime ) not ShipmentPickup( this before $pickupTime )then // shipment not picked up... action required.end

Temporal Relationships

rule Shipment not picked up in timewhen Shipment( $pickupTime : scheduledPickupTime ) not ShipmentPickup( this before $pickupTime )then // shipment not picked up... Action required.end

Temporal Relationship

Allens 13 Temporal Operators

Point-Point

Point-Interval

Interval-Interval

A before B

A meets B

A overlaps B

A finishes B

A includes B

A starts B

A coincides B

A

B

A

B

A

B

A

B

A

B

A

B

A

B

Allens 13 Temporal Operators

Point-Point

Point-Interval

Interval-Interval

A after B

A metBy B

A overlapedBy B

A finishedBy B

A during B

A finishes B

A

B

A

B

A

B

A

B

A

B

A

B

Sliding Window Support

Allows reasoning over a moving window of interest

Time

Length

Sliding window 1

Sliding window 2

Joined window

Sliding Window Support

accumulate( $s : StockTicker( symbol == RHAT ) over window:time( 5s ); $avg : avg( $s.price ); $avg > 100 )

Aggregate ticker price for RHAT over last 5 seconds

Sliding Window Support

Allows reasoning over a moving window of interest

Time

Length

rule Average Order Value over 12 hourswhen $c : Customer() accumulate( BuyOrder( customer == $c, $p : price ) over window:time( 12h ); $a : avg( $p );

$a > 10 )then // do something

end

Calendars

rule "weekdays are high priority" calendars "weekday" timer (int:0 1h)when Alarm()then send( "priority high - we have an alarm );end

rule "weekend are low priority" calendars "weekend" timer (int:0 4h)when Alarm()then send( "priority low - we have an alarm );end

Execute now and after
1 hour durationExecute now and after
4 hour duration

Timers

rule name timer ( cron: 0 0/15 * * * * )when Alarm( )then sendEmail( Alert Alert Alert!!! )

Field Name Mandatory? Allowed Values Allowed Special CharactersSeconds YES 0-59 , - * /Minutes YES 0-59 , - * /Hours YES 0-23 , - * /Day of month YES 1-31 , - * ? / L WMonth YES 1-12 or JAN-DEC , - * /Day of week YES 1-7 or SUN-SAT , - * ? / L #Year NO empty, 1970-2099 , - * /

Send alert every quarter of an hour

CEP Applied at FedEx Custom Critical

* Presented by Adam Mollemkopf at ORF 2009

At least 50% of Alerts can be reasoned automatically, promoting staff savings and improved Customer and Driver experiences.

Risk Avoidance via pro-active monitoring

Reduction in insurance claims and shipment service failures

Minimum 30% efficiency gains in shipment monitoring , saving at least 15% of Operations staff cost.

CEP Applied at FedEx Custom Critical

* Presented by Adam Mollemkopf at ORF 2009

Some numbers (from early 2010):

24 x 7 sessions, no downtime

Average of 500k+ facts/events concurrently in memoryBusiness hours: 1M+ facts/events concurrently

Response time for reasoning cycles:Average: 150 ms

Peak: 1.2 sec

Several hundred rules

CEP Applied at FedEx Custom Critical

* Presented by Adam Mollemkopf at ORF 2009

Differential Update

Differential Update (a.k.a. true modify)

Implements a real modify/update operation, instead of retract+assert.Reuses tuples, reduces GC stress, improves performance

Questions?

Questions?

Dave Bowman: All right, HAL; I'll go in through the emergency airlock.

HAL: Without your space helmet, Dave, you're going to find that rather difficult.

Dave Bowman: HAL, I won't argue with you anymore! Open the doors!

HAL: Dave, this conversation can serve no purpose anymore. Goodbye.

Greetings, Professor Falken.Stephen Joshua:Falken: Hello, Joshua.Joshua: A strange game. The only winning move is not to play. How about a nice game of chess?

CashFlow

dateamounttypeaccountNo

12-Jan-07100CREDIT1

2-Feb-07200DEBIT1

18-May-0750CREDIT1

9-Mar-0775CREDIT1

???Page ??? (???)04/05/2011, 12:38:29Page / CashFlow

dateamounttype

12-Jan-07100CREDIT

9-Mar-0775CREDIT

???Page ??? (???)04/05/2011, 12:38:29Page / CashFlow

dateamounttype

2-Feb-07200DEBIT

???Page ??? (???)04/05/2011, 12:38:29Page /

Click to edit the title text format

Click to edit the outline text format

Click to edit the title text format

Click to edit the outline text formatSecond Outline LevelThird Outline LevelFourth Outline LevelFifth Outline LevelSixth Outline LevelSeventh Outline LevelEighth Outline LevelNinth Outline Level

This is the title
Second line of the title

Click to edit the outline text formatSecond Outline LevelThird Outline LevelFourth Outline LevelFifth Outline LevelSixth Outline LevelSeventh Outline LevelEighth Outline LevelNinth Outline Level

Click to edit the title text format

Click to edit the text format

AccountingPeriod

startend

01-Jan-0731-Mar-07

???Page ??? (???)04/05/2011, 12:38:30Page / Account

accountNobalance

10

???Page ??? (???)04/05/2011, 12:38:30Page / Account

accountNobalance

1-25

???Page ??? (???)04/05/2011, 12:38:30Page /