January 26, 2021
Hot Topics:

Rev Up the Drools 5 Java Rule Engine

  • By Anghel Leonard
  • Send Email »
  • More Articles »

You can talk for hours and hours about how to implement a web/enterprise Java application. You can talk about layers, architectures, frameworks, tools, patterns, and so on, but you probably will skip a sore subject: structuring the business logic! As much as you may hate to admit it, I am sure that you have something like the following somewhere in your code:

//a vote system deciding the proper message depending on the votes average
if average < 0.0 
   { average = 0.0;  }
if average > 10.0 
   { average = 10.0;  }
if (average>=0.0 && average<=3)
   { System.out.println("Bad!");  }
if (average>3.0 && average<=6.0)
   { System.out.println("Good!");  }
if (average>6.0 && average<=9.0)
   { System.out.println("Very Good!");  }
if (average>9.0 && average<=10.0)
   { System.out.println("Excellent!");  }
All those if ... then statements not only look bad but they do nothing for configurability or readability. Just ask yourself a few questions about the above spaghetti code:
  • After a small change in this code, does the application work without recompilation/redeploying?
  • Do you want to be the person who has to maintain this code?
  • Supposing that this code is getting bigger and bigger, can you easily test it and be sure that it is correct?
  • Are you ready to rewrite everything from scratch when you add new rules or different dependencies?
  • After a few months, will you recognize and understand the meaning of this code?

If you answered no more than you answered yes, keep reading to find how to turning those no's into yeses. The solution is Drools, a Java rule engine framework for organizing business logic. Drools allows you to focus on things that are known to be true, rather than on making decisions about low-level mechanics. Using this framework, you will be able to transform the above code into something readable, verifiable, reusable, configurable, scalable, and flexible.

Downloading and Installing Drools 5

At the time of writing, Drools had reached version 5.0 CR 1 (released on March 10, 2009). For this tutorial, download the Drools 5.0.0.CR1 Binaries distribution and extract it in your favorite location. You can use it as you would any set of JARs or you can use it from an IDE such as Eclipse or NetBeans.

Author's Note: Under the JBoss Tools 3 release, you can find a set of dedicated tools for Drools. You can install them under Eclipse and configure a Drools runtime in just a few minutes. The result will be a fully compatible and visual approach to Drools. According to the Drools homepage, "[the] Eclipse plug-in makes it easier than ever to use Drools, with intelligent auto-completion and Debug views, rule flow GUIs, and more." However, this is just a suggestion, not a requirement for this article.

Page 1 of 4

This article was originally published on May 19, 2009

Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Thanks for your registration, follow us on our social networks to keep up-to-date