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, avoiding you to define these versions in the following sections.

Notice that you can change the values for DgroupId, which represents the logical group your application belongs to and DartifactId, which represents the name of your specific Maven module. This command will create a myfirst-drools-project directory in the directory.

Once we run this command, a new project structure will be ready for us to use. This structure looks similar to the following image:

Creating our first Drools project

Here, some important things are as follows:

  • pom.xml: This contains the project definition and first-level dependencies. I strongly recommend you to open this file and take a look at it. You need to become familiar with how to change this file to include new dependencies and project configurations.
  • src/main/java: This contains our Java classes that need to be compiled.
  • src/test/java: This contains our test classes that need to be compiled and executed during the test phase.
  • src/main/resources: We need to create this directory when we need it (or your IDE might need to create this one). It will contain all the static resources that don't need to be compiled; however, it needs to be packaged along with our compiled classes.
  • src/test/resources: We will need to create this directory when we need it (or your IDE might need to create this one). It will contain all the static test resources that don't need to be compiled; however, they are required by our test classes.

This basic structure serves as a starting point for any Java project, no matter which frameworks are you going to use in it. In order to use Drools in this project, we will need to define the framework dependencies in the pom.xml file. The following dependencies (in the dependencies tag) needs to be added to use Drools in our project:

<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <modelVersion>4.0.0</modelVersion>
  <parent>
      <groupId>org.drools.devguide</groupId>
      <artifactId>chapter-02</artifactId>
      <version>1.0</version>
  </parent>
  <artifactId>myfirst-drools-project</artifactId>
  
  <dependencies>
    <dependency>
      <groupId>org.kie</groupId>
      <artifactId>kie-api</artifactId>
    </dependency>
    <dependency>
      <groupId>org.drools</groupId>
      <artifactId>drools-core</artifactId>
    </dependency>
    <dependency>
      <groupId>org.drools</groupId>
      <artifactId>drools-compiler</artifactId>
    </dependency>
  </dependencies>
</project>

The first org.kie:kie-api dependency contains all the public interfaces exposed by the KIE Platform, which is composed by Drools, jBPM, and OptaPlanner. Next, we include the org.drools:drools-core artifact, which contains the Drools rule engine implementation. Finally, we will include the org.drools:drools-compiler artifact that contains the algorithm to translate the rules written in different resources (text files, spreadsheets, your own types, and so on) to executable rules. This artifact is required only because we are compiling our rules in the project. It is possible to separate the rules compilation from the rules execution to remove this dependency from our project; however, for the sake of simplicity, we are going to compile our rules in the same project.

In order to start writing rules about our own domain, we will also need to add a dependency to it. The dependency defined for the domain model provided by the book examples is as follows:

<dependencies>
  (... Drools dependencies here … )
  <dependency>
    <groupId>org.drools.devguide</groupId>
    <artifactId>model</artifactId>
  </dependency>
</dependencies>

In this way, we can add any dependency that we want and directly start writing the rules using the classes provided in this third-party library as we will see in the next section.

There is one last thing that we need to do in order to complete our project configuration, define a file in the chapter-02/myfirst-drools-project/src/main/resources/META-INF/ directory called kmodule.xml. This file will be used to configure how to load the rules defined in the project in the rule engine. For now, the content of kmodule.xml will be quite simple as we will be using all the default configurations. The following is an example of an empty kmodule.xml:

<kmodule xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns="http://jboss.org/kie/6.0.0/kmodule">
</kmodule>

We will take a look at how to customize this file with more fine-grained settings in Chapter 3, Drools Runtime. This file will be picked up when we instantiate a Rule Engine session automatically to figure out what needs to be loaded.

We are now set up and ready to write and execute our first rule.

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

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