Title: Drools An Open Source Java Rules Engine
1DroolsAn Open Source Java Rules Engine
Brian Sam-Bodden Principal Partner Integrallis
Software, LLC.
October 17 - 20, 2005
2Contents
- A simple problem Recipe Finder
- Recipe Finder Procedural Implementation
- Introducing Drools
- Recipe Finder The Drools Way
3Contents
- Rule Engine Applications
- Domain Specific Languages
- Integrating Drools
- Drools Loan Calculator
- Choosing to use a Rule Engine
- Choosing a Rule Engine
4Recipe Finder Application
- Problem Given a list of Ingredients and Recipes
determine which recipes can be prepared
5Recipe 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)
6Recipe Finder - Domain
- Recipe Finder Domain Model
7Recipe Finder - Procedural
- Traditional Procedural Approach
- Loop over all recipes
- check each recipe against available ingredients
- save the matching recipes
8Recipe Finder - Procedural
- Matching Ingredients and Recipes
- for ( Recipe recipe recipes )
- for ( Ingredient ingredient
ingredients ) - recipe.matchIngredient(ingredient)
-
- if (recipe.getIsComplete())
- searchResults.addMatch(recipe)
-
-
9Recipe Finder - Procedural
DEMO
10Drools
- Forward Chaining Inference Engine
- Based on Rete algorithm
- Supports several languages for expressing Rules
- Created by Bob McWhirter
- Hosted at Codehaus
- Open source, BSD Style License
- Simple API (few classes to master)
- http//drools.org/
11Drools The Big Picture
12Recipe Finder - Drools
- 2 Rules
- IngredientMatch Rule
- Matches an ingredient to a recipe
- RecipeMatch Rule
- Collects recipes with all ingredients matched
13Recipe Finder - Drools
identifier"recipe" Recipeaclass identifier"ingredient"
Ingredient
recipe.containsIngredient(ingredient)
if (recipe.matchIngredient(ingredient))
drools.modifyObject(recipe)
ule
14Recipe Finder - Drools
identifier"recipe" Recipeaclass
recipe.getIsComplete()
searchResults.addMatch(recipe)
drools.retractObject(recipe)
15Recipe Finder - Drools
- Rules are declarative
- Follow the pattern
- IF THEN
- A rule firing (execution) can change the state of
the WorkingMemory therefore possibly triggering
other rules to fire
16Recipe Finder DRL File
- DRL Files are XML
- Contain one or more Rules
- Several Semantic Modules Available
- Java
- Groovy
- Python
17Recipe Finder - Loading the Rules
// URL for 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()
18Recipe Finder Asserting Facts
// assert facts - ingredients workingMemory.assert
Object(rice) workingMemory.assertObject(chocolate
Cake) workingMemory.assertObject(eggs) //
assert facts - recipes workingMemory.assertObject(
riceAndBeans) workingMemory.assertObject(chocolat
eCake) workingMemory.assertObject(pancakes) work
ingMemory.assertObject(rancheroEggs)
19Recipe Finder Application Data
// create Search Results object SearchResults
searchResults new SearchResults() // set
application data Search Results workingMemory
.setApplicationData("searchResults"
,searchResults)
20Recipe Finder Rule Firing
workingMemory.fireAllRules() Output Using drl
recipefinder.java.drl You can make Rice and
Beans You can make Ranchero Eggs
21Recipe Finder - Drools
DEMO
22Rule Engines - Applications
- Knowledge Discovery
- Path Optimization
- Insurance Fraud Detection
- Email Spam Filters
- Retail, Holiday Promotions
- Credit Scoring
23Domain Specific Languages
- Drools supports Domain Specific Languages
- High-level abstract programming languages used to
increase productivity - Uses XML W3C Schema be define and represent
business rules - Enable sources closer to the knowledge to express
that knowledge
24Integrating Drools
- In J2SE
- Use native Drools APIs
- Use JSR-94 Integration
- Spring Pojo-Driven Rules
- In J2EE
- Stateless Session Beans
- JNDI
- MBeans for Management
- RuleSet Storage
- File System or Database (DIY)
25Loan Calculator - DSL Example
- Problem
- Application for Mortgage Origination
- Need for business people to easily author rules
- Solution
- Web based application
- DSL for Loan Officers to enter Rules
26Loan Calculator - DSL Example
27Loan Calculator - DSL Example
680
Declined by XYZ Mortgage because
a FICO score of at least 680 is required
28Loan Calculator - DSL Example
- Steps
- Define XSD loans.xsd
- Implement
- org.drools.smf.ConditionFactory
- org.drools.smf.ConsequenceFactory
- Integrate into JBoss-based application
- Web UI
- Stateless Session Bean Services
- MBeans for Management
- JBoss Drools Deployer
29Loan Calculator - Code
// A Simple Drools-enabled SSB _at_Stateless public
class LoanCalculatorServiceBean implements
// Inject RuleBase from JNDI _at_Resource(name
"java/drools/LoansRuleBase") protected RuleBase
ruleBase public LoanApplication
submitApplication() //Create a Working
Memory WorkingMemory workingMemory
ruleBase.newWorkingMemory()
30Loan Calculator - Code
CODE
31Loan Calculator - Demo
DEMO
32Choosing to use a Rule Engine
- Dont use a Rule Engine if
- Problems are computationally intensive and based
on few well known 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 - Large number of business rules that are
constantly changing
33Choosing 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
34Choosing 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
35Questions
36Shameless Plug -)
37Thank You!
Please fill out the speaker evaluation You can
contact me further at bsbodden_at_integrallis.com
38Stand byfor an important Announcement!
39(No Transcript)
40(No Transcript)
41(No Transcript)