![Page 1: Practical Rule Engines Or How I Learned to Stop Worrying and Set Up Us the Bomb](https://reader036.vdocument.in/reader036/viewer/2022062422/56812d0a550346895d91dd92/html5/thumbnails/1.jpg)
Practical Rule EnginesOr
How I Learned to Stop Worrying and Set Up Us the
Bomb
Thomas Meeks
This might not make sense without the corresponding talk.
![Page 2: Practical Rule Engines Or How I Learned to Stop Worrying and Set Up Us the Bomb](https://reader036.vdocument.in/reader036/viewer/2022062422/56812d0a550346895d91dd92/html5/thumbnails/2.jpg)
What is a rule engine?
A system that attempts to act as a domain expert
![Page 3: Practical Rule Engines Or How I Learned to Stop Worrying and Set Up Us the Bomb](https://reader036.vdocument.in/reader036/viewer/2022062422/56812d0a550346895d91dd92/html5/thumbnails/3.jpg)
![Page 4: Practical Rule Engines Or How I Learned to Stop Worrying and Set Up Us the Bomb](https://reader036.vdocument.in/reader036/viewer/2022062422/56812d0a550346895d91dd92/html5/thumbnails/4.jpg)
![Page 5: Practical Rule Engines Or How I Learned to Stop Worrying and Set Up Us the Bomb](https://reader036.vdocument.in/reader036/viewer/2022062422/56812d0a550346895d91dd92/html5/thumbnails/5.jpg)
So lets climb the Peak of Inflated Expectations
•Helps simplify complicated logic
•Lowers the cost of changing business logic
•Usually very fast
•Basically a business logic framework
![Page 6: Practical Rule Engines Or How I Learned to Stop Worrying and Set Up Us the Bomb](https://reader036.vdocument.in/reader036/viewer/2022062422/56812d0a550346895d91dd92/html5/thumbnails/6.jpg)
Wait, did you say... framework?
![Page 7: Practical Rule Engines Or How I Learned to Stop Worrying and Set Up Us the Bomb](https://reader036.vdocument.in/reader036/viewer/2022062422/56812d0a550346895d91dd92/html5/thumbnails/7.jpg)
For my business logic?
![Page 8: Practical Rule Engines Or How I Learned to Stop Worrying and Set Up Us the Bomb](https://reader036.vdocument.in/reader036/viewer/2022062422/56812d0a550346895d91dd92/html5/thumbnails/8.jpg)
So I have a framework for:
• My UI (wicket, JSF, tapestry)
• My Database (hibernate, iBatis)
• Logging (commons logging)
• Compilation (ant, maven)
I even have a framework to tie together all my frameworks!!!! (spring, guice)
![Page 9: Practical Rule Engines Or How I Learned to Stop Worrying and Set Up Us the Bomb](https://reader036.vdocument.in/reader036/viewer/2022062422/56812d0a550346895d91dd92/html5/thumbnails/9.jpg)
And now you want me to use another framework... For my business logic!?
![Page 10: Practical Rule Engines Or How I Learned to Stop Worrying and Set Up Us the Bomb](https://reader036.vdocument.in/reader036/viewer/2022062422/56812d0a550346895d91dd92/html5/thumbnails/10.jpg)
![Page 11: Practical Rule Engines Or How I Learned to Stop Worrying and Set Up Us the Bomb](https://reader036.vdocument.in/reader036/viewer/2022062422/56812d0a550346895d91dd92/html5/thumbnails/11.jpg)
It's a hammer, not a bullet
Use rule engines when it makes sense, or you will hate them
I feel your pain (really), but...
![Page 12: Practical Rule Engines Or How I Learned to Stop Worrying and Set Up Us the Bomb](https://reader036.vdocument.in/reader036/viewer/2022062422/56812d0a550346895d91dd92/html5/thumbnails/12.jpg)
When should I use a Rule Engine?
•Complicated logic (not 1+1 = 2)
•Changes often (whatever that means)
•Traditional approaches are unmaintainable
![Page 13: Practical Rule Engines Or How I Learned to Stop Worrying and Set Up Us the Bomb](https://reader036.vdocument.in/reader036/viewer/2022062422/56812d0a550346895d91dd92/html5/thumbnails/13.jpg)
![Page 14: Practical Rule Engines Or How I Learned to Stop Worrying and Set Up Us the Bomb](https://reader036.vdocument.in/reader036/viewer/2022062422/56812d0a550346895d91dd92/html5/thumbnails/14.jpg)
![Page 15: Practical Rule Engines Or How I Learned to Stop Worrying and Set Up Us the Bomb](https://reader036.vdocument.in/reader036/viewer/2022062422/56812d0a550346895d91dd92/html5/thumbnails/15.jpg)
Don't use for a shopping cart
•Don't use on applications with simple business logic
•If the logic is complicated but will never (ever) change, might not need it.
•Easy logic, changes often?
•Scripting
![Page 16: Practical Rule Engines Or How I Learned to Stop Worrying and Set Up Us the Bomb](https://reader036.vdocument.in/reader036/viewer/2022062422/56812d0a550346895d91dd92/html5/thumbnails/16.jpg)
![Page 17: Practical Rule Engines Or How I Learned to Stop Worrying and Set Up Us the Bomb](https://reader036.vdocument.in/reader036/viewer/2022062422/56812d0a550346895d91dd92/html5/thumbnails/17.jpg)
Didn't you say something about speed?
![Page 18: Practical Rule Engines Or How I Learned to Stop Worrying and Set Up Us the Bomb](https://reader036.vdocument.in/reader036/viewer/2022062422/56812d0a550346895d91dd92/html5/thumbnails/18.jpg)
Rete
• Latin for “net”
• As in network, or graph
• Performance is theoretically independent of the number of rules in the system
• Here's a quick overview
![Page 19: Practical Rule Engines Or How I Learned to Stop Worrying and Set Up Us the Bomb](https://reader036.vdocument.in/reader036/viewer/2022062422/56812d0a550346895d91dd92/html5/thumbnails/19.jpg)
![Page 20: Practical Rule Engines Or How I Learned to Stop Worrying and Set Up Us the Bomb](https://reader036.vdocument.in/reader036/viewer/2022062422/56812d0a550346895d91dd92/html5/thumbnails/20.jpg)
Example
![Page 21: Practical Rule Engines Or How I Learned to Stop Worrying and Set Up Us the Bomb](https://reader036.vdocument.in/reader036/viewer/2022062422/56812d0a550346895d91dd92/html5/thumbnails/21.jpg)
![Page 22: Practical Rule Engines Or How I Learned to Stop Worrying and Set Up Us the Bomb](https://reader036.vdocument.in/reader036/viewer/2022062422/56812d0a550346895d91dd92/html5/thumbnails/22.jpg)
![Page 23: Practical Rule Engines Or How I Learned to Stop Worrying and Set Up Us the Bomb](https://reader036.vdocument.in/reader036/viewer/2022062422/56812d0a550346895d91dd92/html5/thumbnails/23.jpg)
Captn' the Domain Expert
• Arr, Take off every ZIG!!! (For great justice! Grr.)
![Page 24: Practical Rule Engines Or How I Learned to Stop Worrying and Set Up Us the Bomb](https://reader036.vdocument.in/reader036/viewer/2022062422/56812d0a550346895d91dd92/html5/thumbnails/24.jpg)
if (us.somebodySetUsUpTheBomb()) { for (Zig zig : us.getZigs()) { zig.launch(); // for justice! }}
![Page 25: Practical Rule Engines Or How I Learned to Stop Worrying and Set Up Us the Bomb](https://reader036.vdocument.in/reader036/viewer/2022062422/56812d0a550346895d91dd92/html5/thumbnails/25.jpg)
Sally the Mechanic
• Zigs should not launch without their weapon system...
![Page 26: Practical Rule Engines Or How I Learned to Stop Worrying and Set Up Us the Bomb](https://reader036.vdocument.in/reader036/viewer/2022062422/56812d0a550346895d91dd92/html5/thumbnails/26.jpg)
Which, of course, are...
Atomic Kittens!
![Page 27: Practical Rule Engines Or How I Learned to Stop Worrying and Set Up Us the Bomb](https://reader036.vdocument.in/reader036/viewer/2022062422/56812d0a550346895d91dd92/html5/thumbnails/27.jpg)
if (us.somebodySetUsUpTheBomb()) { for (Zig zig : us.getZigs()) { if (zig.hasAtomicKitty()) { zig.launch();
} }}
![Page 28: Practical Rule Engines Or How I Learned to Stop Worrying and Set Up Us the Bomb](https://reader036.vdocument.in/reader036/viewer/2022062422/56812d0a550346895d91dd92/html5/thumbnails/28.jpg)
Johnny the Regulator
• Zigs must have been inspected in the last 24 hours to launch!
![Page 29: Practical Rule Engines Or How I Learned to Stop Worrying and Set Up Us the Bomb](https://reader036.vdocument.in/reader036/viewer/2022062422/56812d0a550346895d91dd92/html5/thumbnails/29.jpg)
if (us.somebodySetUsUpTheBomb()) { for (Zig zig : us.getZigs()) { if (zig.hasAtomicKitty() && zig.isInspected()) { zig.launch();
} }}
![Page 30: Practical Rule Engines Or How I Learned to Stop Worrying and Set Up Us the Bomb](https://reader036.vdocument.in/reader036/viewer/2022062422/56812d0a550346895d91dd92/html5/thumbnails/30.jpg)
Johnny the Regulator
• Oh, and rule 23.43-57a#32.57 explicitly states that no more than 10 Zigs can fly at a time!
![Page 31: Practical Rule Engines Or How I Learned to Stop Worrying and Set Up Us the Bomb](https://reader036.vdocument.in/reader036/viewer/2022062422/56812d0a550346895d91dd92/html5/thumbnails/31.jpg)
if (us.somebodySetUsUpTheBomb()) { int i = 0; for (Zig zig : us.getZigs()) { if (i == 10) { break; } if (zig.hasAtomicKitty() && zig.inspected()) { zig.launch(); i++; } } }
![Page 32: Practical Rule Engines Or How I Learned to Stop Worrying and Set Up Us the Bomb](https://reader036.vdocument.in/reader036/viewer/2022062422/56812d0a550346895d91dd92/html5/thumbnails/32.jpg)
Captn' the Domain Expert
• Arr! Only 10!?
• If a Zig be shot down, launch more!
• Or ye be walkin' the plank!
![Page 33: Practical Rule Engines Or How I Learned to Stop Worrying and Set Up Us the Bomb](https://reader036.vdocument.in/reader036/viewer/2022062422/56812d0a550346895d91dd92/html5/thumbnails/33.jpg)
Hmmm....
• We could keep checking every Zig's status in an infinite loop.
• We could implement the observer pattern on Zigs (when they explode, they tell someone).
• etc...
• Lets stick with the loop for the example
![Page 34: Practical Rule Engines Or How I Learned to Stop Worrying and Set Up Us the Bomb](https://reader036.vdocument.in/reader036/viewer/2022062422/56812d0a550346895d91dd92/html5/thumbnails/34.jpg)
int i = 0;while (us.somebodySetUsUpTheBomb()) { for (Zig zig : us.getZigs()) { if (zig.hasExploded()) { us.getZigs().remove(zig); i--; continue; } if (zig.hasAtomicKitty() && zig.inspected() && i < 10) { zig.launch(); i++; } } }
![Page 35: Practical Rule Engines Or How I Learned to Stop Worrying and Set Up Us the Bomb](https://reader036.vdocument.in/reader036/viewer/2022062422/56812d0a550346895d91dd92/html5/thumbnails/35.jpg)
Sally the Mechanic
• If those Zigs get beat up, they should land so I can fix them!
• And don't try to launch them while I'm working on them!
![Page 36: Practical Rule Engines Or How I Learned to Stop Worrying and Set Up Us the Bomb](https://reader036.vdocument.in/reader036/viewer/2022062422/56812d0a550346895d91dd92/html5/thumbnails/36.jpg)
int i = 0;while (somebodySetUsUpTheBomb()) { for (Zig zig : us.getZigs()) { if (zig.needsMaintenance()) { zig.land(); mechanic.startFixing(zig); i--; continue; } if (zig.hasExploded()) { us.getZigs().remove(zig); i--; continue; } if (zig.hasAtomicKitty() && zig.inspected() && i < 10 && !zig.inMaintenance()) { zig.launch(); i++; } } }
![Page 37: Practical Rule Engines Or How I Learned to Stop Worrying and Set Up Us the Bomb](https://reader036.vdocument.in/reader036/viewer/2022062422/56812d0a550346895d91dd92/html5/thumbnails/37.jpg)
Johnny the Regulator
• I forgot to mention that rule 23.43-57a#32.57a explicitly states that all Zigs can fly if you fax form 453.438-347#B in triplicate
![Page 38: Practical Rule Engines Or How I Learned to Stop Worrying and Set Up Us the Bomb](https://reader036.vdocument.in/reader036/viewer/2022062422/56812d0a550346895d91dd92/html5/thumbnails/38.jpg)
Captn' the Domain Expert
• Arr! That form takes hours to fill!
• Arr! Launch 10 until we fax it, then Take off every Zig! (for great justice, grr.)
![Page 39: Practical Rule Engines Or How I Learned to Stop Worrying and Set Up Us the Bomb](https://reader036.vdocument.in/reader036/viewer/2022062422/56812d0a550346895d91dd92/html5/thumbnails/39.jpg)
int i = 0;while (somebodySetUsUpTheBomb()) { form.asyncFillOutAndFax();
for (Zig zig : us.getZigs()) { if (zig.needsMaintenance()) { zig.land(); mechanic.startFixing(zig); i--; continue; } if (zig.hasExploded()) { us.getZigs().remove(zig); i--; continue; } if (zig.hasAtomicKitty() && zig.inspected() && (i < 10 || form.isFaxed()) && !zig.inMaintenance()) { zig.launch(); i++; } } }
![Page 40: Practical Rule Engines Or How I Learned to Stop Worrying and Set Up Us the Bomb](https://reader036.vdocument.in/reader036/viewer/2022062422/56812d0a550346895d91dd92/html5/thumbnails/40.jpg)
Johnny the Regulator
• We just changed the rules!
• All Zigs must be pink to fly
![Page 41: Practical Rule Engines Or How I Learned to Stop Worrying and Set Up Us the Bomb](https://reader036.vdocument.in/reader036/viewer/2022062422/56812d0a550346895d91dd92/html5/thumbnails/41.jpg)
Sally the Mechanic
• Paint them pink!? That will take months! We have thousands!
![Page 42: Practical Rule Engines Or How I Learned to Stop Worrying and Set Up Us the Bomb](https://reader036.vdocument.in/reader036/viewer/2022062422/56812d0a550346895d91dd92/html5/thumbnails/42.jpg)
Captn' the Domain Expert
• Arr! Take off all pink Zigs!
• If we finish painting a Zig, launch it!
![Page 43: Practical Rule Engines Or How I Learned to Stop Worrying and Set Up Us the Bomb](https://reader036.vdocument.in/reader036/viewer/2022062422/56812d0a550346895d91dd92/html5/thumbnails/43.jpg)
This is getting complicated!
• Thousands of Zigs? That loop could take a while.
• A lot of event-driven logic
• Looks like it is going to end up really messy
• No, the regulator will not stop
![Page 44: Practical Rule Engines Or How I Learned to Stop Worrying and Set Up Us the Bomb](https://reader036.vdocument.in/reader036/viewer/2022062422/56812d0a550346895d91dd92/html5/thumbnails/44.jpg)
Wait a second...
• You might be thinking – that was awfully arbitrary!
• Or – Hey, I can make up stupid requirements that are hard to implement in a huge loop cleanly too!
• I must assume you aren't in a regulated industry...
![Page 45: Practical Rule Engines Or How I Learned to Stop Worrying and Set Up Us the Bomb](https://reader036.vdocument.in/reader036/viewer/2022062422/56812d0a550346895d91dd92/html5/thumbnails/45.jpg)
So Lets Take a Look at Rules
• Specifically Drools / Jboss Rules
• It is a Domain Specific Language (DSL) that wraps plain Java
• Plenty of other implementations in other languages.
• Not necessarily more concise
![Page 46: Practical Rule Engines Or How I Learned to Stop Worrying and Set Up Us the Bomb](https://reader036.vdocument.in/reader036/viewer/2022062422/56812d0a550346895d91dd92/html5/thumbnails/46.jpg)
rule “take off every zig for great justice” no-loop true; when Us(somebodySetUpUsTheBomb == true) zig : Zig(inspected == true, pink == true, atomicKitten == true, inMaintenance == false, launched == false) launched : launched(launched < 10 || formFaxed == true) then zig.launch(); launched.increment(); update(zig); update(launched);end
![Page 47: Practical Rule Engines Or How I Learned to Stop Worrying and Set Up Us the Bomb](https://reader036.vdocument.in/reader036/viewer/2022062422/56812d0a550346895d91dd92/html5/thumbnails/47.jpg)
rule “zig explodes” no-loop true when zig : Zig(destroyed == true) launched : Launched() then retract(zig) launched.decrement(); update(launched);end
![Page 48: Practical Rule Engines Or How I Learned to Stop Worrying and Set Up Us the Bomb](https://reader036.vdocument.in/reader036/viewer/2022062422/56812d0a550346895d91dd92/html5/thumbnails/48.jpg)
rule “repair zig” no-loop true when zig : Zig(needsMaintenance == true) mechanic : Mechanic() launched : Launched() then zig.land(); launched.decrement(); mechanic.startFixing(zig); //update zig when done update(zig);end
![Page 49: Practical Rule Engines Or How I Learned to Stop Worrying and Set Up Us the Bomb](https://reader036.vdocument.in/reader036/viewer/2022062422/56812d0a550346895d91dd92/html5/thumbnails/49.jpg)
Where's the loop?
• It is implied
• Each rule fires for each fact combination that matches
• If you assert 1000 Zigs, the first rule will be checked 1000 times.
![Page 50: Practical Rule Engines Or How I Learned to Stop Worrying and Set Up Us the Bomb](https://reader036.vdocument.in/reader036/viewer/2022062422/56812d0a550346895d91dd92/html5/thumbnails/50.jpg)
This is kinda confusing
• Think about it like an event-driven system
• It will re-schedule rules when insert(), update(), or retract() is called
![Page 51: Practical Rule Engines Or How I Learned to Stop Worrying and Set Up Us the Bomb](https://reader036.vdocument.in/reader036/viewer/2022062422/56812d0a550346895d91dd92/html5/thumbnails/51.jpg)
Neato Spandito
• New rules can be gracefully inserted into a large ruleset
•Will automatically fire when conditions are met
• Caching facts makes execution very fast
• Properly managed rules can create very dynamic systems
![Page 52: Practical Rule Engines Or How I Learned to Stop Worrying and Set Up Us the Bomb](https://reader036.vdocument.in/reader036/viewer/2022062422/56812d0a550346895d91dd92/html5/thumbnails/52.jpg)
Now Lets Descend into Disillusionment
• Easy to overuse rules
• Not all logic (even in complex systems) should be in rules
•Notice that I don't have a rule describing how a Zig launches
• Bugs can be evil
![Page 53: Practical Rule Engines Or How I Learned to Stop Worrying and Set Up Us the Bomb](https://reader036.vdocument.in/reader036/viewer/2022062422/56812d0a550346895d91dd92/html5/thumbnails/53.jpg)
Evil Bugs?
• Usually in a DSL – IDE's are not as mature
• Rules are recursive & loosely coupled by nature
• Rules usually compiled at runtime
•But not every time they are executed
![Page 54: Practical Rule Engines Or How I Learned to Stop Worrying and Set Up Us the Bomb](https://reader036.vdocument.in/reader036/viewer/2022062422/56812d0a550346895d91dd92/html5/thumbnails/54.jpg)
Java Bug
![Page 55: Practical Rule Engines Or How I Learned to Stop Worrying and Set Up Us the Bomb](https://reader036.vdocument.in/reader036/viewer/2022062422/56812d0a550346895d91dd92/html5/thumbnails/55.jpg)
C / C++ Bug
![Page 56: Practical Rule Engines Or How I Learned to Stop Worrying and Set Up Us the Bomb](https://reader036.vdocument.in/reader036/viewer/2022062422/56812d0a550346895d91dd92/html5/thumbnails/56.jpg)
Drools Bug
![Page 57: Practical Rule Engines Or How I Learned to Stop Worrying and Set Up Us the Bomb](https://reader036.vdocument.in/reader036/viewer/2022062422/56812d0a550346895d91dd92/html5/thumbnails/57.jpg)
Onwards! To Enlightenment!
• Don't avoid adding fact classes
•Trying to avoid it leads to messy rules
• Let the rule engine deal with the when, rather than the how
•Orchestrate business logic
•Work with your domain model as if it were a DSL itself
![Page 58: Practical Rule Engines Or How I Learned to Stop Worrying and Set Up Us the Bomb](https://reader036.vdocument.in/reader036/viewer/2022062422/56812d0a550346895d91dd92/html5/thumbnails/58.jpg)
Onwards! To Enlightenment!
• Assert object's, don't walk graphs
• trade.getAccount().getClient() is usually bad
•Helps as you add more rules
• Read!
•A bunch of links are on www.thomasmeeks.com
![Page 59: Practical Rule Engines Or How I Learned to Stop Worrying and Set Up Us the Bomb](https://reader036.vdocument.in/reader036/viewer/2022062422/56812d0a550346895d91dd92/html5/thumbnails/59.jpg)
A quick aside on choosing a rule engine...
• Avoid XML DSL's
• It needs to have a NOT conditional (e.g. Operate on the non-existance of a fact)
• Don't trust claims of graphical programming
• It is neat, but not a bullet
• Non-programmers probably shouldn't write the final rules in the rule engine
![Page 60: Practical Rule Engines Or How I Learned to Stop Worrying and Set Up Us the Bomb](https://reader036.vdocument.in/reader036/viewer/2022062422/56812d0a550346895d91dd92/html5/thumbnails/60.jpg)
Questions?
![Page 61: Practical Rule Engines Or How I Learned to Stop Worrying and Set Up Us the Bomb](https://reader036.vdocument.in/reader036/viewer/2022062422/56812d0a550346895d91dd92/html5/thumbnails/61.jpg)
Thanks!