Coverage Report - org.kuali.rice.kew.rule.MetaRuleExpression
 
Classes in this File Line Coverage Branch Coverage Complexity
MetaRuleExpression
0%
0/33
0%
0/14
7.5
 
 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.kew.rule;
 17  
 
 18  
 import org.apache.commons.lang.StringUtils;
 19  
 import org.apache.log4j.Logger;
 20  
 import org.kuali.rice.core.api.exception.RiceIllegalStateException;
 21  
 import org.kuali.rice.kew.api.rule.RuleExpressionContract;
 22  
 import org.kuali.rice.kew.engine.RouteContext;
 23  
 import org.kuali.rice.kew.api.exception.WorkflowException;
 24  
 
 25  
 import java.text.ParseException;
 26  
 import java.util.ArrayList;
 27  
 import java.util.List;
 28  
 
 29  
 
 30  
 /**
 31  
  * Expression implementation for "meta rules".  A "meta rule" consists of a sequence of
 32  
  * subordinate rule evaluations each processed according to an associated modifier:
 33  
  * <dl>
 34  
  *   <dt>next<dt>
 35  
  *   <dd>proceed with rule evaluation</dd>
 36  
  *   <dt>true</dt>
 37  
  *   <dd>if this rule evaluates to true, then return the responsibilities associated with the rule</dd>
 38  
  *   <dt>false</dt>
 39  
  *   <dd>if this rule evaluates to false, then return the responsibilities associated with the rule</dd>
 40  
  * </dl>
 41  
  * E.g.
 42  
  * <div><tt>bizRule1: next; bizRule2: true; bizRule3: false</tt></div>
 43  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 44  
  */
 45  0
 public class MetaRuleExpression extends AccumulatingBSFRuleExpression {
 46  0
     private static final Logger LOG = Logger.getLogger(MetaRuleExpression.class);
 47  
 
 48  
     @Override
 49  
     public RuleExpressionResult evaluate(Rule rule, RouteContext context) {
 50  0
         org.kuali.rice.kew.api.rule.RuleContract ruleDefinition = rule.getDefinition();
 51  0
         RuleExpressionContract exprDef = ruleDefinition.getRuleExpressionDef();
 52  0
         if (exprDef == null) {
 53  0
             throw new RiceIllegalStateException("No expression defined in rule definition: " + ruleDefinition);
 54  
         }
 55  0
         String expression = exprDef.getExpression();
 56  0
         if (StringUtils.isEmpty(expression)) {
 57  0
             throw new RiceIllegalStateException("Empty expression in rule definition: " + ruleDefinition);
 58  
         }
 59  
 
 60  0
         String lang = parseLang(ruleDefinition.getRuleExpressionDef().getType(), null);
 61  0
         if (lang == null) {
 62  
             // if no language qualifier is specified, parse it as a built-in meta rule expression
 63  0
             return evaluateBuiltinExpression(expression, rule, context);
 64  
         } else {
 65  0
             return super.evaluate(rule, context);
 66  
         }
 67  
     }
 68  
 
 69  
     /**
 70  
      * Evaluates the builtin "meta" rule expression
 71  
      * @param expression the builtin meta rule expression
 72  
      * @param rule the rule
 73  
      * @param context the route context
 74  
      * @return RuleExpressionResult the result
 75  
      * @throws WorkflowException
 76  
      */
 77  
     private RuleExpressionResult evaluateBuiltinExpression(String expression, Rule rule, RouteContext context) {
 78  
         try {
 79  0
             KRAMetaRuleEngine engine = new KRAMetaRuleEngine(expression);
 80  
 
 81  0
             int responsibilityPriority = 0; // responsibility priority, lower value means higher priority (due to sort)...increment as we go
 82  0
             RuleExpressionResult result = null;
 83  0
             boolean success = false;
 84  0
             List<org.kuali.rice.kew.api.rule.RuleResponsibility> responsibilities = new ArrayList<org.kuali.rice.kew.api.rule.RuleResponsibility>();
 85  0
             while (!engine.isDone()) {
 86  0
                 result = engine.processSingleStatement(context);
 87  0
                 if (result.isSuccess() && result.getResponsibilities() != null) {
 88  
                     // accumulate responsibilities if the evaluation was successful
 89  
                     // make sure to reduce priority for each subsequent rule in order for sequential activation to work as desired
 90  0
                     for (org.kuali.rice.kew.api.rule.RuleResponsibility responsibility: result.getResponsibilities()) {
 91  0
                         org.kuali.rice.kew.api.rule.RuleResponsibility.Builder builder =
 92  
                                 org.kuali.rice.kew.api.rule.RuleResponsibility.Builder.create(responsibility);
 93  0
                         builder.setPriority(Integer.valueOf(responsibilityPriority));
 94  0
                         responsibilities.add(builder.build());
 95  0
                     }
 96  
                     // decrement responsibilityPriority for next rule expression result responsibilities
 97  0
                     responsibilityPriority++;
 98  0
                     success = true;
 99  
                 }
 100  
             }
 101  0
             result = new RuleExpressionResult(rule, success, responsibilities);
 102  0
             LOG.debug("MetaRuleExpression returning result: " + result);
 103  0
             return result;
 104  0
         } catch (ParseException pe) {
 105  0
             throw new RiceIllegalStateException("Error parsing expression", pe);
 106  
         }
 107  
     }
 108  
 }