- Mastering JBoss Drools 6
- Mauricio Salatino Mariano De Maio Esteban Aliverti
- 781字
- 2021-07-09 19:58:41
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:

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.
- OpenStack for Architects
- 自動(dòng)控制原理
- WOW!Illustrator CS6完全自學(xué)寶典
- Learning Social Media Analytics with R
- 模型制作
- Maya 2012從入門(mén)到精通
- 電腦上網(wǎng)輕松入門(mén)
- 奇點(diǎn)將至
- 過(guò)程控制系統(tǒng)
- 人工智能:語(yǔ)言智能處理
- Apache源代碼全景分析(第1卷):體系結(jié)構(gòu)與核心模塊
- 網(wǎng)絡(luò)脆弱性掃描產(chǎn)品原理及應(yīng)用
- Mastering Exploratory Analysis with pandas
- 計(jì)算機(jī)應(yīng)用基礎(chǔ)實(shí)訓(xùn)(職業(yè)模塊)
- WPF專業(yè)編程指南