Rule Engine – Drools

Leave a comment

December 26, 2010 by huionn

Last few weeks, I was doing a location-based service project using a rule engine. Initially, I thought applying rule engine will be hard and challenging.

Surprisingly, the rule syntax is very simple and precise. Therefore its learning curve is not as steep as expected.

However, when I wanted to apply what I have learnt into writing some simple rules, I got stuck immediately. Then I only realize the difficulty in writing rules is not about its syntax but paradigm shift.

For example, the common programming paradigms includes procedural programming and object-oriented programming. When a C programmer start programming in C++, the difficulty is not about C++ syntax but thinking in terms of objects instead of procedures.

Method Rule
Methods are called directly. Rules can never be called directly.
Specific instances are passed. Specific instances cannot be passed to a rule.
Rules execute by matching against any data as long it is inserted into the engine.
One call results in a single execution. Depending on the matches, a rule may fire once or several times, or not at all.
Order of method execution is deterministic Order of Activation execution is arbitrary without additional rule attribute Salience/Priority or flow construct

One of the problem I encountered is to find the shortest distance between a person and a location. I solved this using accumulate function. I thought this is a good solution as I saw there are MaxAccumulateFunction and MinAccumulateFunction implementations. So I wrote a class which implements AccumulateFunction. It requires non-trivial effort and I felt good about it.

$min : Movement(context != null) from accumulate($m : Movement() from entry-point MovementStream, minimumDistance($m))

After I completed the project and submitted it, now I have the time to re-learn Drools and retrospect my own work. Actually, there is a simpler solution to the problem.

$min : Movement()
not Movement(distance < $min.distance)

‘not’ here means there is no distance smaller than $min.distance.

In conclusion, it may be quite easy to get it work, it is hard to get it work correctly and efficiently. Rule engine is deceptively easy to use (just need to write a few line of rule and it can perform all jobs), but the rules are hard to verify and its execution cannot be profiled easily (at least from my beginner’s perspective). A subtle mistake can invalidate whole rule.

Whether I will still use Drools as rule engine? Definitely YES. I just need more time to master it.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: