Chapter 6. The Rule Language

6.1. Overview

Note

(updated to Drools 4.0)

Drools 4.0 has a "native" rule language that is non XML textual format. This format is very light in terms of punctuation, and supports natural and domain specific languages via "expanders" that allow the language to morph to your problem domain. This chapter is mostly concerted with the native rule format. The Diagrams used are known as "rail road" diagrams, and are basically flow charts for the language terms. For the technically very keen, you can also refer to "DRL.g" which is the Antlr3 grammar for the rule language. If you use the Rule Workbench, a lot of the rule structure is done for you with content assistance, for example, type "ru" and press ctrl+space, and it will build the rule structure for you.

6.1.1. A rule file

A rule file is typically a file with a .drl extension. In a drl file you can have multiple rules, queries and functions, as well as some resource declarations like imports, globals and attributes that are assigned and used by your rules and queries. However, you are also able to spread your rules across multiple rule files (in that case, the extension .rule is suggested, but not required) - spreading rules across files can help with managing large numbers of rules. A DRL file is simply a text file.

The overall structure of a rule file is:

Example 6.1. Rules file

package package-name

imports

globals

functions

queries

rules

The order in which the elements are declared is not important, except for the package name that, if declared, must be the first element in the rules file. All elements are optional, so you will use only those you need. We will discuss each of them in the following sections.

6.1.2. What makes a rule

For the inpatients, just as an early view, a rule has the following rough structure:

rule "name"
    attributes
    when
        LHS
    then
        RHS
end

Its really that simple. Mostly punctuation is not needed, even the double quotes for "name" are optional, as are newlines. Attributes are simple (always optional) hints to how the rule should behave. LHS is the conditional parts of the rule, which follows a certain syntax which is covered below. RHS is basically a block that allows dialect specific semantic code to be executed.

It is important to note that white space is not important, EXCEPT in these case of domain specific languages, in which case each line is processed before the following line (and spaces may be significant to the domain language).

6.1.3. Reserved words

There are some reserved keywords that are used in the rule language. It is wise to avoid collisions with these words when naming your domain objects, properties, methods, functions and other elements that are used in the rule text. The parser is a bit smart and sometimes knows when a keyword is not being used as a keyword, but avoiding the use of them might prevent some headaches.

The following is a list of keywords that must be avoided as identifiers when writing rules:

  • rule

  • query

  • when

  • then

  • end

  • null

  • and

  • or

  • not

  • exists

  • collect

  • accumulate

  • from

  • forall

  • true

  • false

  • eval

The following list are keywords that you should try and avoid in the rule contents if possible, but the parser usually will work fine, even if you use them for something else.

  • package

  • function

  • global

  • import

  • template

  • attributes

  • enabled

  • salience

  • duration

  • init

  • action

  • reverse

  • result

  • contains

  • excludes

  • memberOf

  • matches

  • in

  • date-effective

  • date-expires

  • no-loop

  • auto-focus

  • activation-group

  • agenda-group

  • dialect

  • rule-flow-group

Of course, you can have words as part of a method name in camel case, like notSomething() - there are no issues with that scenario.