private Long id;
private String firstName;
private String lastName;
private DateTime birthDate;
private String ageCategory;
// Getter/setter methods omitted
public String toString() {
return "Contact - Id: " + id + ", First name: " + firstName
+ ", Last name: " + lastName + ", Birthday: " + birthDate
+ ", Age category: " + ageCategory;
}
}
In Listing 22-7, the Contact class is a simple contact information. For the ageCategory property, we
want to develop a dynamic rule that can be used to perform classification. The rule will calculate the age
based on the birthDate property and then assign the ageCategory property (for example, kid, youth,
adult, and so on) based on the rule.
Implementing the Rule Engine
The next step is to develop a simple rule engine for applying the rules on the domain object. First we
need to define what information a rule needs to contain. Listing 22-8 shows the Rule class, which is a
Groovy class (the file name is Rule.groovy).
Listing 22-8. The Rule Class
package com.apress.prospring3.ch22.rule.domain
class Rule {
private
boolean singlehit = true
private
conditions = new ArrayList()
private
actions = new ArrayList()
private
parameters = new ArrayList()
}
In Listing 22-8, each rule has several properties. The conditions property defines the various
conditions that the rule engine should check for with the domain object under processing. The actions
property defines the actions to take when a match on the condition is hit. The parameters property
defines the behavior of the rule, which is the outcome of the action for different conditions. Finally, the
singlehit property defines whether the rule should end its execution immediately whenever a match of
condition is found.
The next step is the engine for rule execution. Listing 22-9 shows the RuleEngine interface (note it's a
Java interface).
Search WWH :
Custom Search
Previous Page
Spring Framework 3 Topic Index
Next Page
Spring Framework 3 Bookmarks
Home