What is Drools, Guvnor and Planner? 2012 02-17 Brno Dev Conference

Embed Size (px)

Citation preview

drools

Geoffrey De Smet

What are Drools, Guvnor and Planner?

Business Logic Integration

BusinessLogicIntegration

Rule
engine

Workflow

Complex event
processing
(CEP)

Business Rule
Management System(BRMS)

Automated
planning

When should I use them?

Drools and Guvnor

A Drools rule

rule "No-risk mortgage" when $m : Mortgage( amount < 5000.0 ) Applicant( age < 50, income > 1000.0, mortage == $m ) then $m.setApproved(true); $m.setInterest(2.00);end

DRL pattern

Java code

The myth

Drools is a rule engine

So becauseDrools can fulfil business rules

Drools is Turing complete

we should write the entire business layer in Drools?No!

Not imperativeNot like Java, Scala, Groovy, ...

DeclarativeLike RegEx, SQL, JPA-QL,

Easier to read, better at scaling

When to use Drools

Extract business decisions in Drools that either:Require schooled domain expertise

Change often/fast

Use cases

Financial decisionsMortgages acceptance and interest calculation

Insurances acceptance and fee calculation

Use cases

Financial decisionsMortgages acceptance and interest calculation

Insurances acceptance and fee calculation

Price calculationsPhone/Telecom bills

Use cases

Financial decisionsMortgages acceptance and interest calculation

Insurances acceptance and fee calculation

Price calculationsPhone/Telecom bills

DiagnosticsPossible diseases based on symptoms

Use cases

Financial decisionsMortgages acceptance and interest calculation

Insurances acceptance and fee calculation

Price calculationsPhone/Telecom bills

DiagnosticsPossible diseases based on symptoms

Not a good use case: an XML parserUse Java code for that

Mortgages example

Developers don't like
data input from spreadsheets

Before App 1.0: Mortgages rules in a spreadsheetWhich version/copy of the spreadsheet file?

Which field has the result our application needs?

Version 1

Version 2

Mortgage experts like
the flexibility of spreadsheets

Since App 1.0: Mortgages rules in source codeChanging business rules not flexible enough

Business rules are hard coded in Java

Invalid: must be a number

Mortgage experts change their mind a lot

Flooded with new improvement requests:when income below 500

when income below 1000 and age above 50

when income below 1% of mortgage amount

Invalid: must be a number

Mortgage experts like the flexibility of Guvnor

App 2.0: Mortgage rules in Guvnor repositoryFlexible like spreadsheetBut backed by formal DRL and JavaBeans

No spreadsheet versions/copies

My business requires
domain expertise

Only mortgage managers
understand mortgage rules

Schooled domain expertise

Are we, the developers, domain experts?Yes, we've been working in this company for year(s).

Schooled domain expertise

Are we, the developers, domain experts?Yes, we've been working in this company for year(s).

Do we haveuniversity's degree in economics? No

participated in mortgages conferences/courses? No

years of mortgage customer experience? No

Schooled domain expertise

Are we, the developers, domain experts?Yes, we've been working in this company for year(s).

Do we haveuniversity's degree in economics? No

participated in mortgages conferences/courses? No

years of mortgage customer experience? No

Do we have a clue when the mortgage rules need to change? No

Schooled domain expertise

Are we, the developers, domain experts?Yes, we've been working in this company for year(s).

Do we haveuniversity's degree in economics? No

participated in mortgages conferences/courses? No

years of mortgage customer experience? No

Do we have a clue when the mortgage rules need to change? No

Are we, the developers, domain experts? NoMortgage manager is the domain expert

Domain experts can not read/write Java source code

Communication problemDomain expert can not read it:

for (Mortgage m : mortgages) { for (Applicant a : m.getApplicants()) { if (a.getAge() > 70) { boolean existsGuarantor = false; for (Guarantor g : m.getGuarantors() { if (g.getAge < 60) { existsGuarantor = true; } } // Will they spot the bug in this code? if (existsGuarantor) { m.setApproved(false); } } }}

Domain experts can read rules
in the Guided Rule Editor

Domain experts can write rules
in the Guided Rule Editor

Change

Translated to DRL
Executed on JavaBeans

Natural language (DSL)

DSL is even easier to read/write

Developers create the building blocks

Domain experts make the building

Source: wikipedia

My business rules
change often

This mortgage rule change must be
released by noon

Deliver changes fast

Deliver changes fast

Deliver changes fast

Business rule change

Not a data changeRequires test and release lifecycle

Logic changes

Not a software changeDelivered in hours, not days

Self-service by mortgage manager

Test before releasing

Domain experts need to unit test tooExample: mortgage of Erik must be disapproved

Do more with less

Planner

DemoDrools PlannerTSP example

Every organization has planning problems.

What is a planning problem?

Complete goals

With limited resources

Under constraints

Hospital bed scheduling

Assign eachPatient

ToBed

ConstraintsLength of stay

Room requirements

Room preferences

http://www.flickr.com/photos/markhillary/2227726759/

Hospital nurse rostering

Assign eachShift

ToNurse

ConstraintsEmployment contract

Free time preferences

Skills

http://www.flickr.com/photos/glenpooh/709704564/

School timetabling

Assign eachCourse

ToRoom

Timeslot

ConstraintsNo simultaneous coursesPer room

Per teacher

Per student

http://www.flickr.com/photos/phelyan/2281095105/

Airline routing

Assign eachFlight

ToAirplane

Crew

ConstraintsAirplane/crew depart from where they arrive

Minimize mileage

http://www.flickr.com/photos/yorickr/3674349657/

Bin packing in the cloud

Assign eachProcess

ToServer

ConstraintsHardware requirements

Minimize server cost

http://www.flickr.com/photos/torkildr/3462607995/

Bin packing in the cloud

Assign eachProcess

ToServer

ConstraintsHardware requirements

Minimize server cost

http://www.flickr.com/photos/torkildr/3462607995/

Which processes do we assign to this server?

How did we find that solution?

First fit by decreasing size

First fit by decreasing size

First fit by decreasing size

First fit by decreasing size

Another case

Try FFD again

Try FFD again

Try FFD again

Try FFD again

FFD failure

NP complete

NP complete

No silver bullet knownHoly grail of computer scienceP == NP

Probably does not existP != NP

Root problem of
all planning problems

http://www.flickr.com/photos/annguyenphotography/3267723713/

Multiple servers...

Multiple servers...

Multiple servers...

Multiple servers...

Multiple constraints...

Multiple constraints...

Multiple constraints...

Multiple constraints...

Organizations rarely optimize
planning problems.

Reuse optimization algorithms?

Open source (ASL 2.0)

Regular releases

Reference manual

Examples

DemoDrools PlannerCloudBalance example

Optimization algorithms

Brute Force

Brute force scalability

6 processes = 15 ms

9 processes = 1.5 seconds

* 100

Brute force scalability

12 processes = 17 minutes

6 processes = 15 ms

9 processes = 1.5 seconds

* 100

* 680

Plan 1200 processes
with brute force?

First Fit

First Fit scalability

First Fit results

CPU: OKRAM: OKNetwork: OKCost active servers: shown

First Fit Decreasing

First Fit Decreasing scalability

First Fit Decreasing results

Local Search

Local Search results

Cost ($) reduction

Compared to First FitFirst Fit Decreasing49 480 $ = 4 %

Tabu Search160 860 $ = 14 %

Simulated annealing128 950 $ = 11 %

Few constraints here=> low ROI

Organizations rarely optimize
planning problems.

http://www.flickr.com/photos/techbirmingham/345897594/

Organizations waste resources.

Summary

Summary

Use Drools and Guvnor when your business decisions eitherRequire domain expertise

Change often/fast

Use Planner to optimize planningAdding constraints is easy and scalable

Switching/combining algorithms is easy

Try an example!

Q&A

Drools, Guvnor and Planner homepagehttp://www.jboss.org/drools

Reference manual:http://www.jboss.org/drools/documentation

Download this presentationhttp://www.jboss.org/drools/presentations

Twitter: @geoffreydesmet

Google+: Geoffrey De Smet

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

This is the chapter title

This is the subtitle