Development, Design, Architecture
How to develop systems with rule engines involved?
My rules don't work as expected. What can I do?
Welcome to real life. Don't ever believe that rules can and will be written by business-people only.
I suggest the following tactics:
-
Try to modularize the problem. Start with a smaller ruleset or fewer facts.
-
Add output to the consequence-parts of the rules - a few
System.outwon't hurt during development time. -
Does working-memory contain the correct facts?
-
Simple version: Create a new rule to output important facts (catch them on the LHS, print them on RHS). An Example:
rule "debug: watch for my facts" when f: MyCrucialFact() then System.out.println( f.toString() ); endBe careful with too specific LHS: If any fact you state in LHS is not present, the rule won't fire.
-
Before calling
fireAllRules(), retrieve ALL existing facts from working-memory.for ( Iterator it = session.iterateObjects(); it.hasNext(); ) { System.out.println( it.next() ); } session.fireAllRules(); -
You can
querythe working memory with a query-rule of the following format:query "MyCrucialFacts" f : MyCrucialFact( ) endYou can get the results with the following lines of java code:
QueryResults results = workingMemory.getQueryResults( "MyCrucialFacts" ); System.out.println( "found " + results.size() + " MyCrucialFacts" ); for ( Iterator it = results.iterator; it.hasNext(); ) { QueryResult result = ( QueryResult ) it.next(); MyCrucialFact mf = (MyCrucialFacts) result.get( "f"); System.out.println( mf.toString() ); }
-
-
Write a JUnit-test for every rule. Create the facts specifically to the rules' LHS (conditions), assert on the consequences.
-
Try adding a
DebugEventListenerto your working memory (that's JBoss-Rules specific). Output is hard to read! -
Add some breakpoints to the consequence-part of suspicious rules and debug your application. You need to add a new Debug-configuration in Eclipse - consult the JBoss-Rules documentation for details. A sample screenshot from JBoss-Drools-IDE:

How do I incorporate rule engines into my architecture?
A long version here - you'll need to adapt it.
The short version:
- Hide away all the rule-engine specific stuff
behind a wrapper (I prefer to call this one
RuleInvokerorRuleEngineWrapper). - Have one component add business objects to this
RuleEngineWrapper, call the rule engine and propagate its results back to your application. - Construct a proper testing facility. It is difficult to test rules with pure unit tests, so you gotta invest some brain here. Start with simple unit tests.
- Care for operations: Integrate logging facilities
into your
RuleEngineWrapper- many rule engines support you with clever strategies. - Take special care for an effective rule
governance: Somebody within your organization
has to care for rule management:
- When to introduce new rules?
- When to delete rules from the rule base?
- How to modularize your rule base?

