Samples > Rule Studio Authoring Extensions > How to Extend the BOM with Typed Numbers and Modify Core Types

JRules provides several predefined core types which are ready to use in rules. These types include String, Number, Boolean, Date and Time.

This sample shows how to extend the BOM and vocabulary to support the manipulation of typed numbers, such as percentages, currencies or any other numerical value with a unit, and thus prevent users from combining incompatible value units when creating or editing rules.

The sample also provides a number of resource files that you can reuse in your own projects if you need to modify the behavior of the core types.

Key Features

Key features include:

Installing this Sample

To install the sample:

  1. Install the bomextensions sample:
  2. Click File > Import. In the Import Wizard, select ILOG Rule Studio Samples and Tutorials, click Next and select the samples > authoring > bomextensions sample.
  3. Click Finish.
  4. The required sample project files are imported and displayed in the Rule Explorer.

Running this Sample

To run the sample:

  1. Click Run > Open Run Dialog. Select the bomextensions launch configuration, then click Run.
  2. The Console view shows the output generated by the rule project.

Highlights

Development languages do not natively support typed numbers, and allow values expressed in different units to be combined (for example: 1 Euro - $1 = 0?).

The techniques described in this sample allow you to extend the BOM and vocabulary to support manipulation of typed numbers, such as percentages, currencies and any other numerical values of a specific type.

The sample provides reusable .bom, .voc and .b2x files that you can incorporate into or use as templates for your own projects:

This BOM defines a Percentage BOM virtual class, which can be used in much the same way as the Number class, except that percentage values cannot be used everywhere in a business rule in place of any other number. Furthermore, a percentage cannot be semantically mixed with other numbers. For example, the operation 15% + 18 is meaningless.

As a consequence, the Percentage class redefines most of the operators and phrases that are defined on the Number type. Operators that were not relevant have been excluded. The type is decorated with a % symbol. Literal values of this type are suffixed with this symbol, for example: 15%. The Percentage type is mapped to the primitive double type using the BOM to XOM mapping execution name.

This BOM also defines CurrencyDollar and CurrencyEuro BOM virtual classes, which can also be used in much the same way as a Number, but which does not extend the Number class (since currency values may not be used everywhere where a number can be used). The CurrencyDollar and CurrencyEuro BOM classes are mapped to the primitive double type using the BOM to XOM mapping execution name.

The sample also contains a second BOM which is used to demonstrate the use of these Percentage, CurrencyDollar and CurrencyEuro virtual classes in rules. This BOM includes a simple virtual Item class which is mapped to a Map through the BOM to XOM mapping mechanism.

The rule project contains just one rule, useExtensions, which makes use of percentages and currencies in BAL rules. The sole purpose of this rule is to show various ways in which percentages and currency values can be expressed and combined.

The provided launch configuration simply runs the rule to check that everything is interpreted and executed as expected.

If you need to modify the behavior of core BOM classes, the resources folder of the sample contains a boot.bom file, a boot_en.voc file and a boot.b2x file that can be edited and incorporated into your own projects.

Related Concepts

BOM and XOM
Business Object Model (BOM)
Vocabulary

Related Reference

Business Action Language

Related Samples and Tutorials

Import Samples and Tutorials Wizard