View Javadoc

1   /**
2    * Copyright 2005-2011 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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  public class BasicRule implements Rule {
25  	private static final ResultLogger LOG = ResultLogger.getInstance();
26  
27  	private String name;
28  	private Proposition proposition;
29  	private List<Action> actions;
30  	
31  	public BasicRule(String name, Proposition proposition, List<Action> actions) {
32  		if (proposition == null) {
33  			throw new IllegalArgumentException("Proposition cannot be null.");
34  		}
35  		this.name = name;
36  		this.proposition = proposition;
37  		this.actions = actions;
38  	}
39  	
40  	public BasicRule(Proposition proposition, List<Action> actions) {
41  		this(null, proposition, actions);
42  	}
43  	
44  	@Override
45  	public boolean evaluate(ExecutionEnvironment environment) {
46  		boolean result = proposition.evaluate(environment).getResult();
47  		if (actions != null) {
48  			for (Action action : actions) {
49  				if (shouldExecuteAction(result)) {
50  					action.execute(environment);
51  				}
52  			}
53  		}
54  		if (LOG.isEnabled(environment)){
55  			LOG.logResult(new BasicResult(ResultEvent.RuleEvaluated, this, environment, result));
56  		}
57  		return result;
58  	}
59  	
60  	protected boolean shouldExecuteAction(boolean ruleExecutionResult) {
61  		return ruleExecutionResult;
62  	}
63  
64  	public String getName() {
65  		return name;
66  	}
67  	
68  	public String toString(){
69  		return name;
70  	}
71  }