Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Mastering JBoss Drools 6
Mastering JBoss Drools 6

Mastering JBoss Drools 6: Discover the power of Drools 6 and Business Rules for developing complex scenarios in your applications

Arrow left icon
Profile Icon Aliverti Profile Icon Salatino Profile Icon Mariano De Maio
Arrow right icon
$9.99 $43.99
Full star icon Full star icon Full star icon Empty star icon Empty star icon 3 (1 Ratings)
eBook Mar 2016 330 pages 1st Edition
eBook
$9.99 $43.99
Paperback
$54.99
Subscription
Free Trial
Renews at $19.99p/m
Arrow left icon
Profile Icon Aliverti Profile Icon Salatino Profile Icon Mariano De Maio
Arrow right icon
$9.99 $43.99
Full star icon Full star icon Full star icon Empty star icon Empty star icon 3 (1 Ratings)
eBook Mar 2016 330 pages 1st Edition
eBook
$9.99 $43.99
Paperback
$54.99
Subscription
Free Trial
Renews at $19.99p/m
eBook
$9.99 $43.99
Paperback
$54.99
Subscription
Free Trial
Renews at $19.99p/m

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
OR
Modal Close icon
Payment Processing...
tick Completed

Billing Address

Table of content icon View table of contents Preview book icon Preview Book

Mastering JBoss Drools 6

Chapter 2. Writing and Executing Rules

The best way to learn something new is by trying it out. For this reason, in this chapter, we are going to cut to the chase to write and execute our first rules. We will also cover the most important points about the rule language and how to effectively write rules that mean something to your domain. This chapter will use the eShop model introduced in the previous chapter to demonstrate a set of scenarios where rules can be applied.

Before we start coding, in the first half of this chapter, you will learn how to set up all the standard tools required to work with Drools and the examples provided with this book. We will be creating a project from scratch, therefore, you can use this chapter as a reference to start your applications from the ground up. The second half of the chapter will cover the introduction to the DRL language and how we recommend to organize your projects when you are using rules or other knowledge assets such as business...

Setting up our environment

In order to start working with rules, there are a couple of things that we need to consider. First of all, we will be relying on Maven to provide the structure for our projects. I strongly recommend you read about Maven if you are not familiar with it as most of the Drools and jBPM infrastructure is nowadays aligned with Maven, therefore, you will feel much more comfortable when you know how it works. I recommend the following link from the Maven project website if you are completely new to the subject:

https://maven.apache.org/guides/getting-started/index.html

This is not a strong requirement for Drools; however, it is a recommended way to use it. Most of the integration with the project tooling and other modules relies on Maven serving as the standard for project structure and project life cycle and dependency management.

In order to get started, we need to make sure that we have the following software installed on our computer:

  • Java 8 JDK, notice that Drools...

Creating our first Drools project

As mentioned in the previous section, we will use Maven to provide us with the project structure. For this, Maven provides the concept of archetypes, which are project templates that we can use to bootstrap our projects. Most of the IDEs provide a way to use these archetypes in order to create and initialize our projects. Check for your IDE if you need to download a Maven plugin or if Maven support is already bundled. If you want to do this via the command line, we can run the following command:

mvn -B archetype:generate -DarchetypeGroupId=org.apache.maven.archetypes 
  -DgroupId=org.drools.devguide
  -DartifactId=myfirst-drools-project

I recommend you to run this command in the drools6-dev-guide/chapter-02/ directory. This will enable Maven to inherit all the configuration from the parent project configuration defined in drools6-dev-guide/pom.xml. If you do this, your project will know the version of Drools that all the other examples in the book are using...

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...

The Rule language

Now that we have executed our first rule, it is time to learn a little bit more about the language that we use to define them. In order to do this, we will start by analyzing the rule that we wrote previously and then we will start creating more advanced rules.

All the rules and examples contained in this section can be found in the chapter-02/chapter-02-kjar/ project. We will use this project throughout the rest of the chapter to store different rules.

As it was mentioned in the first chapter, the rule structure is composed of the conditions and consequence, as follows:

rule "name"
when
    (Conditions) - also called Left Hand Side of the Rule (LHS)
then
    (Actions/Consequence) - also called Right Hand Side of the Rule (RHS)
end

The Conditions (LHS) of the rule are written following the DRL language, which for the sake of simplicity, will not be entirely explained in here. We will be looking at the most common usage of the DRL language throughout the book examples...

Organizing our projects

The more complex our rules become, the more important it is to have tests for them and keep them organized as much as possible. In this section, we will discuss how we organized our example projects for keeping our rules, their tests, and related classes in a structure that can be easily maintained.

We recommend this way of structuring the projects so that each of them keeps a very well-defined scope, set of dependencies, and they can be tested independently. You should keep all the application infrastructural code (user interfaces, system integration, services, and so on) in separate Maven modules as well, therefore, the infrastructure can be maintained in separate cycles from the business knowledge that tends to be updated more frequently by the business needs.

The example repository contains a high-level parent project that is composed by each individual chapter modules. Each individual chapter contains the following three main modules:

  • -kjar: This will contain our...

Setting up our environment


In order to start working with rules, there are a couple of things that we need to consider. First of all, we will be relying on Maven to provide the structure for our projects. I strongly recommend you read about Maven if you are not familiar with it as most of the Drools and jBPM infrastructure is nowadays aligned with Maven, therefore, you will feel much more comfortable when you know how it works. I recommend the following link from the Maven project website if you are completely new to the subject:

https://maven.apache.org/guides/getting-started/index.html

This is not a strong requirement for Drools; however, it is a recommended way to use it. Most of the integration with the project tooling and other modules relies on Maven serving as the standard for project structure and project life cycle and dependency management.

In order to get started, we need to make sure that we have the following software installed on our computer:

  • Java 8 JDK, notice that Drools doesn...

Left arrow icon Right arrow icon
Download code icon Download Code

Key benefits

  • Implement and model different rules using the DRL full syntax
  • Model complex business decisions and domain models in order to automate and improve your operational decisions with the Drools framework
  • A practical, fast-paced, hands-on guide to help you use the different components provided by the Drools Rule Engine

Description

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.

Who is this book for?

This book is for Java developers and architects who need to have a deep understanding of how to create or integrate your applications with the Drools Rules Framework. The book assumes that you know the Java language well and also have experience with some widely used frameworks, such as Spring. You should also know the basics of Maven-based applications.

What you will learn

  • Automate your application's decisions, such as promotion applying, discount policies, fraud detection, and more.
  • Quickly get started with writing your first rules using the DRL full syntax.
  • Discover the power of the new syntax components of the rule language.
  • Define inferences in your business rules to simplify complex decisions.
  • Write decision tables, templates,domain-specific languages, and scorecards, and learn how to map them to the Drools framework.
  • Harness the full operational power of Drools through all of its configuration points.
  • Use Drools configurations and architectures for different environments and scenarios.

Product Details

Country selected
Publication date, Length, Edition, Language, ISBN-13
Publication date : Mar 31, 2016
Length: 330 pages
Edition : 1st
Language : English
ISBN-13 : 9781783288632
Vendor :
Red Hat
Languages :
Tools :

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
OR
Modal Close icon
Payment Processing...
tick Completed

Billing Address

Product Details

Publication date : Mar 31, 2016
Length: 330 pages
Edition : 1st
Language : English
ISBN-13 : 9781783288632
Vendor :
Red Hat
Languages :
Tools :

Packt Subscriptions

See our plans and pricing
Modal Close icon
$19.99 billed monthly
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Simple pricing, no contract
$199.99 billed annually
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just $5 each
Feature tick icon Exclusive print discounts
$279.99 billed in 18 months
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just $5 each
Feature tick icon Exclusive print discounts

Frequently bought together


Stars icon
Total $ 175.97
Mastering jBPM 5
$65.99
jBPM6 Developer Guide
$54.99
Mastering JBoss Drools 6
$54.99
Total $ 175.97 Stars icon
Banner background image

Table of Contents

12 Chapters
1. Rules Declarative Nature Chevron down icon Chevron up icon
2. Writing and Executing Rules Chevron down icon Chevron up icon
3. Drools Runtime Chevron down icon Chevron up icon
4. Improving Our Rule Syntax Chevron down icon Chevron up icon
5. Understanding KIE Sessions Chevron down icon Chevron up icon
6. Complex Event Processing Chevron down icon Chevron up icon
7. Human-Readable Rules Chevron down icon Chevron up icon
8. Rules' Testing and Troubleshooting Chevron down icon Chevron up icon
9. Introduction to PHREAK Chevron down icon Chevron up icon
10. Integrating Rules and Processes Chevron down icon Chevron up icon
11. Integrating Drools with our Apps Chevron down icon Chevron up icon
Index Chevron down icon Chevron up icon

Customer reviews

Rating distribution
Full star icon Full star icon Full star icon Empty star icon Empty star icon 3
(1 Ratings)
5 star 0%
4 star 0%
3 star 100%
2 star 0%
1 star 0%
D. Cyze Apr 23, 2018
Full star icon Full star icon Full star icon Empty star icon Empty star icon 3
I'm only 30 pages into the book, and the content seems alright. However, the number of typos and poor copy-editing is making it difficult to continue.Nearly every page contains multiple sentences similar to the following:"This modify() method is another operation provided by the Rule Engine to make sure that the engine knows that a fact has been changed and ***the change needs to be notified to other rules*** that might be looking to match these changes."I will update my review when I finish the book. At this point, three stars is the absolute highest rating I could give the book.
Amazon Verified review Amazon
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

How do I buy and download an eBook? Chevron down icon Chevron up icon

Where there is an eBook version of a title available, you can buy it from the book details for that title. Add either the standalone eBook or the eBook and print book bundle to your shopping cart. Your eBook will show in your cart as a product on its own. After completing checkout and payment in the normal way, you will receive your receipt on the screen containing a link to a personalised PDF download file. This link will remain active for 30 days. You can download backup copies of the file by logging in to your account at any time.

If you already have Adobe reader installed, then clicking on the link will download and open the PDF file directly. If you don't, then save the PDF file on your machine and download the Reader to view it.

Please Note: Packt eBooks are non-returnable and non-refundable.

Packt eBook and Licensing When you buy an eBook from Packt Publishing, completing your purchase means you accept the terms of our licence agreement. Please read the full text of the agreement. In it we have tried to balance the need for the ebook to be usable for you the reader with our needs to protect the rights of us as Publishers and of our authors. In summary, the agreement says:

  • You may make copies of your eBook for your own use onto any machine
  • You may not pass copies of the eBook on to anyone else
How can I make a purchase on your website? Chevron down icon Chevron up icon

If you want to purchase a video course, eBook or Bundle (Print+eBook) please follow below steps:

  1. Register on our website using your email address and the password.
  2. Search for the title by name or ISBN using the search option.
  3. Select the title you want to purchase.
  4. Choose the format you wish to purchase the title in; if you order the Print Book, you get a free eBook copy of the same title. 
  5. Proceed with the checkout process (payment to be made using Credit Card, Debit Cart, or PayPal)
Where can I access support around an eBook? Chevron down icon Chevron up icon
  • If you experience a problem with using or installing Adobe Reader, the contact Adobe directly.
  • To view the errata for the book, see www.packtpub.com/support and view the pages for the title you have.
  • To view your account details or to download a new copy of the book go to www.packtpub.com/account
  • To contact us directly if a problem is not resolved, use www.packtpub.com/contact-us
What eBook formats do Packt support? Chevron down icon Chevron up icon

Our eBooks are currently available in a variety of formats such as PDF and ePubs. In the future, this may well change with trends and development in technology, but please note that our PDFs are not Adobe eBook Reader format, which has greater restrictions on security.

You will need to use Adobe Reader v9 or later in order to read Packt's PDF eBooks.

What are the benefits of eBooks? Chevron down icon Chevron up icon
  • You can get the information you need immediately
  • You can easily take them with you on a laptop
  • You can download them an unlimited number of times
  • You can print them out
  • They are copy-paste enabled
  • They are searchable
  • There is no password protection
  • They are lower price than print
  • They save resources and space
What is an eBook? Chevron down icon Chevron up icon

Packt eBooks are a complete electronic version of the print edition, available in PDF and ePub formats. Every piece of content down to the page numbering is the same. Because we save the costs of printing and shipping the book to you, we are able to offer eBooks at a lower cost than print editions.

When you have purchased an eBook, simply login to your account and click on the link in Your Download Area. We recommend you saving the file to your hard drive before opening it.

For optimal viewing of our eBooks, we recommend you download and install the free Adobe Reader version 9.