![]() |
|
A situational trigger point is used when the rule(s) to be triggered depend on the business situation. This example evaluates a customer's past purchasing history to place them into one of three levels (Gold, Silver, or Bronze) which in turn determines how much of a discount they receive.
To use a situational trigger point to handle this case, it is first necessary to define four rules:
All of the classified rules have the same name and each is marked as applying to one of the three customer levels by specifying the level in its classification attribute. For example, the rule to determine the discount for a Gold level customer will contain the string "Gold" in its classification attribute.
The situational trigger point then proceeds in two phases:
The following shows an example of using a situational trigger point to handle the case described above.
...
// create an instance of TriggerPoint for triggering the rule and specify that the
// ReturnFirstCombiningStrategy is to be used to return only the first result if
// multiple rules are found.
TriggerPoint tp = new TriggerPoint();
tp.setCombiningStrategy(CombiningStrategy.RETURN_FIRST, TriggerPoint.ALL_RULES);
// define parameter list that's passed to the classifier rule
Object [ ] classifierPlist = new Object[1];
// define parameter list that's passed to the classified rule
Object [ ] classifiedPlist = new Object[1];
// information about the customer to be checked is stored in this object
Customer cust = ...;
// define name of classifier rule to be fired
String classifierRuleName = "com/acme/customerClassifiers/determineCustomerLevel";
// define name of classified rule to be fired
String classifiedRuleName = "com/acme/discountRules/determineDiscount";
// define result of rule firing
Object result = null;
// initialize parameter lists
classifierPlist[0] = cust;
classifiedPlist[0] = cust;
try {
// fire the rules to get the discount to offer
// Note: in this case the target object is not used and could be null.
result = tp.triggerSituational(this, classifiedPlist, classifierPlist, classifiedRuleName, classifierRuleName);
// put result into usable format. A single result is returned since we specified to use
// the ReturnFirstCombiningStrategy. By default an array of results would be returned.
Float discountToOffer = (Float) result;
// make use of result
...
}
catch(BusinessRuleBeansException e ) {
// handle exception
...
}
Related concepts... | |
Parent: Types of Trigger Points - overview | |
Simple Trigger Point - an example | |
Classifier Trigger Point - an example | |
Using strategy objects to control triggers | |
View PDF file... | |
To view a PDF file containing this article and related articles, click: | |
![]() | |
![]() |
![]() |