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