Business Rules Engines in Java and J2EE An Introduction to Drools - PowerPoint PPT Presentation

1 / 39
About This Presentation
Title:

Business Rules Engines in Java and J2EE An Introduction to Drools

Description:

Loop over all recipes ...check each recipe against available ingredients ... Collects recipes with all ingredients matched. Recipe Finder Rules rule ... – PowerPoint PPT presentation

Number of Views:1367
Avg rating:3.0/5.0
Slides: 40
Provided by: integr8
Category:

less

Transcript and Presenter's Notes

Title: Business Rules Engines in Java and J2EE An Introduction to Drools


1
Business Rules Engines in Java and J2EEAn
Introduction to Drools
  • Brian Sam-Bodden
  • Integrallis Software, LLC.

2
Agenda
  • A Simple Example Recipe Finder
  • Recipe Finder Procedural Implementation
  • Introducing Drools
  • Drools Overview
  • Recipe Finder using Drools
  • Drools The Big Picture
  • When and Why?
  • Rules
  • DRL File

3
Agenda
  • Rule Engine Applications
  • Drools Advanced Topics
  • Drools in J2EE
  • JSR-94
  • Domain Specific Languages
  • Conclusions
  • Choosing to use a Rule Engine
  • Choosing a Rule Engine

4
Recipe Finder Application
  • Problem Given a list of Ingredients and Recipes
    determine which recipes can be prepared

5
Recipe Finder - Domain
  • Recipe Finder Domain Model

6
Recipe Finder - Domain
  • The POJOs Recipes and Ingredients
  • // Ingredients
  • Ingredient rice new Ingredient("rice")
  • Ingredient beans new Ingredient("beans")
  • // Recipes
  • Recipe riceAndBeans new Recipe("Rice and
    Beans")
  • riceAndBeans.addIngredient(rice)
  • riceAndBeans.addIngredient(beans)

7
Recipe Finder Procedural Way
  • Traditional Procedural Approach
  • Loop over all recipes
  • check each recipe against available ingredients
  • save the matching recipes

8
Recipe Finder - Procedural Way
  • Matching Ingredients and Recipes
  • for (IteratorltRecipegt i recipes.iterator()
    i.hasNext())
  • Recipe recipe i.next()
  • for (IteratorltIngredientgt j
    ingredients.iterator()
  • j.hasNext())
  • Ingredient ingredient j.next()
  • recipe.matchIngredient(ingredient)
  • if (recipe.getIsComplete())
  • searchResults.addMatch(recipe)

9
Recipe Finder - Procedural Way
  • DEMO

10
Drools
  • Forward Chaining Inference Engine
  • Based on Rete algorithm
  • Supports several languages for expressing Rules
  • Created by Bob McWhirter
  • Hosted at Codehaus
  • Open source, LGPL
  • Simple API (few classes to master)
  • http//drools.org/

11
Recipe Finder - Drools Way
  • Basic Steps
  • Define the Rules
  • Create a RuleSet by loading the Rules
  • Create a WorkingMemory from the RuleSet
  • Assert Facts into the WorkingMemory
  • Fire the Rules

12
Recipe Finder - Drools Terms
  • RuleSet
  • Container for parsed Rules
  • Working Memory
  • Created from a RuleSet
  • Contains Facts
  • Fact
  • A Java object, a POJO or JavaBean
  • Facts are evaluated against the Rules

13
Recipe Finder - Drools Terms
  • Application Data
  • Object available to the Rules that do not affect
    the Agenda
  • Agenda
  • The rules that apply to the current state (the
    facts) of the WorkingMemory

14
Drools The Big Picture
15
Recipe Finder - Rules
  • 2 Rules
  • IngredientMatch Rule
  • Matches an ingredient to a recipe
  • RecipeMatch Rule
  • Collects recipes with all ingredients matched

16
Recipe Finder Rules
  • ltrule name"IngredientMatch"gt
  • ltparameter identifier"recipe"gt
  • ltjavaclassgtRecipelt/javaclassgt
  • lt/parametergt
  • ltparameter identifier"ingredient"gt
  • ltjavaclassgtIngredientlt/javaclassgt
  • lt/parametergt
  • ltjavaconditiongt
  • recipe.containsIngredient(ingredient)
  • lt/javaconditiongt
  • ltjavaconsequencegt
  • if (recipe.matchIngredient(ingredient))
  • drools.modifyObject(recipe)
  • lt/javaconsequencegt
  • lt/rulegt

17
Recipe Finder Rules
  • ltrule nameRecipeMatch"gt
  • ltparameter identifier"recipe"gt
  • ltjavaclassgtRecipelt/javaclassgt
  • lt/parametergt
  • ltjavaconditiongt
  • recipe.getIsComplete()
  • lt/javaconditiongt
  • ltjavaconsequencegt
  • searchResults.addMatch(recipe)
  • drools.retractObject(recipe)
  • lt/javaconsequencegt
  • lt/rulegt

18
Recipe Finder Rules
  • Rules are declarative
  • Follow the pattern
  • IF ltconditiongt THEN ltconsequencegt
  • A rule firing (execution) can change the state of
    the WorkingMemory therefore possibly triggering
    other rules to fire

19
Recipe Finder DRL File
  • DRL Files are XML
  • Contain one or more Rules
  • Several Semantic Modules Available
  • Java
  • Groovy
  • Python

20
Recipe Finder Loading the Rules
  • // URL to DRL File
  • URL url RecipeFinder.class
  • .getResource("recipefinder.java.drl")
  • // Create RuleBase
  • RuleBase ruleBase RuleBaseLoader.loadFromUrl(url
    )
  • //Create a Working Memory
  • WorkingMemory workingMemory
  • ruleBase.newWorkingMemory()

21
Recipe Finder Asserting Facts
  • // assert facts - ingredients
  • workingMemory.assertObject(rice)
  • workingMemory.assertObject(chocolateCake)
  • workingMemory.assertObject(eggs)
  • // assert facts - recipes
  • workingMemory.assertObject(riceAndBeans)
  • workingMemory.assertObject(chocolateCake)
  • workingMemory.assertObject(pancakes)
  • workingMemory.assertObject(rancheroEggs)

22
Recipe Finder Application Data
  • // create Search Results object
  • SearchResults searchResults
  • new SearchResults()
  • // set application data Search Results
  • workingMemory
  • .setApplicationData("searchResults

  • ,searchResults)

23
Recipe Finder Application Data
  • workingMemory.fireAllRules()
  • Output
  • Using drl recipefinder.java.drl
  • You can make Rice and Beans
  • You can make Ranchero Eggs

24
Recipe Finder Drools
  • DEMO

25
Rule Engines Applications
  • Knowledge Discovery
  • Path Optimization
  • Insurance Fraud Detection
  • Email Spam Filters
  • Retail, Holiday Promotions
  • Credit Scoring

26
Drools in a J2EE Application
  • Do it yourself choices
  • Stateless Session Bean Approach
  • MBeans
  • Storing and Retrieving RuleSets
  • JNDI
  • Spring (JSR-94) Integration Available

27
Drools in JBoss
  • DEMO

28
JSR-94
  • Rules Engine API (javax.rules)
  • Standard for Accessing Rule Engines
  • JSR-94 TCK
  • Technology Compatibility Kit
  • J2SE or J2EE
  • Does not impose a Rule Language
  • API cleanly divided between
  • Admin Tasks
  • Runtime Tasks

29
JSR-94 Example
  • DEMO

30
DSL Domain Specific Languages
  • Drools supports Domain Specific Languages
  • Enables Business Analyst and Source closer to the
    knowledge to express that knowledge
  • DSL Example
  • Create a Loan Logic Calculator for Mortgage
    Origination Application
  • Provide a DSL so that business people can easily
    author rules

31
DSL Domain Specific Languages
  • ltrule name"XYZ-FicoScore"gt
  • ltloansconditiongt
  • ltloanslender name"XYZ Mortage" /gt
  • ltloansapplicationgt
  • ltloansficogt
  • ltloansless-thangt680lt/loansless-thangt
  • lt/loansficogt
  • lt/loansapplicationgt
  • lt/loansconditiongt
  • ltloansactionsgt
  • ltloansapplication name"messages"gt
  • ltloansaddgt
  • Declined by XYZ Mortgage because a FICO score
    of at least 680 is required
  • lt/loansaddgt
  • lt/loansapplicationgt
  • lt/loansactionsgt
  • lt/rulegt

32
DSL Example
  • DEMO

33
Choosing to use a Rule Engine
  • Dont use a Rule Engine if
  • Problems are computationally intensive based on
    few well know rules
  • If your rules are not likely to change over
    time
  • You need a Rule Engine if
  • Need a clean separation of business policy from
    technology
  • Business constrains change rapidly

34
Choosing to use a Rule Engine
  • Procedural thinking execution path is predefined
  • If there is no exact recipe, ill defined problems
    dealing with search, discovery and evaluation
  • If it is hard to produce a flow chart or diagram
    then it is equally hard to produce a program
  • Rule based programming is the only technique to
    codify expertise

35
Choosing a Rule Engine
  • Authoring
  • Rule Language Support
  • Business people friendly
  • Management
  • Complex decision making need to be monitored
  • Deployment
  • Hot Deploy
  • Performance
  • Rule Caching
  • Pre-compilation
  • Standard Compliance

36
Questions?
37
Shameless Plug
38
Thank You
  • Please fill out the speaker evaluation
  • You can contact me further at bsbodden_at_integrall
    is.com

39
(No Transcript)
Write a Comment
User Comments (0)
About PowerShow.com