Listing 22-9. The RuleEngine Interface
package com.apress.prospring3.ch22.rule.engine;
import com.apress.prospring3.ch22.rule.domain.Rule;
public interface RuleEngine {
public void run(Rule rule, Object object);
}
The interface defines only a method run(), which is to apply the rule to the domain object argument.
We will provide the implementation of the rule engine in Groovy. Listing 22-10 shows the Groovy
class RuleEngineImpl (the file name is RuleEngineImpl.groovy).
Listing 22-10. The RuleEngineImpl Groovy Class
package com.apress.prospring3.ch22.rule.engine.impl
import org.springframework.stereotype.Component
import com.apress.prospring3.ch22.rule.domain.Rule
import com.apress.prospring3.ch22.rule.engine.RuleEngine
@Component("ruleEngine")
class RuleEngineImpl implements RuleEngine {
public void run(Rule rule, Object object) {
println "Executing rule"
def exit=false // Exit flag for singlehit mode
// Iterate over the parameter sets
rule.parameters.each{ArrayList params ->
def paramIndex=0 // Points to the current parameter
def success=true
if(!exit){
// Check all conditions
rule.conditions.each{
println "Condition Param index: " + paramIndex
success = success && it(object,params[paramIndex])
println "Condition success: " + success
paramIndex++
}
// If all conditions true, perform actions
if(success && !exit){
rule.actions.each{
println "Action Param index: " + paramIndex
it(object,params[paramIndex])
paramIndex++
Search WWH :
Custom Search
Previous Page
Spring Framework 3 Topic Index
Next Page
Spring Framework 3 Bookmarks
Home