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