Skip to: Site menu | Main content

Drools

Java Rules Engine

House Example Print

Introduction

The House example is a very simple Domain Specific Language example (DSL). The DSL in the example creates and maps custom xml elements to java conditions and consequences.

Running the House Example

D:\java\workspaces\drools-2.0\drools-examples>maven house
 __  __
|  \/  |__ _Apache__ ___
| |\/| / _` \ V / -_) ' \  ~ intelligent projects ~
|_|  |_\__,_|\_/\___|_||_|  v. 1.0

(bunch of output)

lounge:on
lounge:off
BUILD SUCCESSFUL
Total time: 21 seconds
Finished at: Tue Mar 22 21:32:19 CET 2005

Understanding the House Example

The first rule turns the heating off when the temperature is greater than 20 degrees celsius.

The second rule turns the heating on when the temperature is less than 20 degrees celsius.

To get a DSL working in drools first register the configuration file in the drools.conf file:

drools-house

This tells the Semantics Module Framework (SMF) to load the drools-house SMF configuration file at startup:

module.uri=http://drools.org/semantics/house

Condition(condition) : org.drools.examples.house.HouseConditionFactory
Consequence(actions) : org.drools.examples.house.HouseConsequenceFactory

This configuration file tells drools which factories to use for conditions and actions in the namespace house. Notice that the xml name for consequences and conditions are configurable and in this case the consequence is referred to as actions.

The contents of each condition or actions element is recorded in a Configuration object, which is a hierarchical representation of the sub elements, and then passed to the specified factory implemention to create the underlying condition or consequence. To understand how Configuration object is interpreted and the conditions and consequences made see HouseConditionFactory and HouseConsequenceFactory.