Skip to: Site menu | Main content

Drools

Java Rules Engine

Precompiled Examples Print

Introduction

This tutorial will show you how to obtain and run the Drools examples using only the Drools distributed JAR files.

Instructions for Obtaining Examples

Drools is distributed as a set of JAR files. One of the JAR files contains the set of examples that comes with Drools, but you will need to obtain all of them to satisfy the various runtime dependencies of the examples. You will also need to obtain various auxiliary JAR files from other projects to satisfy the various dependencies of each example.

Note that you can use your IDE of choice, by simply satisfying the classpaths as listed below (and launching the appropriate class). As part of the distribution, there is a "drools-all" jar that provides all the drools modules in one single jar. Many people find this the easiest jar to use as all the features are available.

Obtaining Drools Distribution JAR Files

The Drools JAR files can be found on the public Maven repository located at ibiblio.org. Point your web browser to http://www.ibiblio.org/maven/drools/jars/ to see the list of JAR files published by the Drools project. the "all" jar is best to use. If you only want certain features, you can pick and choose the drools modules if you want to reduce the size of your dependencies, but most people are happy to grab the whole shebang, just in case they want a feature.

You will need to download the appropriate release of Drools. It is recommended to use the Drools-all jar, which combines all the drools features

(but does not include the dependencies).

  • drools-all.jar

    This is the current release with JDK 5 support. If you want the indivivual models, or another version, please browser ibiblio on the link above.

At the time of this writing, the latest version was "2.1".

Obtaining Other Dependent JAR Files

In addition to the Drools JAR files, Drools depends on a number of other projects' JAR files depending on which example you are running and how you are running it.

Python Dependencies

The Python examples depend on this additional JAR file:

This file can be found on the public Maven repository located at ibiblio.org at http://www.ibiblio.org/maven/jython/jars/ .

Groovy Dependencies

The Groovy examples depend on these additional JAR files:

These file can be found on the public Maven repository located at ibiblio.org at http://www.ibiblio.org/maven/groovy/jars/ and http://www.ibiblio.org/maven/asm/jars/ .

Java Dependencies

The Java examples depend on these additional JAR files:

These file can be found on the public Maven repository located at ibiblio.org at http://www.ibiblio.org/maven/antlr/jars/ and http://www.ibiblio.org/maven/janino/jars/ .

Instructions for Running Examples

There are several different examples, each of which can be ran in a number of different ways. For example, the Fibonacci example can be executed using DRL rule-set files encoded using Python syntax, Groovy syntax, and Java syntax. Furthermore it can be executed without a DRL file using rules coded natively into the example using pure Java. The following sections will highlight how to run each of these types of examples.

Note: For this tutorial, it is assumed that you have downloaded all of the aforementioned JAR files and stored them in the current working directory. If you have placed these JAR files elsewhere, you will need to modify the given classpaths accordingly.

DRL with Python Syntax

Java Classpath for Python DRL Syntax Examples

The following JAR files need to be in the Java classpath to run the Python examples:

Hello World Example (Python)

The Python version of the Hello World example is executed by calling the HelloWorldExample class with an argument specifying the Python-syntax DRL file, "helloworld.python.drl". The complete Java command-line would be:

java -classpath drools-base-2.0-rc2.jar:drools-core-2.0-rc2.jar:drools-io-2.0-rc2.jar:drools-smf-2.0-rc2.jar:drools-examples-2.0-rc2.jar:drools-python-2.0-rc2.jar:jython-20020827-no-oro.jar:xml-apis-1.0.b2.jar:xercesImpl-2.6.2.jar org.drools.examples.helloworld.HelloWorldExample helloworld.python.drl

Fibonacci Example (Python)

The Python version of the Fibonacci example is executed by calling the FibonacciExample class with an argument specifying the Python-syntax DRL file, "fibonacci.python.drl". The complete Java command-line would be:

java -classpath drools-base-2.0-rc2.jar:drools-core-2.0-rc2.jar:drools-io-2.0-rc2.jar:drools-smf-2.0-rc2.jar:drools-examples-2.0-rc2.jar:drools-python-2.0-rc2.jar:jython-20020827-no-oro.jar:xml-apis-1.0.b2.jar:xercesImpl-2.6.2.jar org.drools.examples.fibonacci.FibonacciExample fibonacci.python.drl

DRL with Groovy Syntax

Java Classpath for Groovy DRL Syntax Examples

The following JAR files need to be in the Java classpath to run the Groovy examples:

Fibonacci Example (Groovy)

java -classpath drools-base-2.0-rc2.jar:drools-core-2.0-rc2.jar:drools-io-2.0-rc2.jar:drools-smf-2.0-rc2.jar:drools-examples-2.0-rc2.jar:drools-groovy-2.0-rc2.jar:groovy-1.0-rc1-snapshot.jar:asm-1.4.1.jar:asm-util-1.4.1.jar:xml-apis-1.0.b2.jar:xercesImpl-2.6.2.jar org.drools.examples.fibonacci.FibonacciExample fibonacci.groovy.drl

Hello World Example (Groovy)

java -classpath drools-base-2.0-rc2.jar:drools-core-2.0-rc2.jar:drools-io-2.0-rc2.jar:drools-smf-2.0-rc2.jar:drools-examples-2.0-rc2.jar:drools-groovy-2.0-rc2.jar:groovy-1.0-rc1-snapshot.jar:asm-1.4.1.jar:asm-util-1.4.1.jar:xml-apis-1.0.b2.jar:xercesImpl-2.6.2.jar org.drools.examples.helloworld.HelloWorldExample helloworld.groovy.drl

Pet Store Example (Groovy)

java -classpath drools-base-2.0-rc2.jar:drools-core-2.0-rc2.jar:drools-io-2.0-rc2.jar:drools-smf-2.0-rc2.jar:drools-examples-2.0-rc2.jar:drools-groovy-2.0-rc2.jar:groovy-1.0-rc1-snapshot.jar:asm-1.4.1.jar:asm-util-1.4.1.jar:xml-apis-1.0.b2.jar:xercesImpl-2.6.2.jar org.drools.examples.petstore.PetStore petstore.groovy.drl

State Example (Groovy)

java -classpath drools-base-2.0-rc2.jar:drools-core-2.0-rc2.jar:drools-io-2.0-rc2.jar:drools-smf-2.0-rc2.jar:drools-examples-2.0-rc2.jar:drools-groovy-2.0-rc2.jar:groovy-1.0-rc1-snapshot.jar:asm-1.4.1.jar:asm-util-1.4.1.jar:xml-apis-1.0.b2.jar:xercesImpl-2.6.2.jar org.drools.examples.state.StateExample state.groovy.drl

DRL with Java Syntax

Fibonacci Example (Java)

java -classpath drools-base-2.0-rc2.jar:drools-core-2.0-rc2.jar:drools-io-2.0-rc2.jar:drools-smf-2.0-rc2.jar:drools-examples-2.0-rc2.jar:drools-java-2.0-rc2.jar:antlr-2.7.5.jar:janino-2.3.2.jar:xml-apis-1.0.b2.jar:xercesImpl-2.6.2.jar org.drools.examples.fibonacci.FibonacciExample fibonacci.java.drl

Hello World Example (Java)

java -classpath drools-base-2.0-rc2.jar:drools-core-2.0-rc2.jar:drools-io-2.0-rc2.jar:drools-smf-2.0-rc2.jar:drools-examples-2.0-rc2.jar:drools-java-2.0-rc2.jar:antlr-2.7.5.jar:janino-2.3.2.jar:xml-apis-1.0.b2.jar:xercesImpl-2.6.2.jar org.drools.examples.helloworld.HelloWorldExample helloworld.java.drl

Pet Store Example (Java)

java -classpath drools-base-2.0-rc2.jar:drools-core-2.0-rc2.jar:drools-io-2.0-rc2.jar:drools-smf-2.0-rc2.jar:drools-examples-2.0-rc2.jar:drools-java-2.0-rc2.jar:antlr-2.7.5.jar:janino-2.3.2.jar:xml-apis-1.0.b2.jar:xercesImpl-2.6.2.jar org.drools.examples.petstore.PetStore petstore.java.drl

State Example (Java)

java -classpath drools-base-2.0-rc2.jar:drools-core-2.0-rc2.jar:drools-io-2.0-rc2.jar:drools-smf-2.0-rc2.jar:drools-examples-2.0-rc2.jar:drools-java-2.0-rc2.jar:antlr-2.7.5.jar:janino-2.3.2.jar:xml-apis-1.0.b2.jar:xercesImpl-2.6.2.jar org.drools.examples.state.StateExample state.java.drl

Native Java

Java Classpath for Native Java Examples

The following JAR files need to be in the Java classpath to run the Native Java examples:

Fibonacci Example (Native)

The native version of the Fibonacci example is executed by calling the FibonacciNative class. The complete Java command-line would be:

java -classpath drools-core-2.0-rc2.jar:drools-examples-2.0-rc2.jar:drools-java-2.0-rc2.jar org.drools.examples.fibonacci.FibonacciNative

Prime Factors Example (Native)

There is only one version of the Prime Factors example, and that is a native Java version which is executed by calling the PrimeFactors class. The complete Java command-line would be:

java -classpath drools-core-2.0-rc2.jar:drools-examples-2.0-rc2.jar:drools-java-2.0-rc2.jar org.drools.examples.primefactors.PrimeFactors

Miss Manners Example (Native)

The native version of the Miss Manners example is executed by calling the MannersNative class. The complete Java command-line would be:

java -classpath drools-core-2.0-rc2.jar:drools-java-2.0-rc2.jar:drools-examples-2.0-rc2.jar org.drools.examples.manners.MannersNative