Chapter 4. The Rule Language

Table of Contents

Overview
A rule file
What makes a rule
Keywords
Comments
Single line comment
Multi-line comment
Error Messages
Message format
Error Messages Description
Other Messages
Package
import
global
Function
Type Declaration
Declaring New Types
Declaring Metadata
Declaring Metadata for Existing Types
Accessing Declared Types from the Application Code
Rule
Rule Attributes
Left Hand Side (when) Conditional Elements
The Right Hand Side (then)
A Note on Auto-boxing and Primitive Types
Query
Domain Specific Languages
When to use a DSL
Editing and managing a DSL
Using a DSL in your rules
Adding constraints to facts
How it works
Creating a DSL from scratch
Scope and keywords
DSLs in the BRMS and IDE
XML Rule Language
When to use XML
The XML format
Legacy Drools 2.x XML rule format
Automatic transforming between formats (XML and DRL)

Overview

Drools has a "native" rule language. 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 this native rule format. The diagrams used to present the syntax are known as "railroad" diagrams, and they are basically flow charts for the language terms. The technically very keen may 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.

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 4.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.

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

It's 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 the case of domain specific languages, where lines are processed one by one and spaces may be significant to the domain language.