Monday, January 12, 2009

Lots and lots a rules.... why not script it?

You can do a lot with rules - often way more then any one person would need. This often brings up the case of when you should or shouldn't use rules (as in drools) opposed to some simple (if large) sequence of "if statements".

This is not something you can answer in general, but what can be shown is that using drools don't have to be any worse then doing something similar as a sequence of if statements in a scripting language (or probably even java) for a reasonable number of rules.

To try this out, I wrote a script to generate 5000 randomised rules, which were of the useless and contrived form: "when p: Person(age > 40, age < 42, name =...." and so on...

So no inferencing, no higher order logic and such, but a few propositions. Really just no more then what you would do with an if statement.

For about 5000 rules I would typically get a result in less than 1ms.
Generating it as lots of if statements, it took around 5 or 6ms in jruby (just simple if statements - but the same logic) - so certainly a scripting approach is fast enough for that sort of sequential case (well its a few times slower, but if you get a response in less than 5ms, thats always pretty good I think !). What you don't get with the scripting approach is inferencing, higher order logic and much more (I am sure others can think up some other advantages).

So in summary: the features are there when you want them, but in really you don't pay a runtime speed penalty to not use them (and in some cases can get a benefit).