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 |
package org.gvsig.expressionevaluator.impl; |
---|---|
2 |
|
3 |
import java.net.URI; |
4 |
import java.net.URL; |
5 |
import java.util.ArrayList; |
6 |
import java.util.Iterator; |
7 |
import java.util.List; |
8 |
import java.util.Objects; |
9 |
import org.apache.commons.lang3.StringUtils; |
10 |
import org.gvsig.expressionevaluator.Code; |
11 |
import org.gvsig.expressionevaluator.Compiler; |
12 |
import org.gvsig.expressionevaluator.Expression; |
13 |
import org.gvsig.expressionevaluator.ExpressionEvaluatorLocator; |
14 |
import org.gvsig.expressionevaluator.ExpressionEvaluatorManager; |
15 |
import org.gvsig.expressionevaluator.ExpressionUtils; |
16 |
import org.gvsig.expressionevaluator.Interpreter; |
17 |
import org.gvsig.expressionevaluator.Optimizer; |
18 |
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 |
import org.gvsig.tools.util.LabeledValue; |
27 |
import org.gvsig.tools.util.UnmodifiableBasicList; |
28 |
import org.gvsig.tools.util.UnmodifiableBasicListAdapter; |
29 |
import org.json.JSONArray; |
30 |
import org.json.JSONObject; |
31 |
|
32 |
/**
|
33 |
*
|
34 |
* @author jjdelcerro
|
35 |
*/
|
36 |
public class DefaultExpression implements Expression, LabeledValue<Expression> { |
37 |
|
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 |
private boolean hasNotBeenOptimized = true; |
46 |
private SymbolTable mySymbolTable = null; |
47 |
private boolean useBracketsForIdentifiers = false; |
48 |
|
49 |
public DefaultExpression() {
|
50 |
|
51 |
} |
52 |
|
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 |
|
82 |
@Override
|
83 |
public SymbolTable getSymbolTable() {
|
84 |
if( this.mySymbolTable==null ) { |
85 |
this.mySymbolTable = ExpressionUtils.createSymbolTable();
|
86 |
} |
87 |
return this.mySymbolTable; |
88 |
} |
89 |
|
90 |
@Override
|
91 |
public String getPhrase() { |
92 |
return this.phrase; |
93 |
} |
94 |
|
95 |
@Override
|
96 |
public boolean isPhraseEmpty() { |
97 |
return StringUtils.isBlank(this.phrase); |
98 |
} |
99 |
|
100 |
@Override
|
101 |
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 |
if( this.userScript!=null && !StringUtils.isBlank(this.userScript.getCode()) ) { |
109 |
return false; |
110 |
} |
111 |
return true; |
112 |
} |
113 |
|
114 |
@Override
|
115 |
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 |
public Expression setPhrase(String phrase) { |
132 |
this.phrase = phrase;
|
133 |
this.code = null; |
134 |
this.hasNotBeenOptimized = true; |
135 |
return this; |
136 |
} |
137 |
|
138 |
@Override
|
139 |
public Expression setUserScript(String code, String languaje) { |
140 |
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 |
return this; |
150 |
} |
151 |
|
152 |
@Override
|
153 |
public Expression setUserScript(Script script) { |
154 |
this.userScript = script;
|
155 |
return this; |
156 |
} |
157 |
|
158 |
@Override
|
159 |
public Expression setUserScript(String code) { |
160 |
this.setUserScript(code, "python"); |
161 |
return this; |
162 |
} |
163 |
|
164 |
@Override
|
165 |
public void removeAllScripts() { |
166 |
this.scripts = null; |
167 |
this.unmodifiableScripts = null; |
168 |
} |
169 |
|
170 |
@Override
|
171 |
public Expression addScript(Script script) { |
172 |
if (this.scripts == null) { |
173 |
this.scripts = new ArrayList<>(); |
174 |
} |
175 |
this.scripts.add(script);
|
176 |
return this; |
177 |
} |
178 |
|
179 |
@Override
|
180 |
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 |
this.hasNotBeenOptimized = true; |
188 |
} |
189 |
|
190 |
@Override
|
191 |
public Code getCode() {
|
192 |
if (this.code == null) { |
193 |
ExpressionEvaluatorManager manager = ExpressionEvaluatorLocator.getManager(); |
194 |
Compiler compiler = manager.createCompiler();
|
195 |
compiler.getLexicalAnalyzer().setUseBracketsForIdentifiers( |
196 |
this.useBracketsForIdentifiers
|
197 |
); |
198 |
this.code = compiler.compileExpression(this.phrase); |
199 |
} |
200 |
return code;
|
201 |
} |
202 |
|
203 |
@Override
|
204 |
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 |
public Object execute(SymbolTable symbolTable) { |
223 |
if (this.interpreter == null) { |
224 |
ExpressionEvaluatorManager manager = ExpressionEvaluatorLocator.getManager(); |
225 |
this.interpreter = manager.createInterpreter();
|
226 |
} |
227 |
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 |
} |
243 |
} |
244 |
|
245 |
@Override
|
246 |
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 |
public void saveToState(PersistentState state) throws PersistenceException { |
262 |
state.set("phrase", this.phrase); |
263 |
if (this.userScript == null) { |
264 |
state.setNull("userScript_code");
|
265 |
state.setNull("userScript_language");
|
266 |
} else {
|
267 |
state.set("userScript_code", this.userScript.getCode()); |
268 |
state.set("userScript_language", this.userScript.getTypeName()); |
269 |
} |
270 |
if (this.scripts != null && !this.scripts.isEmpty()) { |
271 |
List<URL> l = new ArrayList<>(); |
272 |
for (Script script : this.scripts) { |
273 |
URL location = script.getURL();
|
274 |
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 |
this.clear();
|
293 |
|
294 |
this.phrase = state.getString("phrase"); |
295 |
String userScript_code = state.getString("userScript_code"); |
296 |
String userScript_language = state.getString("userScript_language"); |
297 |
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 |
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 |
} |
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 |
definition.addDynFieldString("userScript_code").setMandatory(false); |
327 |
definition.addDynFieldString("userScript_language").setMandatory(false); |
328 |
definition.addDynFieldList("scripts")
|
329 |
.setClassOfItems(URL.class)
|
330 |
.setMandatory(false);
|
331 |
} |
332 |
} |
333 |
|
334 |
@Override
|
335 |
public String toJSON() { |
336 |
JSONObject expressionJson = new JSONObject();
|
337 |
expressionJson.put("phrase", this.phrase); |
338 |
|
339 |
if (this.userScript != null) { |
340 |
JSONObject userScriptJson = new JSONObject();
|
341 |
userScriptJson.put("code", this.userScript.getCode()); |
342 |
userScriptJson.put("language", this.userScript.getTypeName()); |
343 |
expressionJson.put("userScript", userScriptJson);
|
344 |
} |
345 |
|
346 |
if (this.scripts != null && !this.scripts.isEmpty()) { |
347 |
JSONArray scriptsJson = new JSONArray();
|
348 |
for (Script script : this.scripts) { |
349 |
scriptsJson.put(script.getURL()); |
350 |
} |
351 |
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 |
if (expressionJson.has("phrase")) { |
363 |
this.phrase = expressionJson.getString("phrase"); |
364 |
} |
365 |
if (expressionJson.has("userScript")) { |
366 |
String theCode = ""; |
367 |
String theLanguage = "python"; |
368 |
JSONObject userScriptJson = expressionJson.getJSONObject("userScript");
|
369 |
if (userScriptJson.has("code")) { |
370 |
theCode = userScriptJson.getString("code");
|
371 |
} |
372 |
if (userScriptJson.has("language")) { |
373 |
theCode = userScriptJson.getString("language");
|
374 |
} |
375 |
this.userScript = scriptMananger.createScript("user", theCode, theLanguage); |
376 |
} |
377 |
if (expressionJson.has("scripts")) { |
378 |
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 |
} |
386 |
} |
387 |
|
388 |
@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 |
@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 |
} |