Context-aware rule definition sample details
You use Rule Designer to run the context-aware rule definition sample.
Running this sample
You use Rule Designer to run the sample in your new workspace.
- In Rule Explorer, open the ruleawareofcontext-rules project
and click the A1 rule package. The package property state is
set to Alabama.

- Open the rule package A1 and double-click Albertville Silver rule.
- Double-click Albertville in the rule definition.
A list of Alabama cities opens in the completion list. This list is filtered based on the state parameter value set on the StatePackage A1.

- Open the rule Barrow rule in the rule package A2 and double-click Barrow in the rule definition. A list of cities in Alaska opens in the completion list.
- Open the No state rule package, which is not of type StatePackage (and therefore does not have the state property). Open the Barrow rule in this package and click the city value in the rule definition. The completion list shows the full list of cities that are associated with the BOM member (across the four states: Alabama, Alaska, Colorado, Florida).
- 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.
For example, open the F rule package and double-click Miami Bronze Rule. Notice that the promo_category property is set to Bronze and that the completion list of promotional codes contains only codes that belong to that category.

- In the Problems view, notice that some rules have errors, warnings, or information messages that
relate 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 when a problem is detected.Note: Errors make the compilation of the rule fail, whereas warnings and information messages are informational.If a value is rejected by the value checker, you can either output an info, a warning, or an error with a message. In this example:
- An error is thrown when the input city is unknown and does not belong to any state.
- A warning displays when the promotional code does not exist, or the city does not belong to the state associated with the rule package.
- An info displays when the promotional code is not inside the promotional category of the rule.
- Open the rule package C1, and double-click Unknown city Gold rule.
- In the rule statement, double-click UnknownCity and select Denver. Save your changes: the error is no longer displayed.
- Open the Bad Promotional code rule in the C1 rule package. In the rule statement, double-click BAD and select F6. Save your changes: the warning is no longer displayed.
- In the Rule perspective, on the Run menu, click Run
Configurations, select the Decision Operation >
ruleawareofcontext-rules launch configuration, and then click
Run.
The Console displays the execution output that is generated by setter methods inside the Customer class:
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.
How this sample works
- The sample rule project demonstrates two different ways of making
the rule definition aware of the context:
- By filtering a list of cities based on a state property that is attached to a rule package.
- By filtering a list of promotional codes with a promotional category defined by a
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) are 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. The value management implementations access rule and package properties and offer filtered lists of values based on those properties.
- Two valueinfo values are defined for the sample.ruleawareofcontext.model.Customer class
in the ruleawareofcontext > sample BOM entry:
- One on the isBillingAddress method for the list of cities.
- One on the 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] might 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 the ilog.rules.studio.model.brl.valueInfo extensions. The definition of the extensions is in the plugin.xml file, which is in the ilog.rules.studio.samples.ruleawareofcontext package of the sample plug-in.
- The list of cities and promotional codes are defined in the value
provider implementations (in the ruleawareofcontext-xom Java™ project):
sample.ruleawareofcontext.data.CityDataProviderImpl and sample.ruleawareofcontext.data.PromoCodeDataProviderImpl
- Eclipse plug-in:
- The sample.ruleawareofcontext.model package contains the same code as the XOM project in Rule Designer: ruleawareofcontext-xom. To keep the sample as simple as possible, the existing code is duplicated. You can also reference a jar file (to reuse the same code as your XOM), or use some other code. This model is used as a basis for the value providers and checkers. Depending on your use of valueinfo, you might have a model that is different from your XOM.
- The sample.ruleawareofcontext.data contains the data provider interfaces and implementations that are used by the value providers and checkers. It populates the list of states and cities as well as the promotional categories and codes.
- The 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 for the value checkers and providers are retrieved from a property file (value.properties) in the src folder.
- The ilog.rules.studio.samples.ruleawareofcontext.impacthandler package
contains the impact handler class (StatePackageImpactHandler) which propagates
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 retrieves the list of affected elements when this change occurs.
Extension of the ilog.rules.studio.model.impactHandlers extension point is required for the impact handler class to work.
Note: Use of an impact handler ensures that changes to a property on a rule package do not have an impact on the build of the rule artifacts inside this rule package.
Note: The sample Eclipse plug-in has two source folders. One is used for Rule Designer code only (src) and one for code that can be shared between Rule Designer and Decision Center (commonsrc). If you look at the build.properties file in the plug-in root folder, you note that two distinct java archives are created (one for each source folder) to dissociate Rule Designer code from Decision Center code. - Value providers:
- The getCurrentState method in CityStudioValueProvider
retrieves a package property given a language rule element.
The getCurrentState method is called from the prepare method in CityValueProvider that is extended by CityStudioValueProvider.
The prepare method gets the rule that is being edited from the syntax tree node.
You can use the rule to retrieve the package and its properties, including the state. The state returns a filter list of cities when 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 getCurrentState for CityStudioValueProvider. The prepare and getValues methods are similar to the ones in CityValueProvider.
- The getCurrentState method in CityStudioValueProvider
retrieves a package property given a language rule element.
- Value checkers:
- CityValueChecker and its Rule Designer specific implementation CityStudioValueChecker: The value checker is linked to the value provider to know the state that is associated with the rule. The value checker is also linked to the data provider to check whether the city passed as the value Object argument to the check method is known. In the check method, if the value is not accepted, you can parametrize an IlrValueError object with a message to display in the Problems view and define how it is rendered (for example, its severity: warning or error).
- PromoCodeValueChecker is similar to CityValueChecker. Examine the check method.
Source files
- Click .
- In the Import wizard open Plug-in Development, select Plug-ins and Fragments 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 and select the ilog.rules.studio.samples.ruleawareofcontext plug-in, and then click Add.
- Click Finish.
Rule Explorer displays the source code of the plug-in.
- Switch to the Plug-in Development perspective. In the Package Explorer, navigate to the Java source files in the ilog.rules.studio.samples.ruleawareofcontext > ruleawareofcontextsrc and ruleawareofcontext-commonsrc folders.