Samples > Rule Studio Business Rule Management Extensions > How to Customize the Rule Model

This sample shows how to extend the Business Rule model of ILOG Rule Studio with additional dynamic properties and dedicated editors. The editors are declared and implemented in an Eclipse plug-in.

Key Features

Key features include:

Importing this Sample

This sample extends the Rule Studio API, and the required sample plug-ins are delivered as pre-deployed. If you have not already done so, you must install the samples extension site in Eclipse before running the sample.

To install the plug-in samples extension site:

  1. Click Help > Software Updates > Manage Configuration.
  2. In the Product Configuration dialog, click the Add an Extension Location link.
  3. In the Browse dialog, select the <InstallDir>/studio/samples/eclipse folder.
  4. If prompted to restart the Eclipse Workbench, click Yes.
  5. Eclipse shuts down and restarts.

To check that the Custom Rule Model Sample feature is enabled:

  1. In the Rule Explorer, select the loanvalidation-rules project.
  2. Click Help > Software Updates > Manage Configuration.
  3. In the left pane of the Product Configuration page, expand C:\ILOG\JRules\studio\samples\eclipse and check that Custom Rule Model Sample 1.0.0. is listed.

You can now import the sample project files. However, as this sample installs a custom rule model that replaces the default rule model, it is strongly recommended that you install and run it in a separate workspace to avoid any unwanted impact on other rule projects in your default workspace.

To import the sample project files:

  1. Click File > Switch Workspace and specify a new workspace directory.
  2. Eclipse shuts down and restarts with a blank workspace.
  3. Click Window > Preferences, and in ILOG Rule Studio > RuleModelExtension, change the Default extension to:
  4. <InstallDir>/studio/samples/brmanagement/customrulemodel/rules/data/rulestudiocustommodel.brmx
  5. Change the data extension file to:
  6. <InstallDir>/studio/samples/brmanagement/customrulemodel/rules/data/rulestudiocustommodel.brdx
  7. Click OK.
  8. If prompted to restart Eclipse, click Yes.
  9. Click File > Switch Workspace and then click OK without changing the workspace location.
  10. Eclipse shuts down and restarts. The rule model extension and its attached data file are now recognized by Rule Studio.
  11. Import the customrulemodel sample:
    1. Click File > Import.
    2. In the Import Wizard, select ILOG Rule Studio Samples and Tutorials then click Next.
    3. Select the sample brmanagement > customrulemodel.
  12. Click Finish.
  13. If one or more files already exist in your workspace, a warning message is displayed indicating the file(s) were not re-imported. Close the message window.
  14. The imported sample project files are displayed in the Rule Explorer.

Running the Sample

To run the sample:

  1. Open the customrulemodel-rules project and open one of the rule project's rules. You will see that some new properties have been added in the Properties view.
  2. These new properties are defined inside the model file data/rulestudiocustommodel.brmx. All the business rules inside the customrulemodel-rules rule project are of type ExtendedRule which extends the default type: ActionRule.
    The new properties are:
    - minorVersion, majorVersion and patchLevel: Integer
    - productionLevel: Enum
    - authority: Hierarchy
    - requirement, territory, requestorMail and customerNames: String
    The data for Enum and Hierarchy property types is defined in the model's data file data/rulestudiocustommodel.brdx located inside the rule project. For example, productionLevel items and authority node values are defined in this file.
  3. Change the value of the Region property. Now edit the territory property: the list of choices is filled according to the value of the Region property. If you set the Region property to another value, the territory property is automatically cleaned.
  4. Create an ExtendedRule type's business rules using the new business rule assistant (New > Business Rule) by selecting ExtendedRule in the Type drop down list selector.
  5. Look at the properties of the new ExtendedRule: its major version has been set to 1, and the expiration date has been set to the current date.

Source Files

To display the source code of the customrulemodel sample plug-in:

  1. Click File > Import.
  2. In the Import Wizard, select External Plug-ins and Fragments and click Next.
  3. In the Import As section, select the Projects with source folders option and click Next.
  4. In the Plug-ins and Fragments Found list, scroll down and select the ilog.rules.studio.samples.customrulemodel plug-in (be careful not to select ilog.rules.studio.samples.customrulemodel.source), then click Add.
  5. Click Finish.
  6. The plug-in source code is imported and appears in the Rule Explorer.
  7. Switch to the Plug-in Development perspective.
  8. In the Package Explorer, navigate to the Java source files.

Highlights

Through code, this sample demonstrates how to define validators to check the content when Properties view's cells are edited.

There are two types of validators:

When a property is edited and its cell content does not match what is defined inside its validator, an error message is raised immediately in the Eclipse status bar in the bottom left corner of the application window.

In order to redefine the default cell editors used in the Properties view for our custom properties, we need to use an extension point provided by Rule Studio ilog.rules.studio.ui.propertyEditors.

RequestorMailCellEditor class, in the package ilog.rules.studio.samples.customrulemodel.editors, extends org.eclipse.jface.viewers.TextCellEditor and provides a validator to check if the cell content is an email address. The validator class needs to implement org.eclipse.jface.viewers.ICellEditorValidator.

In our example, RequestorMailCellEditor's MailValidator inner class is the validator that will be used.

The validator provides a method called isValid which takes a Java object as parameter and returns:

You can try to edit the requestorEmail property of an Extended type rule to see the validator in action. If the cell content does not contain "@" or "." characters, error messages will be output to the Eclipse status bar in the bottom left corner of the application window.

We used the same technique to check if an integer is within bounds. ConstrainedIntegerCellEditor class, in the same package as RequestorMailCellEditor class, also extends TextCellEditor.

Some methods of TextCellEditor need to be redefined to work with integers: doGetValue and doSetValue.

In this example, we read the value of the boundaries from a property file: data/boundaries.properties. Boundaries are retrieved on a class name basis. For each class, values are set for:

These values are red inside the cell editor validator class. All our constrained properties: minorVersion, majorVersion and patchLevel have their implementation classes inheriting from ConstrainedIntegerCellEditor (abstract class).

You can add one of your own by creating an empty class extending ConstrainedIntegerCellEditor and by adding your lower and upper bound values inside the data/boundaries.properties file.

The CountryCellEditor class demonstrates how to dynamically change a list of possible values for a property. In our example, the drop down list displays the names of the countries but the value that is returned and persisted when an item is selected, is a country code.

We redefined createControl() in order to change the combo box default behaviors, and customized doGetValue(), doSetValue() and getItems() in order to populate and set the values we wanted.

CountryCellEditor retrieves its value from an xml file. The current list depends on another property: region.

In ilog.rules.studio.samples.customrulemodel.propertyextension.MyPropertyExtension which implements IlrPropertyExtension, we created a custom case in afterChange() in order to reset the territory (selected country code) when another region is selected.

The MyPropertyExtension class is also used to make a constraint on the date property effectiveDate. If the date does not match the predefined pattern, it can not be set and an error message is displayed in the status bar.

The ilog.rules.studio.samples.customrulemodel.propertyextension.MyInitialValue is a callback class that implements the IlrInitialValue interface. It is triggered when the expirationDate or the majorVersion properties are about to be set for a new artefact. The connection with these properties is specified in the plug-in.xml file of this sample:

<extension
   id="ilog.rules.studio.samples.customrulemodel.initialValue"
   point="ilog.rules.studio.model.initialValueCallbacks">
   <initialValueCallback 
      id="BusinessRule.expirationDate"
      callbackClass="ilog.rules.studio.samples.customrulemodel.propertyextension.MyInitialValue">
     </initialValueCallback>
   <initialValueCallback
      id="ExtendedRule.majorVersion"
      callbackClass="ilog.rules.studio.samples.customrulemodel.propertyextension.MyInitialValue">
     </initialValueCallback>
</extension> 

The extension point ilog.rules.studio.ui.propertyEditors is used to set the class associated to a rule model property.

For example:

<propertyEditor target="ExtendedRule.minorVersion"
cellEditorClass="ilog.rules.studio.samples.customrulemodel.editors.MinorVersionCellEditor"> </propertyEditor>

target references one property defined inside the rule model extension.

cellEditorClass references the class associated with the rule model property. This class needs to extend the abstract class org.eclipse.jface.viewers.CellEditor or one of its children.

The extension point ilog.rules.studio.ui.propertyExtensions is used to perform custom actions on the rule properties cell editor events. In our sample:

<propertyExtension
  propertyExtensionClass="ilog.rules.studio.samples.customrulemodel.propertyextension.
MyPropertyExtension"> </propertyExtension>

Related Concepts

Rule Model Extension Schema
Localizing JRules
Rule Model

Related Tasks

Defining Rule Model Extensions

Related Samples and Tutorials

Import Samples and Tutorials Wizard