View Javadoc

1   /**
2    * Copyright 2005-2012 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  /**
25   * A {@link org.kuali.rice.krms.framework.engine.Rule} that executes a {@link org.kuali.rice.krms.framework.engine.Action} when the {@link Proposition} is true.
26   *
27   * @author Kuali Rice Team (rice.collab@kuali.org)
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       * Constructor
38       * @param name Rule name to set the name to
39       * @param proposition {@link Proposition} to set the proposition to, cannot be null
40       * @param actions Rule Actions to set the actions to
41       * @throws IllegalArgumentException if the {@link Proposition} is null
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       * Constructor
54       * @param proposition {@link Proposition} to set the proposition to, cannot be null
55       * @param actions Rule Actions to set the actions to
56       * @throws IllegalArgumentException if the {@link Proposition} is null
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.RULE_EVALUATED, this, environment, result));
74  		}
75  		return result;
76  	}
77  
78      /**
79       * Based on the ruleExecutionResult should the {@link Action} be executed?  Default behavior is to return the given ruleExecutionResult.
80       * Over-writable by subclasses.
81       * @param ruleExecutionResult the result of the engines evaluation method.
82       * @return boolean should the action execute
83       */
84  	protected boolean shouldExecuteAction(boolean ruleExecutionResult) {
85  		return ruleExecutionResult;
86  	}
87  
88      /**
89       * Return the Rule name
90       * @return name Rule name
91       */
92  	public String getName() {
93  		return name;
94  	}
95  	
96      @Override
97  	public String toString(){
98  		return name;
99  	}
100 }