Samples > Rule Studio Authoring Extensions > How to Make a Rule Definition Aware of Context

This sample shows how to filter the completion menu of a BOM member based on the rule context.

During editing of a rule definition, the list of proposed and authorized completion values changes depending on the value of a property set on the rule or its package. This is done using a valueinfo provider and checker associated with the BOM member.

Key Features

Key features include:

Installing this Sample

This sample extends the Rule Studio API. 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 this 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 folder <InstallDir>/studio/samples/eclipse.
  4. If prompted to restart the Eclipse Workbench, click Yes.
  5. Eclipse shuts down and restarts.

To check that the Rule Definition aware of context Sample feature is enabled:

  1. Click Help > Software Updates > Manage Configuration.
  2. In the left pane of the Product Configuration page, expand C:\ILOG\JRules\studio\samples\eclipse and check that Rule Definition aware of context Sample 1.0.0. is listed.

You can now import the sample project files. 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 other rule projects in your default workspace.

To import the ruleawareofcontext sample:

  1. Click File > Switch Workspace and specify a new workspace directory.
  2. Eclipse shuts down and restarts with a blank workspace.
  3. Click File > Import.
  4. In the Import Wizard, select ILOG Rule Studio Samples and Tutorials.
  5. Click Next.
  6. On the Sample or Tutorial page, select the sample authoring > ruleawareofcontext.
  7. Click Finish.
  8. 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.
  9. The imported sample project files are displayed in the Rule Explorer. You should see two new projects:
    - ruleawareofcontext-rules: rule project
    - ruleawareofcontext-xom: XOM project

To set the new rule model extension:

  1. Click Window > Preferences.
  2. In the Preferences dialog, select ILOG Rule Studio > Rule Model Extension.
  3. Select Model: Default extension.
  4. Click Change from Workspace and navigate to the file ruleawareofcontext-rules > data > ruleawareofcontext.brmx.
  5. Select Data: Default extension.
  6. Click Change from Workspace and navigate to the file ruleawareofcontext-rules > data > ruleawareofcontext.brdx.
  7. Click OK.
  8. A dialog reminds you that you must restart Eclipse to see the changes.
  9. Click File > Switch Workspace and click OK without changing the workspace location.
  10. Eclipse shut downs and restarts. The rule model extension is now taken into account by Rule Studio. You may have to rebuild the rule project for the extension to be taken into account in the rules.

Running this Sample

To run the sample.

  1. Browse the ruleawareofcontext-rules project and click on the A1 rule package. The package property state is set to Alabama.
  2. Properties of the ruleawareofcontext-rules project
  3. Open the rule Albertville Silver rule in the rule package A1 and double-click Albertville in the rule definition.
  4. A list of Alabama cities appears in the completion list. This list is filtered based on the state parameter value set on the StatePackage A1.
    This values in the completion list of the Albertville Silver rule are filtered by the state parameter value set on the StatePackage A1
  5. Open the rule Barrow rule in the StatePackage A2 and click on "Barrow" in the rule definition. A list of Alaskan cities appears in the completion list.
  6. Open the No state rule package, which is not of type StatePackage (and therefore does not have the state property). Open the Barrow rule located in this package and click on the city value in the rule definition. The completion list shows the full list of cities associated with the BOM member (across the four states: Alabama, Alaska, Colorado, Florida).
  7. Examine the promotional codes in different rules and packages of the rule project. The list of available values is filtered in a similar way to the list of cities, except that the property used to filter the list is set directly on the rule.
  8. For example, open the Miami Bronze Rule in the F rule package and notice that the promo_category property is set to Bronze and that the completion list of promotional codes contains only codes belonging to that category.
    The completion list of promotional codes for the Miami Bronze Rule is filtered by the promo_category property
  9. In the Problems view, notice that some rules have errors, warnings and/or information messages relating to the values entered in the rule definition placeholders for the promotional code or city. Value checkers are used to decide if the value entered is valid or not, and to define how to handle the value in case a problem is detected.
  10. If a value is rejected by the value checker, you can either output an info, a warning or an error with a message. In our example:
    - An error is thrown when the input city is unknown and does not belong to any state
    - A warning is displayed when the promotional code does not exist, or the city does not belong to the state associated with the rule package
    - An info is displayed when the promotional code is not inside the promotional category of the rule
    Note
    Errors make the compilation of the rule fail, whereas warnings and information messages are just informational. You can still execute the rule project if one or more rules fail to compile but you might not have the desired execution output.
  11. In the Rule perspective, on the Run menu, click Run, select the ruleawareofcontext-rules launch configuration, then click Run.
  12. The execution output generated by setter methods inside the Customer class is displayed in the Console:
The salary of Mike Cooper is set to 40000
Mike Cooper is eligible for the Br promotional code.
Marc Green is eligible for the DF5 promotional code.
John Doe is eligible for the G23 promotional code.

Source Files

To display the source code of the ruleawareofcontext Eclipse plug-in:

  1. Click File > Import.
  2. In the Import Wizard, select Plug-in Development > 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.ruleawareofcontext plug-in (be careful not to select ilog.rules.studio.samples.ruleawareofcontext.source), then click Add.
  5. Click Finish.
  6. The source code of the plug-in is imported and appears in the Rule Explorer.
  7. Switch to the Plug-in Development perspective. In the Package Explorer, navigate to the Java source files in the ilog.rules.studio.samples.ruleawareofcontext > src-ruleawareofcontext and src-ruleawareofcontext-common folders.

The source files of interest are described in Highlights.

Highlights

In the sample rule project, two different ways of making the rule definition aware of the context are demonstrated:

In the model extension file (ruleawareofcontext.brmx), a new type of rule package (StatePackage) and a new type of business rule (PromoRule) have been defined. StatePackage contains a state property that is not in the default RulePackage class and PromoRule adds a promo_category property to the default ActionRule class used for business rule creation.

ruleawareofcontext.brmx resource set

The sample implements two specific value management interfaces and value infos that integrate these value management implementations. Our value management implementations access rule and package properties and offer filtered lists of values based on those properties.

Two valueinfo are defined for the sample.ruleawareofcontext.model.Customer class in the ruleawareofcontext > sample BOM entry:

isBillingAddress is a virtual method and uses BOM to XOM Mapping. The property name is valueInfo[0] because it is associated with the first argument of the method (valueInfo[1], valueInfo[2], etc. could be used to refer to second and subsequent arguments, if the method had any).

Custom property for the isBillingAddress method

promoCode is an attribute and the property name is valueInfo.

The property values are the keys of our ilog.rules.studio.model.brl.valueInfo extensions. You can see the definition of the extensions in the plugin.xml file of the sample plug-in.

The list of cities and promotional codes are defined in our value provider implementations (in the ruleawareofcontext-xom Java project):

sample.ruleawareofcontext.data.CityDataProviderImpl and sample.ruleawareofcontext.data.PromoCodeDataProviderImpl

The sample Eclipse plug-in has two source folders. One is used for Rule Studio code only (src) and one for code that can be shared between Rule Studio and Rule Team Server (commonsrc). If you look at the build.properties file in the plug-in root folder, you'll see that two distinct java archives are created (one for each source folder) in order to dissociate Rule Studio code from Rule Team Server code.

The getImpactedElements method is used to retrieve the list of impacted elements when this change occurs.

For the impact handler class to work, we need to extend the ilog.rules.studio.model.impactHandlers extension point.

Note
We need to use an impact handler here because changes to a property on a rule package might have an impact on the build of the rule artifacts inside this rule package.

For value providers:

For value checkers:

Related Concepts

Custom BOM Properties

Related Tasks

Managing BOM Value Types

Related Samples and Tutorials

How to Populate a BOM Domain from a Data Source
How to Use a Custom Value Editor in the Rule Editor
How to Write Rules Independently of the Execution Model
Import Samples and Tutorials Wizard