November 24, 2014
Hot Topics:

Rev Up the Drools 5 Java Rule Engine

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

Define a Rule Resource

Drools is based on rule resources. Most of the time, the rules are mapped in files with the .drl extension or in a spreadsheet format as decision tables (supported formats are Excel and CSV). A basic .drl file is a list of rules written in a file like this:
#package name
#list any import classes here.
#declare any global variables here
 
rule "Your First Rule"
     
     when
          #conditions
     then 
          #actions
 
end
 
rule "Your Second Rule"
     #include attributes such as "salience" here...
     when
          #conditions
     then 
          #actions
          
end

As you can see, every rule is encapsulated between the rule and end keywords. A rule has a name and a when-then pair that represents the condition and the action. The optional parts are marked with pound signs (#). For a better understanding of what goes on in the code, consider the following code sample written as a .drl file (vote.drl):

Listing vote.drl
 
package com.sample
 
import com.sample.DroolsTest.Vote;
 
rule "WrongValue – less than 0"
     when
          m : Vote( average < 0.0, vote : vote )
     then          
          m.setAverage(0.0f);
          update( m );               
end
 
rule "WrongValue – bigger than 10"
     when
          m : Vote( average > 10.0, vote : vote )
     then          
          m.setAverage(10.0f);
          update( m );               
end
 
rule "BadValue – between 0-3"
     when
          m : Vote( average >= 0.0 && average <=3.0, vote : vote )
     then
           m.setVote("Bad!");
          System.out.println( m.getVote() );
end
 
rule "GoodValue – between 3-6"
     when
          m : Vote( average >3.0 && average <=6.0, vote : vote )
     then
           m.setVote("Good!");
          System.out.println( m.getVote() );
end
 
rule "VeryGoodValue – between 6-9"
     when
          m : Vote( average >6.0 && average <=9.0, vote : vote )
     then
           m.setVote("Very Good!");
          System.out.println( m.getVote() );
end
 
rule "ExcellentValue – between 9-10"
     when
          m : Vote( average >9.0 && average <=10.0, vote : vote )
     then
           m.setVote("Excellent!");
          System.out.println( m.getVote() );
end

The getVote, setVote, getAverage, and setAverage functions are provided by a simple POJO like this (Vote.java):

Vote.java
 
public static class Vote {
          
            private String vote;
          private float average;
 
          public String getVote() {
               return this.vote;
          }
 
          public void setVote(String vote) {
               this.vote = vote;
          }
 
          public float getAverage() {
               return this.average;
          }
 
          public void setAverage(float average) {
               this.average = average;
          }
          
     }

As to decision tables, consider them if you have rules that can be expressed as rule templates plus data. In each row of a decision table, you collect data and combine it with the templates to generate a rule.
Author's Note: Keep in mind that in a decision table, each row is a rule and each column in that row is either a condition or an action for that rule.
In practice, you build decision tables on a set of keywords that you place in the right rows and columns. For example, the RuleTable keyword indicates the start of a rule table (both the starting row and column). Usually under the starting row, you will have a row that contains the keywords CONDITION and/or ACTION. Be sure to indicate that the data in the columns below are for either the LHS (Left Hand Side) or the RHS (Right Hand Side) parts of a rule. The next row will contain declarations of ObjectTypes. When you use this row, the values in the cells below become constraints on that object type.

Further down, you have the rule templates themselves. Here you can use the $param placeholder to indicate where data from the cells below will be populated (or $1, $2, $3, etc. to indicate parameters from a comma-separated list in a cell below). Finally, the last two rows will contain data, which are interpolated with templates, to generate rules. Figure 1 shows a table for the voting example (vote.xls).



Click here for larger image

Figure 1: Decision Table for the Voting Example

Table 1 lists more keywords (this table is available in the Drools Reference Manual).

Table 1. Drools Keywords

Keyword Description Required
RuleSet The cell to the right of RuleSet contains the ruleset name. One only (or default)
Sequential The cell to the right of Sequential can be true or false. If true, then "salience" is used to ensure that rules fire from the top down. Optional
Import The cell to the right of Import contains a comma-separated list of Java classes to import. Optional
RuleTable A cell starting with RuleTable indicates the start of a definition of a rule table. The actual rule table starts the next row down. The rule table is read left to right and top down until there is one blank row. At least one. If there are more, then they are all added to the one ruleset.
CONDITION This keyword indicates that this column will be for rule conditions. At least one per rule table
ACTION This keyword indicates that this column will be for rule consequences. At least one per rule table
PRIORITY This keyword indicates that this column's values will set the "salience" values for the rule row. It overrides the 'Sequential' flag. Optional
DURATION This keyword indicates that this column's values will set the duration values for the rule row. Optional
NAME This keyword indicates that this column's values will set the name for the rule generated from that row. Optional
Functions The cell immediately to the right of Functions can contain functions, which can be used in the rule snippets. Drools supports functions defined in the DRL, allowing logic to be embedded in the rule, and changed without hard coding. Use with care and with the same syntax as regular DRL. Optional
Variables The cell immediately to the right of Variables can contain global declarations, which Drools supports. This is a type, followed by a variable name. (If multiple variables are required, comma-separate them). Optional
UNLOOP This keyword indicates that if there are cell values in this column, the no-loop attribute should be set. Optional
XOR-GROUP Cell values in this column mean that the rule-row belongs to the given XOR/activation group. An activation group means that only one rule in the named group will fire (i.e., the first one to fire cancels the other rule activations). Optional
Worksheet By default, only the first worksheet is looked at for decision tables. N/A

Source: Drools Reference Manual


Tags: Java, JBoss, Drools, Rule engine, Business logic



Page 2 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