1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.kew.rule;
17
18 import java.text.ParseException;
19 import java.util.ArrayList;
20 import java.util.List;
21
22 import org.apache.commons.lang.StringUtils;
23 import org.apache.log4j.Logger;
24 import org.kuali.rice.kew.engine.RouteContext;
25 import org.kuali.rice.kew.exception.WorkflowException;
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43 public class MetaRuleExpression extends AccumulatingBSFRuleExpression {
44 private static final Logger LOG = Logger.getLogger(MetaRuleExpression.class);
45
46 @Override
47 public RuleExpressionResult evaluate(Rule rule, RouteContext context) throws WorkflowException {
48 RuleBaseValues ruleDefinition = rule.getDefinition();
49 RuleExpressionDef exprDef = ruleDefinition.getRuleExpressionDef();
50 if (exprDef == null) {
51 throw new WorkflowException("No expression defined in rule definition: " + ruleDefinition);
52 }
53 String expression = exprDef.getExpression();
54 if (StringUtils.isEmpty(expression)) {
55 throw new WorkflowException("Empty expression in rule definition: " + ruleDefinition);
56 }
57
58 String lang = parseLang(ruleDefinition.getRuleExpressionDef().getType(), null);
59 if (lang == null) {
60
61 return evaluateBuiltinExpression(expression, rule, context);
62 } else {
63 return super.evaluate(rule, context);
64 }
65 }
66
67
68
69
70
71
72
73
74
75 private RuleExpressionResult evaluateBuiltinExpression(String expression, Rule rule, RouteContext context) throws WorkflowException {
76 try {
77 KRAMetaRuleEngine engine = new KRAMetaRuleEngine(expression);
78
79 int responsibilityPriority = 0;
80 RuleExpressionResult result = null;
81 boolean success = false;
82 List<RuleResponsibility> responsibilities = new ArrayList<RuleResponsibility>();
83 while (!engine.isDone()) {
84 result = engine.processSingleStatement(context);
85 if (result.isSuccess() && result.getResponsibilities() != null) {
86
87
88 for (RuleResponsibility responsibility: result.getResponsibilities()) {
89 responsibility.setPriority(Integer.valueOf(responsibilityPriority));
90 responsibilities.add(responsibility);
91 }
92
93 responsibilityPriority++;
94 success = true;
95 }
96 }
97 result = new RuleExpressionResult(rule, success, responsibilities);
98 LOG.debug("KRAMetaRuleExpression returning result: " + result);
99 return result;
100 } catch (ParseException pe) {
101 throw new WorkflowException("Error parsing expression", pe);
102 }
103 }
104 }