A classifier trigger point is identical to a simple trigger point except
that it only finds rules marked as being classifiers. These are rules whose
purpose is to determine what sort of business situation is present and return
a classification string indicating the result. Usually these rules are used
as part of a situational trigger point, but they can be triggered on their
own too. This type of trigger point is used by invoking the triggerClassifier
method
on an instance of the TriggerPoint
class.
The following shows an example of using a classifier trigger point to trigger
a rule named determineCustomerLevel
(in folder com/acme/customerClassfiers
).
This rule classifies customers into levels (Gold, Silver, and Bronze) based
on their spending history.
...
// 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 paramdter list that's passed to the rule
Object [ ] plist = new Object[1];
// information about the customer to be checked is stored in this object
Customer cust = ...;
// define name of rule to be fired
String ruleName = "com/acme/customerClassifiers/determineCustomerLevel";
// define result of rule firing
Object result = null;
// initialize parameter list
plist[0] = cust;
try {
// fire "com/acme/customerClassifiers/determineCustomerLevel" rule passing parameter
// list containing the customer to be checked.
// Note: in this case the target object is not used and could be null.
result = tp.triggerClassifier(this, plist, ruleName);
// 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.
String customerLevel = (String) result;
// make use of result
if( customerLevel.equals("Gold") ) {
...
} else if ( customerLevel.equals("Silver") ) {
...
} else if ( customerLevel.equals("Bronze") ) {
...
} else {
...
}
}
catch(BusinessRuleBeansException e ) {
// handle exception
...
}