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