drools euos05

Uploaded from authorPOINTLite
Views:
 
Category: Entertainment
     
 

Presentation Description

No description available.

Comments

Presentation Transcript

Drools An Open Source Java Rules Engine: 

Brian Sam-Bodden Principal Partner Integrallis Software, LLC. October 17 - 20, 2005 Drools An Open Source Java Rules Engine

Contents: 

A simple problem – Recipe Finder Recipe Finder – Procedural Implementation Introducing Drools Recipe Finder – The Drools Way Contents

Contents: 

Rule Engine Applications Domain Specific Languages Integrating Drools Drools Loan Calculator Choosing to use a Rule Engine Choosing a Rule Engine Contents

Recipe Finder Application: 

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

Recipe Finder - Domain: 

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);

Recipe Finder - Domain: 

Recipe Finder - Domain Recipe Finder Domain Model

Recipe Finder - Procedural: 

Recipe Finder - Procedural Traditional Procedural Approach Loop over all recipes …check each recipe against available ingredients …save the matching recipes

Recipe Finder - Procedural: 

Recipe Finder - Procedural Matching Ingredients and Recipes for ( Recipe recipe : recipes ) { for ( Ingredient ingredient : ingredients ) { recipe.matchIngredient(ingredient); } if (recipe.getIsComplete()) { searchResults.addMatch(recipe); } }

Recipe Finder - Procedural: 

Recipe Finder - Procedural DEMO

Drools: 

Drools 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/

Drools – The Big Picture: 

Drools – The Big Picture

Recipe Finder - Drools: 

Recipe Finder - Drools 2 Rules IngredientMatch Rule Matches an ingredient to a recipe RecipeMatch Rule Collects recipes with all ingredients matched

Recipe Finder - Drools: 

Recipe Finder - Drools <rule name="IngredientMatch"> <parameter identifier="recipe"> <java:class>Recipe</java:class> </parameter> <parameter identifier="ingredient"> <java:class>Ingredient</java:class> </parameter> <java:condition> recipe.containsIngredient(ingredient) </java:condition> <java:consequence> if (recipe.matchIngredient(ingredient)) { drools.modifyObject(recipe); } </java:consequence> </rule>

Recipe Finder - Drools: 

Recipe Finder - Drools <rule name=“RecipeMatch"> <parameter identifier="recipe"> <java:class>Recipe</java:class> </parameter> <java:condition> recipe.getIsComplete() </java:condition> <java:consequence> searchResults.addMatch(recipe); drools.retractObject(recipe); </java:consequence> </rule>

Recipe Finder - Drools: 

Recipe Finder - Drools Rules are declarative Follow the pattern IF <condition> THEN <consequence> A rule firing (execution) can change the state of the WorkingMemory therefore possibly triggering other rules to fire

Recipe Finder – DRL File: 

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

Recipe Finder - Loading the Rules: 

Recipe 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();

Recipe Finder – Asserting Facts: 

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);

Recipe Finder – Application Data: 

Recipe Finder – Application Data // create Search Results object SearchResults searchResults = new SearchResults(); // set application data – Search Results workingMemory .setApplicationData("searchResults" ,searchResults);

Recipe Finder – Rule Firing: 

Recipe Finder – Rule Firing workingMemory.fireAllRules(); Output: Using drl: recipefinder.java.drl … You can make Rice and Beans You can make Ranchero Eggs

Recipe Finder - Drools: 

Recipe Finder - Drools DEMO

Rule Engines - Applications: 

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

Domain Specific Languages: 

Domain 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

Integrating Drools: 

Integrating 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)

Loan Calculator - DSL Example: 

Loan 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

Loan Calculator - DSL Example: 

Loan Calculator - DSL Example

Loan Calculator - DSL Example: 

Loan Calculator - DSL Example <rule name="XYZ-FicoScore"> <loans:condition> <loans:lender name="XYZ Mortage" /> <loans:application> <loans:fico> <loans:less-than>680</loans:less-than> </loans:fico> </loans:application> </loans:condition> <loans:actions> <loans:application name="messages"> <loans:add> Declined by XYZ Mortgage because a FICO score of at least 680 is required </loans:add> </loans:application> </loans:actions> </rule>

Loan Calculator - DSL Example: 

Loan 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

Loan Calculator - Code: 

Loan Calculator - Code // A Simple Drools-enabled SSB @Stateless public class LoanCalculatorServiceBean implements … // Inject RuleBase from JNDI @Resource(name = "java:/drools/LoansRuleBase") protected RuleBase ruleBase; public LoanApplication submitApplication(…) { //Create a Working Memory WorkingMemory workingMemory = ruleBase.newWorkingMemory();

Loan Calculator - Code: 

Loan Calculator - Code CODE

Loan Calculator - Demo: 

Loan Calculator - Demo DEMO

Choosing to use a Rule Engine: 

Choosing to use a Rule Engine Don’t 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

Choosing to use a Rule Engine: 

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

Choosing a Rule Engine: 

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

Questions: 

Questions

Shameless Plug ;-): 

Shameless Plug ;-)

Thank You!: 

Thank You! Please fill out the speaker evaluation You can contact me further at … bsbodden@integrallis.com

Stand by… for an important Announcement!: 

Stand by… for an important Announcement!