Skip to end of metadata
Go to start of metadata

In order to get started writing DRL files, you'll need to learn about DRL syntax, semantic modules and how to think like a declarative programmer. For now, we'll show you a simple example to get you started.

This is what a basic DRL file looks like:

It uses two semantic modules--the base semantic module and the java semantic module. It's impossible to get by with only base semantics. You must use either one of the three language modules that ship with drools or write your own domain specific language.

Our rule-set has a single rule element, named "Bob Likes Cheese". This rule looks in the working memory for instances of the org.drools.examples.simple.Bob object. If it finds an instance of the Bob object, and if that instance likes cheese, the rule will fire off a consequence, which prints "Bob likes cheese" to the command line. All rule elements are composed of one or more parameters, one or more conditions and a consequence. The form those conditions and consequences take are different for each semantic module.

The above consequence will fire off each time an instance of Bob that likes cheese is passed into the rule execution set.

Syntax used in this example:

semantic module

element name

required?

base

rule-set

exactly one

base

rule

one or more

base

parameter

one or more

java

class

exactly one

java

condition

one or more

java

consequence

exactly one

The API at runtime

The drools API can be used as follows

Note that the RuleBaseLoader utility class supports loading rules in every way imaginable, from a stream, a reader. You can load from a file, classpath or just a string. Thus the rules can be deployed any way you like - stored in a database, or a filesystem are most common ways.

In the above example, typically you would want to cache the ruleBase object once loaded. And have a means to refresh it on the fly (typically this is what many web apps require). There are lots of other ways, dive into the API to find out more ! Also, there is the JSR-94 rule engine API which is another option (although more verbose for a simple example like the one above).

See Also:

2 Minute DRL Tutorial

Please send us feedback!

If you have feedback, questions, tips or suggestions about this tutorial, please send it to users@drools.codehaus.org, so we can make improvements.

Labels: