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

Briefing this up, this chapter we will cover the following topics:

  • Setting up our environment
  • Creating our first Drools project
  • Writing and executing our first rule
  • Discussing the DRL rule language
  • Organizing our projects

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't require JDK 8, yet all the book examples were created using that. To avoid problems with the examples, we recommend using this version.
  • Maven 3.1.x or newer version to compile, test, and package our projects.
  • GIT 1.9.x or newer version for source version control.

We will be using Git to get the project examples from our repository hosted on Bitbucket. If you are not familiar with Git, we recommend you to take a look at their official documentation at http://git-scm.com/docs.

In order to get a copy of the examples in our local environment, we will clone the remote repository by executing the following command in the terminal:

>git clone https://bitbucket.org/drools-6-developer-guide/drools6-dev-guide.git

Depending on the platform (operating system) that you are running, you can use a Git client to interact with the remote repositories. Feel free to look at the different alternatives for your operating system, here is a list provided by the Git project at http://git-scm.com/downloads/guis.

Now, you have a copy of all the examples of this book in your local environment. To make sure that everything is correctly set up, go to the drools6-dev-guide/ directory and run the following command:

> mvn clean install

This command tells Maven to clean all the previously compiled classes and packaged resources and remove them from our projects to start a fresh compilation process. In order to compile all the Java classes, Maven needs to make sure that it has all the third-party dependencies required by this projects. In our case, Drools is one of the third-party libraries that will need to be downloaded by Maven if we don't have it locally. However, this will not only compile the Java classes, but also run all the tests defined in the projects and package every project if, and only if, all the tests are successful.

This process of downloading, compiling, and running the tests might take some time the first time you run it in your local environment. This is mostly due to the initial downloads and unless you are using nightly builds (SNAPSHOTS), it should only happen the first time you run the command. Notice that the repository hosting the examples doesn't host any third-party libraries, not even Drools.

If you are behind a proxy, read the Maven guide to set up your proxy configurations at https://maven.apache.org/guides/mini/guide-proxies.html.

If this command ends with BUILD SUCCESS, we are good to go. It should look similar to the following:

Setting up our environment

Now, we know that the JDK and Maven are working correctly, we are ready to move forward and create our first Drools project.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.138.119.106