November 28, 2014
Hot Topics:

Rev Up the Drools 5 Java Rule Engine

  • May 19, 2009
  • By Anghel Leonard
  • Send Email »
  • More Articles »

Brief Overview of Drools Flow

Drools Flow is based on flow charts. It represents a process engine that sustains the integration of processes and rules. Practically, a flow chart will allocate ruleflow groups to rules so that you can include or exclude rules. (A ruleflow is a process that describes the order in which a series of steps need to be executed, using a flow chart). In addition, you may run rules in loops, execute rules in parallel, use timers, use events, and so on. Figure 2 shows the set of components that can construct a flow chart, as presented in the Drools plug-in for Eclipse IDE.

Click here for larger image

Figure 2: Flow Chart Components in Drools Plug-in for Eclipse

For the voting example, you will build a simple flow chart that will force the engine to avoid checking the "WrongValue – less than 0" and "WrongValue – bigger than 10" rules. For this, you will place these two rules in a group named IgnoreGroup, while the rest of the rules will be under a group named ValidGroup. For this, you should edit the vote.drl by using the ruleflow-group keyword, like this:

package com.sample
import com.sample.DroolsTest.Vote;
rule "WrongValue – less than 0"
ruleflow-group "IgnoreGroup"
          m : Vote( average < 0.0, vote : vote )
          update( m );               
rule "WrongValue – bigger than 10"
ruleflow-group "IgnoreGroup"
          m : Vote( average > 10.0, vote : vote )
          update( m );               
rule "BadValue – between 0-3"
ruleflow-group "ValidGroup"
          m : Vote( average >= 0.0 && average <=3.0, vote : vote )
          System.out.println( m.getVote() );
rule "GoodValue – between 3-6"
ruleflow-group "ValidGroup"
          m : Vote( average >3.0 && average <=6.0, vote : vote )
          System.out.println( m.getVote() );
rule "VeryGoodValue – between 6-9"
ruleflow-group "ValidGroup"
          m : Vote( average >6.0 && average <=9.0, vote : vote )
         m.setVote("Very Good!");
          System.out.println( m.getVote() );
rule "ExcellentValue – between 9-10"
ruleflow-group "ValidGroup"
          m : Vote( average >9.0 && average <=10.0, vote : vote )
          System.out.println( m.getVote() );

Now, you can construct the flow chart. If you use the Drools plug-in for Eclipse, you can use its graphical editor to view/design this flow chart (see Figure 3).

Click here for larger image

Figure 3: A Flow Chart Example

In the background, this is an XML document. Therefore, you can manually edit it.

Every flow chart starts with a Start node and ends with an End node. Between these nodes, you have the flow components. In this case, you have a single component (a RuleFlowGroup component). This component will indicate the ValidGroup group of rules.

Next, you have to set some properties for the RuleFlowGroup and for the flow chart. First, though, you should know that a flow chart exposes the following properties:

  • Id – The unique id of the process
  • Name – The display name of the process
  • Version – The version number of the process
  • Package – The package (namespace) in which the process is defined
  • Variables – Can be defined to store data during the execution of your process
  • Swimlanes – Specify the actor responsible for the execution of human tasks
  • Exception Handlers – Specify the behavior when a fault occurs in the process
  • Connection Layout – Specify how the connections are visualized on the canvas using the connection layout property:
    • 'Manual' – always draws your connections as lines going straight from their start points to their end points (although it's possible to use intermediate break points)
    • 'Shortest path' – similar to 'Manual', but tries to go around any obstacles it might encounter between the start and end points to avoid lines crossing nodes
    • 'Manhattan' – draws connections by using only horizontal and vertical lines

In addition, a RuleFlowGroup node exposes the following properties:

  • Id – The id of the node (which is unique within one node container)
  • Name – The display name of the node
  • RuleFlowGroup – The name of the ruleflow group that represents the set of rules of this RuleFlowGroup node
  • Timers – Timers that are linked to this node

In the case of the RuleFlowGroup component, the properties that you want to set are Name and RuleFlowGroup. For the Name property, use the VoteFlow value. For the RuleFlowGroup property, use the ValidGroup value (as you can see, it corresponds to the value of ruleflow-group from vote.drl).

For the flow chart, you should set Connection Layout to Shortest Path, Id to votes, Name to ruleflow, and Package to com.sample.

You can accomplish these tasks from the Drools plug-in for Eclipse or by entering them manually. The result is an XML document with the .rf extension. It should look like this:


<?xml version="1.0" encoding="UTF-8"?> 
<process xmlns=""
         xs:schemaLocation=" drools-processes-5.0.xsd"
         type="RuleFlow" name="ruleflow" id="votes" package-name="com.sample" version="1" routerLayout="2" >
    <start id="1" name="Start" x="16" y="16" />
    <end id="3" name="End" x="240" y="16" />
    <ruleSet id="4" name="VoteFlow" x="125" y="17" width="80" height="40" ruleFlowGroup="ValidGroup" />
    <connection from="4" to="3" />
    <connection from="1" to="4" />

Save this file as vote.rf in the project directory. Now you can use the Drools API to connect the flow chart (vote.rf) with the rule resource (vote.drl) and test it. For this, you will make two small modifications to the application:

  1. Load the flow chart as you have loaded the rule resource.
  2. Call the StatefulKnowledgeSession.startProcess before you fire any rules.

This method gets a String argument that represents the ruleflow id (in this case, votes). After making these modifications, you get the following application:
package com.sample;
import org.drools.KnowledgeBase;
import org.drools.KnowledgeBaseFactory;
import org.drools.builder.KnowledgeBuilder;
import org.drools.builder.KnowledgeBuilderError;
import org.drools.builder.KnowledgeBuilderErrors;
import org.drools.builder.KnowledgeBuilderFactory;
import org.drools.builder.ResourceType;
import org.drools.logger.KnowledgeRuntimeLogger;
import org.drools.logger.KnowledgeRuntimeLoggerFactory;
import org.drools.runtime.StatefulKnowledgeSession;
 * This is a sample file to launch a process.
public class RuleFlowTest {
     public static final void main(String[] args) {
          try {
               // load up the knowledge base
               KnowledgeBase kbase = readKnowledgeBase();
               StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession();
               KnowledgeRuntimeLogger logger = KnowledgeRuntimeLoggerFactory.newFileLogger(ksession, "log");
               // go !
               Vote vote = new Vote();
               //insert the "vote"
               //start the "votes" process
               //"fire all rules"
               //clean up
          } catch (Throwable t) {
     private static KnowledgeBase readKnowledgeBase() throws Exception {
          KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
          kbuilder.add(ResourceFactory.newClassPathResource("vote.drl", RuleFlowTest.class), ResourceType.DRL);
          kbuilder.add(ResourceFactory.newClassPathResource("vote.rf", RuleFlowTest.class), ResourceType.DRF);
          KnowledgeBuilderErrors errors = kbuilder.getErrors();
          if (errors.size() > 0) {
               for (KnowledgeBuilderError error: errors) {
               throw new IllegalArgumentException("Could not parse knowledge.");
          KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
          return kbase;
     public static class Vote {
         private String vote;
          private float average;
          public String getVote() {
          public void setVote(String vote) {
      = vote;
          public float getAverage() {
               return this.average;
          public void setAverage(float average) {
               this.average = average;

The output, conforming to vote.drl, will be the text "Excellent!" As an exercise, you may want to try a value that is in IgnoreGroup.

Author’s Note: For a complete tutorial on Drools Flow, check out this write-up.

Flexibility, Readability, and Functionality

Drools 5 provides access to many powerful new features that, although not very easy to use, can bring your Java projects to a new level of flexibility, readability, and functionality. This article was just a brief introduction to get you familiar with Drools; there is much more to explore than you have seen here. With the basic knowledge you now have, you are ready to rev up this rule engine.
Tags: Java, JBoss, Drools, Rule engine, Business logic

Page 4 of 4

Comment and Contribute


(Maximum characters: 1200). You have characters left.



Enterprise Development Update

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

Sitemap | Contact Us

Rocket Fuel