Rule model extensions sample details
You use Rule Designer to run this sample.
Running this sample
- In the Rule perspective in Rule Designer, open the
customrulemodel-rules project, and open one of the rule project rules. Some new
properties are added in the Properties view.
The definitions for the new properties are in the model file data/rulestudiocustommodel.brmx. All the business rules inside the customrulemodel-rules rule project are of type
ExtendedRule, which extends the default typeActionRule.The new properties:
minorVersion,majorVersion, andpatchLevel:IntegerproductionLevel:Enumauthority:Hierarchyrequirement,territory,requestorMailandcustomerNames:String- The data for the
EnumandHierarchyproperty types is defined in the data file of the model data/rulestudiocustommodel.brdx, which is inside the rule project. For example, the definitions forproductionLevelitems andauthoritynode values are in this data file.
- Change the value of the
Regionproperty. Now, edit theterritoryproperty. The list of choices is filled according to the value of theRegionproperty. If you set theRegionproperty to another value, Rule Designer cleans theterritoryproperty automatically. - Create an ExtendedRule type action rule in the new action rule assistant () by selecting
ExtendedRulein the Type drop-down list selector.Look at the properties of the new ExtendedRule. Its major version is set to 1, and the expiration date is the current date.
How this sample works
This sample demonstrates how to define validators to check the content when Properties view cells are edited.
There are two types of validators:
- One for a
StringpropertyrequestorEmail, to check whether the string is a valid email address. - One for the
IntegerpropertiesminorVersion,majorVersion, andpatchLevelto check whether the values are within bounds.
When you edit a property and its cell content does not match what is defined inside its validator, the Eclipse status bar displays an error message in the application window.
To redefine the default cell editors that are used in the Properties view for the custom properties, use the extension point in Rule Designer: 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 whether the cell content is an email address. The validator class implements org.eclipse.jface.viewers.ICellEditorValidator.
In this example, the RequestorMailCellEditor MailValidator inner class is the validator.
The validator provides a method, called isValid, that takes a Java™ object as parameter and returns the following responses:
- A
Stringthat contains an error message if the content is not validated. nullwhen the cell content is validated.
You can edit the requestorEmail property of an Extended type rule to see the
validator in action. If the cell content does not contain “@” or
“.” characters, the Eclipse status bar displays error messages in the
application window.
The same technique checks whether an integer is within bounds. ConstrainedIntegerCellEditor class, in the same package as RequestorMailCellEditor class, also extends TextCellEditor.
You can redefine some of the methods of TextCellEditor to work with integers: doGetValue and doSetValue.
This example reads the value of the boundaries from the property file data/boundaries.properties. Boundaries are retrieved on a class name basis. For each class, values are set as follows:
<class name>_LOWER_BOUND_VALUE<class name>_UPPER_BOUND_VALUE
These values are read inside the cell editor validator class. The constrained properties
(minorVersion, majorVersion, and patchLevel) get
their implementation classes from ConstrainedIntegerCellEditor (abstract
class).
You can add boundaries by creating an empty class that extends 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 this 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.
Redefining createControl() changes the combination box default behaviors and customization of doGetValue(), doSetValue(), and getItems() populates and sets the required values.
CountryCellEditor retrieves its value from an XML file. The current list
depends the property region.
In ilog.rules.studio.samples.customrulemodel.propertyextension.MyPropertyExtension, which implements IlrPropertyExtension, a custom case in afterChange() resets the territory (selected country code) when you select another region.
You can use the MyPropertyExtension class to set a constraint on the
date property effectiveDate. If the date does not match the
predefined pattern, the status bar displays an error message.
The ilog.rules.studio.samples.customrulemodel.propertyextension.MyInitialValue
is a callback class that implements the IlrInitialValue interface. The class is
run when you set the expirationDate or the majorVersion properties
for a new artifact. You specify the connection with these properties in the plug-in.xml file. For
example:
<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.MyIn
itialValue">
</initialValueCallback>
<initialValueCallback
id="ExtendedRule.majorVersion"
callbackClass="ilog.rules.studio.samples.customrulemodel.propertyextension.MyIn
itialValue">
</initialValueCallback>
</extension>
The extension point ilog.rules.studio.ui.propertyEditors sets the class that is associated with a rule model property. For example:
<propertyEditor target="ExtendedRule.minorVersion"
cellEditorClass="ilog.rules.studio.samples.customrulemodel.editors.MinorVersion
CellEditor">
</propertyEditor>
target references one property that is defined inside the rule model
extension.
cellEditorClass references the class that is associated with the rule model
property. This class extends the abstract class
org.eclipse.jface.viewers.CellEditor or one of its children.
The extension point ilog.rules.studio.ui.propertyExtensions does custom actions on the rule properties cell editor events. For example:
<propertyExtension
propertyExtensionClass="ilog.rules.studio.samples.customrulemodel.propertyexten
sion.
MyPropertyExtension">
</propertyExtension>
Source files
To display the source code of the customrulemodel sample plug-in:
- Click .
- In the Import wizard, select , and click Next.
- In the Import As section, select the Projects with source folders option, and click Next.
- In the Plug-ins and Fragments Found list, scroll down, select the ilog.rules.studio.samples.customrulemodel plug-in, and then click Add.
- Click Finish.Rule Designer imports the plug-in source code and displays it in Rule Explorer.Note: You can ignore the warning that the build path specifies execution environment JavaSE-1.7. You can also remove it by either installing Java 7 on your Rule Designer instance, or setting the execution environment of the sample plug-in to 1.7.
- Switch to the Plug-in Development perspective.
- In the Package Explorer, navigate to the Java source files.