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