|
In order to execute JRules code, you need to include some libraries on your classpath
and you need to do a few things in your Java code. Consult the documentation that comes
with your version of JRules for a required list of libraries. You can expect that list
to change with different versions of JRules.
Your stand alone Java code needs to do several things in order to successfully run
JRules. You must:
- Load a ruleset archive into memory
- Parse that ruleset
- Create a context object that contains the actual rules engine within it
- Populate a parameter map with any input values that you plan to pass as parameters. (This step is optional)
- Populate the rules engine's working memory with any data that you wish to put directly into working memory. (This step is optional)
- Tell the rules engine to execute the rules in your ruleset
- Deal with any results from the rules engine. (This step is optional)
We describe loading and parsing a ruleset
on one page. We describe the rest of the steps
on another page.
Rule Studio can create a Java project for Rules.
That project can create some sample code that you can customize to suit your needs.
One of the pieces of sample code, SimpleRuleEngineRunner.java, could probably be better documented.
In the pages that follow, we are going to rewrite the SimpleRuleEngineRunner.java code.
We will explain what happens and why in each step and we will highlight the
relevant lines of sample code.
The following source files are relevant to this article.
-
EmbeddedRulesEngine.java
This file contains the main() method. All the code we discuss in this article is in this method. This file was originally the SimpleRuleEngineRunner.java
file that Rule Studio generates automatically for you when you create a Java project for Rules. Although the automatically generated file
is useful, the authors feel the need to make certain changes on a regular basis. This article describes a more
useful version of that file.
-
TruckDrivers.java
This file contains the code that creates some sample data for the rules engine.
-
Report.java
This file contains the source code for the class that we use to capture messages generated by
rules as they execute.
-
ruleArchive1.jar
is a JAR file containing a ruleset that we can pass to to the rules engine in this article.
In order to actually run this example, you need a copy of JRules from IBM. You will need the "Trucks and Drivers" sample project.
This article is meant to explain how to use that product. It is not meant to bypass any IBM licensing requirements.
|