Artwork implying World Wide Web Architecture

Embedding JRules Rules Engine in Java Code -- Part 1

Part of the IBM / iLOG JRules Series
Most recently modified on 2010-03-12
Back to home page Back to articles page WAS architecture articles page Script articles page .
See also these related articles:
Load a ruleset Create rules engine and execute rules Other JRules Articles

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:

  1. Load a ruleset archive into memory
  2. Parse that ruleset
  3. Create a context object that contains the actual rules engine within it
  4. Populate a parameter map with any input values that you plan to pass as parameters. (This step is optional)
  5. Populate the rules engine's working memory with any data that you wish to put directly into working memory. (This step is optional)
  6. Tell the rules engine to execute the rules in your ruleset
  7. 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.

Arthur Kevin McGrath

Bio:

The author is an engineer with the consulting firm, Contract Engineers. He has consulted and lectured extensively since 1987 about the infrastructure that makes electronic commerce possible. His publications include Leading Practices for WebSphere Dynamic Process Edition V6.2 (SG24-7776-00) and Websphere Application Server Administration Using Jython (ISBN 0137009526), the definitive book on WAS scripting.

Photo of the author
To schedule a speaker at your location, write speakers@edu4eng.com
To inquire about consulting for your company, write consultants@edu4eng.com
To inquire about training for your company, write training@edu4eng.com