001/**
002 * Copyright 2005-2016 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.kuali.rice.krms.framework.engine;
017
018import java.util.Collections;
019import java.util.List;
020
021import org.kuali.rice.krms.api.engine.ExecutionEnvironment;
022import org.kuali.rice.krms.api.engine.ResultEvent;
023import org.kuali.rice.krms.api.engine.Term;
024import org.kuali.rice.krms.api.engine.TermResolutionException;
025import org.kuali.rice.krms.framework.engine.expression.ComparisonOperator;
026import org.kuali.rice.krms.framework.engine.result.BasicResult;
027
028/**
029 * An implementation of {@link Proposition} which uses a {@link ComparisonOperator} and {@link Term}
030 * @param <T>
031 *
032 * @author Kuali Rice Team (rice.collab@kuali.org)
033 */
034public class ComparableTermBasedProposition<T> implements Proposition {
035        private static final ResultLogger LOG = ResultLogger.getInstance();
036
037        private final ComparisonOperator operator;
038        private final Term term;
039        private final T expectedValue;
040
041    /**
042     * Create a ComparableTermBasedProposition with the given values
043     * @param operator {@link ComparisonOperator} to set the operator to
044     * @param term {@link Term} to set the term to
045     * @param expectedValue to set the expectedValue to
046     */
047        public ComparableTermBasedProposition(ComparisonOperator operator, Term term, T expectedValue) {
048                this.operator = operator;
049                this.term = term;
050                this.expectedValue = expectedValue;
051        }
052
053        /**
054         * @see org.kuali.rice.krms.framework.engine.Proposition#evaluate(org.kuali.rice.krms.api.engine.ExecutionEnvironment)
055         * @throws TermResolutionException if there is a problem resolving the {@link Term}
056         */
057        @Override
058        public PropositionResult evaluate(ExecutionEnvironment environment) {
059                Comparable<T> termValue;
060
061                termValue = environment.resolveTerm(term, this);
062
063                boolean result = compare(termValue);
064
065                if (LOG.isEnabled(environment)){
066                        LOG.logResult(new BasicResult(ResultEvent.PROPOSITION_EVALUATED, this, environment, result));
067                }
068                return new PropositionResult(result);
069        }
070
071        /**
072         * Compares the term value w/ the expected value
073         *
074         * @param termValue Comparable which makes up the {@link ComparisonOperator}.compare() left hand side object
075         * @return the boolean result of the comparison
076         */
077        protected boolean compare(Comparable<T> termValue) {
078                boolean result = Boolean.valueOf(operator.compare(termValue, getExpectedValue()));
079                return result;
080        }
081
082    /**
083     * Returns an empty list.  Collections.emptyList()
084     *
085     * {@inheritDoc}
086     *
087     * @return an empty list.  Collections.emptyList()
088     */
089        @Override
090        public List<Proposition> getChildren() {
091            return Collections.emptyList();
092        }
093        
094        @Override
095        public boolean isCompound() {
096            return false;
097        }
098
099        /**
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}