Book Image

JBoss Drools Business Rules

By : Paul Browne
Book Image

JBoss Drools Business Rules

By: Paul Browne

Overview of this book

<p>In business, a lot of actions are trigged by rules: "Order more ice cream when the stock is below 100 units and temperature is above 25&deg; C", "Approve credit card application when the credit background check is OK, past relationship with the customer is profitable, and identity is confirmed", and so on. Traditional computer programming languages make it difficult to translate this "natural language" into a software program. But JBoss Rules (also known as Drools) enables anybody with basic IT skills and an understanding of the business to turn statements such as these into running computer code.<br /><br />This book will teach you to specify business rules using JBoss Drools, and then put them into action in your business. You will be able to create rules that trigger actions and decisions, based on data that comes from a variety of sources and departments right across your business. Regardless of the size of your business, you can make your processes more effective and manageable by adopting JBoss Rules.<br /><br />Banks use business rules to process your mortgage (home loan) application, and to manage the process through each step (initial indication of amount available, actual application, approval of the total according to strict rules regarding the amount of income, house value, previous repayment record, swapping title deeds, and so on).<br /><br />Countries such as Australia apply business rules to visa applications (when you want to go and live there)&mdash;you get points for your age, whether you have a degree or masters, your occupation, any family members in the country, and a variety of other factors.<br /><br />Supermarkets apply business rules to what stock they should have on their shelves and where&mdash;this depends upon analyzing factors such as how much shelf space there is, what location the supermarket is in, what people have bought the week before, the weather forecast for next week (for example, ice cream in hot weather), and what discounts the manufacturers are giving.<br /><br />This book shows how you can use similar rules and processes in your business or organization. It begins with a detailed, clear explanation of business rules and how JBoss Rules supports them.<br /><br />You will then see how to install and get to grips with the essential software required to use JBoss Rules. Once you have mastered the basic tools, you will learn how to build practical and effective of the business rule systems.<br /><br />The book provides clear explanations of business rule jargon. You will learn how to work with Decision Tables, Domain-Specifi c Languages (DSL)s, the Guvnor and JBoss Integrated Development Environment (IDE), workflow and much more.<br /><br />By the end of the book you will know exactly how to harness the power of JBoss Rules in your business.</p>
Table of Contents (18 chapters)
JBoss Drools Business Rules
Credits
Foreword
About the author
About the reviewer
Preface
6
More rules in the JBoss IDE

Preface

In business, a lot of actions are trigged by rules: "Order more ice cream when the stock is below 100 units and temperature is above 25° C", "Approve credit card application when the credit background check is OK, past relationship with the customer is profitable, and identity is confirmed", and so on. Traditional computer programming languages make it difficult to translate this "natural language" into a software program. But JBoss Rules (also known as Drools) enables anybody with basic IT skills and an understanding of the business to turn statements such as these into running computer code.

This book will teach you to specify business rules using JBoss Drools, and then put them into action in your business. You will be able to create rules that trigger actions and decisions, based on data that comes from a variety of sources and departments right across your business. Regardless of the size of your business, you can make your processes more effective and manageable by adopting JBoss Rules.

Banks use business rules to process your mortgage (home loan) application, and to manage the process through each step (initial indication of amount available, actual application, approval of the total according to strict rules regarding the amount of income, house value, previous repayment record, swapping title deeds, and so on).

Countries such as Australia apply business rules to visa applications (when you want to go and live there)—you get points for your age, whether you have a degree or masters, your occupation, any family members in the country, and a variety of other factors.

Supermarkets apply business rules to what stock they should have on their shelves and where—this depends upon analyzing factors such as how much shelf space there is, what location the supermarket is in, what people have bought the week before, the weather forecast for next week (for example, ice cream in hot weather), and what discounts the manufacturers are giving.

This book shows how you can use similar rules and processes in your business or organization. It begins with a detailed, clear explanation of business rules and how JBoss Rules supports them.

You will then see how to install and get to grips with the essential software required to use JBoss Rules. Once you have mastered the basic tools, you will learn how to build practical and effective of the business rule systems.

The book provides clear explanations of business rule jargon. You will learn how to work with Decision Tables, Domain-Specific Languages (DSL)s, the Guvnor and JBoss Integrated Development Environment(IDE), workflow and much more.

By the end of the book you will know exactly how to harness the power of JBoss Rules in your business.

What this book covers

Chapter 1: This chapter gives you a good platform to understand business rules and JBoss rules. We look at the problems that you might have (and why you're probably reading this book). We look at what business rule engines are, and how they evaluate business rules that appear very simple and how they become powerful when multiple rules are combined.

Chapter 2: This chapter explains setting up Java, setting up Business Rule Management System(BRMS)/Guvnor running on the JBoss App Server, setting up Eclipse, and installing the Drools Plug-in. It also details the installation of the Drools examples for this book and the Maven to build them.

Chapter 3: Guvnor is the user-friendly web editor that's also powerful enough to test our rules as we write them. We take up an example to make things easier. Then we look at the various Guvnor screens, and see that it can not only write rules (using both guided and advanced editors), but that it can also organize rules and other assets in packages, and also allow us to test and deploy those packages. Finally, we write our very first business rule—the traditional 'Hello World' message announcing to everyone that we are now business rule authors.

Chapter 4: This chapter shows how to use the Guvnor rule editor to write some more sophisticated rules. It also shows how to get information in and out of our rules, and demonstrates how to create the fact model needed to do this. We import our new fact model into the Guvnor and then build a guided rule around it. Finally we test our rule as a way of making sure that it runs correctly.

Chapter 5: This chapter pushes the boundries of what we can do with the Guvnor rule editor, and then brings in the JBoss IDE as an even more powerful way of writing rules. We start by using variables in our rules example. Then we discuss rule attributes (such as salience) to stop our rules from making changes that cause them to fire again and again. After testing this successfully, we look at text-based rules, in both the Guvnor and the JBoss IDE, for running 'Hello World' in the new environment.

Chapter 6: This chapter looks again at the structure of a rule file. At the end of this chapter, we look at some more advanced rules that we can write and run in the IDE.

Chapter 7: This chapter explains how testing is not a standalone activity, but part of an ongoing cycle. In this chapter we see how to test our rules, not only in the Guvnor, but also using FIT for rule testing against requirements documents. This chapter also explains Unit Testing using JUnit.

Chapter 8: This chapter explains how to use Excel Spreadsheets (cells and ranges) as our fact model to hold information, instead of the write-your-own-JavaBean approach we took earlier. Then we use Excel spreadsheets to hold Decision tables, to make repetitive rules easier to write.

Chapter 9: This chapter aims to make our rules both easier to use, and more powerful. We start with DSLs—Domain-Specific Languages. This chapter follows on from the 'easy to write rules' theme from the previous chapter and also discusses both ruleflow and workflow.. It would be great to draw a workflow diagram to see/control what (groups of) rules should fire and when. Rule flow gives us this sort of control.

Chapter 10: This chapter shows you how to deploy your business rules into the real world. We look at the pieces that make up an entire web application, and where rules fit into it. We see the various options to deploy rules as part of our application, and the team involved in doing so. Once they are deployed, we look at the code that would load and run the rules—both home-grown and using the standard RuleAgent. Finally we see how to combine this into a web project using the framework of your choice.

Chapter 11: This chapter looks at what happens under the cover by opening up the internals of the Drools rule engine to understand concepts such as truth maintenance, conflict resolution, pattern matching, and the rules agenda. In this chapter, we explore the Rete algorithm and discuss why it makes rules run faster than most comparable business logic. Finally we see the working memory audit log and the rules debug capabilities of the Drools IDE.

Chapter 12: This chapter deals with the other advanced Drools features that have not yet been covered. This includes Smooks to bulk load data, Complex Event Processing, and Drools solver to provide solutions where traditional techniques would take too long.

What you need for this book

We need four pieces of software for this book. All of these are open source, can be downloaded easily from the Internet, and are available under a business-friendly license.

We need Java as this is the core computer language upon which all of the other tools are built. We need BRMS/Guvnor and JBoss App Server to provide a web-based rules editor aimed at business users. We also need to install Maven, a build tool that takes the various Java scripts (source) and transforms them into a package that we can deploy on a web server. We need Eclipse and the Drools plug-in to edit the Java files that we will use for transporting information around the system. We also need to download Drools examples for this book which are available at http://code.google.com/p/red-piranha.

Who this is book for

If you are a business analyst—somebody involved with enterprise IT at a high level, who understands problems and planning solutions, rather than coding in-depth implementations—then this book is for you.

If you are a business user who needs to write rules, or a technical person who needs to support rules, this book is for you.

If you are looking for an introduction to rule engine technology, this book will satisfy your needs.

If you are a business user and want to write rules using Guvnor or the JBoss IDE, this book will be suitable for you.

This book will also suit your need if you are a business user and who wants to understand what Drools can do and how it works, but would rather leave the implementation to a developer.

Conventions

In this book, you will find a number of styles of text that distinguish between different kinds of information. Here are some examples of these styles, and an explanation of their meaning.

Code words in text are shown as follows: "We'll see that one file contains two rules: Hello World and GoodBye."

A block of code will be set as follows:

public void setChocolateOnlyCustomer
(boolean choclateOnlyCustomer) {
this.chocolateOnlyCustomer = chocolateOnlyCustomer;

When we wish to draw your attention to a particular part of a code block, the relevant lines or items will be made bold:

for ( int i = 0; i < rules.length; i++ ) {

String ruleFile = rules[i];
log.info( "Loading file: " + ruleFile );

Any command-line input and output is written as follows:

cd C:\projects\drools-book-examples

New terms and important words are introduced in a bold-type font. Words that you see on the screen, in menus or dialog boxes for example, appear in our text like this: "When Eclipse opens (and you've selected the workspace), select File | New Project from the menu".

Note

Warnings or important notes appear in a box like this.

Note

Tips and tricks appear like this.

Reader Feedback

Feedback from our readers is always welcome. Let us know what you think about this book, what you liked or may have disliked. Reader feedback is important for us to develop titles that you really get the most out of.

To send us general feedback, simply drop an email to , making sure to mention the book title in the subject of your message.

If there is a book that you need and would like to see us publish, please send us a note in the SUGGEST A TITLE form on www.packtpub.com or email .

If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide on www.packtpub.com/authors.

Customer Support

Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.

Errata

Although we have taken every care to ensure the accuracy of our contents, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in text or code—we would be grateful if you would report this to us. By doing this you can save other readers from frustration, and help to improve subsequent versions of this book. If you find any errata, report them by visiting http://www.packtpub.com/support, selecting your book, clicking on the let us know link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata added to the list of existing errata. The existing errata can be viewed by selecting your title from http://www.packtpub.com/support.

Piracy

Piracy of copyright material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works in any form on the Internet, please provide the location address or website name immediately so we can pursue a remedy.

Please contact us at with a link to the suspected pirated material.

We appreciate your help in protecting our authors, and our ability to bring you valuable content.

Questions

You can contact us at if you are having a problem with some aspect of the book, and we will do our best to address it.