What is a rule engine ?
- Published: 08 March 2012
What is a rule engine ?
This question is often heard at interviews, usually when comparing business rules with business processes. So let's make a clarification:
Business processes: Represent what the business does
Business rules: Represent decisions that the business does
A rule engine may be viewed as a sophisticated if/then statement interpreter. The if/then statements that are interpreted are called rules.
The if portions of rules contain conditions such as account.getMoney() < 0. The then portions of rules contain actions such as sendWarning(account).
if (account.getMoney() < 0)
The inputs to a rule engine are a rule execution set and some data objects. The outputs from a rule engine are determined by the inputs and may include the original input data objects with possible modifications, new data objects, and side effects such as sendMail('Thank you for shopping').
Rules are stored in a forward-chaining rule engine, i.e. the engine implements an execution cycle that allows the action of one rule to cause the condition of other rules to become met. In this way, a cascade of rules may become activated and each rule action executed. Forward-chaining rule engines are suitable for problems that require drawing higher-level conclusions from simple input facts.
So, here's how you could rewrite the above check as a rule:
global AccountManager manager;
$account : Account( money < 0 )
As you can see, a Rule file is based on two basic concepts:
Rules: declarative statements that govern the conduct of business processes. A rule consists of a condition and actions. The condition is evaluated, and if it evaluates to true, the rule engine initiates one or more actions.
Facts: are the data upon which rules operate. In our example, the available money are facts.
Advantages of Rule Engines
Turning to rules adds a set of advantages to your applications:
Greater flexibility: keeping your rules into a Knowledge base let you adapt easily your decisions when they are changing.
Easier to grasp: Rules are easier to understand than procedural code so they can be effectively used to bridge the gap between business analyst and developers.
Reduced complexity: When embedding lots of decision points to your procedural codes it can easily turn your application into a nightmare. On the other hand rules can handle much better increasing complexity because they use a consistent representation of business rules.
Reusability: By keeping rules are kept in one place leads to a greater reusability of your business rules. Also, traditional procedural code often impose unnecessary variations of base rules which are therefore more difficult to reuse in other contexts.
On the other hand, using a rule engine might prove to be unnecessary if your business logic is static and the number of business rules are pretty simple and limited in number.
Open source rule engines
There are quite a bunch of available open source rule engines on the market. We will mention here just those ones written in Java:
Drools is an Object-Oriented Rule Engine for Java. Drools is an augmented implementation of Forgy's Rete algorithm tailored for the Java language. Adapting Rete to an object-oriented interface allows for more natural expression of business rules with regards to business objects. More importantly, Drools provides for declarative logic programming and is flexible enough to match the semantics of your problem domain. The various parts that compose a rule may be extended to lend domain-specific sense to the rule.
Open Rules: offers methodology and proven open source tools that enable business analysts to create a Business Rules Repository to be used across an enterprise as a foundation for rules-based applications with complex business, processing, and presentation logic. OpenRules customers maintain their rules repositories using familiar graphical interfaces provided by MS Excel™ or OpenOffice™. Being integrated with Google Docs™, OpenRules supports collaborative rules management. The use of standard tools gives customers easy access to very powerful features including editing, searching, and integration capabilities.
JRule Engine: is a java rule engine, based on Java Specification Request 94, release 1.1, i.e. Rules can be loaded by an XML file or by means of JRuleEngine APIs, so rules can be stored externally into a database, too. The distribution consists of a library that can be embedded into a java application, so it can be used in any kind of application (web based or not).
Zilonis: provides a Multithreaded Rules Engine platform and a scripting environment for Java based applications. The core of the Zilonis platform is based on a variation of the forward chainning Rete algorithm.