Monday, November 15, 2010

Learning to Drool... Part 2

In Part 1 I went through the basic syntax and requirements to get a rule developed and tested.

Now to extend that, the Drools documentation is actually quite good, there is just a ton of it, so I will try to just focus on some of the main topics.

First a little thing I had to do to get the rules to run from your tests using maven, the .drls are not in the classpath by default, a simple way around that was to add the following to the POM:

Now more rules scenarios and usages:

Querying the contents of a can be done in 2 ways, contains and memberOf, the difference is that the collection used in conjunction with memberOf must be a variable.

The drl:

Regular Expressions:
You can use a regex as a selection criteria as well with the key words Matches and Not Matches.

The drl:

Global Variables:
You can define global variables, they should not be used as the are sometimes in code, to pass information between methods or in this case rules. They should rather be used to provide data or services that the rules use. An example would be something like an application specific logger, or perhaps constant lookup data loaded when the application starts.

The test:

The drl:

Rule Attributes:
On a rule you can specify attribute, there are a number of these. I'll just mention a couple handy ones (quoting the official documentation):

default value: false
type: Boolean
When the rule's consequence modifies a fact it may cause the Rule to activate again, causing recursion. Setting no-loop to true means the attempt to create the Activation for the current set of data will be ignored.

default value : 0
type : integer
Salience is a form of priority where rules with higher salience values are given higher priority when ordered in the Activation queue.

default value: as specified by the package
type: String
possible values: "java" or "mvel"
The dialect species the language to be used for any code expressions in the LHS or the RHS code block. Currently two dialects are available, Java and MVEL. While the dialect can be specified at the package level, this attribute allows the package definition to be overridden for a rule.

default value: N/A
type: String, containing a date and time definition
A rule can only activate if the current date and time is after date-effective attribute.

default value: N/A
type: String, containing a date and time definition
A rule cannot activate if the current date and time is after the date-expires attribute.

Guided Rule:
The guided rule editor seems to allow for everything you can do in code, just visually, and potentially more intuitively for those non developers. Only thing you need to do is make sure to have the objects you want to use imported in a .package in the same location as the created .brl file.

Decision Tables:
In my opinion the world actually runs on spreadsheets. We all like to think that it only functions because of us and our fancy applications, but truth be told the world would miss spreadsheets more than any other single application.

The business people giving us our requirements understand spread sheets, some of them better than us developers and this is the biggest single bonus on decision tables. The Drools decision table at first glance did look as if it would not be as simple to hand over to business users as the Quickrules one, but it is actually split quite clearly into "code" and "data"

Now to breakdown a decision table into its parts...
When creating a decision table, the eclipse plugin gives you an example, I am going to work through that.

C2: Keyword ruleset, just to state that this spread sheet is a ruleset (package).
D2: The ruleset (package) name.
Under this row you can specify the following optional keywords:
Sequential - The cell to the right of this can be true or false. If true, then salience is used to ensure that rules fire from the top down.
Import - The cell to the right contains a comma separated list of classes to import.
Variables - The cell immediately to the right can contain global declarations which Drools supports. This is a type, followed by a variable name. (if multiple variables are needed, comma separate them).

C3/D4: Notes heading and the actual notes.

C5: The RuleTable keyword and name. The "RuleTable" keyword must appear on top of the first condition column. You can have multiple rule tables on a sheet, they must just be separated by a line.

C6: Marks the column as a CONDITION column, G6, does this for the ACTION. You need at least one of each for a table to be valid. If there is no data in a CONDITION column, then that condition does not apply.
Other column optional keywords are:
PRIORITY - This states that this column's values will set the salience
DURATION - This will set the duration values for the rule row.
NO-LOOP - Same as in the drl, this specifies if rule is not allowed to loop.

C7: This and subsequent columns in the row, define the actual variables referenced in the rule table.

C8: This and subsequent columns in the row specifies where we are getting the data from.

Row 9 and Column B are just labels / headings making the data simpler to understand, all the other fields and columns before there can be hidden as not to scare the "less technical". The table inside B9 is then were the specific rule data is defined, hopefully directly from the specification, by a non developer.

I have uploaded this project to my Google code project, incase anyone wants it. I had a little issue with the decision table because I run OpenOffice at home and not Microsoft Office, the plugin expects Excel, so it leaves a ugly little red X in my project, but it opens fine outside the IDE and still compiles in maven.

Part 3, I'll have a look at the Rule Flow, and start tackling the monster that is Guvnor


  1. Hi,
    I have been going through the same issues for my company. We have gotten quite far, but are not through it all yet.

    One of my developers (I am but a lowly monkey) had the same view of Guvnor (the monster that is...), but we found a very convenient way through.

    We had some consultants over to do a number of things. One of the tasks I set was for them walk the developers through the Guvnor system. Getting the overview along with a detail frame sped things up to no end.

    The consultants I used are illation. They are not me, but it may be useful to have someone walk you through the system as it is quite flexible.

    Thanks for this posting. It is nice to see others doing the same thing we are. But we weren't awesome enough to do our own posts.

  2. I can see this was updated long back. What about part 3 on Ruleflow and Guvnor

  3. I thought protecting the website from the hackers is a hectic task. This post makes it easy for the developers and business people to protect the website. Keep sharing posts like this…
    Hire Magento Developer
    Hire Web Developer
    Hire Wordpress Programmer
    Hire PHP Developer India
    Mobile App Development India

  4. Many thanks for the exciting blog posting! Simply put your blog post to my favorite blog list and will look forward for additional updates. Thanks all for share your tips and tricks. I also would like to share with you Business Essentials Products Online.

  5. Thanks for sharing this valuable information. Visit Ogen Infosystem for responsive website design and Development Company in Delhi, India.
    Top 5 Website Design Company in Delhi

  6. You can Hire PHP developer to create a stunning responsive website that is capable to provide your visitors with an optimal experience no matter on which device they access it.

  7. This is exactly the information I'm looking for, I couldn't have asked for a simpler read with great tips like this...

    Data Science Training in Hyderabad

  8. Before read your blog. I never read like that topic. But, I am appreciating to your efforts. At this time, you can get Local SEO Company in USA and avail the good service to my experts.

  9. Everyone here and love your good work. It look you spend a lot of time or effort in this article. Keep it up. Now it's time to avail vacuum sealer bags for more information.


Popular Posts