1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package org.kuali.rice.krms.framework.engine; |
17 | |
|
18 | |
import java.util.ArrayList; |
19 | |
import java.util.Collections; |
20 | |
import java.util.List; |
21 | |
import java.util.Map; |
22 | |
|
23 | |
import org.kuali.rice.krms.api.engine.ExecutionEnvironment; |
24 | |
import org.kuali.rice.krms.api.engine.ResultEvent; |
25 | |
import org.kuali.rice.krms.api.engine.ExecutionFlag; |
26 | |
import org.kuali.rice.krms.api.repository.LogicalOperator; |
27 | |
import org.kuali.rice.krms.framework.engine.result.BasicResult; |
28 | |
|
29 | |
public final class CompoundProposition implements Proposition { |
30 | |
|
31 | 1 | private static final ResultLogger LOG = ResultLogger.getInstance(); |
32 | |
|
33 | |
private final LogicalOperator logicalOperator; |
34 | |
private final List<Proposition> propositions; |
35 | |
|
36 | 2 | public CompoundProposition(LogicalOperator logicalOperator, List<Proposition> propositions) { |
37 | |
|
38 | 2 | if (propositions == null || propositions.isEmpty()) { |
39 | 0 | throw new IllegalArgumentException("Propositions must be non-null and non-empty."); |
40 | |
} |
41 | 2 | if (logicalOperator == null) { |
42 | 0 | throw new IllegalArgumentException("Logical operator must be non-null."); |
43 | |
} |
44 | 2 | this.logicalOperator = logicalOperator; |
45 | 2 | this.propositions = new ArrayList<Proposition>(propositions); |
46 | 2 | } |
47 | |
|
48 | |
@Override |
49 | |
public PropositionResult evaluate(ExecutionEnvironment environment) { |
50 | |
|
51 | 2 | PropositionResult result = evaluateInner(environment); |
52 | |
|
53 | |
|
54 | 2 | if (LOG.isEnabled(environment)) { |
55 | 2 | LOG.logResult(new BasicResult(ResultEvent.PropositionEvaluated, this, environment, result.getResult())); |
56 | |
} |
57 | |
|
58 | 2 | return result; |
59 | |
} |
60 | |
|
61 | |
|
62 | |
|
63 | |
|
64 | |
|
65 | |
|
66 | |
|
67 | |
|
68 | |
private PropositionResult evaluateInner(ExecutionEnvironment environment) { |
69 | |
|
70 | |
boolean collatedResult; |
71 | 2 | boolean evaluateAll = environment.getExecutionOptions().getFlag(ExecutionFlag.EVALUATE_ALL_PROPOSITIONS); |
72 | |
|
73 | 2 | if (logicalOperator == LogicalOperator.AND) { |
74 | |
|
75 | 2 | collatedResult = true; |
76 | |
|
77 | 2 | for (Proposition proposition : propositions) { |
78 | |
|
79 | 3 | PropositionResult singleResult = proposition.evaluate(environment); |
80 | 3 | logPropositionResult(proposition, singleResult, environment); |
81 | |
|
82 | 3 | if (!singleResult.getResult()) { |
83 | 1 | collatedResult = false; |
84 | 1 | if(!evaluateAll) break; |
85 | |
} |
86 | 2 | } |
87 | |
|
88 | 2 | return new PropositionResult(collatedResult); |
89 | |
|
90 | 0 | } else if (logicalOperator == LogicalOperator.OR) { |
91 | |
|
92 | 0 | collatedResult = false; |
93 | |
|
94 | 0 | for (Proposition proposition : propositions) { |
95 | |
|
96 | 0 | PropositionResult singleResult = proposition.evaluate(environment); |
97 | |
|
98 | 0 | logPropositionResult(proposition, singleResult, environment); |
99 | |
|
100 | 0 | if (!singleResult.getResult()) { |
101 | 0 | collatedResult = true; |
102 | 0 | if(!evaluateAll) break; |
103 | |
} |
104 | 0 | } |
105 | |
|
106 | 0 | return new PropositionResult(collatedResult); |
107 | |
} |
108 | 0 | throw new IllegalStateException("Invalid logical operator: " + logicalOperator); |
109 | |
} |
110 | |
|
111 | |
|
112 | |
|
113 | |
|
114 | |
|
115 | |
|
116 | |
|
117 | |
public void logPropositionResult(Proposition proposition, PropositionResult propositionResult, ExecutionEnvironment environment) { |
118 | |
|
119 | 3 | if(!proposition.isCompound()) { |
120 | 3 | LOG.logResult(new BasicResult(propositionResult.getExecutionDetails(), ResultEvent.PropositionEvaluated, proposition, environment, propositionResult.getResult())); |
121 | |
} |
122 | |
|
123 | 3 | } |
124 | |
|
125 | |
|
126 | |
@Override |
127 | |
public List<Proposition> getChildren() { |
128 | 0 | return Collections.unmodifiableList(propositions); |
129 | |
} |
130 | |
|
131 | |
@Override |
132 | |
public boolean isCompound() { |
133 | 0 | return true; |
134 | |
} |
135 | |
|
136 | |
} |