Book Image

Drools JBoss Rules 5.X Developer's Guide

By : Michal Bali
Book Image

Drools JBoss Rules 5.X Developer's Guide

By: Michal Bali

Overview of this book

<p>Writing business rules has always been a challenging task. Business rules tend to change often leading to a maintenance nightmare. This book shows you various ways to code your business rules using Drools, the open source Business Rules Management System.<br /><br />Drools JBoss Rules 5.X Developer's Guide shows various features of the Drools platform by walking the reader through several real-world examples. Each chapter elaborates on different aspects of the Drools platform. The reader will also learn about the inner workings of Drools and its implementation of the Rete algorithm.<br /><br />The book starts with explaining rule basics, then builds on this information by going through various areas like human readable rules, rules for validation, and stateful rules, using examples from the banking domain. A loan approval process example shows the use of the jBPM module. Parts of a banking fraud detection system are implemented with the Drools Fusion module which is the complex event processing part of Drools. Finally, more technical details are shown detailing the inner workings of Drools, the implementation of the ReteOO algorithm, indexing, node sharing, and partitioning.</p>
Table of Contents (22 chapters)
Drools JBoss Rules 5.X Developer's Guide
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Setting Up the Development Environment
Creating Custom Operators
Dependencies of Sample Application
Index

Environment setup


Java Version 1.5+ is required to run examples in this book. Drools can be downloaded from http://www.jboss.org/drools/downloads.html. You'll need the Drools (drools-distribution-5.5.0.Final.zip) and Drools and jBPM tools (droolsjbpm-tools-distribution-5.5.0.Final.zip) downloads. The former is the rule engine itself, that is, the binaries. The latter is the Drools Eclipse plugin. It greatly helps at writing rules. The Drools new project wizard in Eclipse can create a simple Drools project that is ready to run. When setting it up, you need to tell it the location of Drools Runtime (where you extracted the Drools binaries).

Next, you'll also need to download jBPM. Go to http://www.jboss.org/jbpm/ and select the Downloads link. From there go to jBPM 5 | jbpm-5.4.0.Final and download the jbpm-5.4.0.Final-bin.zip file. Similar to setting up the Drools runtime, we'll set up jBPM Runtime inside Eclipse.

If for some reason the Eclipse plugin is not an option, Drools can be set up by maven or manually. When using maven, add at least the following dependencies to your project's pom.xml file:

<dependencies>
  <dependency>
    <groupId>org.drools</groupId>
    <artifactId>knowledge-api</artifactId>
    <version>${drools.version}</version>  
  </dependency>
  <dependency>
    <groupId>org.drools</groupId>
    <artifactId>drools-core</artifactId>
    <version>${drools.version}</version>  
  </dependency>
  <dependency>
    <groupId>org.drools</groupId>
    <artifactId>drools-compiler</artifactId>
    <version>${drools.version}</version>  
  </dependency> 
  <dependency>
    <groupId>org.jbpm</groupId>
    <artifactId>jbpm-flow</artifactId>
    <version>${jbpm.version}</version>
  </dependency>
  <dependency>
    <groupId>org.jbpm</groupId>
    <artifactId>jbpm-flow-builder</artifactId>
    <version>${jbpm.version}</version>
  </dependency>  
  <dependency>
    <groupId>org.jbpm</groupId>
    <artifactId>jbpm-bpmn2</artifactId>
    <version>${jbpm.version}</version>
  </dependency>
</dependencies>
<properties>
  <drools.version>5.5.0.Final</drools.version>
  <jbpm.version>5.4.0.Final</jbpm.version>
</properties>

Code listing 1: Drools and jBPM dependencies in a maven's pom.xml file

By adding these dependencies into the project's pom file, we're declaring that our project depends on the Drools libraries such as knowledge-api, drools-code, and drools-compiler and the jBPM libraries such as jbpm-flow, jbpm-flow-builder, and jbpm-bpmn2. Depending on the features used, we may need to add or remove other Drools/jBPM libraries. Please note the drools.version property that is set to Version 5.5.0.Final. You may need to change it depending on the latest release available. The same applies to the jBPM version property.

Tip

If you are not sure about which Drools version is compatible with which jBPM version, just Google for Drools compatibility matrix. This information is part of the Drools Introduction documentation.

We may also need to tell maven where to get these libraries. They can be downloaded from the official JBoss maven repository, which is located at https://repository.jboss.org/nexus/content/repositories/releases/. The following code snippet does the trick:

<repositories>
  <repository>
    <id>JBoss Repository</id>
    <url>https://repository.jboss.org/nexus/content/repositories/releases/</url>
    <snapshots>
      <enabled>false</enabled>
    </snapshots>
    <releases>
      <enabled>true</enabled>
    </releases>
  </repository>
</repositories>   

Code listing 2: JBoss maven repository in a maven's pom.xml file

Note that the latest snapshot releases can be downloaded from https://repository.jboss.org/nexus/content/repositories/snapshots/.

Let's now look at libraries that are needed in more detail.

Dependencies and their licenses

Drools is licensed under Apache License, Version 2.0 (ASL is a free software license that allows us to develop free, open source, as well as proprietary software, and its contents can be found at http://www.apache.org/licenses/LICENSE-2.0.html). In order to run the examples in this book, at least the following libraries will be needed on the Java classpath:

  • antlr-runtime-3.3.jar: This is a parser generator and helps with parsing rule files (licensed under ANTLR 3 License, which is based on the BSD license)

  • ecj-3.5.1.jar: This is a generic eclipse Java compiler and part of Eclipse Java Development Tools (licensed under the Eclipse Public License v1.0)

  • knowledge-api-5.5.0.Final.jar: This is a Drools and jBPM user API or also known as the public API, and most of the classes we'll be dealing with are located here (licensed under ASL)

  • knowledge-internal-api-5.5.0.Final.jar: This is a Drools and jBPM internal user API (licensed under ASL)

  • drools-compiler-5.5.0.Final.jar: This is a knowledge compiler, understands rule syntax, and compiles rules into Java classes (licensed under ASL)

  • mvel2-2.1.3.jar: mvel is property extraction and expression language for Java; some core Drools features are implemented using mvel, and it is also used as a dialect in the rule language (licensed under ASL)

  • drools-core-5.5.0.Final.jar: This is a Drools engine itself (licensed under ASL)

  • slf4j-api-1.6.4.jar: This is a logging library (licensed under MIT license)

  • protobuf-java-2.4.1.jar: This is a serialization library (licensed under BSD 3 license)

In order to build and run processes, we'll also need the following jBPM libraries:

  • jbpm-flow-5.4.0.Final.jar: This is a library for process execution (licensed under ASL)

  • jbpm-flow-builder-5.4.0.Final.jar: This is a library for building processes (licensed under ASL)

  • jbpm-bpmn2-5.4.0.Final: This is used to build processes in the BPMN2 format (licensed under ASL)

These libraries are valid for Drools Version 5.5.0.Final and jBPM Version 5.4.0.Final. Please note that you may need different versions of these libraries, depending on your version of Drools/jBPM.