此内容没有您所选择的语言版本。
Chapter 6. Other methods for creating DRL rules
As an alternative to creating and managing DRL rules within the Business Central interface, you can create DRL rule files in external standalone projects using Red Hat Developer Studio, Java objects, or Maven archetypes. These standalone projects can then be integrated as knowledge JAR (KJAR) dependencies in existing Red Hat Process Automation Manager projects in Business Central. The DRL files in your standalone project must contain at minimum the required package
specification, import
lists, and rule
definitions. Any other DRL components, such as global variables and functions, are optional. All data objects related to a DRL rule must be included with your standalone DRL project or deployment.
You can use Red Hat JBoss Developer Studio to create DRL files with rules and integrate the files with your Red Hat Process Automation Manager decision service. This method of creating DRL rules is helpful if you already use Red Hat Developer Studio for your decision service and want to continue with the same work flow. If you do not already use this method, then the Business Central interface of Red Hat Process Automation Manager is recommended for creating DRL files and other rule assets.
Prerequisite
Red Hat JBoss Developer Studio has been installed from the Red Hat Customer Portal.
Procedure
-
In the Red Hat JBoss Developer Studio, click File
New Project. -
In the New Project window that opens, select Drools
Drools Project and click Next. - Click the second icon to Create a project and populate it with some example files to help you get started quickly. Click Next.
Enter a Project name and select the Maven radio button as the project building option. The GAV values are generated automatically. You can update these values as needed for your project:
-
Group ID:
com.sample
-
Artifact ID:
my-project
-
Version:
1.0.0-SNAPSHOT
-
Group ID:
Click Finish to create the project.
This configuration sets up a basic project structure, class path, and sample rules. The following is an overview of the project structure:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Notice the following elements:
-
A
Sample.drl
rule file in thesrc/main/resources
directory, containing an exampleHello World
andGoodBye
rules. -
A
DroolsTest.java
file under thesrc/main/java
directory in thecom.sample
package. TheDroolsTest
class can be used to execute theSample.drl
rule. -
The
Drools Library
directory, which acts as a custom class path containing JAR files necessary for execution.
You can edit the existing
Sample.drl
file andDroolsTest.java
files with new configurations as needed, or create new rule and object files. In this procedure, you are creating a new rule and new Java objects.-
A
Create a Java object on which the rule or rules will operate.
In this example, a
Person.java
file is created inmy-project/src/main/java/com.sample
. ThePerson
class contains getter and setter methods to set and retrieve the first name, last name, hourly rate, and the wage of a person:Copy to Clipboard Copied! Toggle word wrap Toggle overflow -
Click File
Save to save the file. Create a rule file in
.drl
format inmy-project/src/main/resources/rules
. The DRL file must contain at minimum a package specification, an import list of data objects to be used by the rule or rules, and one or more rules withwhen
conditions andthen
actions.The following
Wage.drl
file contains aWage
rule that imports thePerson
class, calculates the wage and hourly rate values, and displays a message based on the result:Copy to Clipboard Copied! Toggle word wrap Toggle overflow -
Click File
Save to save the file. Create a main class and save it to the same directory as the Java object that you created. The main class will load the knowledge base and execute rules.
NoteYou can also add the
main()
method andPerson
class within a single Java object file, similar to theDroolsTest.java
sample file.In the main class, add the required
import
statements to import KIE services, a KIE container, and a KIE session. Then load the knowledge base, insert facts, and execute the rule from themain()
method that passes the fact model to the rule.In this example, a
RulesTest.java
file is created inmy-project/src/main/java/com.sample
with the required imports andmain()
method:Copy to Clipboard Copied! Toggle word wrap Toggle overflow -
Click File
Save to save the file. -
After you create and save all DRL assets in your project, right-click your project folder and select Run As
Java Application to build the project. If the project build fails, address any problems described in the Problems tab of the lower window in Developer Studio, and try again to validate the project until the project builds.
If Java Application is not an option when you right-click your project and select Run As, then go to Run As
To integrate the new rule assets with an existing project in Red Hat Process Automation Manager, you can compile the new project as a knowledge JAR (KJAR) and add it as a dependency in the pom.xml
file of the project in Business Central.
6.2. Creating and executing DRL rules using Java 复制链接链接已复制到粘贴板!
You can use Java objects to create DRL files with rules and integrate the objects with your Red Hat Process Automation Manager decision service. This method of creating DRL rules is helpful if you already use external Java objects for your decision service and want to continue with the same work flow. If you do not already use this method, then the Business Central interface of Red Hat Process Automation Manager is recommended for creating DRL files and other rule assets.
Procedure
Create a Java object on which the rule or rules will operate.
In this example, a
Person.java
file is created in a directorymy-project
. ThePerson
class contains getter and setter methods to set and retrieve the first name, last name, hourly rate, and the wage of a person:Copy to Clipboard Copied! Toggle word wrap Toggle overflow Create a rule file in
.drl
format under themy-project
directory. The DRL file must contain at minimum a package specification (if applicable), an import list of data objects to be used by the rule or rules, and one or more rules withwhen
conditions andthen
actions.The following
Wage.drl
file contains aWage
rule that calculates the wage and hourly rate values and displays a message based on the result:Copy to Clipboard Copied! Toggle word wrap Toggle overflow - Create a main class and save it to the same directory as the Java object that you created. The main class will load the knowledge base and execute rules.
In the main class, add the required
import
statements to import KIE services, a KIE container, and a KIE session. Then load the knowledge base, insert facts, and execute the rule from themain()
method that passes the fact model to the rule.In this example, a
RulesTest.java
file is created inmy-project
with the required imports andmain()
method:Copy to Clipboard Copied! Toggle word wrap Toggle overflow -
Download the Red Hat Process Automation Manager 7.0.0 Source Distribution ZIP file from the Red Hat Customer Portal and extract it under
my-project/pam-engine-jars/
. In the
my-project/META-INF
directory, create akmodule.xml
metadata file with the following content:<?xml version="1.0" encoding="UTF-8"?> <kmodule xmlns="http://www.drools.org/xsd/kmodule"> </kmodule>
<?xml version="1.0" encoding="UTF-8"?> <kmodule xmlns="http://www.drools.org/xsd/kmodule"> </kmodule>
Copy to Clipboard Copied! Toggle word wrap Toggle overflow This
kmodule.xml
file is a KIE module descriptor that selects resources to knowledge bases and configures sessions. This file enables you to define and configure one or more KIE bases, and to include DRL files from specificpackages
in a specific KIE base. You can also create one or more KIE sessions from each KIE base.The following example shows a more advanced
kmodule.xml
file:Copy to Clipboard Copied! Toggle word wrap Toggle overflow This example defines two KIE bases. Two KIE sessions are instantiated from the
KBase1
KIE base, and one KIE session fromKBase2
. The KIE session fromKBase2
is astateless
KIE session, which means that data from a previous invocation of the KIE session (the previous session state) is discarded between session invocations. Specificpackages
of rule assets are included with both KIE bases. When you specify packages in this way, you must organize your DRL files in a folder structure that reflects the specified packages.After you create and save all DRL assets in your Java object, navigate to the
my-project
directory in the command line and run the following command to build your Java files. ReplaceRulesTest.java
with the name of your Java main class.javac -classpath "./pam-engine-jars/*:." RulesTest.java
javac -classpath "./pam-engine-jars/*:." RulesTest.java
Copy to Clipboard Copied! Toggle word wrap Toggle overflow If the build fails, address any problems described in the command line error messages and try again to validate the Java object until the object passes.
After your Java files build successfully, run the following command to execute the rules locally. Replace
RulesTest
with the prefix of your Java main class.java -classpath "./pam-engine-jars/*:." RulesTest
java -classpath "./pam-engine-jars/*:." RulesTest
Copy to Clipboard Copied! Toggle word wrap Toggle overflow - Review the rules to ensure that they executed properly, and address any needed changes in the Java files.
To integrate the new rule assets with an existing project in Red Hat Process Automation Manager, you can compile the new Java project as a knowledge JAR (KJAR) and add it as a dependency in the pom.xml
file of the project in Business Central.
6.3. Creating and executing DRL rules using Maven 复制链接链接已复制到粘贴板!
You can use Maven archetypes to create DRL files with rules and integrate the archetypes with your Red Hat Process Automation Manager decision service. This method of creating DRL rules is helpful if you already use external Maven archetypes for your decision service and want to continue with the same work flow. If you do not already use this method, then the Business Central interface of Red Hat Process Automation Manager is recommended for creating DRL files and other rule assets.
Procedure
Navigate to a directory where you want to create a Maven archetype and run the following command:
mvn archetype:generate -DgroupId=com.sample.app -DartifactId=my-app -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
mvn archetype:generate -DgroupId=com.sample.app -DartifactId=my-app -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
Copy to Clipboard Copied! Toggle word wrap Toggle overflow This creates a directory
my-app
with the following structure:Copy to Clipboard Copied! Toggle word wrap Toggle overflow The
my-app
directory contains the following key components:-
A
src/main
directory for storing the application sources -
A
src/test
directory for storing the test sources -
A
pom.xml
file with the project configuration
-
A
Create a Java object on which the rule or rules will operate within the Maven archetype.
In this example, a
Person.java
file is created in the directorymy-app/src/main/java/com/sample/app
. ThePerson
class contains getter and setter methods to set and retrieve the first name, last name, hourly rate, and the wage of a person:Copy to Clipboard Copied! Toggle word wrap Toggle overflow Create a rule file in
.drl
format inmy-app/src/main/resources/rules
. The DRL file must contain at minimum a package specification, an import list of data objects to be used by the rule or rules, and one or more rules withwhen
conditions andthen
actions.The following
Wage.drl
file contains aWage
rule that imports thePerson
class, calculates the wage and hourly rate values, and displays a message based on the result:Copy to Clipboard Copied! Toggle word wrap Toggle overflow In the
my-app/src/main/resources/META-INF
directory, create akmodule.xml
metadata file with the following content:<?xml version="1.0" encoding="UTF-8"?> <kmodule xmlns="http://www.drools.org/xsd/kmodule"> </kmodule>
<?xml version="1.0" encoding="UTF-8"?> <kmodule xmlns="http://www.drools.org/xsd/kmodule"> </kmodule>
Copy to Clipboard Copied! Toggle word wrap Toggle overflow This
kmodule.xml
file is a KIE module descriptor that selects resources to knowledge bases and configures sessions. This file enables you to define and configure one or more KIE bases, and to include DRL files from specificpackages
in a specific KIE base. You can also create one or more KIE sessions from each KIE base.The following example shows a more advanced
kmodule.xml
file:Copy to Clipboard Copied! Toggle word wrap Toggle overflow This example defines two KIE bases. Two KIE sessions are instantiated from the
KBase1
KIE base, and one KIE session fromKBase2
. The KIE session fromKBase2
is astateless
KIE session, which means that data from a previous invocation of the KIE session (the previous session state) is discarded between session invocations. Specificpackages
of rule assets are included with both KIE bases. When you specify packages in this way, you must organize your DRL files in a folder structure that reflects the specified packages.In the
my-app/pom.xml
configuration file, specify the libraries that your application requires. Provide the Red Hat Process Automation Manager dependencies as well as thegroup ID
,artifact ID
, andversion
(GAV) of your application.Copy to Clipboard Copied! Toggle word wrap Toggle overflow For information about Maven dependencies and the BOM (Bill of Materials) in Red Hat Process Automation Manager, see What is the mapping between Red Hat Process Automation Manager and Maven library version?.
-
Use the
testApp
method inmy-app/src/test/java/com/sample/app/AppTest.java
to test the rule. TheAppTest.java
file is created by Maven by default. In the
AppTest.java
file, add the requiredimport
statements to import KIE services, a KIE container, and a KIE session. Then load the knowledge base, insert facts, and execute the rule from thetestApp()
method that passes the fact model to the rule.Copy to Clipboard Copied! Toggle word wrap Toggle overflow After you create and save all DRL assets in your Maven archetype, navigate to the
my-app
directory in the command line and run the following command to build your files:mvn clean install
mvn clean install
Copy to Clipboard Copied! Toggle word wrap Toggle overflow If the build fails, address any problems described in the command line error messages and try again to validate the files until the build is successful.
After your files build successfully, run the following command to execute the rules locally. Replace
com.sample.app
with your package name.mvn exec:java -Dexec.mainClass="com.sample.app"
mvn exec:java -Dexec.mainClass="com.sample.app"
Copy to Clipboard Copied! Toggle word wrap Toggle overflow - Review the rules to ensure that they executed properly, and address any needed changes in the files.
To integrate the new rule assets with an existing project in Red Hat Process Automation Manager, you can compile the new Maven project as a knowledge JAR (KJAR) and add it as a dependency in the pom.xml
file of the project in Business Central.