| Samples > Rule Studio Authoring Extensions > How to Make a Rule Definition Aware of Context |
How to Make a Rule Definition Aware of Context |
PREVIOUS NEXT |
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 include:
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:
<InstallDir>/studio/samples/eclipse.
To check that the Rule Definition aware of context Sample feature is enabled:
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:
ILOG Rule Studio Samples and Tutorials.
authoring > ruleawareofcontext.
To set the new rule model extension:
ruleawareofcontext-rules > data > ruleawareofcontext.brmx.
ruleawareofcontext-rules > data > ruleawareofcontext.brdx.
To run the sample.
ruleawareofcontext-rules project and click on the A1 rule package. The package property state is set to Alabama.
Albertville Silver rule in the rule package A1 and double-click Albertville in the rule definition.
state parameter value set on the StatePackage A1.
Barrow rule in the StatePackage A2 and click on "Barrow" in the rule definition. A list of Alaskan cities appears in the completion list.
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).
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.
ruleawareofcontext-rules launch configuration, then click Run.
To display the source code of the ruleawareofcontext Eclipse plug-in:
Plug-in Development > Plug-ins and Fragments and click Next.
ilog.rules.studio.samples.ruleawareofcontext plug-in (be careful not to select ilog.rules.studio.samples.ruleawareofcontext.source), then click Add.
ilog.rules.studio.samples.ruleawareofcontext > src-ruleawareofcontext and src-ruleawareofcontext-common folders.
The source files of interest are described in Highlights.
In the sample rule project, two different ways of making the rule definition aware of the context are demonstrated:
state property attached to a rule package.
promo_category rule property.
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.
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 method for the list of cities.
promoCode attribute for the list of promotional codes.
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).
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.
sample.ruleawareofcontext.model package contains the same code as the XOM project in Rule Studio: ruleawareofcontext-xom. To keep the sample as simple as possible, we duplicated the existing code. You could also reference a jar file (if you want to reuse the same code as your XOM), or use some other code. This model is used as a basis for our value providers and checkers. Depending on your use of valueinfo, you might have a model that is completely different from your XOM.
sample.ruleawareofcontext.data contains the data provider interfaces and implementations used by our value providers and checkers. It populates the list of states and cities as well as the promotional categories and codes.
sample.ruleawareofcontext.value package in the commonsrc folder and the ilog.rules.studio.samples.ruleawareofcontext.value package in the src folder contain the source of the value infos, providers and checkers. In the first package, the value info classes (CityValueInfo and PromoCodeValueInfo) are just grouping value providers and checker services. The names of the classes used for the value checkers and providers are retrieved from a property file (value.properties) located in the src folder.
ilog.rules.studio.samples.ruleawareofcontext.impacthandler package contains the impact handler class (StatePackageImpactHandler) used to propagate the changes to the rule artifacts inside a rule package of type StatePackage, or its sub packages, when its state property changes.
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:
getCurrentPromoCategory method in CityStudioValueProvider is used to retrieve a package property given a language rule element.
prepare method in CityValueProvider that is extended by CityStudioValueProvider.
getValues is called.
PromoCodeValueProvider works the same as CityStudioValueProvider. The only difference is that getCurrentPromoCategory retrieves a property on the rule itself and not on its package like getCurrentPromoCategory for CityStudioValueProvider. The prepare and getValues methods are similar to the ones in CityValueProvider.
For value checkers:
CityValueChecker and its Rule Studio specific implementation CityStudioValueChecker: The value checker is linked to the value provider in order to know the state associated with the rule. The value checker is also linked to the data provider to check if the city passed as the value Object argument to the check method is known. In the check method, if the value is not accepted, we can parametrize an IlrValueError object with a message that will be displayed in the Problems view and define how it will be rendered (i.e. its severity: warning or error).
PromoCodeValueChecker is similar to CityValueChecker. Examine the check method.
| Copyright © 1987-2008 ILOG S.A. All rights reserved. Legal terms. Documentation homepage. | PREVIOUS NEXT |