Samples > Rule-Based Programming > How to Write Rules Independently of the Execution Model

This sample shows how to use the Business Object Model (BOM) to Execution Object Model (XOM) mapping mechanism to isolate rules from the "physical" execution layer.

The same rules can sometimes be applied on different execution applications and platforms. For example:

In this sample, a single BOM is mapped to two very different XOMs.

The BOM was initially designed for regular Java static classes and has been remapped to a model made of generic objects (also called synthetic, or dynamic objects) made of key-value properties, such as hash maps. The same BAL rules can be executed on the two execution layers.

The sample is derived from the engine > patternmatching sample (the Java XOM is shared, and the rules have been rewritten in BAL). The execution results are the same.

Key Features

Key features include:

Importing this Sample

To import the bom2xom sample:

  1. Click File > Import.
  2. In the Import Wizard, select ILOG Rule Studio Samples and Tutorials and click Next.
  3. Select the samples > engine > bom2xom sample.
  4. Click Finish.
  5. If one or more files already exist in your workspace, a warning message is displayed indicating that the corresponding file(s) were not re-imported. Close the message window.

The imported sample project files are displayed in the Rule Explorer.

Running this Sample

Make sure that you are in the Rule Perspective before running this sample.

To prepare your environment for the sample:

  1. Adjust the filter settings in the Rule Explorer so that BOM to XOM mapping files will be visible:
    1. In the Rule Explorer, open the Filters menu.
    2. Uncheck the BOM to XOM mapping files entry in the list of elements to exclude.
    3. Click OK.
  2. For this sample, it is also useful to have multiple consoles visible at once.
    1. Click Windows > Preferences.
    2. Select Run/Debug > Launching in the list of options and uncheck the Remove terminated launches when a new launch is created option.
    3. Click OK.

To run the sample:

  1. Click Run > Open Run Dialog.
  2. Select the Rule Project > bom2xom-rules launch configuration and click Run.
  3. This runs the sample with the XOM made of generic objects.
  4. Note the way in which objects are displayed in the Console view:
type CashEvent accountID=AC1 amount=-120.0 parentID=null type=DEBIT currency=EUR ...
This format is the one implemented in the toString() method of the generic objects.
  1. Expand the b2x > financialevent-xom folder in the Rule Explorer and copy the file financial.b2x to the bom folder.
  2. Click OK to confirm replacement of the existing file.
  3. The project rebuilds automatically.
  4. Run the bom2xom-rules launch configuration again.
  5. This time, the sample runs with the XOM made of Java regular objects.
  6. Note the way objects are displayed in the Console view:
Type: DEBIT Date:...
This format is the one implemented in the toString() method of the financialevent xom objects.
If you have multiple consoles, you can switch between the two traces and compare the execution results.
  1. Expand the bom2xom > generic-xom folder in the Rule Explorer and copy the file financial.b2x to the bom folder to revert to the initial configuration.
  2. Click OK to confirm replacement of the existing file.
  3. The project rebuilds automatically.
  4. Run each launch configuration again, but this time in debug mode.
  5. Step into the rules, into the BOM to XOM mapping translations, and finally into the Java XOM code.

You will see that the XOM code invoked in each run is different.

Highlights

The Java XOM is defined in financialevent-xom. The whole model is based on interfaces and implementation classes. The interfaces have been imported to create the BOM.

The generic XOM is defined in bom2xom-genericxom. It consists of three classes:

The generic XOM is completed by an adapter XOM, called bom2xom-extenderxom, which is specific to this sample and is defined in the bom2xom-extenderxom project. This provides an extender class for one of the BOM classes, and a model.properties that models the extension relationship between CashEvent and FinancialEvent classes.

As a generic object model does not support behavioral methods (that is, methods other than accessors and mutators), these methods are implemented either with:

As the Java XOM uses constructors to create objects, and the generic XOM requires the GenericObjectHelper 'static factory' to create objects, the choice has been to create static BOM 'create' methods on each BOM class. These methods are translated into calls to the appropriate constructors in the case of the Java XOM, and into calls to the GenericObjectHelper.create(String type) static method in the case of the generic XOM.

For the sake of simplicity, both configurations use utility classes for currency and date management that belong to the financialevent-xom XOM project. But these could have been different as well.

Related Concepts

BOM and XOM

Related Samples and Tutorials

How to Use Pattern Matching to Apply Rules on a Set of Objects
How to Run Rules Against XML Objects
Import Samples and Tutorials Wizard