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 | 43983 | jjdelcerro | 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 | 44009 | jjdelcerro | import org.gvsig.expressionevaluator.Optimizer; |
14 | 43983 | jjdelcerro | 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 | 43984 | jjdelcerro | import org.json.JSONArray; |
25 | import org.json.JSONObject; |
||
26 | 43983 | jjdelcerro | |
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 | 44009 | jjdelcerro | private boolean hasNotBeenOptimized = true; |
41 | 43983 | jjdelcerro | |
42 | public DefaultExpression() {
|
||
43 | |||
44 | } |
||
45 | |||
46 | @Override
|
||
47 | public String getPhrase() { |
||
48 | return this.phrase; |
||
49 | } |
||
50 | |||
51 | @Override
|
||
52 | 44126 | jjdelcerro | public boolean isPhraseEmpty() { |
53 | return StringUtils.isBlank(this.phrase); |
||
54 | } |
||
55 | |||
56 | @Override
|
||
57 | 44163 | jjdelcerro | 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 | 44182 | jjdelcerro | if( this.userScript!=null && !StringUtils.isBlank(this.userScript.getCode()) ) { |
65 | 44163 | jjdelcerro | return false; |
66 | } |
||
67 | return true; |
||
68 | } |
||
69 | |||
70 | @Override
|
||
71 | 43983 | jjdelcerro | 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 | 44163 | jjdelcerro | public Expression setPhrase(String phrase) { |
88 | 43983 | jjdelcerro | this.phrase = phrase;
|
89 | this.code = null; |
||
90 | 44009 | jjdelcerro | this.hasNotBeenOptimized = true; |
91 | 44163 | jjdelcerro | return this; |
92 | 43983 | jjdelcerro | } |
93 | |||
94 | @Override
|
||
95 | 44163 | jjdelcerro | public Expression setUserScript(String code, String languaje) { |
96 | 43983 | jjdelcerro | 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 | 44163 | jjdelcerro | return this; |
106 | 43983 | jjdelcerro | } |
107 | |||
108 | @Override
|
||
109 | 44163 | jjdelcerro | public Expression setUserScript(Script script) { |
110 | 43983 | jjdelcerro | this.userScript = script;
|
111 | 44163 | jjdelcerro | return this; |
112 | 43983 | jjdelcerro | } |
113 | |||
114 | @Override
|
||
115 | 44163 | jjdelcerro | public Expression setUserScript(String code) { |
116 | 43983 | jjdelcerro | this.setUserScript(code, "python"); |
117 | 44163 | jjdelcerro | return this; |
118 | 43983 | jjdelcerro | } |
119 | |||
120 | @Override
|
||
121 | public void removeAllScripts() { |
||
122 | this.scripts = null; |
||
123 | this.unmodifiableScripts = null; |
||
124 | } |
||
125 | |||
126 | @Override
|
||
127 | 44163 | jjdelcerro | public Expression addScript(Script script) { |
128 | 43983 | jjdelcerro | if (this.scripts == null) { |
129 | this.scripts = new ArrayList<>(); |
||
130 | } |
||
131 | this.scripts.add(script);
|
||
132 | 44163 | jjdelcerro | return this; |
133 | 43983 | jjdelcerro | } |
134 | |||
135 | @Override
|
||
136 | 43984 | jjdelcerro | 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 | 44009 | jjdelcerro | this.hasNotBeenOptimized = true; |
144 | 43984 | jjdelcerro | } |
145 | |||
146 | @Override
|
||
147 | 43989 | jjdelcerro | public Code getCode() {
|
148 | 43983 | jjdelcerro | if (this.code == null) { |
149 | ExpressionEvaluatorManager manager = ExpressionEvaluatorLocator.getManager(); |
||
150 | this.code = manager.compile(this.phrase); |
||
151 | } |
||
152 | 43989 | jjdelcerro | return code;
|
153 | } |
||
154 | |||
155 | @Override
|
||
156 | 44191 | jjdelcerro | 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 | 43989 | jjdelcerro | public Object execute(SymbolTable symbolTable) { |
175 | 43983 | jjdelcerro | if (this.interpreter == null) { |
176 | ExpressionEvaluatorManager manager = ExpressionEvaluatorLocator.getManager(); |
||
177 | this.interpreter = manager.createInterpreter();
|
||
178 | } |
||
179 | this.interpreter.setSymbolTable(symbolTable);
|
||
180 | 44009 | jjdelcerro | if( this.hasNotBeenOptimized ) { |
181 | Optimizer optimizer = new DefaultOptimizer(symbolTable);
|
||
182 | this.code = optimizer.optimize(this.getCode()); |
||
183 | this.hasNotBeenOptimized = false; |
||
184 | } |
||
185 | 43989 | jjdelcerro | Object x = this.interpreter.run(this.getCode()); |
186 | 43983 | jjdelcerro | return x;
|
187 | } |
||
188 | |||
189 | @Override
|
||
190 | 44191 | jjdelcerro | 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 | 43983 | jjdelcerro | public void saveToState(PersistentState state) throws PersistenceException { |
206 | state.set("phrase", this.phrase); |
||
207 | if (this.userScript == null) { |
||
208 | 43991 | jjdelcerro | state.setNull("userScript_code");
|
209 | state.setNull("userScript_language");
|
||
210 | 43983 | jjdelcerro | } else {
|
211 | 43991 | jjdelcerro | state.set("userScript_code", this.userScript.getCode()); |
212 | state.set("userScript_language", this.userScript.getTypeName()); |
||
213 | 43983 | jjdelcerro | } |
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 | 44009 | jjdelcerro | this.clear();
|
237 | |||
238 | 43983 | jjdelcerro | this.phrase = state.getString("phrase"); |
239 | 43991 | jjdelcerro | String userScript_code = state.getString("userScript_code"); |
240 | String userScript_language = state.getString("userScript_language"); |
||
241 | 43983 | jjdelcerro | 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 | 43991 | jjdelcerro | 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 | 43983 | jjdelcerro | } |
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 | 43991 | jjdelcerro | definition.addDynFieldString("userScript_code").setMandatory(false); |
271 | definition.addDynFieldString("userScript_language").setMandatory(false); |
||
272 | 44190 | jjdelcerro | definition.addDynFieldList("scripts")
|
273 | .setClassOfItems(URI.class)
|
||
274 | .setMandatory(false);
|
||
275 | 43983 | jjdelcerro | } |
276 | } |
||
277 | |||
278 | @Override
|
||
279 | 43984 | jjdelcerro | public String toJSON() { |
280 | JSONObject expressionJson = new JSONObject();
|
||
281 | expressionJson.put("phrase", this.phrase); |
||
282 | 43991 | jjdelcerro | |
283 | if (this.userScript != null) { |
||
284 | 43984 | jjdelcerro | JSONObject userScriptJson = new JSONObject();
|
285 | userScriptJson.put("code", this.userScript.getCode()); |
||
286 | userScriptJson.put("language", this.userScript.getTypeName()); |
||
287 | expressionJson.put("userScript", userScriptJson);
|
||
288 | 43983 | jjdelcerro | } |
289 | 43991 | jjdelcerro | |
290 | if (this.scripts != null && !this.scripts.isEmpty()) { |
||
291 | 43984 | jjdelcerro | JSONArray scriptsJson = new JSONArray();
|
292 | 43983 | jjdelcerro | for (Script script : this.scripts) { |
293 | 43984 | jjdelcerro | scriptsJson.put(script.getURI()); |
294 | 43983 | jjdelcerro | } |
295 | 43984 | jjdelcerro | 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 | 43991 | jjdelcerro | if (expressionJson.has("phrase")) { |
307 | 43984 | jjdelcerro | this.phrase = expressionJson.getString("phrase"); |
308 | } |
||
309 | 43991 | jjdelcerro | if (expressionJson.has("userScript")) { |
310 | 43984 | jjdelcerro | String theCode = ""; |
311 | String theLanguage = "python"; |
||
312 | JSONObject userScriptJson = expressionJson.getJSONObject("userScript");
|
||
313 | 43991 | jjdelcerro | if (userScriptJson.has("code")) { |
314 | 43984 | jjdelcerro | theCode = userScriptJson.getString("code");
|
315 | 43983 | jjdelcerro | } |
316 | 43991 | jjdelcerro | if (userScriptJson.has("language")) { |
317 | 43984 | jjdelcerro | theCode = userScriptJson.getString("language");
|
318 | } |
||
319 | this.userScript = scriptMananger.createScript("user", theCode, theLanguage); |
||
320 | 43983 | jjdelcerro | } |
321 | 43991 | jjdelcerro | if (expressionJson.has("scripts")) { |
322 | 43984 | jjdelcerro | 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 | 43983 | jjdelcerro | } |
330 | } |
||
331 | |||
332 | 43984 | jjdelcerro | @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 | 43983 | jjdelcerro | } |