Open Source Software Technical Articles

Want the Best of the Wazi Blogs Delivered Directly to your Inbox?

Subscribe to Wazi by Email

Your email:

Connect with Us!

Current Articles | RSS Feed RSS Feed

Make application rules with Drools

  
  
  

Drools is a Java-based business rule management system (BRMS) that allows developers to embed scalable and flexible business rules in their applications in a more elegant way than they can by expressing complex conditions in program code. A BRMS makes for more efficient, scalable, and maintainable applications, saving developer time and system resources.

A rule in Drools specifies a set of conditions and an action that should occur when the conditions are met. For example, suppose an application should offer bonuses to employees who have been with a company for more than 10 years. You could always write an if-then-else block that says "if age >= 10 then bonus = True." No issues there, but after a while you may need to implement another rule, and therefore write another condition block. Eventually you may need to add more conditions, and modify existing ones, and things start to go downhill from there.

Rules engines were created to deal with these situations. They make it easier to write rules, even for the non-programmers, and let developers modify rules easily.

Drools comprises a group of components that, together, make a single software ecosystem:

  • Drools Guvnor is a repository for the system's knowledge bases. It includes graphical tools that help you manage rules, making it especially useful in bigger projects. As the name implies, you can look at it as a centralized management tool.
  • Drools Expert is a coding environment for developers.
  • jBPM, for business process management, helps non-coders such as business analysts design process management workflows. Developers can also use it when planning projects.
  • Drools Fusion deals with complex event processing, such as the pricing and rating process for a telecom company. With it you can create rules that say, for instance, in the event that one customer uses more minutes than he has allocated, the rate per minute will grow, depending on the customer's data plan. You can imagine similar examples in domains like insurance, credit, trading, and many more.
  • Drools Planner is a resource management engine, written in Java, that uses complex heuristics to help programmers optimize solutions to problems involving limited resources.

How a BRMS uses inferences

The method that Drools uses for reasoning – that is, to decide whether data meets the conditions of a rule – is forward chaining. Forward chaining starts with the data and ends with a goal, depending on the data. By contrast, backward chaining, used in systems like ECLiPSE, and supported by AI languages such as Prolog, starts from the goal. In forward chaining, as more knowledge (data) is gathered as logical results of previous inferences, the already available data is updated with the new findings. Inference, by definition, is the act of taking knowledge known to be true and drawing logical conclusions from it.

Let's see how forward chaining works by looking at a simple example.

If a pet bleats and has horns, it's a goat. //existing knowledge
If a pet flies and is yellow, it's a canary. //existing knowledge
My pet Wilma bleats and has horns. //User-added data from which we can infer further.
Therefore, Wilma is a goat. //This data is now added to the existing knowledge.

Drools' rules engine, also called a production rule system (PRS), deals with the rules and knows what to do with them in a given context. (Note that the term "production" is not used in the sense of a "production system," meaning a system that's tested and ready for use in the real world, but rather as a concept in artificial intelligence, where it means, essentially, a rule.) BRMS logic is expressed in a language that's designed for the purpose, so writing a complex set of rules doesn't become the nightmare it can with nested ifs. The PRS matches the data against the rules and applies the specified actions. At the heart of the PRS is a Rete algorithm that does pattern-matching.

Inside the BRMS the core code is separate from the rules repository, so that altering the rules doesn't imply altering the code. Developers interface with the rule engine, meaning that the actual work involves little code and lots of rules. Given this architecture, a developer can create a web interface to the rules engine that lets non-technical people with the proper privileges change rules and actions without touching the code. This separation makes for easy deployment and expansion. Besides the repository, a BRMS also offers a runtime environment for developers that lets them expand the system.

Despite all the positive points of a BRMS, there are some drawbacks too. In order to make full use of a system such as Drools, you need vast theoretical knowledge of pattern-matching systems, artificial intelligence, and concepts like relations. If this scares you, a good way to start is take it one step at a time, with simple rules, and learn as you go along; you don't need to be an AI expert to start using Drools.

Installing and trying Drools

You can install Drools via an Eclipse plugin that you download from the JBoss site and unzip in the Eclipse plugins directory. You'll need to have the Eclipse Graphical Editing Framework (GEF) installed too. You can develop rules with Drools by using the JBoss IDE, which includes everything you need to get started. For this article, we used Drools version 5.5.x (still in beta but perfectly usable) and Eclipse 3.4.x. If you need help, refer to Drools' excellent documentation.

You can start simply – so simply, in fact, that you don't use inferences, but instead rely on something known as a stateless knowledge session, so called because different steps in the rules are independent of one another. You can see how it's done via an employee bonus example in the Drools documentation.

If you need more complex rules with variables that change over time, stateless won't give you what you need – you need stateful rules. With this approach, the session class must be aware of the current state of the parties involved. The Drools documentation uses as an example a system that decides whether to raise a fire alarm in a house, depending on the given room, whether there is a fire, and whether a sprinkler is working.

From this introduction and the information in the documentation, I hope you can see how Drools and BRMSes can be useful. Of course, these systems also have drawbacks, and one of them is memory consumption; if you don't have a large environment to support, you're probably better off using conditionals in the programming language of your choice. Nor is Drools your only alternative among open source BRMSes – similar applications include CLIPS and Lisa. But if you find Drools intriguing, try implementing some simple use cases, then more into the more complex. The more you use Drools, the more you'll like it.




This work is licensed under a Creative Commons Attribution 3.0 Unported License
Creative Commons License.

Comments

You probably would not necessarily think of Drools being a Workflow solution but what originally started out as a rules engine seems to venture increasingly into other areas as well.
Posted @ Tuesday, February 04, 2014 8:20 AM by Michael Thompson
Post Comment
Name
 *
Email
 *
Website (optional)
Comment
 *

Allowed tags: <a> link, <b> bold, <i> italics