Skip to: Site menu | Main content

Drools

Java Rules Engine

Java semantic module Print

The Java semantic module allows us to embed Java code directly in our DRL files. This can be a very powerful and very dangerous thing. Powerful because we can use Java's object model hierarchy and dot operators to navigate object trees, but dangerous because we can also be tempted to stray from the declarative programming model. This is very useful for getting started with DRL files and Rule Engines, but we recommend you isolate patterns in your Java code and encapsulate them into Domain Specific Languages.

The Java semantic module uses a library called Janino to parse the embedded Java code and compile to bytecode. Conditions and Consequences are compiled during the building of the rulebase, however of de-serialization compilation is done on the fly.

All semantic modules must be used in conjunction with the base semantic module. Base semantics alone cannot be used to write rules--they require semantic extensions.

XML Syntax

xml namespace

To start writing rules using the Java semantic module, you should first declare the xml namespace in your DRL file like the following, substituting your rule set name.

condition element

This element is used to determine if a specific condition exists. A rule requires one or more conditions, and each condition must evaluate to true if the consequence is to fire.

Examples:

Notice how the conditional expression does not end with a semicolon. You mustn't use semicolons in your conditional expressions--these are not executable blocks. Conditions are not mutators. Altering values in the parameter objects in your conditions is bad form and it'll probably break your rule set at execution time. Just Say No.

The above condition is correct. You cannot use left or right angle brackets directly inside your conditional expressions. In order to use the < and > operators you've got to write < and >, respectively.

consequence element

The consequence element lets you use Java language semantics to alter information in the Working Memory via the drools variable, which is an instance of KnowledgeHelper. There are a few guidelines you need to bear in mind when writing Java consequence blocks.

First, you can use any variables declared in a parameter element earlier in the rule, you also have access to application-data. If you want to create other variables, you must import their class types inside the consequence; unless they are already imported via a rule-set's import or they are from the java.lang package.

Example:

The block above imports the Cheese class, instantiates a new Cheese object and asserts the cheese Object into the Working Memory. It also alters the state of the Bob object. Notice that every line of Java code ends in a semicolon. In the consequence block, you must use semicolons.

One of the powers of the consequence block is that it can assert objects into and remove them from the working memory at runtime. This is absolutely crucial to declarative programming and working with rules. The consequence element has three built-in methods that it uses to alter the contents of the working memory.

Method Signature*

What it does

void assertObject(Object object)

Asserts an object, notice that it does not return the FactHandle.

void assertObject(Object object, boolean dynamic)

Asserts an object specifying that it implement the onPropertyChange listener, notice that it does not return the FactHandle.

void modifyObject(Object object)

Modifies an object. Notice that the FactHandle is not passed so the implementation must lookup the FactHandle form the working memory.

void modifyObject(Object oldObject, Object newObject)

Modifies an object by looking up the handle of the oldObject and replacing the oldObject with the newObject for the FactHandle. This isu sed for updating immutable objects.

vooid retractObject(Object object)

Retracts an object from the WorkingMemory. All Activations on the Agenda that are cancelled should emit ActivationCancelled events.

String getRuleName()

The current rule name.

List getObjects()

A List of the objects in the WorkingMemory.

List getObjects(Class objectClass)

All the Objects in the WorkingMemory that match the given Class filter.

void clearAgenda()

Clears the agenda causing all existing Activations to fire ActivationCancelled events.

Functions:

User defined functions allow you to put snippets of code in the ruleset. These functions can be referred to in your rules.

Note that functions must be writted as public static methods.

For example