View Javadoc

1   /**
2    * Copyright 2005-2013 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 org.kuali.rice.krms.api.engine.ExecutionEnvironment;
19  
20  /**
21   * A {@link AgendaTreeEntry} which executes its ifTrue {@link AgendaTree} if the given {@link Rule} result is true or
22   * its ifFalse {@link AgendaTree} if the result is false.
23   *
24   * @author Kuali Rice Team (rice.collab@kuali.org)
25   */
26  public final class BasicAgendaTreeEntry implements AgendaTreeEntry {
27  	
28  	private final Rule rule;
29  	private final AgendaTree ifTrue;
30  	private final AgendaTree ifFalse;
31  
32      /**
33       * Create a BasicAgendaTreeEntry with the given {@link Rule}.
34       * @param rule {@link Rule} to create the BasicAgendaTreeEntry with.
35       * @throws IllegalArgumentException if the rule is null.
36       */
37  	public BasicAgendaTreeEntry(Rule rule) {
38  		this(rule, null, null);
39  	}
40  
41      /**
42       * Create a BasicAgendaTreeEntry with the given {@link Rule} and ifTrue, ifFalse {@link AgendaTree}s.
43       * @param rule {@link Rule} to create the BasicAgendaTreeEntry with.
44       * @param ifTrue executed if the given rule's result is true.
45       * @param ifTrue executed if the given rule's result is false.
46       * @throws IllegalArgumentException if the rule is null.
47       */
48  	public BasicAgendaTreeEntry(Rule rule, AgendaTree ifTrue, AgendaTree ifFalse) {
49  		if (rule == null) {
50  			throw new IllegalArgumentException("rule was null");
51  		}
52  		this.rule = rule;
53  		this.ifTrue = ifTrue;
54  		this.ifFalse = ifFalse;
55  	}
56  	
57  	@Override
58  	public void execute(ExecutionEnvironment environment) {
59  		boolean result = rule.evaluate(environment);
60  		if (result && ifTrue != null) {
61  			ifTrue.execute(environment);
62  		}
63  		if (!result && ifFalse != null) {
64  			ifFalse.execute(environment);
65  		}
66  	}
67  	
68  }