001/**
002 * Copyright 2005-2015 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.Collection;
019
020import org.kuali.rice.krms.api.engine.ExecutionEnvironment;
021import org.kuali.rice.krms.api.engine.ResultEvent;
022import org.kuali.rice.krms.api.engine.Term;
023import org.kuali.rice.krms.api.engine.TermResolutionException;
024import org.kuali.rice.krms.framework.engine.expression.ComparisonOperator;
025import org.kuali.rice.krms.framework.engine.result.BasicResult;
026
027public class CollectionOfComparablesTermBasedProposition<T> extends ComparableTermBasedProposition<T> {
028        private static final ResultLogger LOG = ResultLogger.getInstance();
029
030        private CollectionOperator collectionOper;
031        private Term term;
032
033        public CollectionOfComparablesTermBasedProposition(CollectionOperator collectionOper, ComparisonOperator compareOper, Term term, T expectedValue) {
034                super(compareOper, term, expectedValue);
035                this.term = term;
036                this.collectionOper = collectionOper;
037        }
038
039        /**
040         * @see org.kuali.rice.krms.framework.engine.ComparableTermBasedProposition#evaluate(org.kuali.rice.krms.api.engine.ExecutionEnvironment)
041         * @throws TermResolutionException if there is a problem resolving a {@link Term}
042         */
043        @Override
044        public PropositionResult evaluate(ExecutionEnvironment environment) {
045                boolean collatedResult = collectionOper.getInitialCollatedResult();
046
047                Collection<? extends Comparable<T>> termValue;
048
049                termValue = environment.resolveTerm(term, this);
050
051                if (termValue != null) {
052                        for (Comparable<T> item : termValue) {
053                                collatedResult = collectionOper.reduce(compare(item), collatedResult);
054                                if (collectionOper.shortCircuit(collatedResult)) break;
055                        }
056                }
057
058                // TODO: log this appropriately
059                if (LOG.isEnabled(environment)) {
060                        LOG.logResult(new BasicResult(ResultEvent.PROPOSITION_EVALUATED, this, environment, collatedResult));
061                }
062
063                return new PropositionResult(collatedResult);
064        }
065
066        public String toString() {
067                StringBuilder sb = new StringBuilder();
068                sb.append(collectionOper.toString());
069                sb.append(" "+super.toString());
070                return sb.toString();
071        }
072}