1 | // Copyright 2004-2007 Jean-Francois Poilpret |
2 | // |
3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
4 | // you may not use this file except in compliance with the License. |
5 | // You may obtain a copy of the License at |
6 | // |
7 | // http://www.apache.org/licenses/LICENSE-2.0 |
8 | // |
9 | // Unless required by applicable law or agreed to in writing, software |
10 | // distributed under the License is distributed on an "AS IS" BASIS, |
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 | // See the License for the specific language governing permissions and |
13 | // limitations under the License. |
14 | |
15 | package net.sourceforge.hiveevents; |
16 | |
17 | import java.io.StringReader; |
18 | import java.util.Map; |
19 | |
20 | import org.apache.commons.logging.Log; |
21 | import org.apache.commons.logging.LogFactory; |
22 | |
23 | import org.apache.hivemind.ApplicationRuntimeException; |
24 | |
25 | import antlr.CommonAST; |
26 | import antlr.RecognitionException; |
27 | import antlr.TokenStreamException; |
28 | |
29 | /** |
30 | * Utility class to evaluate an OCL (Object Constraint Language) expression. |
31 | * |
32 | * @author Jean-Francois Poilpret |
33 | */ |
34 | public class ConstraintEvaluator |
35 | { |
36 | static private final Log _logger = LogFactory.getLog(ConstraintEvaluator.class); |
37 | |
38 | /** |
39 | * Build a new evaluator for a constraint expression. |
40 | * The expression is immediately parsed and it its AST (abstract syntax tree) |
41 | * calculated for later evaluation with actual variable values. |
42 | * <p> |
43 | * Each instance is thread-safe and can thus be used by simultaneous threads |
44 | * to evaluate the same expression with different variable values. |
45 | * |
46 | * @param constraint the expression to be evaluated later |
47 | * @param variableClasses the classes of all variables appearing in |
48 | * <code>constraint</code> indexed by name (used for reflection checks) |
49 | */ |
50 | public ConstraintEvaluator(String constraint, Map<String, Class> variableClasses) |
51 | { |
52 | try |
53 | { |
54 | _constraint = constraint; |
55 | ConstraintLexer lexer = new ConstraintLexer(new StringReader(constraint)); |
56 | lexer.setFilename(constraint); |
57 | lexer.initVariables(variableClasses); |
58 | |
59 | ConstraintParser parser = new ConstraintParser(lexer); |
60 | parser.setFilename(constraint); |
61 | // Parse the input expression |
62 | parser.expr(); |
63 | _tree = (CommonAST) parser.getAST(); |
64 | if (_logger.isDebugEnabled()) |
65 | { |
66 | _logger.debug( "<init> constraint <" + constraint + "> AST = " + |
67 | _tree.toStringTree()); |
68 | } |
69 | } |
70 | catch (TokenStreamException e) |
71 | { |
72 | _logger.error("<init>", e); |
73 | throw new ApplicationRuntimeException( |
74 | "Problem parsing <" + constraint + ">", e); |
75 | } |
76 | catch (RecognitionException e) |
77 | { |
78 | _logger.error("<init>", e); |
79 | throw new ApplicationRuntimeException( |
80 | "Problem parsing <" + constraint + ">", e); |
81 | } |
82 | } |
83 | |
84 | /** |
85 | * Evaluates the constraint expression with actual values for all variables. |
86 | * @param variables values for all variables, indexed by name |
87 | * @return the result of the expression evaluation |
88 | */ |
89 | public boolean evaluate(Map<String, Object> variables) |
90 | { |
91 | try |
92 | { |
93 | ConstraintTreeWalker walker = new ConstraintTreeWalker(); |
94 | walker.initVariables(variables); |
95 | // Traverse the tree created by the parser |
96 | return walker.expr(_tree); |
97 | } |
98 | catch (RecognitionException e) |
99 | { |
100 | _logger.error("evaluate()", e); |
101 | throw new ApplicationRuntimeException( |
102 | "Problem evaluating <" + _constraint + ">", e); |
103 | } |
104 | } |
105 | |
106 | final private String _constraint; |
107 | final private CommonAST _tree; |
108 | } |