/
RuleVerifyUtils.java
123 lines (108 loc) · 4.9 KB
/
RuleVerifyUtils.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package com.indeed.proctor.common;
import com.google.common.collect.Lists;
import org.apache.el.lang.ExpressionBuilder;
import org.apache.el.parser.AstIdentifier;
import org.apache.el.parser.Node;
import org.apache.log4j.Logger;
import javax.el.ELContext;
import javax.el.ELException;
import javax.el.ExpressionFactory;
import javax.el.ValueExpression;
import java.util.List;
import java.util.Set;
import static com.indeed.proctor.common.ProctorUtils.isEmptyWhitespace;
import static com.indeed.proctor.common.ProctorUtils.removeElExpressionBraces;
public class RuleVerifyUtils {
private static final Logger LOGGER = Logger.getLogger(RuleVerifyUtils.class);
private RuleVerifyUtils() {
}
public static void verifyRule(
final String testRule,
final boolean shouldEvaluate,
final ExpressionFactory expressionFactory,
final ELContext elContext,
final Set<String> absentIdentifiers
) throws InvalidRuleException {
final String bareRule = removeElExpressionBraces(testRule);
if (!isEmptyWhitespace(bareRule)) {
final ValueExpression valueExpression;
try {
valueExpression = expressionFactory.createValueExpression(elContext, testRule, Boolean.class);
} catch (final ELException e) {
throw new InvalidRuleException(e, String.format("Rule %s has invalid syntax or unknown function.", testRule));
}
if (shouldEvaluate) {
/*
* must have a context to test against, even if it's "Collections.emptyMap()", how to
* tell if this method is used for ProctorBuilder or during load of the testMatrix.
* also used to check to make sure any classes included in the EL can be found.
* Class
*/
// Parse test rule as expression language and create AST
final Node root;
try {
root = ExpressionBuilder.createNode(testRule);
} catch (final ELException e) {
throw new InvalidRuleException(e, String.format("Rule %s has invalid syntax.", testRule));
}
// Check identifiers in the AST and verify variable names
final Node undefinedIdentifier = checkUndefinedIdentifier(root, elContext, absentIdentifiers);
if (undefinedIdentifier != null) {
throw new InvalidRuleException(String.format("Rule %s contains undefined identifier '%s'", testRule, undefinedIdentifier.getImage()));
}
// Evaluate rule with given context
try {
valueExpression.getValue(elContext);
} catch (final ELException e) {
if (isIgnorable(root, absentIdentifiers)) {
LOGGER.debug(String.format("Rule %s contains uninstantiated identifier(s) in %s, ignore the failure", testRule, absentIdentifiers), e);
} else {
throw new InvalidRuleException(e, String.format("Failed to evaluate a rule %s: " + e.getMessage(), testRule));
}
}
}
}
}
private static boolean isIgnorable(final Node node, final Set<String> absentIdentifiers) {
final List<Node> leaves = getLeafNodes(node);
for (final Node n : leaves) {
final String image = n.getImage();
if (absentIdentifiers.contains(image)) {
/* we can ignore this test failure since the identifier context is not provided **/
return true;
}
}
return false;
}
private static Node checkUndefinedIdentifier(final Node node, final ELContext elContext, final Set<String> absentIdentifiers) {
if (node instanceof AstIdentifier) {
final String name = node.getImage();
final boolean hasVariable = elContext.getVariableMapper().resolveVariable(name) != null;
if (!hasVariable && !absentIdentifiers.contains(name)) {
return node;
}
} else {
for (int i = 0; i < node.jjtGetNumChildren(); i++) {
final Node result = checkUndefinedIdentifier(node.jjtGetChild(i), elContext, absentIdentifiers);
if (result != null) {
return result;
}
}
}
return null;
}
private static List<Node> getLeafNodes(final Node node) {
final List<Node> nodes = Lists.newArrayList();
find(node, nodes);
return nodes;
}
private static void find(final Node node, final List<Node> res) {
if (node.jjtGetNumChildren() > 0) {
for (int i = 0; i < node.jjtGetNumChildren(); i++) {
find(node.jjtGetChild(i), res);
}
} else {
res.add(node);
}
}
}