Drools Expert User Guide

Mark Proctor

Michael Neale

Edson Tirelli

5.0.0.CR1


Table of Contents

1. Acknowledgements
Documentation
2. The Rule Engine
What is a Rule Engine?
Introduction and Background
Why use a Rule Engine?
Advantages of a Rule Engine
When should you use a Rule Engine?
When not to use a Rule Engine
Scripting or Process Engines
Strong and Loose Coupling
Knowledge Representation
First Order Logic
Rete Algorithm
The Drools Rule Engine
Overview
Authoring
RuleBase
WorkingMemory and Stateful/Stateless Sessions
StatefulSession
Stateless Session
Agenda
Truth Maintenance with Logical Objects
Event Model
???
3. Decision tables in spreadsheets
When to use Decision tables
Overview
How decision tables work
Keywords and syntax
Syntax of templates
Keywords
Creating and integrating Spreadsheet based Decision Tables
Managing business rules in decision tables.
Workflow and collaboration.
Using spreadsheet features
Rule Templates
A decision table-like example
4. The (Eclipse based) Rule IDE
Features outline
Creating a Rule project
Creating a new rule and wizards
Textual rule editor
Guided editor (rule GUI)
Views
The Working Memory View
The Agenda View
The Global Data View
The Audit View
Domain Specific Languages
Editing languages
The Rete View
Large drl files
Debugging rules
Creating breakpoints
Debugging rules
5. Deployment and Testing
Deployment options
Deployment using the RuleAgent
Deployment using drl source
Deploying rules in your classpath
Deployable objects, RuleBase, Package etc.
Deployment patterns
Web Services
Future considerations
Testing
Testing frameworks
FIT for Rules - a rule testing framework
6. The Java Rule Engine API
Introduction
How To Use
Building and Registering RuleExecutionSets
Using Stateful and Stateless RuleSessions
References
7. The Rule Language
Overview
A rule file
What makes a rule
Keywords
Comments
Single line comment
Multi line comment
Error Messages
Message format
Error Messages Description
Other Messages
Package
import
expander
global
Function
Type Declaration
Declaring New Types
Declaring Metadata
Declaring Metadata for Existing Types
Accessing Declared Types from the Application Code
Rule
Rule Attributes
Left Hand Side (when) Conditional Elements
The Right Hand Side (then)
A note on auto boxing/unboxing and primitive types
Query
Domain Specific Languages
When to use a DSL
Editing and managing a DSL
Using a DSL in your rules
Adding constraints to facts
How it works
Creating a DSL from scratch
Scope and keywords
DSLs in the BRMS and IDE
XML Rule Language
When to use XML
The XML format
Legacy Drools 2.x XML rule format
Automatic transforming between formats (XML and DRL)
8. Examples
Getting the examples
Hello World
State Example
Understanding the State Example
Fibonacci Example
Banking Tutorial
Pricing Rule Decision Table Example
Executing the example
The decision table
Pet Store Example
Honest Politician Example
Sudoku Example
Sudoku Overview
Running the Example
Java Source and Rules Overview
Sudoku Validator Rules (validatorSudoku.drl)
Sudoku Solving Rules (solverSudoku.drl)
Suggestions for Future Developments
Number Guess
Miss Manners and Benchmarking
Introduction
Indepth look
Output Summary
Conways Game Of Life Example
Index

List of Figures

2.1. High-level View of a Rule Engine
2.2. Forward Chaining
2.3. Backward Chaining
2.4. SQL as a simplistic Inference Engine
2.5. Rete Nodes
2.6. ObjectTypeNodes
2.7. AlphaNodes
2.8. JoinNode
2.9. Node Sharing
2.10. Authoring Components
2.11. Runtime Components
2.12. PackageBuilder
2.13. PackageBuilderConfiguration
2.14. JavaDialectConfiguration
2.15. MvelDialectConfiguration
2.16. RuleBaseFactory
2.17. RuleBase
2.18. RuleBaseConfiguration
2.19. WorkingMemory
2.20. StatefulSession
2.21. StatelessSession
2.22. StatelessSessionResult
2.23. Two Phase Execution
2.24. Two Phase Execution
2.25. AgendaFilters
2.26. Stated Insertion
2.27. Logical Insertion
2.28. WorkingMemoryEventListener
2.29. AgendaEventListener
2.30. RuEventListener
2.31. EventManager
4.1. Overview
4.2. New rule project scaffolding
4.3. New rule project result
4.4. The wizard menu
4.5. New rule wizard
4.6. The rule editor in action
4.7. The rule outline view
4.8. The guided editor
4.9. The Domain Specific Language editor
4.10. Language Mapping editor dialog
4.11. Debugging
4.12. Debug as Drools Application
4.13. Debug as Drools Application Configuration
4.14. Debugging
7.1. Single line comment
7.2. Multi line comment
7.3. Error Message Format
7.4. package
7.5. import
7.6. expander
7.7. global
7.8. function
7.9. rule
7.10. rule attributes
7.11. Left Hand Side
7.12. Pattern Entity Relationship Diagram
7.13. Pattern
7.14. Constraints
7.15. Constraint
7.16. Group Constraint
7.17. fieldConstraint
7.18. restriction
7.19. literal
7.20. qualifiedIdentifier
7.21. variable
7.22. returnValue
7.23. singleValueRestriction
7.24. Operators
7.25. literalRestriction
7.26. variableRestriction
7.27. returnValueRestriction
7.28. compoundValueRestriction
7.29. multiRestriction
7.30. restrictionGroup
7.31. Inline Eval Expression
7.32. prefixAnd
7.33. infixAnd
7.34. prefixOr
7.35. infixOr
7.36. eval
7.37. not
7.38. exists
7.39. forall
7.40. from
7.41. collect
7.42. accumulate
7.43. query
8.1. Hello World : fireAllRules Agenda View
8.2. Hello World : rule "Hello World" Agenda View
8.3. Hello World : Audit View
8.4. Salience State Example Audit View
8.5. State Example Agenda View
8.6. Fibonacci Example "Recurse" Audit View 1
8.7. Fibonacci Example "Recurse" Agenda View 1
8.8. Fibonacci Example "Recurse" Agenda View 2
8.9. Fibonacci Example "Bootstrap" Audit View 1
8.10. Decision table configuration
8.11. Base price calculation
8.12. Discount calculation
8.13. Figure 1 - PetStore Demo just after Launch
8.14. Figure 2 - PetStore Demo with Products Selected
8.15. Figure 3 - Do we want to buy a fish tank?
8.16. Figure 4 - Petstore Demo after all rules have fired.
8.17. Honest Politician Example Audit View
8.18. RuleFlow for the NumberGuess Example
8.19. Properties for the Number Guess RuleFlow
8.20. Edit Constraints for the GuessCorrect Node
8.21. Constraints Editor for the GuessCorrect Node / value too high
8.22. Miss Manners' Guests
8.23. Manners Activity Diagram
8.24. Rete Diagram
8.25. Conways Example : Starting a new game
8.26. Conways Example : A running game
8.27. Conways Example : ruleflow "Generation"

List of Tables

3.1. Keywords

List of Examples

2.1. Building a Package from Multiple Sources
2.2. Checking the PackageBuilder for errors
2.3. Configuring the JavaDialectConfiguration to use JANINO via a setter
2.4. Configuring the JavaDialectConfiguration to use JANINO
2.5. Configuring the PackageBuilder to use JANINO via a property
2.6. Adding a Package to a new RuleBase
2.7. Example: modify() block using Java dialect
2.8. Example: modify() block using MVEL dialect
2.9. Example: safely modifying attributes in the application code
2.10. Createing a StatefulSession
2.11. Createing a StatelessSession
2.12. GlobalExporter with StatelessSessions
3.1.
6.1. Automatic RuleServiceProvider Registration
6.2. Registering a LocalRuleExecutionSet with the RuleAdministration API
6.3. Specifying a DSL when registering a LocalRuleExecutionSet
6.4. Register the RuleExecutionSet
6.5. Getting the RuleRuntime
6.6. Stateful Rule
6.7. Stateless
7.1. Rules file
7.2.
7.3.
7.4.
7.5.
7.6.
7.7.
7.8.
7.9.
7.10. declaring a new fact type: Address
7.11. declaring a new fact type: Person
7.12. avoiding the need to use fully qualified class names by using import
7.13. generated Java class for the previous Person fact type declaration
7.14. using the declared types in rules
7.15. declaring an arbitrary metadata attribute
7.16. declaring metadata attributes for fact types and attributes
7.17. declaring metadata for an existing type
7.18. declaring metadata using the fully qualified class name
7.19. declaring a type in the org.drools.examples package
7.20. handling declared fact types through the API
7.21. Rule Syntax Overview Example
7.22. A rule example
7.23. Some attribute examples
7.24. Rule Syntax Overview Example
7.25. Pattern
7.26. Pattern
7.27. Constraint Group connective ','
7.28. && and || Constraint Connectives
7.29. Using parenthesis to change evaluation priority
7.30. Not Equivalent connectives
7.31. Regular Expression Constraint
7.32. Regular Expression Constraint
7.33. Contains with Collections
7.34. Literal Constraints with Collections
7.35. Literal Constraints with Collections
7.36. Literal Constraints with Collections
7.37. Text with soundslike (Sounds Like)
7.38. Numeric Literal Restriction
7.39. Date Literal Restriction
7.40. String Literal Restriction
7.41. Boolean Literal Restriction
7.42. Boolean Literal Restriction
7.43. Bound Field using '==' operator
7.44. Bound Fact using 'contains' operator
7.45. Return Value Restriction
7.46. Compound Restriction using 'in'
7.47. Multi Restriction
7.48. Return Value operator
7.49. Nested Accessors
7.50. prefixAnd
7.51. implicit root prefixAnd
7.52. infixAnd
7.53. prefixOr
7.54. infixAnd
7.55. or with binding
7.56. eval
7.57. No Busses
7.58. No red Busses
7.59. Atleast one Bus
7.60. Atleast one red Bus
7.61. Single Pattern Forall
7.62. Multi-Pattern Forall
7.63. Combining Forall with Not CE
7.64. Query People over the age of 30
7.65. Query People over the age of X, and who live in y
7.66. Query People over the age of 30
7.67. Example mapping
7.68. Example with quotes
7.69. Some more examples
7.70. Some examples as processed
7.71. Chaining DSL Expressions
7.72. Example
7.73. Detail of rule element
8.1. HelloWorld example: Creating the KnowledgeBase and Session
8.2. HelloWorld example: Event logging and Auditing
8.3. HelloWorld example: Message Class
8.4. HelloWorld example: Execution
8.5. HelloWorld example: Console.out
8.6. HelloWorld example: Console.err
8.7. HelloWorld example: rule "Hello World"
8.8. HelloWorld example: rule "Good Bye"
8.9. State Class
8.10. Salience State Example Execution
8.11. Salience State Example Console Output
8.12. Salience State Example: Rule "Bootstrap"
8.13. Salience State Example: Rule "A to B"
8.14. Salience State Example: Rule "B to C"
8.15. Salience State Example: Rule "B to D"
8.16. Inserting a Dynamic Fact
8.17. Setter Example with PropertyChangeSupport
8.18. Agenda Group State Example: Rule "B to C"
8.19. Agenda Group State Example: Rule "B to D"
8.20. Dynamic State Example: Rule "D to E"
8.21. Dynamic Sate Example Output
8.22. Fibonacci Class
8.23. Fibonacci Example Console Output
8.24. Fibonacci Example Execution
8.25. Fibonacci Example : Rule "Recurse"
8.26. Fibonacci Example : Rule "Bootstrap"
8.27. Fibonacci Example : Rule "Calculate"
8.28. Banking Tutorial : RuleRunner
8.29. Banking Tutorial : Java Example1
8.30. Banking Tutorial : Rule Example1
8.31. Banking Tutorial : Output Example1
8.32. Banking Tutorial : Java Example2
8.33. Banking Tutorial : Rule Example2
8.34. Banking Tutorial : Output Example2
8.35. Banking Tutorial : Java Example3
8.36. Banking Tutorial : Rule Example3
8.37. Banking Tutorial : Output Example3
8.38. Banking Tutoria : Class Cashflow
8.39. Banking Tutorial : Java Example4
8.40. Banking Tutorial : Java SimpleDate
8.41. Banking Tutorial : Rule Example4
8.42. Banking Tutorial : Output Example4
8.43. Banking Tutoria : Class TypedCashflow
8.44. Banking Tutorial : Java Example5
8.45. Banking Tutorial : Rule Example5
8.46. Banking Tutorial : Output Example5
8.47. Banking Tutoria : Class Account
8.48. Banking Tutoria : Class AllocatedCashflow
8.49. Banking Tutorial : Java Example5
8.50. Banking Tutorial : Rule Example6
8.51. Banking Tutorial : Output Example6
8.52. Creating the PetStore RuleBase - extract from PetStore.java main() method
8.53. Firing the Rules - extract from the CheckOutCallBack.checkout() method
8.54. Package, Imports , Globals and Dialect - extract (1) from PetStore.drl
8.55. Java Functions in the Rules - extract (2) from PetStore.drl
8.56. Putting each (individual) item into working memory - extract (3) from PetStore.drl
8.57. Show Items in the GUI extract (4) from PetStore.drl
8.58. Evaluate Agenda Group extract (5) from PetStore.drl
8.59. Doing the Checkout - extract (6) from PetStore.drl
8.60. Checkout Rules- extract (7) from PetStore.drl
8.61. Console (System.out) from running the PetStore GUI
8.62. Politician Class
8.63. Honest Politician Example Execution
8.64. Honest Politician Example Console Output
8.65. Honest Politician Example : Rule "We have an honest politician"
8.66. Honest Politician Example : Rule "Hope Lives"
8.67. Honest Politician Example : Rule "Corrupt the Honest"
8.68. Honest Politician Example : Rule "Hope is Dead"
8.69. Creating the Number Guess RuleBase - extract 1 from NumberGuessExample.java main() method
8.70. Starting the RuleFlow - extract 2 from NumberGuessExample.java main() method
8.71. A Rule that will fire only a specific point in the RuleFlow - extract from NumberGuess.drl
8.72. Example Console output where the Number Guess Example beat the human!
8.73. Conways Example : Register all Cell Neighbour relations
8.74. Conways Example : Evaluate Cells with state changes
8.75. Conways Example : Apply the state changes
8.76. Conways Example : Evaluate Cells with state changes