View Javadoc

1   /**
2    * Copyright 2005-2012 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  public class MetaRuleExpression extends AccumulatingBSFRuleExpression {
46      private static final Logger LOG = Logger.getLogger(MetaRuleExpression.class);
47  
48      @Override
49      public RuleExpressionResult evaluate(Rule rule, RouteContext context) {
50          org.kuali.rice.kew.api.rule.RuleContract ruleDefinition = rule.getDefinition();
51          RuleExpressionContract exprDef = ruleDefinition.getRuleExpressionDef();
52          if (exprDef == null) {
53              throw new RiceIllegalStateException("No expression defined in rule definition: " + ruleDefinition);
54          }
55          String expression = exprDef.getExpression();
56          if (StringUtils.isEmpty(expression)) {
57              throw new RiceIllegalStateException("Empty expression in rule definition: " + ruleDefinition);
58          }
59  
60          String lang = parseLang(ruleDefinition.getRuleExpressionDef().getType(), null);
61          if (lang == null) {
62              // if no language qualifier is specified, parse it as a built-in meta rule expression
63              return evaluateBuiltinExpression(expression, rule, context);
64          } else {
65              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              KRAMetaRuleEngine engine = new KRAMetaRuleEngine(expression);
80  
81              int responsibilityPriority = 0; // responsibility priority, lower value means higher priority (due to sort)...increment as we go
82              RuleExpressionResult result = null;
83              boolean success = false;
84              List<org.kuali.rice.kew.api.rule.RuleResponsibility> responsibilities = new ArrayList<org.kuali.rice.kew.api.rule.RuleResponsibility>();
85              while (!engine.isDone()) {
86                  result = engine.processSingleStatement(context);
87                  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                      for (org.kuali.rice.kew.api.rule.RuleResponsibility responsibility: result.getResponsibilities()) {
91                          org.kuali.rice.kew.api.rule.RuleResponsibility.Builder builder =
92                                  org.kuali.rice.kew.api.rule.RuleResponsibility.Builder.create(responsibility);
93                          builder.setPriority(Integer.valueOf(responsibilityPriority));
94                          responsibilities.add(builder.build());
95                      }
96                      // decrement responsibilityPriority for next rule expression result responsibilities
97                      responsibilityPriority++;
98                      success = true;
99                  }
100             }
101             result = new RuleExpressionResult(rule, success, responsibilities);
102             LOG.debug("MetaRuleExpression returning result: " + result);
103             return result;
104         } catch (ParseException pe) {
105             throw new RiceIllegalStateException("Error parsing expression", pe);
106         }
107     }
108 }