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