1   
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  
15  
16  package org.kuali.rice.krms.framework.engine;
17  
18  import java.util.List;
19  
20  import org.kuali.rice.krms.api.engine.ExecutionEnvironment;
21  import org.kuali.rice.krms.api.engine.ResultEvent;
22  import org.kuali.rice.krms.framework.engine.result.BasicResult;
23  
24  
25  
26  
27  
28  
29  public class BasicRule implements Rule {
30  	private static final ResultLogger LOG = ResultLogger.getInstance();
31  
32  	private String name;
33  	private Proposition proposition;
34  	private List<Action> actions;
35  
36      
37  
38  
39  
40  
41  
42  
43      public BasicRule(String name, Proposition proposition, List<Action> actions) {
44  		if (proposition == null) {
45  			throw new IllegalArgumentException("Proposition cannot be null.");
46  		}
47  		this.name = name;
48  		this.proposition = proposition;
49  		this.actions = actions;
50  	}
51  
52      
53  
54  
55  
56  
57  
58  	public BasicRule(Proposition proposition, List<Action> actions) {
59  		this(null, proposition, actions);
60  	}
61  	
62  	@Override
63  	public boolean evaluate(ExecutionEnvironment environment) {
64  		boolean result = proposition.evaluate(environment).getResult();
65  		if (actions != null) {
66  			for (Action action : actions) {
67  				if (shouldExecuteAction(result)) {
68  					action.execute(environment);
69  				}
70  			}
71  		}
72  		if (LOG.isEnabled(environment)){
73  			LOG.logResult(new BasicResult(ResultEvent.RuleEvaluated, this, environment, result));
74  		}
75  		return result;
76  	}
77  
78      
79  
80  
81  
82  
83  	protected boolean shouldExecuteAction(boolean ruleExecutionResult) {
84  		return ruleExecutionResult;
85  	}
86  
87      
88  
89  
90  
91  	public String getName() {
92  		return name;
93  	}
94  	
95      @Override
96  	public String toString(){
97  		return name;
98  	}
99  }