1 | |
package org.kuali.rice.krms.framework.engine; |
2 | |
|
3 | |
import org.kuali.rice.krms.api.Asset; |
4 | |
import org.kuali.rice.krms.api.AssetResolutionException; |
5 | |
import org.kuali.rice.krms.api.ExecutionEnvironment; |
6 | |
import org.kuali.rice.krms.api.Proposition; |
7 | |
import org.kuali.rice.krms.api.ResultEvent; |
8 | |
import org.kuali.rice.krms.framework.engine.result.BasicResult; |
9 | |
|
10 | |
public class ComparableTermBasedProposition<T> implements Proposition { |
11 | 0 | private static final ResultLogger LOG = ResultLogger.getInstance(); |
12 | |
|
13 | |
private final ComparisonOperator operator; |
14 | |
private final Asset term; |
15 | |
private final T expectedValue; |
16 | |
|
17 | |
|
18 | 0 | public ComparableTermBasedProposition(ComparisonOperator operator, Asset term, T expectedValue) { |
19 | 0 | this.operator = operator; |
20 | 0 | this.term = term; |
21 | 0 | this.expectedValue = expectedValue; |
22 | 0 | } |
23 | |
|
24 | |
@Override |
25 | |
public boolean evaluate(ExecutionEnvironment environment) { |
26 | |
Comparable<T> termValue; |
27 | |
try { |
28 | 0 | termValue = environment.resolveTerm(term); |
29 | 0 | } catch (AssetResolutionException e) { |
30 | |
|
31 | 0 | throw new RuntimeException(e); |
32 | 0 | } |
33 | |
|
34 | 0 | boolean result = compare(termValue); |
35 | |
|
36 | 0 | if (LOG.isEnabled(environment)){ |
37 | 0 | LOG.logResult(new BasicResult(ResultEvent.PropositionEvaluated, this, environment, result)); |
38 | |
} |
39 | 0 | return result; |
40 | |
} |
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
protected boolean compare(Comparable<T> termValue) { |
49 | 0 | boolean result = Boolean.valueOf(operator.compare(termValue, getExpectedValue())); |
50 | 0 | return result; |
51 | |
} |
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
protected T getExpectedValue() { |
57 | 0 | return this.expectedValue; |
58 | |
} |
59 | |
|
60 | |
public String toString(){ |
61 | 0 | StringBuilder sb = new StringBuilder(); |
62 | 0 | sb.append(term.toString()); |
63 | 0 | sb.append(" "+operator.toString()); |
64 | 0 | sb.append(" "+expectedValue.toString()); |
65 | 0 | return sb.toString(); |
66 | |
} |
67 | |
} |