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