
Four classes are used for authoring: DrlParser, XmlParser, ProcessBuilder and PackageBuilder. The two parser classes produce "descr" (description) AST models from a provided Reader instance. ProcessBuilder reads in an xstream serialisation representation of the Rule Flow. PackageBuilder provides convienience APIs so that you can mostly forget about those classes. The three convenience methods are "addPackageFromDrl", "addPackageFromXml" and addRuleFlow - all take an instance of Reader as an argument. The example below shows how to build a package that includes both XML, DRL and rule files and a ruleflow file, which are in the classpath. Note that all added package sources must be of the same package namespace for the current PackageBuilder instance!
PackageBuilder builder = new PackageBuilder(); builder.addPackageFromDrl( new InputStreamReader( getClass().getResourceAsStream( "package1.drl" ) ) ); builder.addPackageFromXml( new InputStreamReader( getClass().getResourceAsStream( "package2.xml" ) ) ); builder.addRuleFlow( new InputStreamReader( getClass().getResourceAsStream( "ruleflow.rfm" ) ) ); Package pkg = builder.getPackage();
It is essential that you always check your PackageBuilder for errors before attempting to use it. While the ruleBase does throw an InvalidRulePackage when a broken Package is added, the detailed error information is stripped and only a toString() equivalent is available. If you interrogate the PackageBuilder itself much more information is available.
PackageBuilder builder = new PackageBuilder(); builder.addPackageFromDrl( new InputStreamReader( getClass().getResourceAsStream( "package1.drl" ) ) ); PackageBuilderErrors errors = builder.getErrors();
PackageBuilder is configurable using PackageBuilderConfiguration class.

It has default values that can be overridden programmatically via setters or on first use via property settings. At the heart of the settings is the ChainedProperties class which searches several locations looking for drools.packagebuilder.conf files. As it finds them it adds the properties to the master propperties list, providing a level precedence. In order of precedence those locations are: System Properties, user defined file in System Properties, user home directory, working directory, various META-INF locations. Further to this the droosl-compiler jar has the default settings in its META-INF directory.
Currently the PackageBulderConfiguration handles the registry of Accumulate functions, registry of Dialects and the main ClassLoader.
Drools has a pluggeable Dialect system which allows other languages to compile and execute expressions and blocks. The two currently supported dialects are Java and MVEL. Each has its own DialectConfiguration Implementation. The javadocs provide details for each setter/getter and the property names used to configure them.

The JavaDialectConfiguration allows the compiler and language levels to be supported. You can override by setting the drools.dialect.java.compiler property in a packagebuilder.conf file that the ChainedProperties instance will find, or you can do it at runtime as shown below.
PackageBuilderConfiguration cfg = new PackageBuilderConfiguration( ); JavaDialectConfiguration javaConf = (JavaDialectConfiguration) cfg.getDialectConfiguration( "java" ); javaConf.setCompiler( JavaDialectConfiguration.JANINO );
if you do not have Eclipse JDT Core in your classpath you must override the compiler setting before you instantiate this PackageBuilder, you can either do that with a packagebuilder properties file the ChainedProperties class will find, or you can do it programmatically as shown below. In this example properties are used to inject the value for startup.
Properties properties = new Properties(); properties.setProperty( "drools.dialect.java.compiler","JANINO" ); PackageBuilderConfiguration cfg = new PackageBuilderConfiguration( properties ); JavaDialectConfiguration javaConf = (JavaDialectConfiguration) cfg.getDialectConfiguration( "java" ); // demonstrate that the compiler is correctly configured assertEquals( JavaDialectConfiguration.JANINO, javaConf.getCompiler() );
Currently it allows alternative compilers (Janino, Eclipse JDT) to be specified, different JDK source levels ("1.4" and "1.5") and a parent class loader. The default compiler is Eclipse JDT Core at source level "1.4" with the parent class loader set to "Thread.currentThread().getContextClassLoader()".
The following show how to specify the JANINO compiler programmatically:
PackageBuilderConfiguration conf = new PackageBuilderConfiguration(); conf.setCompiler( PackageBuilderConfiguration.JANINO ); PackageBuilder builder = new PackageBuilder( conf );
The MVELDialectConfiguration is much simpler and only allows strict mode to be turned on and off, by default strict is true; this means all method calls must be type safe either by inference or by explicit typing.
