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.Collections;
19  import java.util.List;
20  
21  import org.kuali.rice.krms.api.engine.ExecutionEnvironment;
22  import org.kuali.rice.krms.api.engine.ResultEvent;
23  import org.kuali.rice.krms.api.engine.Term;
24  import org.kuali.rice.krms.api.engine.TermResolutionException;
25  import org.kuali.rice.krms.framework.engine.expression.ComparisonOperator;
26  import org.kuali.rice.krms.framework.engine.result.BasicResult;
27  
28  /**
29   * An implementation of {@link Proposition} which uses a {@link ComparisonOperator} and {@link Term}
30   * @param <T>
31   *
32   * @author Kuali Rice Team (rice.collab@kuali.org)
33   */
34  public class ComparableTermBasedProposition<T> implements Proposition {
35  	private static final ResultLogger LOG = ResultLogger.getInstance();
36  
37  	private final ComparisonOperator operator;
38  	private final Term term;
39  	private final T expectedValue;
40  
41      /**
42       * Create a ComparableTermBasedProposition with the given values
43       * @param operator {@link ComparisonOperator}
44       * @param term {@link Term}
45       * @param expectedValue
46       */
47  	public ComparableTermBasedProposition(ComparisonOperator operator, Term term, T expectedValue) {
48  		this.operator = operator;
49  		this.term = term;
50  		this.expectedValue = expectedValue;
51  	}
52  
53  	/**
54  	 * @see org.kuali.rice.krms.framework.engine.Proposition#evaluate(org.kuali.rice.krms.api.engine.ExecutionEnvironment)
55  	 * @throws TermResolutionException if there is a problem resolving the {@link Term}
56  	 */
57  	@Override
58  	public PropositionResult evaluate(ExecutionEnvironment environment) {
59  		Comparable<T> termValue;
60  
61  		termValue = environment.resolveTerm(term, this);
62  
63  		boolean result = compare(termValue);
64  
65  		if (LOG.isEnabled(environment)){
66  			LOG.logResult(new BasicResult(ResultEvent.PropositionEvaluated, this, environment, result));
67  		}
68  		return new PropositionResult(result);
69  	}
70  
71  	/**
72  	 * This method does the actual comparison of the term value w/ the expected value
73  	 *
74  	 * @param termValue Comparable which makes up the {@link ComparisonOperator}.compare() left hand side object
75  	 * @return the boolean result of the comparison
76  	 */
77  	protected boolean compare(Comparable<T> termValue) {
78  		boolean result = Boolean.valueOf(operator.compare(termValue, getExpectedValue()));
79  		return result;
80  	}
81  	
82  
83  	@Override
84  	public List<Proposition> getChildren() {
85  	    return Collections.emptyList();
86  	}
87  	
88  	@Override
89  	public boolean isCompound() {
90  	    return false;
91  	}
92  
93  	/**
94  	 * @return the expectedValue
95  	 */
96  	protected T getExpectedValue() {
97  		return this.expectedValue;
98  	}
99  
100     @Override
101 	public String toString(){
102 		StringBuilder sb = new StringBuilder();
103 		sb.append(term.toString());
104 		sb.append(" "+operator.toString());
105 		sb.append(" "+expectedValue.toString());
106 		return sb.toString();
107 	}
108 }