Computing is all about decision making and we do it at multiple levels. At the integrated circuit CPU level we do it with hard wired logic gates with fantastic speed but limited flexibility. To get flexibility we invent languages which let us write conditional statements to control a program. At some point in the increasing complexity of requirements for decision making the programming gets really complicated. My rule of thumb is "when your IF-ELSE statements fill up one screen, your program is trying to tell you something."
When your IF-ELSE statements fill up one screen, your program is trying to tell you something.
Trying to maintain such code in the face of changing requirements is a programmer's nightmare. For frequently changed decision making requirements you may want to look at rules engines.
rules engines and their relatives
Of course, the concept of computer decision making as "artificial intelligence" or AI, has been on computer scientist's minds for a long time. AI seems to be a goal that is forever in sight but never reached, probably because expectations are incremented every time something close to AI is achieved. Right now I want to talk about tools with real success records and ignore the question of whether or not they represent AI.
One of the earliest successes for computer decision systems was MYCIN. In the 1970s MYCIN, used a set of rules with confidence factors to achieve a high rate of success at identifying bacterial infections and recommending antibiotics. MYCIN could recognize which facts would lead to an improved diagnosis and direct questions to the physician user. Originally written in LISP, MYCIN has been translated into many computer languages. Since the rules are in a text format, the underlying language can be whatever a developer is familiar with.
There is a major distinction in rules engines between those which use simple true/false facts and those capable of weighting logic with confidence factors such as MYCIN. You may see the term fuzzy logic applied to rules engines which can assert facts with a truth value between zero and one. Research on fuzzy logic rules engines is ongoing but I didn't find any wide application in the real world.
An example problem domain
Suppose your problem is to support a web based shopping system where there are special discount rules for members, seasonal price changes, special offers, shipping options and variable tax laws to consider. Trying to change the code for variations when marketing gets a sudden brainstorm could become a nightmare, but you can prepare sets of rules offline and substitute a new set without changing any other part of the system. You can see why "business rules" applications for rules engines are popular.
Some example rules engine projects
Many developers have experimented with various ways to implement a rules engine in various languages and operating systems. Frequently developers have used Java since object orientation is helpful in conceiving the operation of a rules engine. There was an attempt in 2003 to create a Java API for rules engines but real Java implementations seem to have developed well beyond JSR 94. This article gives short summaries of more than 30 open source Java based rules engines and related tools. Here is more specific information on two current rules engine projects. Of these two, Drools has the largest support community.
- Jess is a rules engine written in Java at Sandia National Labs. Jess operates in a declarative style from a set of rules and facts which may be constants or Java variables to create a list of logical conclusions. Jess is not open-source.
- Drools is an open-source Java implementation which became integrated with the widely used JBoss Java EE application server. JBoss became a division of the Red Hat company in 2006. Red hat hosts the community supported version of Drools and can also supply commercial support. Drools provides for a variety of ways to state rules and connect the rule logic to programs. You don't have to be using JBoss to make use of the large variety of open-source Drools tools available for download.
Representing the rules
One of the obstacles to adopting a rules engine solution is the necessity of learning yet another syntax for expressing facts and logic. This is a subject way beyond the scope of this article, but just to give you an idea, here is an example from the JSR 94 documentation which defines customer and invoice objects and rules for handling a given invoice.
(defclass org.jcp.jsr94.tck.model.Customer org.jcp.jsr94.tck.model.Customer) (defclass org.jcp.jsr94.tck.model.Invoice org.jcp.jsr94.tck.model.Invoice) (defrule rule-1 ?customer <- (org.jcp.jsr94.tck.model.Customer (creditLimit ?limit) (OBJECT ?C)) ?invoice <- (org.jcp.jsr94.tck.model.Invoice (amount ?amt&:(> ?limit ?amt)) (status "unpaid") (OBJECT ?I)) => (modify ?customer (creditLimit (- ?limit ?amt))) ; (printout t "The credit limit of the customer is " ; (get ?C creditLimit) crlf) (modify ?invoice (status paid)) ; (printout t "The status of the invoice is " ; (get ?I status) crlf) )
Fortunately there are programming tools to assist the developer in expressing rules. For example, there is an Eclipse IDE plugin for Drools projects which provides a variety of user interfaces for defining rules, including a spreadsheet-like interface.
Rules and the Semantic Web
In researching the current state of work by the Semantic Web Activity at the W3C I found a sub project entitled the "Rules Interchange Format Working Group" or (RIF). This group was created in 2005 with the intent "to create a standard for exchanging rules among rule systems, in particular among Web rules engines." Given the current state of implementation of the Semantic Web, this is a rather speculative project, and I doubt that commercial sites will be interested in sharing proprietary rules.
Additional rules resources
This was first published in December 2010