Skip to: Site menu | Main content

Drools

Java Rules Engine

Semantics Module Framework Print

Introduction

The Semantics Module Framework (SMF) allows domain specific rule languages (DSL) to be plugged in to Drools. Drools provides SMF implementations for java, groovy and python DSL examples are also provided in drools-examples; House Example, Conway's Game Of Life Example.

SemanticRepository

DefaultSemanticRepository implements SemanticsRepository and returns the SemanticModule registered for the given URL. DefaultSemanticsRepository is provided as a singleton and is instantiated with:

this.repo = DefaultSemanticsRepository.getInstance();

At instantiation time it takes the given property file System.getProperty( "drools.config" ) and also searches the class path returning an Enumeration of drools.conf files. Drools uses a variety of techniques to try to over come common ClassLoader issues:

String droolsConfigProp = System.getProperty( "drools.conf" );

if ( droolsConfigProp != null )
{
    loadConfig( droolsConfigProp );
}

ClassLoader cl = Thread.currentThread( ).getContextClassLoader( );

if ( cl == null )
{
    cl = getClass( ).getClassLoader( );
}

Enumeration configUrls = cl.getResources( "META-INF/drools.conf" );

if ( !configUrls.hasMoreElements( ) )
{
    cl = getClass( ).getClassLoader( );
    configUrls = cl.getResources( "META-INF/drools.conf" );
}

if ( !configUrls.hasMoreElements( ) )
{
    cl = ClassLoader.getSystemClassLoader( );
    configUrls = cl.getResources( "META-INF/drools.conf" );
}

this.classLoader = cl;
while ( configUrls.hasMoreElements( ) )
{
    URL configUrl = (URL) configUrls.nextElement( );
    loadConfig( configUrl );
}

Each drools.conf file specifies one or more configuration files; each of which registers the sementic implementation as a SemanticModule. So drools-java/src/conf/META-INF/drools.conf contains drools-java making DefaultSemanticRepository search the classpath for drools-java.conf; each drools-<semantic>.conf file is then passed to the SemanticReader to create a SemanticModule which is then registered within the SemanticRepository. drools-all-version.jar contains the following drools.conf content:

drools-base
drools-groovy
drools-java
drools-python

To provide a unified ClassLoader within Drools the ClassLoader that is used to read the drools.conf file is held as an instance variable and publicly available with the method getSemanticModuleClassLoader().

SemanticsReader

Each drools-<semantic>.conf needs to specify the module.uri and then a factory class for some of the interfaces in the Service Provider Interfaces (SPI). The drools-java.conf is specified as:

module.uri=http://drools.org/semantics/java
Condition(condition)     : org.drools.semantics.java.JavaExprConditionFactory
Consequence(consequence) : org.drools.semantics.java.JavaBlockConsequenceFactory

SemanticsReader then creates a SimpleSemanticModule object, which implements the SemanticModule interface and adds each of the given classes to the appropriate factory map.

org.drools.semantics.java.JavaExprConditionFactory is added to the Condition factory map with a key of condition, the key is the value in the brackets. This key also specifies the local name to be used for the factory for the given namespace in the parsed XML files.

Consequence(actions) : org.drools.examples.house.HouseConsequenceFactory

Allows "actions" to be used as the local name for a consequence.

<house:actions>
....
<house:actions>

SemanticModule

The SemanticModule is retrieved from the SemanticRepository and returns the semantic implementation for each of the Service Provider Interfaces, as specified in the drools.conf file.

SemanticModule module = this.repo.lookupSemanticModule( uri );

Each method from the above UML diagram returns implementations for the various Service Provider Interfaces.

For a Java SemanticModule module.getConditionFactory( "condition" ) would return org.drools.semantics.java.JavaExprConditionFactory.

Configuration

Each Factory takes a Configuration object which provides the information necessary for a semantic implementation to create and return the SPI implementation.



A Configuration is a tree like object with attributes for each node, in terms of an XML document it is a DOM fragment.