Skip to: Site menu | Main content

Drools

Java Rules Engine

Base Semantic Module Print

The Base Semantics Module provides a base language for building a RuleSet in XML. By itself, the base semantic module is not capable of writing a complete and valid rule file. It must be used in conjunction with other semantic modules. This can be the Java semantic module, the Groovy Semantic Module, the Python Semantic Module, or a custom, domain-specific semantic module you've written specifically for your problem domain.

Syntax Guide

rule-set element

Each DRL file must have exactly one rule-set element, which must be uniquely named in the rule base and declare the appropriate namespace bindings for semantic module lookup and schema validation; see Semantics Module Framework and Schema Validation.

attribute optional? description
name no This string must uniquely identify the rule-set in the rule base
<rule-set name="HelloWorld"
          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-set>

import element

Imports can be specified which are then available to any element that references a Class.

attribute optional? description

The body of the element can be either a fully qualified import :

<import>java.util.HashMap</import>

Or a dynamic on demand import:

<import>java.util.*</import>

application-data element

Application Data elements allow objects to be made available to conditions and consequences without the need to assert them into the Working Memory.

attribute optional? description
identifier no This string must uniquely identify the application-data in the rule-set

Application data cannot be set that is not declared in the{{rule-set}} of the rule base. To set application data:

workingMemory.setApplicationData("amount", new Integer(3));

To declare application data inside a rule-set:

<application-data identifier="amount">java.lang.Integer</application-data>

rule element

Each rule-set must contain at least one rule element. Each rule element must be uniquely named in the rule-set, and may contain optional salience and no-loop attributes. no-loop is currently ignored if the duration element is specified.

attribute optional? description
name no This string must uniquely identify the rule in the rule-set.
salience yes Default to 0. Must be numeric, integral, signed. Must be numeric, non-float values, can be negative. Example: salience="-42", salience="2".
no-loop yes Default to "false". Must be boolean. Example: no-loop="true", no-loop="false".
xor-group yes Allow only a single rule within a group to fire, all other rules in that group are then removed from the agenda. A rule can be in only a single xor group.
<rule name="Hello World" salience="10" no-loop="true" xor-group="group1">
  ...
  ...
</rule>

parameter element

Each rule element must contain at least one parameter element, which must use a unique name for the identifer attribute within the rule scope. By itself, the parameter element can do nothing. It requires a nested element representing an ObjectType, like class, class-field and semaphore object (or one from your own semantic module).

attribute optional? description
identifier no This string must uniquely identify the parameter in the rule
<parameter identifier="goodbye">
  <class>java.lang.String</class>
</parameter>

class element

The class element is an ObjectType implementation to be used inside a parameter element. It has no attributes and its contents is either a fully qualified class or a class name available from a previously imported package.

attribute optional? description
<class>java.util.HashMap</class>
<class>HashMap</class>

class-field element

The class-field element is an ObjectType implementation to be used inside a parameter element. It not only constrains the parameter to a Class, like the class element, but it also constrains to a given value for the specified field. The field is limited to String types and the value is also a static String.

attribute optional? description
field no The name of the field of the type String, written to JavaBean specifications with setters and getters
value no Static value constraint for the given field
<class-field field="name" value="A">State</class-field>

This is akin to saying:

<parameter identifier="state">
    <class>State</class>
</parameter>

<java:condition>state.getName().equals("A")<java:condition>

semaphore element

The semaphore element is a specialised ObjectType implementation to be used inside a parameter element. It provides global named variables, where the identifier is the global name, for use within rules; this can be useful for control execution.

attribute optional? description
type no The type of semaphore

To assert a semaphore without an initial value:

workingMemory.assertObject( new StringSemaphore( "state" ) );

To assert a semaphore with an initial value:

workingMemory.assertObject( new StringSemaphore( "state", "START" ) );

To specify a semaphore within a parameter

<parameter identifier="state">
    <semaphore type="String" />
</parameter>

The following supported semaphore types are:

  • CharSemaphore
  • DoubleSemaphore
  • FloatSemaphore
  • IntegerSemaphore
  • ListSemaphore
  • LongSemaphore
  • MapSemaphore
  • SetSemaphore
  • ShortSemaphore
  • StringSemaphore

duration element

The duration element enables Temporal Rules where Activations fire after a given time if they are still true. A duration can have can have one or more unit of time, but must specify atleast one. no-loop is currently ignored if the duration element is specified.

attribute optional? description
seconds yes Unit of time
minutes yes Unit of time
hours yes Unit of time
days yes Unit of time
<duration seconds="2" />