1 package org.kuali.rice.krms.framework.engine;
2
3 import java.util.ArrayList;
4 import java.util.Collections;
5 import java.util.List;
6
7 import org.kuali.rice.krms.api.engine.ExecutionEnvironment;
8 import org.kuali.rice.krms.api.engine.ResultEvent;
9 import org.kuali.rice.krms.api.repository.LogicalOperator;
10 import org.kuali.rice.krms.framework.engine.result.BasicResult;
11
12 public final class CompoundProposition implements Proposition {
13 private static final ResultLogger LOG = ResultLogger.getInstance();
14
15 private final LogicalOperator logicalOperator;
16 private final List<Proposition> propositions;
17
18 public CompoundProposition(LogicalOperator logicalOperator, List<Proposition> propositions) {
19 if (propositions == null || propositions.isEmpty()) {
20 throw new IllegalArgumentException("Propositions must be non-null and non-empty.");
21 }
22 if (logicalOperator == null) {
23 throw new IllegalArgumentException("Logical operator must be non-null.");
24 }
25 this.logicalOperator = logicalOperator;
26 this.propositions = new ArrayList<Proposition>(propositions);
27 }
28
29 @Override
30 public boolean evaluate(ExecutionEnvironment environment) {
31 boolean result = evaluateInner(environment);
32
33
34 if (LOG.isEnabled(environment)){
35 LOG.logResult(new BasicResult(ResultEvent.PropositionEvaluated, this, environment, result));
36 }
37
38 return result;
39 }
40
41
42
43
44
45
46
47 private boolean evaluateInner(ExecutionEnvironment environment) {
48 if (logicalOperator == LogicalOperator.AND) {
49 for (Proposition proposition : propositions) {
50 boolean result = proposition.evaluate(environment);
51 if (!result) {
52 return false;
53 }
54 }
55 return true;
56 } else if (logicalOperator == LogicalOperator.OR) {
57 for (Proposition proposition : propositions) {
58 if (proposition.evaluate(environment)) {
59 return true;
60 }
61 }
62 return false;
63 }
64 throw new IllegalStateException("Invalid logical operator: " + logicalOperator);
65 }
66
67
68 @Override
69 public List<Proposition> getChildren() {
70 return Collections.unmodifiableList(propositions);
71 }
72
73 @Override
74 public boolean isCompound() {
75 return true;
76 }
77
78 }