2008-10-09

When should I use a rule engine?

This is an interesting question that I think about quite a lot. I consider myself somewhat of a rule engine evangelist and I've always felt a bit annoyed that it's so difficult to explain the benefits of a rules based approach to others.

The textbook answer to the question is that a rule engine should be used when the problem is ill-structured and the solution is difficult or impractical to describe with an algorithm or when the knowledge required to formulate a solution changes frequently. But that's just words.

I've tried to find an ill-structured problem that is small enough to use as an example but I'm not really sure one exists. The best I've got so far is the problem of translating a number to text, where 0 <= number <= 99. It's an ill-structured problem that is easy to grasp but still translates to an ugly implementation with a lot of special-cases-handling in most programming languages. Not that it translates to a very pretty CLIPS application either... but I guess that comes with the territory.

Anyone got a better idea?

10 kommentarer:

woolfel sa...

there are no "good" small examples, or atleast I haven't found one. In all the examples I can think of, it boils down to what is the right choice for the given project. Even if rule engine is a good technical fit, it may be a poor fit for the project.

Johan Lindberg sa...

Yes, "buy-in" from the rest of the project team is crucial and that's actually the reason I'd like to find a good example that shows the strengths of the rules based approach.

I was hoping to find a problem where the solution in a rules based approach would be smaller and more elegant. Unfortunately, solutions to ill-structured problems look as terrible in CLIPS as in Python or Java. The real difference is that it's easier to make changes to and extend the CLIPS version. But since most people think CLIPS syntax *looks* weird they don't believe that and would choose the Python or Java approach regardless of the extra time.

This was actually one of the major reasons that I chose the syntax I did for PyRete. I wanted rules to blend in with the rest of the code as much as possible.

Emily Bache sa...

Have you thought about card games? The rules of Poker are pretty ill structured. There is a Kata called TexasHoldEm (http://www.codingdojo.org/cgi-bin/wiki.pl?KataTexasHoldEm) I'd be interested to see a solution that states the rules of poker in a readable manner. btw this is one of the harder Katas I've tried, and I'm not happy with any solution I've come up with myself.

woolfel sa...

One example I've used in the past is from video game agents. Trying to code all the possible branches procedurally becomes a huge headache. It's more elegant to have meta rules and use data driven techniques. This way, one set of rules guide the development/learning of in-game non-player characters. The game alters the dataset for each non-player character and allows them to behave and evolve separately.

Johan Lindberg sa...

That's a great idea Emily. Thanks for the tip.

I especially like the last paragraph on that page:

Another interesting aspect which you may or may not want to go into, is how you express the rules of Poker. These rules are both repetitive and arbitrary and in many ways resemble the kinds of business rules you meet in the real world. Can you find a way of expressing them that will leave an expert poker player able to verify that they are implemented correctly, even if they are not themselves a programmer?

Johan Lindberg sa...

Peter, I guess the "only" problem is that autonomous AI players in video games are a huge effort ;-)

fogus sa...

I think that the wine.clp example included with CLIPS is a great example of a small application, illustrating perfectly the benefits of a rule engine. ~370 lines, including comments and a small set of facts.
-m

woolfel sa...

haha.. yeah they are challenging to implement. :( it's hard to come up with a simple example that shows the benefits.

Johan Lindberg sa...

Hi Mike,

you're right. The Wine Demo is a good, but still quite small, example. It even manages to include a DSL (sort of) and high-level control constructs such as modules and focus.

woolfel sa...

jess 6 used to have animal example that is similar to the wine example. in jess 7.1 it's not included anymore.