Coverage Report - org.kuali.rice.krms.framework.engine.CompoundProposition
 
Classes in this File Line Coverage Branch Coverage Complexity
CompoundProposition
62%
25/40
42%
11/26
4
 
 1  
 /**
 2  
  * Copyright 2005-2011 The Kuali Foundation
 3  
  *
 4  
  * Licensed under the Educational Community License, Version 2.0 (the "License");
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at
 7  
  *
 8  
  * http://www.opensource.org/licenses/ecl2.php
 9  
  *
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13  
  * See the License for the specific language governing permissions and
 14  
  * limitations under the License.
 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  
                 // handle compound proposition result logging
 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  
      * This method handles the evaluation logic
 63  
      * 
 64  
      * @param environment
 65  
      * @return
 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  
      * only log if the proposition is not compound
 114  
      * and have the compound proposition log its own result
 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  
 }