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.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.result.BasicResult;
26  
27  public class ComparableTermBasedProposition<T> implements Proposition {
28  	private static final ResultLogger LOG = ResultLogger.getInstance();
29  
30  	private final ComparisonOperator operator;
31  	private final Term term;
32  	private final T expectedValue;
33  
34  
35  	public ComparableTermBasedProposition(ComparisonOperator operator, Term term, T expectedValue) {
36  		this.operator = operator;
37  		this.term = term;
38  		this.expectedValue = expectedValue;
39  	}
40  
41  	/**
42  	 * @see org.kuali.rice.krms.framework.engine.Proposition#evaluate(org.kuali.rice.krms.api.engine.ExecutionEnvironment)
43  	 * @throws TermResolutionException if there is a problem resolving the {@link Term}
44  	 */
45  	@Override
46  	public PropositionResult evaluate(ExecutionEnvironment environment) {
47  		Comparable<T> termValue;
48  
49  		termValue = environment.resolveTerm(term, this);
50  
51  		boolean result = compare(termValue);
52  
53  		if (LOG.isEnabled(environment)){
54  			LOG.logResult(new BasicResult(ResultEvent.PropositionEvaluated, this, environment, result));
55  		}
56  		return new PropositionResult(result);
57  	}
58  
59  	/**
60  	 * This method does the actual comparison of the term value w/ the expected value
61  	 *
62  	 * @param termValue
63  	 * @return the boolean result of the comparison
64  	 */
65  	protected boolean compare(Comparable<T> termValue) {
66  		boolean result = Boolean.valueOf(operator.compare(termValue, getExpectedValue()));
67  		return result;
68  	}
69  	
70  
71  	@Override
72  	public List<Proposition> getChildren() {
73  	    return Collections.emptyList();
74  	}
75  	
76  	@Override
77  	public boolean isCompound() {
78  	    return false;
79  	}
80  
81  	/**
82  	 * @return the expectedValue
83  	 */
84  	protected T getExpectedValue() {
85  		return this.expectedValue;
86  	}
87  
88  	public String toString(){
89  		StringBuilder sb = new StringBuilder();
90  		sb.append(term.toString());
91  		sb.append(" "+operator.toString());
92  		sb.append(" "+expectedValue.toString());
93  		return sb.toString();
94  	}
95  }