Skip to: Site menu | Main content

Drools

Java Rules Engine

1 Minute DRL Tutorial Print

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:

<rule-set name="cheese rules"
    xmlns="http://drools.org/rules"    
    xmlns:java="http://drools.org/semantics/java"
    xmlns:xs="http://www.w3.org/2001/XMLSchema-instance"
    xs:schemaLocation="http://drools.org/rules rules.xsd
    				   http://drools.org/semantics/java java.xsd">

    <rule name="Bob Likes Cheese">
        <parameter identifier="bob">
            <class>org.drools.examples.simple.Bob</class>
        </parameter>
        <java:condition>bob.likesCheese() == true</java:condition>
        <java:consequence>
            System.out.println( "Bob likes cheese." );
        </java:consequence>
    </rule>

</rule-set>

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

//load the rule base from the classpath
        //in this example it expects that there is a CheeseRules.java.drl file in the class path as shown (containing the DRL above)
        RuleBase ruleBase = RuleBaseLoader.loadFromStream(
            this.getClass().getResourceAsStream( "/rules/CheeseRules.java.drl" ) );

        WorkingMemory workingMemory = ruleBase.newWorkingMemory( );

        //put "bob" in working memory
        workingMemory.assertObject( bob );

        workingMemory.fireAllRules( );

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

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.