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} to set the operator to
44       * @param term {@link Term} to set the term to
45       * @param expectedValue to set the expectedValue to
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.PROPOSITION_EVALUATED, this, environment, result));
67  		}
68  		return new PropositionResult(result);
69  	}
70  
71  	/**
72  	 * Compares 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       * Returns an empty list.  Collections.emptyList()
84       *
85       * {@inheritDoc}
86       *
87       * @return an empty list.  Collections.emptyList()
88       */
89    	@Override
90  	public List<Proposition> getChildren() {
91  	    return Collections.emptyList();
92  	}
93  	
94  	@Override
95  	public boolean isCompound() {
96  	    return false;
97  	}
98  
99  	/**
100      * Returns the expectedValue
101 	 * @return the expectedValue
102 	 */
103 	protected T getExpectedValue() {
104 		return this.expectedValue;
105 	}
106 
107     @Override
108 	public String toString(){
109 		StringBuilder sb = new StringBuilder();
110 		sb.append(term.toString());
111 		sb.append(" "+operator.toString());
112 		sb.append(" "+expectedValue.toString());
113 		return sb.toString();
114 	}
115 }