View Javadoc

1   package org.kuali.rice.krms.framework.engine;
2   
3   import java.util.Collection;
4   
5   import org.kuali.rice.krms.api.engine.ExecutionEnvironment;
6   import org.kuali.rice.krms.api.engine.ResultEvent;
7   import org.kuali.rice.krms.api.engine.Term;
8   import org.kuali.rice.krms.api.engine.TermResolutionException;
9   import org.kuali.rice.krms.framework.engine.result.BasicResult;
10  
11  public class CollectionOfComparablesTermBasedProposition<T> extends ComparableTermBasedProposition<T> {
12  	private static final ResultLogger LOG = ResultLogger.getInstance();
13  
14  	private CollectionOperator collectionOper;
15  	private Term term;
16  
17  	public CollectionOfComparablesTermBasedProposition(CollectionOperator collectionOper, ComparisonOperator compareOper, Term term, T expectedValue) {
18  		super(compareOper, term, expectedValue);
19  		this.term = term;
20  		this.collectionOper = collectionOper;
21  	}
22  
23  	/**
24  	 * @see org.kuali.rice.krms.framework.engine.ComparableTermBasedProposition#evaluate(org.kuali.rice.krms.api.engine.ExecutionEnvironment)
25  	 * @throws TermResolutionException if there is a problem resolving a {@link Term}
26  	 */
27  	@Override
28  	public boolean evaluate(ExecutionEnvironment environment) {
29  		boolean collatedResult = collectionOper.getInitialCollatedResult();
30  
31  		Collection<? extends Comparable<T>> termValue;
32  
33  		termValue = environment.resolveTerm(term);
34  
35  		if (termValue != null) {
36  			for (Comparable<T> item : termValue) {
37  				collatedResult = collectionOper.reduce(compare(item), collatedResult);
38  				if (collectionOper.shortCircuit(collatedResult)) break;
39  			}
40  		}
41  
42  		// TODO: log this appropriately
43  		if (LOG.isEnabled(environment)) {
44  			LOG.logResult(new BasicResult(ResultEvent.PropositionEvaluated, this, environment, collatedResult));
45  		}
46  
47  		return collatedResult;
48  	}
49  
50  	public String toString() {
51  		StringBuilder sb = new StringBuilder();
52  		sb.append(collectionOper.toString());
53  		sb.append(" "+super.toString());
54  		return sb.toString();
55  	}
56  }