Book Image

Mastering JBoss Drools 6

By : Mariano De Maio, Mauricio Salatino, Esteban Aliverti
Book Image

Mastering JBoss Drools 6

By: Mariano De Maio, Mauricio Salatino, Esteban Aliverti

Overview of this book

Mastering JBoss Drools 6 will provide you with the knowledge to develop applications involving complex scenarios. You will learn how to use KIE modules to create and execute Business Rules, and how the PHREAK algorithm internally works to drive the Rule Engine decisions. This book will also cover the relationship between Drools and jBPM, which allows you to enrich your applications by using Business Processes. You will be briefly introduced to the concept of complex event processing (Drools CEP) where you will learn how to aggregate and correlate your data based on temporal conditions. You will also learn how to define rules using domain-specific languages, such as spreadsheets, database entries, PMML, and more. Towards the end, this book will take you through the integration of Drools with the Spring and Camel frameworks for more complex applications.
Table of Contents (18 chapters)
Mastering JBoss Drools 6
Credits
About the Authors
About the Reviewer
www.PacktPub.com
Preface
Index

Writing and executing our first rule


Now that we have our project structure ready, we can write our first rule. For that, we will create a new empty text file. This will be a static resource, therefore, we need to place it in the src/main/resources directory. This text file needs to have the .drl extension so that it can be picked up as a rule file. In the .drl files, we will write as many rules as we want. Now, we will start easy with just one rule.

Let's write our first rule to classify the items based on what they cost us. Our rules.drl text file will look similar to the following:

package myfirstproject.rules
import org.drools.devguide.eshop.model.Item;
import org.drools.devguide.eshop.model.Item.Category;
rule "Classify Item - Low Range"
    when
        $i: Item(cost < 200)
    then
        $i.setCategory(Category.LOW_RANGE);
end

This rule checks for each item that costs less than 200 USD and automatically tags it with a category, in this case, LOW_RANGE. For our shop, it makes sense...