Your Application Needs a Rules Engine

I can feel developers cringing just from reading the title. Until recently, I felt the same way; I thought rules engines were archaic, hard to implement, and unnecessary. The truth is that modern rules engines can be fairly lightweight and can add a lot of value to your application.

What is a Rules Engine?

Simply stated, a rules engine implements a collection of production, or business, rules. The implementation often appears to just be a set of if-then conditional statements. The value of the engine comes from the flexibility; rules can be written and grouped in any order, dynamically managed to turn certain rules on and off during execution, and re-executed automatically when the state changes.

A rules engine fundamentally doesn’t do anything that a long, elaborate string of if-else conditionals couldn’t implement, but it provides that same ability with modularity. When you abstract your rules out of your core application code, it makes the entire application easier to manage and test. A specification change no longer means a complete rebuild of your logic as rules don’t inherently interact with each other.

Example Application

Some types of applications lend very well to implementing a rules engine. Imagine an insurance application that processes a customer’s profile to produce a monthly rate. The calculation is derived from hundreds or thousands of variables with iterative computations and predictive forecasting. No matter the implementation, the application will be intensive and difficult to maintain.

If you can abstract the business logic away into segmented sections of rules, then the core application code becomes much simpler. Say a new business rule is added to increase rates because the customer drives a red car. The developer would just need to add a new rule to the correct rules group that matches the red car criteria and update the appropriate variables in the execution section of that rule. The application code then wouldn’t need to be retested, just that particular section of rules.

Rules Engine Variability

Choosing the correct engine for your particular scenario may be the most important decision in the implementation process. Rules engines range in complexity from simple conditionals to Rete algorithm implementations, which uses a complex path finding implementation of a node network. As expected, a simple engine will have an easy learning curve and little overhead, but the additional complexity of an advanced engine will have an edge in flexibility and computational speed.

If your application is strongly based on production rules or can be visualized as a state machine, then a rules engine implementation is something you should consider. However, if your production rules for the application are not well defined or won’t change over time, it probably won’t be worth the effort to learn and implement a rules engine library.