Introduction to Rules Engines Brian McCallister brianm@ninginc.com - - PowerPoint PPT Presentation

introduction to rules engines
SMART_READER_LITE
LIVE PREVIEW

Introduction to Rules Engines Brian McCallister brianm@ninginc.com - - PowerPoint PPT Presentation

Introduction to Rules Engines Brian McCallister brianm@ninginc.com http://www.ninginc.com/ Objectives Understanding of forward-chaining rules engines Introduction to the Drools rules engine Insights into where rules engines are a


slide-1
SLIDE 1

Introduction to Rules Engines

Brian McCallister

brianm@ninginc.com http://www.ninginc.com/

slide-2
SLIDE 2

Objectives

  • Understanding of forward-chaining rules

engines

  • Introduction to the Drools rules engine
  • Insights into where rules engines are a

good solution

slide-3
SLIDE 3

Rules Engine Basics

  • Forward Chaining
  • Rules match facts and execute
  • Rete and company
  • Reverse Chaining
  • Answer questions based on facts
  • Prolog style
slide-4
SLIDE 4

Examples are Good

Ecommerce Personalized Specials (e.g. Product Selection in Front of Amazon)

slide-5
SLIDE 5

A (Small) Set of Rules

<rule-set name="basics" xmlns="http://drools.org/rules" xmlns:java="http://drools.org/semantics/java" xmlns:xs="http://www.w3.org/2001/XMLSchema-instance" xs:schemaLocation="http://drools.org/rules rules.xsd http://drools.org/semantics/java java.xsd"> <import>org.skife.drools.*</import> <application-data identifier="specials">Specials</application-data> <rule name="Favorite Colors"> <parameter identifier="person"> <class>Person</class> </parameter> <parameter identifier="item"> <class>Item</class> </parameter> <java:condition> person.getFavoriteColor().equals(item.getColor()) </java:condition> <java:consequence> specials.increment(item); </java:consequence> </rule> </rule-set>

slide-6
SLIDE 6

Just a Rule...

<rule name="Favorite Colors"> <parameter identifier="person"> <class>Person</class> </parameter> <parameter identifier="item"> <class>Item</class> </parameter> <java:condition> person.getFavoriteColor().equals(item.getColor()) </java:condition> <java:consequence> specials.increment(item); </java:consequence> </rule>

slide-7
SLIDE 7

Parameters (Match Facts)

<rule name="Favorite Colors"> <parameter identifier="person"> <class>Person</class> </parameter> <parameter identifier="item"> <class>Item</class> </parameter> <java:condition> person.getFavoriteColor().equals(item.getColor()) </java:condition> <java:consequence> specials.increment(item); </java:consequence> </rule>

slide-8
SLIDE 8

Conditions (If ...)

<rule name="Favorite Colors"> <parameter identifier="person"> <class>Person</class> </parameter> <parameter identifier="item"> <class>Item</class> </parameter> <java:condition> person.getFavoriteColor().equals(item.getColor()) </java:condition> <java:consequence> specials.increment(item); </java:consequence> </rule>

slide-9
SLIDE 9

Head or LHS

<rule name="Favorite Colors"> <parameter identifier="person"> <class>Person</class> </parameter> <parameter identifier="item"> <class>Item</class> </parameter> <java:condition> person.getFavoriteColor().equals(item.getColor()) </java:condition> <java:consequence> specials.increment(item); </java:consequence> </rule>

Head or LHS

slide-10
SLIDE 10

Left Hand Side

  • Parameters and Conditions
  • Logically Anded Together
  • Rules “Match”
slide-11
SLIDE 11

Parameters

  • Usually based on types
  • Sometimes Type Annotation
  • Product of all available facts applied to

rule parameters and conditions

slide-12
SLIDE 12

Conditions

  • Given condition general evaluated only
  • nce, unless facts change
  • Becomes graph traversal
  • Quite optimizable
slide-13
SLIDE 13

Four Rules (LHS Only)

rule: Favorite Color param: Person => person param: Item => item condition: person.favoriteColor == item.color rule: Fits param: Person => person param: Item => item condition: item.fits(person) rule: Fits and Favorite Color Booster param: Person => person param: Item => item condition: item.fits(person) condition: person.favoriteColor == item.color rule: Don’t sell what they just bought param: Person => person param: Item => item condition: person.recentPurchases.contains(item)

slide-14
SLIDE 14

Drools Network (Not RETE)

Conditions Parameters

slide-15
SLIDE 15

Right Hand Side

  • The “Stuff” of the Rule
  • Can manipulate facts
  • What else?
  • Depends on the engine
slide-16
SLIDE 16

Consequence (Does Stuff)

<rule name="Favorite Colors"> <parameter identifier="person"> <class>Person</class> </parameter> <parameter identifier="item"> <class>Item</class> </parameter> <java:condition> person.getFavoriteColor().equals(item.getColor()) </java:condition> <java:consequence> specials.increment(item); </java:consequence> </rule>

slide-17
SLIDE 17

Body or RHS

<rule name="Favorite Colors"> <parameter identifier="person"> <class>Person</class> </parameter> <parameter identifier="item"> <class>Item</class> </parameter> <java:condition> person.getFavoriteColor().equals(item.getColor()) </java:condition> <java:consequence> specials.increment(item); </java:consequence> </rule>

Body or RHS

slide-18
SLIDE 18

Using the Rules (Drools)

WorkingMemory wm = rule_base.newWorkingMemory(); Person brian = new Person("Brian", "blue"); wm.assertObject(brian); Item blue_item = new BlueItem("Blue Item 1"); wm.assertObject(blue_item); Item red_item = new RedItem("Red Item"); wm.assertObject(red_item); Item another_blue = new BlueItem("Blue Item 2"); wm.assertObject(another_blue); Specials specials = new Specials(); wm.setApplicationData("specials", specials); wm.fireAllRules(); assertEquals(1, specials.scoreFor(blue_item)); assertEquals(1, specials.scoreFor(another_blue)); assertEquals(0, specials.scoreFor(red_item));

slide-19
SLIDE 19

Knowledge Base

  • Like a Class
  • Contains/Reads/Parses Rules
  • “Heavy”
slide-20
SLIDE 20

Working Memory

  • Instance of Knowledge Base
  • Contains Facts
  • “Lightweight”
slide-21
SLIDE 21

Knowledge Base & Working Memory

URL rules = new URL("http://rules.local/basic.drl"); RuleBase rule_base = RuleBaseLoader.loadFromUrl(rules); WorkingMemory wm = rule_base.newWorkingMemory();

slide-22
SLIDE 22

Asserting Facts

Person brian = new Person("Brian", "blue"); wm.assertObject(brian); Item blue_item = new BlueItem("Blue Item 1"); wm.assertObject(blue_item); Item red_item = new RedItem("Red Item"); wm.assertObject(red_item); Item another_blue = new BlueItem("Blue Item 2"); wm.assertObject(another_blue); Specials specials = new Specials(); wm.setApplicationData("specials", specials); wm.fireAllRules();

slide-23
SLIDE 23

Firing the Rules

Person brian = new Person("Brian", "blue"); wm.assertObject(brian); Item blue_item = new BlueItem("Blue Item 1"); wm.assertObject(blue_item); Item red_item = new RedItem("Red Item"); wm.assertObject(red_item); Item another_blue = new BlueItem("Blue Item 2"); wm.assertObject(another_blue); Specials specials = new Specials(); wm.setApplicationData("specials", specials); wm.fireAllRules();

slide-24
SLIDE 24

They Work!

Item another_blue = new BlueItem("Blue Item 2"); wm.assertObject(another_blue); Specials specials = new Specials(); wm.setApplicationData("specials", specials); wm.fireAllRules(); assertEquals(1, specials.scoreFor(blue_item)); assertEquals(1, specials.scoreFor(another_blue)); assertEquals(0, specials.scoreFor(red_item));

slide-25
SLIDE 25

Working Memories

  • Facts + Facts == more Facts!
  • or less, or sometimes just different
  • Execution of rules has handle on the

working memory

  • Alter, retract, modify facts
slide-26
SLIDE 26

Modifying Knowledge

<rule name="10% Discount at end of month" no-loop="true"> <parameter identifier="today"> <class>Calendar</class> </parameter> <parameter identifier="item"> <class>Item</class> </parameter> <java:condition> today.get(Calendar.DAY_OF_MONTH) > 20 </java:condition> <java:consequence> item.setPrice(item.getPrice() .multiply(new BigDecimal("0.90"))); drools.modifyObject(item); </java:consequence> </rule>

slide-27
SLIDE 27

Modifying Knowledge

<rule name="10% Discount at end of month" no-loop="true"> <parameter identifier="today"> <class>Calendar</class> </parameter> <parameter identifier="item"> <class>Item</class> </parameter> <java:condition> today.get(Calendar.DAY_OF_MONTH) > 20 </java:condition> <java:consequence> item.setPrice(item.getPrice() .multiply(new BigDecimal("0.90"))); drools.modifyObject(item); </java:consequence> </rule>

slide-28
SLIDE 28

With Drools...

  • Currently doesn’t use actual RETE
  • Probably slightly slower algorithm for

really large rule sets

  • Built In “Languages”
  • Java
  • Jython
  • Groovy
  • BeanShell
  • Strong DSL capabilities
slide-29
SLIDE 29

Okay, So What?

  • So, it’s a bunch of fast ands...
  • Classics:
  • Financial and Insurance
  • Highly regulated
  • Rules (from the gov’t) change often
  • In General:
  • Rules change often
  • Very complex behaviors
  • Correlations
slide-30
SLIDE 30

Some Real Examples

No real code, though, I don’t own that, unfortunately =(

slide-31
SLIDE 31

Inventory Reduction

Ads the Smart Way

slide-32
SLIDE 32

Inventory Reduction

  • Online retailer wants to lower inventory
  • Could be used to control supply chain

shape as well

slide-33
SLIDE 33

Facts

  • Items to be made available as specials
  • All known marketing data on the user
  • Demographic
  • Past purchases
  • etc
  • Contents of Shopping Cart
  • Trail (though this didn’t work so well)
slide-34
SLIDE 34

Rules

  • First Try
  • Multiple regression analysis of mass

trends

  • Good in theory, too complicated
  • Second Try
  • Best Guesses
  • “Common Sense”
slide-35
SLIDE 35

Remember These?

rule: Favorite Color param: Person => person param: Item => item condition: person.favoriteColor == item.color rule: Fits param: Person => person param: Item => item condition: item.fits(person) rule: Fits and Favorite Color Booster param: Person => person param: Item => item condition: item.fits(person) condition: person.favoriteColor == item.color rule: Don’t sell what they just bought param: Person => person param: Item => item condition: person.recentPurchases.contains(item)

slide-36
SLIDE 36

Design

  • Rules system is a queryable service
  • Lots of facts, small number of rules
  • Tiny by scale of traditional rule systems
  • Rules implement core logic of the service
slide-37
SLIDE 37

Instance Level Security

With lots of complex silly stuff

slide-38
SLIDE 38

1000m View

slide-39
SLIDE 39

Authorization Based On...

  • Role
  • Entity Properties
  • Organizational Affiliation
  • Differential Data Expiration
  • Locality
  • EU Data Protection Laws

(Blame Marketing)

slide-40
SLIDE 40

Facts

  • Subject (user)
  • Instance to check access for
  • User Roles
  • Obtainable from principal, but more

efficient to just assert

  • User’s organizational affiliation
  • User’s home country
  • Today’s Date
slide-41
SLIDE 41

For Example

rule: Acme Parts Managers See All param: Organization => org param: Role => role condition: role == Role.MANAGER condition: org.name.equals(“Acme Parts”) consequence: token.sufficient() rule: Acme Parts Data Expires in Two Weeks param: Organization => org param: Date => today param: Entity => it condition: org.name.equals(“Acme Parts”) condition: today > (it.expiresOn + (Dates.WEEK * 2)) consequence: assertFact(Facts.STALE_ENTITY)

slide-42
SLIDE 42

Design

  • Rules modify behavior of core domain

model

  • Implemented as a concern
  • Queryable as a service
  • This wasn’t used except in testing

though

  • Lots of rules, few facts
slide-43
SLIDE 43

Where Else?

  • Application Customization
  • Directed Questioning
  • Complex

Validation

  • Business Automation
  • Workflow / Orchestration
  • “Routing” problems

Questions, Discussion, Commentary?

slide-44
SLIDE 44

Thank you!

Brian McCallister http://www.chariotsolutions.com/ brianm@chariotsolutions.com

slide-45
SLIDE 45

Resources

  • Drools
  • http://drools.org/
  • CLIPS
  • http://www.ghg.net/clips/CLIPS.html
  • JavaRules
  • http://www.javarules.org/
  • JSR-94
  • http://www.jcp.org/en/jsr/detail?id=94