svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.library / org.gvsig.expressionevaluator / org.gvsig.expressionevaluator.lib / org.gvsig.expressionevaluator.lib.impl / src / main / java / org / gvsig / expressionevaluator / impl / DefaultInterpreter.java @ 44198
History | View | Annotate | Download (10.5 KB)
1 | 43512 | jjdelcerro | package org.gvsig.expressionevaluator.impl; |
---|---|---|---|
2 | |||
3 | 43532 | jjdelcerro | import java.util.ArrayList; |
4 | import java.util.Comparator; |
||
5 | import java.util.Date; |
||
6 | import java.util.HashMap; |
||
7 | import java.util.List; |
||
8 | import java.util.Map; |
||
9 | 44191 | jjdelcerro | import java.util.Objects; |
10 | 43532 | jjdelcerro | import org.apache.commons.lang3.tuple.Pair; |
11 | 43512 | jjdelcerro | import org.gvsig.expressionevaluator.SymbolTable; |
12 | import org.gvsig.expressionevaluator.Interpreter; |
||
13 | import org.gvsig.expressionevaluator.Code; |
||
14 | import org.gvsig.expressionevaluator.Code.Constant; |
||
15 | import org.gvsig.expressionevaluator.Code.Identifier; |
||
16 | import org.gvsig.expressionevaluator.Code.Caller; |
||
17 | 43939 | jjdelcerro | import org.gvsig.expressionevaluator.Code.Method; |
18 | 44139 | jjdelcerro | import org.gvsig.expressionevaluator.Codes; |
19 | 44098 | jjdelcerro | import org.gvsig.expressionevaluator.ExpressionRuntimeException; |
20 | 43512 | jjdelcerro | import org.gvsig.expressionevaluator.Function; |
21 | 44154 | jjdelcerro | import org.gvsig.expressionevaluator.impl.DefaultCodeBuilder.RecursionControlSupport; |
22 | 43512 | jjdelcerro | import org.gvsig.expressionevaluator.impl.function.operator.BinaryOperator; |
23 | 44139 | jjdelcerro | import org.gvsig.expressionevaluator.impl.function.programming.CallMethodFunction; |
24 | 43512 | jjdelcerro | import org.gvsig.expressionevaluator.impl.function.operator.UnaryOperator; |
25 | 44139 | jjdelcerro | import org.gvsig.expressionevaluator.impl.function.programming.ReturnFunction.ReturnException; |
26 | 43512 | jjdelcerro | |
27 | public class DefaultInterpreter implements Interpreter { |
||
28 | |||
29 | 44154 | jjdelcerro | public static int DEFAULT_MAX_RECURSION_LIMIT = 20; |
30 | |||
31 | 43532 | jjdelcerro | private class DefaultCache implements Cache { |
32 | |||
33 | private Map<Pair<Object,Object>,Pair<Object,Long>> data; |
||
34 | |||
35 | public DefaultCache() {
|
||
36 | this.data = new HashMap<>(); |
||
37 | } |
||
38 | |||
39 | @Override
|
||
40 | public Object get(Object context, Object key) { |
||
41 | Pair<Object, Long> v = this.data.get( Pair.of(context, key) ); |
||
42 | 44139 | jjdelcerro | if( v == null ) { |
43 | return null; |
||
44 | } |
||
45 | 43532 | jjdelcerro | return v.getLeft();
|
46 | } |
||
47 | |||
48 | @Override
|
||
49 | public void put(Object context, Object key, Object value) { |
||
50 | if( this.data.size()>this.getMaxElements() ) { |
||
51 | this.reduce();
|
||
52 | } |
||
53 | this.data.put(Pair.of(context, key), Pair.of(value, new Date().getTime())); |
||
54 | } |
||
55 | |||
56 | @Override
|
||
57 | public void remove(Object context, Object key) { |
||
58 | this.data.remove(Pair.of(context, key));
|
||
59 | } |
||
60 | |||
61 | @Override
|
||
62 | public void removeAll() { |
||
63 | this.data = new HashMap<>(); |
||
64 | } |
||
65 | |||
66 | private int getMaxElements() { |
||
67 | return 100; |
||
68 | } |
||
69 | |||
70 | private void reduce() { |
||
71 | List<Map.Entry<Pair<Object, Object>, Pair<Object, Long>>> entries = |
||
72 | new ArrayList<>(this.data.entrySet()); |
||
73 | entries.sort(new Comparator<Map.Entry<Pair<Object, Object>, Pair<Object, Long>>>() { |
||
74 | @Override
|
||
75 | public int compare(Map.Entry<Pair<Object, Object>, Pair<Object, Long>> o1, Map.Entry<Pair<Object, Object>, Pair<Object, Long>> o2) { |
||
76 | return o1.getValue().getRight().compareTo(o2.getValue().getRight());
|
||
77 | } |
||
78 | }); |
||
79 | for( int i=0; i<this.getMaxElements()/2; i++ ) { |
||
80 | this.data.remove(entries.get(i).getKey());
|
||
81 | } |
||
82 | } |
||
83 | |||
84 | } |
||
85 | |||
86 | 43521 | jjdelcerro | private SymbolTable symbolTable = null; |
87 | private Double accuracy; |
||
88 | private Code currentCode;
|
||
89 | 43809 | jjdelcerro | private Cache cache;
|
90 | 44154 | jjdelcerro | private int maxRecursionLimit; |
91 | 44191 | jjdelcerro | private boolean sqlCompatible = false; |
92 | 43512 | jjdelcerro | |
93 | public DefaultInterpreter() {
|
||
94 | 43532 | jjdelcerro | this.cache = new DefaultCache(); |
95 | 44154 | jjdelcerro | this.maxRecursionLimit = DEFAULT_MAX_RECURSION_LIMIT;
|
96 | 43512 | jjdelcerro | } |
97 | |||
98 | @Override
|
||
99 | 43809 | jjdelcerro | public Interpreter clone() throws CloneNotSupportedException { |
100 | DefaultInterpreter other = (DefaultInterpreter) super.clone();
|
||
101 | other.cache = new DefaultCache();
|
||
102 | if( this.symbolTable!=null ) { |
||
103 | other.symbolTable = this.symbolTable.clone();
|
||
104 | } |
||
105 | return other;
|
||
106 | } |
||
107 | 44154 | jjdelcerro | |
108 | public int getMaxRecursionLimit() { |
||
109 | return this.maxRecursionLimit; |
||
110 | } |
||
111 | 43809 | jjdelcerro | |
112 | 44154 | jjdelcerro | public void setMaxRecursionLimit(int limit) { |
113 | this.maxRecursionLimit = limit;
|
||
114 | } |
||
115 | |||
116 | 43809 | jjdelcerro | @Override
|
117 | 43532 | jjdelcerro | public Cache getCache() {
|
118 | return this.cache; |
||
119 | } |
||
120 | |||
121 | @Override
|
||
122 | 43512 | jjdelcerro | public void setSymbolTable(SymbolTable symbolTable) { |
123 | this.symbolTable = symbolTable;
|
||
124 | } |
||
125 | |||
126 | @Override
|
||
127 | public SymbolTable getSymbolTable() {
|
||
128 | 43809 | jjdelcerro | if( this.symbolTable==null ) { |
129 | 43939 | jjdelcerro | this.symbolTable = new DefaultSymbolTable(); |
130 | 43809 | jjdelcerro | } |
131 | 43512 | jjdelcerro | return this.symbolTable; |
132 | } |
||
133 | @Override
|
||
134 | 43521 | jjdelcerro | public Double getAccuracy() { |
135 | return this.accuracy; |
||
136 | } |
||
137 | |||
138 | @Override
|
||
139 | public void setAccuracy(Double accuracy) { |
||
140 | this.accuracy = accuracy;
|
||
141 | } |
||
142 | 44009 | jjdelcerro | |
143 | 43521 | jjdelcerro | @Override
|
144 | 44191 | jjdelcerro | public void setSQLCompatible(boolean sqlCompatible) { |
145 | this.sqlCompatible = sqlCompatible;
|
||
146 | } |
||
147 | |||
148 | @Override
|
||
149 | public boolean isSQLCompatible() { |
||
150 | return sqlCompatible;
|
||
151 | } |
||
152 | |||
153 | @Override
|
||
154 | 43512 | jjdelcerro | public Object run(Code code) { |
155 | try {
|
||
156 | return this.runCode(code); |
||
157 | 44139 | jjdelcerro | } catch(ReturnException ex) {
|
158 | return ex.getValue();
|
||
159 | 44098 | jjdelcerro | } catch(RuntimeException ex) { |
160 | throw ex;
|
||
161 | } catch(Exception ex) { |
||
162 | throw new ExpressionRuntimeException(code, "", ex); |
||
163 | 43512 | jjdelcerro | } |
164 | } |
||
165 | |||
166 | @Override
|
||
167 | public void link(Code code) { |
||
168 | 44198 | jjdelcerro | code.link(this.getSymbolTable());
|
169 | 43512 | jjdelcerro | } |
170 | |||
171 | 44139 | jjdelcerro | public Object runCode(Code code) throws Exception { |
172 | 44154 | jjdelcerro | RecursionControlSupport recursionControl = null;
|
173 | if( code instanceof RecursionControlSupport ) { |
||
174 | recursionControl = (RecursionControlSupport) code; |
||
175 | if( !recursionControl.enterCode(this.maxRecursionLimit) ) { |
||
176 | recursionControl.resetRecursionState(); |
||
177 | throw new ExpressionRuntimeException(code, I18N.Maximum_recursion_limit_exceeded()); |
||
178 | } |
||
179 | } |
||
180 | Object value = null; |
||
181 | 43521 | jjdelcerro | this.currentCode = code;
|
182 | 44154 | jjdelcerro | try {
|
183 | switch( code.code() ) {
|
||
184 | case Code.CONSTANT:
|
||
185 | value = ((Constant) code).value(); |
||
186 | break;
|
||
187 | 43512 | jjdelcerro | |
188 | 44154 | jjdelcerro | case Code.IDENTIFIER:
|
189 | String name = ((Identifier) code).name();
|
||
190 | if( !this.getSymbolTable().exists(name) ) { |
||
191 | throw new ExpressionRuntimeException( |
||
192 | code, |
||
193 | I18N.Undefined_variable_XIdentifierX(name), |
||
194 | I18N.Use_single_quotes_to_enter_literal_strings() |
||
195 | ); |
||
196 | } |
||
197 | value = this.getSymbolTable().value(name);
|
||
198 | break;
|
||
199 | 43512 | jjdelcerro | |
200 | 44154 | jjdelcerro | case Code.METHOD: {
|
201 | Method method = (Method) code; |
||
202 | Function function = method.function(); |
||
203 | if( function == null ) { |
||
204 | Object obj = this.runCode(method.obj()); |
||
205 | if( obj == null ) { |
||
206 | throw new NullPointerException("An object pointer was expected to invoke method "+method.methodname()+" and a null was received"); |
||
207 | } |
||
208 | method.function(new CallMethodFunction(obj, method.methodname()));
|
||
209 | 43939 | jjdelcerro | } |
210 | } |
||
211 | 44154 | jjdelcerro | case Code.CALLER:
|
212 | Caller caller = (Caller) code; |
||
213 | Function function = caller.function(); |
||
214 | 43512 | jjdelcerro | if( function == null ) { |
215 | 44154 | jjdelcerro | function = this.getSymbolTable().function(caller.name());
|
216 | if( function == null ) { |
||
217 | throw new ExpressionRuntimeException(code, I18N.Undefined_function_XIdentifierX(caller.name())); |
||
218 | } |
||
219 | caller.function(function); |
||
220 | 43512 | jjdelcerro | } |
221 | 44191 | jjdelcerro | if( !function.isSQLCompatible() && this.sqlCompatible ) { |
222 | throw new ExpressionRuntimeException(code, I18N.Cant_use_non_SQL_compatible_functions(function.name())); |
||
223 | } |
||
224 | 44198 | jjdelcerro | Codes args = caller.parameters(); |
225 | 44154 | jjdelcerro | try {
|
226 | switch( caller.type() ) {
|
||
227 | case Caller.UNARY_OPERATOR:
|
||
228 | if( args == null || args.size() != 1 ) { |
||
229 | throw new ExpressionRuntimeException(code, I18N.Number_of_argument_mistmatch_in_operator_XIdentifierX_expected_1_got_XargcX(function.name(),args==null?0:args.size())); |
||
230 | } |
||
231 | value = ((UnaryOperator) function).call(this, runCode(args.get(0))); |
||
232 | break;
|
||
233 | 43521 | jjdelcerro | |
234 | 44154 | jjdelcerro | case Caller.BINARY_OPERATOR:
|
235 | if( args == null || args.size() != 2 ) { |
||
236 | throw new ExpressionRuntimeException(code, I18N.Number_of_argument_mistmatch_in_operator_XIdentifierX_expected_2_got_XargcX(function.name(),args==null?0:args.size())); |
||
237 | } |
||
238 | value = ((BinaryOperator) function).call(this, runCode(args.get(0)), runCode(args.get(1))); |
||
239 | break;
|
||
240 | 43521 | jjdelcerro | |
241 | 44154 | jjdelcerro | case Caller.FUNCTION:
|
242 | int argc = (args == null) ? 0 : args.size(); |
||
243 | if( !function.argc().contains(argc) ) {
|
||
244 | throw new ExpressionRuntimeException(code, I18N.Number_of_argument_mistmatch_in_function_XIdentifierX_expected_XexpectedX_got_XfoundX(function.name(),function.argc(),argc)); |
||
245 | } |
||
246 | if( function.useArgumentsInsteadObjects() ) {
|
||
247 | value = function.call(this, args);
|
||
248 | } else {
|
||
249 | Object[] argvalues = new Object[argc]; |
||
250 | if( args != null ) { |
||
251 | int i = 0; |
||
252 | for( Code arg : args ) {
|
||
253 | argvalues[i++] = runCode(arg); |
||
254 | } |
||
255 | 43939 | jjdelcerro | } |
256 | 44154 | jjdelcerro | value = function.call(this, argvalues);
|
257 | 43521 | jjdelcerro | } |
258 | } |
||
259 | 44154 | jjdelcerro | } catch (RuntimeException ex) { |
260 | throw ex;
|
||
261 | } catch (Exception ex) { |
||
262 | String argsstr = "???"; |
||
263 | try {
|
||
264 | 44191 | jjdelcerro | argsstr = Objects.toString(args); |
265 | 44154 | jjdelcerro | } catch(Exception ex2) { |
266 | // Ignore.
|
||
267 | } |
||
268 | throw new ExpressionRuntimeException(code, I18N.Problems_calling_function_XIdentifierX_with_args_XargsX(function.name(),argsstr)); |
||
269 | 43512 | jjdelcerro | } |
270 | 44154 | jjdelcerro | break;
|
271 | |||
272 | 43512 | jjdelcerro | } |
273 | 44154 | jjdelcerro | } finally {
|
274 | this.currentCode = null; |
||
275 | if( recursionControl!=null ) { |
||
276 | recursionControl.exitCode(); |
||
277 | } |
||
278 | 43512 | jjdelcerro | } |
279 | return value;
|
||
280 | } |
||
281 | 43521 | jjdelcerro | |
282 | @Override
|
||
283 | public Code getCurrentCode() {
|
||
284 | return this.currentCode; |
||
285 | } |
||
286 | 43512 | jjdelcerro | } |