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 / DefaultExpression.java @ 44390
History | View | Annotate | Download (13.3 KB)
1 | 43983 | jjdelcerro | package org.gvsig.expressionevaluator.impl; |
---|---|---|---|
2 | |||
3 | import java.net.URI; |
||
4 | 44340 | jjdelcerro | import java.net.URL; |
5 | 43983 | jjdelcerro | import java.util.ArrayList; |
6 | import java.util.Iterator; |
||
7 | import java.util.List; |
||
8 | 44390 | jjdelcerro | import java.util.Objects; |
9 | 43983 | jjdelcerro | import org.apache.commons.lang3.StringUtils; |
10 | import org.gvsig.expressionevaluator.Code; |
||
11 | 44215 | jjdelcerro | import org.gvsig.expressionevaluator.Compiler; |
12 | 43983 | jjdelcerro | import org.gvsig.expressionevaluator.Expression; |
13 | import org.gvsig.expressionevaluator.ExpressionEvaluatorLocator; |
||
14 | import org.gvsig.expressionevaluator.ExpressionEvaluatorManager; |
||
15 | 44215 | jjdelcerro | import org.gvsig.expressionevaluator.ExpressionUtils; |
16 | 43983 | jjdelcerro | import org.gvsig.expressionevaluator.Interpreter; |
17 | 44009 | jjdelcerro | import org.gvsig.expressionevaluator.Optimizer; |
18 | 43983 | jjdelcerro | import org.gvsig.expressionevaluator.SymbolTable; |
19 | import org.gvsig.tools.ToolsLocator; |
||
20 | import org.gvsig.tools.dynobject.DynStruct; |
||
21 | import org.gvsig.tools.persistence.PersistenceManager; |
||
22 | import org.gvsig.tools.persistence.PersistentState; |
||
23 | import org.gvsig.tools.persistence.exception.PersistenceException; |
||
24 | import org.gvsig.tools.script.Script; |
||
25 | import org.gvsig.tools.script.ScriptManager; |
||
26 | 44390 | jjdelcerro | import org.gvsig.tools.util.LabeledValue; |
27 | 43983 | jjdelcerro | import org.gvsig.tools.util.UnmodifiableBasicList; |
28 | import org.gvsig.tools.util.UnmodifiableBasicListAdapter; |
||
29 | 43984 | jjdelcerro | import org.json.JSONArray; |
30 | import org.json.JSONObject; |
||
31 | 43983 | jjdelcerro | |
32 | /**
|
||
33 | *
|
||
34 | * @author jjdelcerro
|
||
35 | */
|
||
36 | 44390 | jjdelcerro | public class DefaultExpression implements Expression, LabeledValue<Expression> { |
37 | 43983 | jjdelcerro | |
38 | private String phrase = null; |
||
39 | private Script userScript = null; |
||
40 | private List<Script> scripts = null; |
||
41 | private UnmodifiableBasicList<Script> unmodifiableScripts = null; |
||
42 | |||
43 | private Code code = null; |
||
44 | private Interpreter interpreter;
|
||
45 | 44009 | jjdelcerro | private boolean hasNotBeenOptimized = true; |
46 | 44215 | jjdelcerro | private SymbolTable mySymbolTable = null; |
47 | private boolean useBracketsForIdentifiers = false; |
||
48 | 43983 | jjdelcerro | |
49 | public DefaultExpression() {
|
||
50 | |||
51 | } |
||
52 | 44390 | jjdelcerro | |
53 | @Override
|
||
54 | public String getLabel() { |
||
55 | return StringUtils.abbreviate(
|
||
56 | StringUtils.normalizeSpace(this.getPhrase()),
|
||
57 | 35
|
||
58 | ); |
||
59 | } |
||
60 | |||
61 | @Override
|
||
62 | public Expression getValue() { |
||
63 | return this; |
||
64 | } |
||
65 | |||
66 | @Override
|
||
67 | public boolean equals(Object obj) { |
||
68 | if( obj == null || !(obj instanceof Expression) ) { |
||
69 | return false; |
||
70 | } |
||
71 | String this_s = this.toJSON(); |
||
72 | String other_s = ((Expression)obj).toJSON(); |
||
73 | return this_s.equals(other_s);
|
||
74 | } |
||
75 | |||
76 | @Override
|
||
77 | public int hashCode() { |
||
78 | String this_s = this.toJSON(); |
||
79 | return Objects.hashCode(this_s);
|
||
80 | } |
||
81 | 44215 | jjdelcerro | |
82 | @Override
|
||
83 | public SymbolTable getSymbolTable() {
|
||
84 | if( this.mySymbolTable==null ) { |
||
85 | this.mySymbolTable = ExpressionUtils.createSymbolTable();
|
||
86 | } |
||
87 | return this.mySymbolTable; |
||
88 | } |
||
89 | 43983 | jjdelcerro | |
90 | @Override
|
||
91 | public String getPhrase() { |
||
92 | return this.phrase; |
||
93 | } |
||
94 | |||
95 | @Override
|
||
96 | 44126 | jjdelcerro | public boolean isPhraseEmpty() { |
97 | return StringUtils.isBlank(this.phrase); |
||
98 | } |
||
99 | |||
100 | @Override
|
||
101 | 44163 | jjdelcerro | public boolean isEmpty() { |
102 | if( !StringUtils.isBlank(this.phrase) ) { |
||
103 | return false; |
||
104 | } |
||
105 | if( this.scripts!=null && !this.scripts.isEmpty() ) { |
||
106 | return false; |
||
107 | } |
||
108 | 44182 | jjdelcerro | if( this.userScript!=null && !StringUtils.isBlank(this.userScript.getCode()) ) { |
109 | 44163 | jjdelcerro | return false; |
110 | } |
||
111 | return true; |
||
112 | } |
||
113 | |||
114 | @Override
|
||
115 | 43983 | jjdelcerro | public Script getUserScript() {
|
116 | return this.userScript; |
||
117 | } |
||
118 | |||
119 | @Override
|
||
120 | public UnmodifiableBasicList<Script> getScripts() {
|
||
121 | if (this.unmodifiableScripts == null) { |
||
122 | if (this.scripts == null) { |
||
123 | return null; |
||
124 | } |
||
125 | this.unmodifiableScripts = new UnmodifiableBasicListAdapter<>(this.scripts); |
||
126 | } |
||
127 | return this.unmodifiableScripts; |
||
128 | } |
||
129 | |||
130 | @Override
|
||
131 | 44163 | jjdelcerro | public Expression setPhrase(String phrase) { |
132 | 43983 | jjdelcerro | this.phrase = phrase;
|
133 | this.code = null; |
||
134 | 44009 | jjdelcerro | this.hasNotBeenOptimized = true; |
135 | 44163 | jjdelcerro | return this; |
136 | 43983 | jjdelcerro | } |
137 | |||
138 | @Override
|
||
139 | 44163 | jjdelcerro | public Expression setUserScript(String code, String languaje) { |
140 | 43983 | jjdelcerro | if (this.userScript == null) { |
141 | ScriptManager scriptMananger = ToolsLocator.getScriptManager(); |
||
142 | this.userScript = scriptMananger.createScript("user", code, languaje); |
||
143 | } else if (this.userScript.getTypeName().equalsIgnoreCase(languaje)) { |
||
144 | this.userScript.setCode(code);
|
||
145 | } else {
|
||
146 | ScriptManager scriptMananger = ToolsLocator.getScriptManager(); |
||
147 | this.userScript = scriptMananger.createScript("user", code, languaje); |
||
148 | } |
||
149 | 44163 | jjdelcerro | return this; |
150 | 43983 | jjdelcerro | } |
151 | |||
152 | @Override
|
||
153 | 44163 | jjdelcerro | public Expression setUserScript(Script script) { |
154 | 43983 | jjdelcerro | this.userScript = script;
|
155 | 44163 | jjdelcerro | return this; |
156 | 43983 | jjdelcerro | } |
157 | |||
158 | @Override
|
||
159 | 44163 | jjdelcerro | public Expression setUserScript(String code) { |
160 | 43983 | jjdelcerro | this.setUserScript(code, "python"); |
161 | 44163 | jjdelcerro | return this; |
162 | 43983 | jjdelcerro | } |
163 | |||
164 | @Override
|
||
165 | public void removeAllScripts() { |
||
166 | this.scripts = null; |
||
167 | this.unmodifiableScripts = null; |
||
168 | } |
||
169 | |||
170 | @Override
|
||
171 | 44163 | jjdelcerro | public Expression addScript(Script script) { |
172 | 43983 | jjdelcerro | if (this.scripts == null) { |
173 | this.scripts = new ArrayList<>(); |
||
174 | } |
||
175 | this.scripts.add(script);
|
||
176 | 44163 | jjdelcerro | return this; |
177 | 43983 | jjdelcerro | } |
178 | |||
179 | @Override
|
||
180 | 43984 | jjdelcerro | public void clear() { |
181 | this.phrase = null; |
||
182 | this.userScript = null; |
||
183 | this.unmodifiableScripts = null; |
||
184 | this.scripts = null; |
||
185 | this.code = null; |
||
186 | this.interpreter = null; |
||
187 | 44009 | jjdelcerro | this.hasNotBeenOptimized = true; |
188 | 43984 | jjdelcerro | } |
189 | |||
190 | @Override
|
||
191 | 43989 | jjdelcerro | public Code getCode() {
|
192 | 43983 | jjdelcerro | if (this.code == null) { |
193 | ExpressionEvaluatorManager manager = ExpressionEvaluatorLocator.getManager(); |
||
194 | 44215 | jjdelcerro | Compiler compiler = manager.createCompiler();
|
195 | compiler.getLexicalAnalyzer().setUseBracketsForIdentifiers( |
||
196 | this.useBracketsForIdentifiers
|
||
197 | ); |
||
198 | this.code = compiler.compileExpression(this.phrase); |
||
199 | 43983 | jjdelcerro | } |
200 | 43989 | jjdelcerro | return code;
|
201 | } |
||
202 | |||
203 | @Override
|
||
204 | 44191 | jjdelcerro | public void setSQLCompatible(boolean sqlCompatible) { |
205 | this.getInterpreter().setSQLCompatible(sqlCompatible);
|
||
206 | } |
||
207 | |||
208 | @Override
|
||
209 | public boolean isSQLCompatible() { |
||
210 | return this.getInterpreter().isSQLCompatible(); |
||
211 | } |
||
212 | |||
213 | private Interpreter getInterpreter() {
|
||
214 | if (this.interpreter == null) { |
||
215 | ExpressionEvaluatorManager manager = ExpressionEvaluatorLocator.getManager(); |
||
216 | this.interpreter = manager.createInterpreter();
|
||
217 | } |
||
218 | return this.interpreter; |
||
219 | } |
||
220 | |||
221 | @Override
|
||
222 | 43989 | jjdelcerro | public Object execute(SymbolTable symbolTable) { |
223 | 43983 | jjdelcerro | if (this.interpreter == null) { |
224 | ExpressionEvaluatorManager manager = ExpressionEvaluatorLocator.getManager(); |
||
225 | this.interpreter = manager.createInterpreter();
|
||
226 | } |
||
227 | 44215 | jjdelcerro | boolean added = this.getSymbolTable().addSymbolTable(symbolTable); |
228 | try {
|
||
229 | this.interpreter.setSymbolTable(this.mySymbolTable); |
||
230 | if( this.hasNotBeenOptimized ) { |
||
231 | Optimizer optimizer = new DefaultOptimizer(symbolTable);
|
||
232 | this.code = optimizer.optimize(this.getCode()); |
||
233 | this.hasNotBeenOptimized = false; |
||
234 | } |
||
235 | Object x = this.interpreter.run(this.getCode()); |
||
236 | |||
237 | return x;
|
||
238 | } finally {
|
||
239 | if( added ) {
|
||
240 | this.getSymbolTable().removeSymbolTable(symbolTable);
|
||
241 | } |
||
242 | 44009 | jjdelcerro | } |
243 | 43983 | jjdelcerro | } |
244 | |||
245 | @Override
|
||
246 | 44191 | jjdelcerro | public void link(SymbolTable symbolTable) { |
247 | if (this.interpreter == null) { |
||
248 | ExpressionEvaluatorManager manager = ExpressionEvaluatorLocator.getManager(); |
||
249 | this.interpreter = manager.createInterpreter();
|
||
250 | } |
||
251 | this.interpreter.setSymbolTable(symbolTable);
|
||
252 | if( this.hasNotBeenOptimized ) { |
||
253 | Optimizer optimizer = new DefaultOptimizer(symbolTable);
|
||
254 | this.code = optimizer.optimize(this.getCode()); |
||
255 | this.hasNotBeenOptimized = false; |
||
256 | } |
||
257 | this.interpreter.link(this.getCode()); |
||
258 | } |
||
259 | |||
260 | @Override
|
||
261 | 43983 | jjdelcerro | public void saveToState(PersistentState state) throws PersistenceException { |
262 | state.set("phrase", this.phrase); |
||
263 | if (this.userScript == null) { |
||
264 | 43991 | jjdelcerro | state.setNull("userScript_code");
|
265 | state.setNull("userScript_language");
|
||
266 | 43983 | jjdelcerro | } else {
|
267 | 43991 | jjdelcerro | state.set("userScript_code", this.userScript.getCode()); |
268 | state.set("userScript_language", this.userScript.getTypeName()); |
||
269 | 43983 | jjdelcerro | } |
270 | if (this.scripts != null && !this.scripts.isEmpty()) { |
||
271 | 44340 | jjdelcerro | List<URL> l = new ArrayList<>(); |
272 | 43983 | jjdelcerro | for (Script script : this.scripts) { |
273 | 44340 | jjdelcerro | URL location = script.getURL();
|
274 | 43983 | jjdelcerro | if (location != null) { |
275 | l.add(location); |
||
276 | } |
||
277 | } |
||
278 | if (l.isEmpty()) {
|
||
279 | state.setNull("scripts");
|
||
280 | } else {
|
||
281 | state.set("scripts", l);
|
||
282 | } |
||
283 | } else {
|
||
284 | state.setNull("scripts");
|
||
285 | } |
||
286 | } |
||
287 | |||
288 | @Override
|
||
289 | public void loadFromState(PersistentState state) throws PersistenceException { |
||
290 | ScriptManager scriptManager = ToolsLocator.getScriptManager(); |
||
291 | |||
292 | 44009 | jjdelcerro | this.clear();
|
293 | |||
294 | 43983 | jjdelcerro | this.phrase = state.getString("phrase"); |
295 | 43991 | jjdelcerro | String userScript_code = state.getString("userScript_code"); |
296 | String userScript_language = state.getString("userScript_language"); |
||
297 | 43983 | jjdelcerro | if (StringUtils.isEmpty(userScript_code)) {
|
298 | this.userScript = null; |
||
299 | } else {
|
||
300 | if (StringUtils.isEmpty(userScript_language)) {
|
||
301 | userScript_language = "python";
|
||
302 | } |
||
303 | this.userScript = scriptManager.createScript("user", userScript_code, userScript_language); |
||
304 | } |
||
305 | Iterator scriptsLocations = state.getIterator("scripts"); |
||
306 | 43991 | jjdelcerro | if (scriptsLocations != null) { |
307 | while (scriptsLocations.hasNext()) {
|
||
308 | URI location = (URI) scriptsLocations.next(); |
||
309 | Script script = scriptManager.loadScript(location); |
||
310 | if (script != null) { |
||
311 | if (this.scripts == null) { |
||
312 | this.scripts = new ArrayList<>(); |
||
313 | } |
||
314 | this.scripts.add(script);
|
||
315 | 43983 | jjdelcerro | } |
316 | } |
||
317 | } |
||
318 | } |
||
319 | |||
320 | public static void registerPersistence() { |
||
321 | PersistenceManager manager = ToolsLocator.getPersistenceManager(); |
||
322 | if (manager.getDefinition("Expression") == null) { |
||
323 | DynStruct definition = manager.addDefinition(DefaultExpression.class, |
||
324 | "Expression", "Expression persistence definition", null, null); |
||
325 | definition.addDynFieldString("phrase").setMandatory(false); |
||
326 | 43991 | jjdelcerro | definition.addDynFieldString("userScript_code").setMandatory(false); |
327 | definition.addDynFieldString("userScript_language").setMandatory(false); |
||
328 | 44190 | jjdelcerro | definition.addDynFieldList("scripts")
|
329 | 44340 | jjdelcerro | .setClassOfItems(URL.class)
|
330 | 44190 | jjdelcerro | .setMandatory(false);
|
331 | 43983 | jjdelcerro | } |
332 | } |
||
333 | |||
334 | @Override
|
||
335 | 43984 | jjdelcerro | public String toJSON() { |
336 | JSONObject expressionJson = new JSONObject();
|
||
337 | expressionJson.put("phrase", this.phrase); |
||
338 | 43991 | jjdelcerro | |
339 | if (this.userScript != null) { |
||
340 | 43984 | jjdelcerro | JSONObject userScriptJson = new JSONObject();
|
341 | userScriptJson.put("code", this.userScript.getCode()); |
||
342 | userScriptJson.put("language", this.userScript.getTypeName()); |
||
343 | expressionJson.put("userScript", userScriptJson);
|
||
344 | 43983 | jjdelcerro | } |
345 | 43991 | jjdelcerro | |
346 | if (this.scripts != null && !this.scripts.isEmpty()) { |
||
347 | 43984 | jjdelcerro | JSONArray scriptsJson = new JSONArray();
|
348 | 43983 | jjdelcerro | for (Script script : this.scripts) { |
349 | 44340 | jjdelcerro | scriptsJson.put(script.getURL()); |
350 | 43983 | jjdelcerro | } |
351 | 43984 | jjdelcerro | expressionJson.put("scripts", scriptsJson);
|
352 | } |
||
353 | return expressionJson.toString();
|
||
354 | } |
||
355 | |||
356 | @Override
|
||
357 | public void fromJSON(String json) { |
||
358 | this.clear();
|
||
359 | ScriptManager scriptMananger = ToolsLocator.getScriptManager(); |
||
360 | |||
361 | JSONObject expressionJson = new JSONObject(json);
|
||
362 | 43991 | jjdelcerro | if (expressionJson.has("phrase")) { |
363 | 43984 | jjdelcerro | this.phrase = expressionJson.getString("phrase"); |
364 | } |
||
365 | 43991 | jjdelcerro | if (expressionJson.has("userScript")) { |
366 | 43984 | jjdelcerro | String theCode = ""; |
367 | String theLanguage = "python"; |
||
368 | JSONObject userScriptJson = expressionJson.getJSONObject("userScript");
|
||
369 | 43991 | jjdelcerro | if (userScriptJson.has("code")) { |
370 | 43984 | jjdelcerro | theCode = userScriptJson.getString("code");
|
371 | 43983 | jjdelcerro | } |
372 | 43991 | jjdelcerro | if (userScriptJson.has("language")) { |
373 | 43984 | jjdelcerro | theCode = userScriptJson.getString("language");
|
374 | } |
||
375 | this.userScript = scriptMananger.createScript("user", theCode, theLanguage); |
||
376 | 43983 | jjdelcerro | } |
377 | 43991 | jjdelcerro | if (expressionJson.has("scripts")) { |
378 | 43984 | jjdelcerro | this.scripts = new ArrayList<>(); |
379 | JSONArray scriptsJson = expressionJson.getJSONArray("scripts");
|
||
380 | for (Object object : scriptsJson) { |
||
381 | URI location = (URI) object; |
||
382 | Script script = scriptMananger.loadScript(location); |
||
383 | this.scripts.add(script);
|
||
384 | } |
||
385 | 43983 | jjdelcerro | } |
386 | } |
||
387 | |||
388 | 43984 | jjdelcerro | @Override
|
389 | public String toString() { |
||
390 | return this.toJSON(); |
||
391 | } |
||
392 | |||
393 | @Override
|
||
394 | public Expression clone() throws CloneNotSupportedException { |
||
395 | Expression other = (Expression) super.clone(); |
||
396 | other.fromJSON(this.toJSON());
|
||
397 | return other;
|
||
398 | } |
||
399 | |||
400 | 44215 | jjdelcerro | @Override
|
401 | public void setUseBracketsForIdentifiers(boolean useBracketsForIdentifiers) { |
||
402 | this.useBracketsForIdentifiers = useBracketsForIdentifiers;
|
||
403 | } |
||
404 | |||
405 | @Override
|
||
406 | public boolean getUseBracketsForIdentifiers() { |
||
407 | return this.useBracketsForIdentifiers; |
||
408 | } |
||
409 | |||
410 | 43983 | jjdelcerro | } |