Statistics
| Revision:

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 @ 44361

History | View | Annotate | Download (12.5 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
import org.apache.commons.lang3.StringUtils;
9
import org.gvsig.expressionevaluator.Code;
10 44215 jjdelcerro
import org.gvsig.expressionevaluator.Compiler;
11 43983 jjdelcerro
import org.gvsig.expressionevaluator.Expression;
12
import org.gvsig.expressionevaluator.ExpressionEvaluatorLocator;
13
import org.gvsig.expressionevaluator.ExpressionEvaluatorManager;
14 44215 jjdelcerro
import org.gvsig.expressionevaluator.ExpressionUtils;
15 43983 jjdelcerro
import org.gvsig.expressionevaluator.Interpreter;
16 44009 jjdelcerro
import org.gvsig.expressionevaluator.Optimizer;
17 43983 jjdelcerro
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 43984 jjdelcerro
import org.json.JSONArray;
28
import org.json.JSONObject;
29 43983 jjdelcerro
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 44009 jjdelcerro
    private boolean hasNotBeenOptimized = true;
44 44215 jjdelcerro
    private SymbolTable mySymbolTable = null;
45
    private boolean useBracketsForIdentifiers = false;
46 43983 jjdelcerro
47
    public DefaultExpression() {
48
49
    }
50 44215 jjdelcerro
51
    @Override
52
    public SymbolTable getSymbolTable() {
53
        if( this.mySymbolTable==null ) {
54
            this.mySymbolTable = ExpressionUtils.createSymbolTable();
55
        }
56
        return this.mySymbolTable;
57
    }
58 43983 jjdelcerro
59
    @Override
60
    public String getPhrase() {
61
        return this.phrase;
62
    }
63
64
    @Override
65 44126 jjdelcerro
    public boolean isPhraseEmpty() {
66
        return StringUtils.isBlank(this.phrase);
67
    }
68
69
    @Override
70 44163 jjdelcerro
    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 44182 jjdelcerro
        if( this.userScript!=null && !StringUtils.isBlank(this.userScript.getCode()) ) {
78 44163 jjdelcerro
            return false;
79
        }
80
        return true;
81
    }
82
83
    @Override
84 43983 jjdelcerro
    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 44163 jjdelcerro
    public Expression setPhrase(String phrase) {
101 43983 jjdelcerro
        this.phrase = phrase;
102
        this.code = null;
103 44009 jjdelcerro
        this.hasNotBeenOptimized = true;
104 44163 jjdelcerro
        return this;
105 43983 jjdelcerro
    }
106
107
    @Override
108 44163 jjdelcerro
    public Expression setUserScript(String code, String languaje) {
109 43983 jjdelcerro
        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 44163 jjdelcerro
        return this;
119 43983 jjdelcerro
    }
120
121
    @Override
122 44163 jjdelcerro
    public Expression setUserScript(Script script) {
123 43983 jjdelcerro
        this.userScript = script;
124 44163 jjdelcerro
        return this;
125 43983 jjdelcerro
    }
126
127
    @Override
128 44163 jjdelcerro
    public Expression setUserScript(String code) {
129 43983 jjdelcerro
        this.setUserScript(code, "python");
130 44163 jjdelcerro
        return this;
131 43983 jjdelcerro
    }
132
133
    @Override
134
    public void removeAllScripts() {
135
        this.scripts = null;
136
        this.unmodifiableScripts = null;
137
    }
138
139
    @Override
140 44163 jjdelcerro
    public Expression addScript(Script script) {
141 43983 jjdelcerro
        if (this.scripts == null) {
142
            this.scripts = new ArrayList<>();
143
        }
144
        this.scripts.add(script);
145 44163 jjdelcerro
        return this;
146 43983 jjdelcerro
    }
147
148
    @Override
149 43984 jjdelcerro
    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 44009 jjdelcerro
        this.hasNotBeenOptimized = true;
157 43984 jjdelcerro
    }
158
159
    @Override
160 43989 jjdelcerro
    public Code getCode() {
161 43983 jjdelcerro
        if (this.code == null) {
162
            ExpressionEvaluatorManager manager = ExpressionEvaluatorLocator.getManager();
163 44215 jjdelcerro
            Compiler compiler = manager.createCompiler();
164
            compiler.getLexicalAnalyzer().setUseBracketsForIdentifiers(
165
                    this.useBracketsForIdentifiers
166
            );
167
            this.code = compiler.compileExpression(this.phrase);
168 43983 jjdelcerro
        }
169 43989 jjdelcerro
        return code;
170
    }
171
172
    @Override
173 44191 jjdelcerro
    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 43989 jjdelcerro
    public Object execute(SymbolTable symbolTable) {
192 43983 jjdelcerro
        if (this.interpreter == null) {
193
            ExpressionEvaluatorManager manager = ExpressionEvaluatorLocator.getManager();
194
            this.interpreter = manager.createInterpreter();
195
        }
196 44215 jjdelcerro
        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 44009 jjdelcerro
        }
212 43983 jjdelcerro
    }
213
214
    @Override
215 44191 jjdelcerro
    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 43983 jjdelcerro
    public void saveToState(PersistentState state) throws PersistenceException {
231
        state.set("phrase", this.phrase);
232
        if (this.userScript == null) {
233 43991 jjdelcerro
            state.setNull("userScript_code");
234
            state.setNull("userScript_language");
235 43983 jjdelcerro
        } else {
236 43991 jjdelcerro
            state.set("userScript_code", this.userScript.getCode());
237
            state.set("userScript_language", this.userScript.getTypeName());
238 43983 jjdelcerro
        }
239
        if (this.scripts != null && !this.scripts.isEmpty()) {
240 44340 jjdelcerro
            List<URL> l = new ArrayList<>();
241 43983 jjdelcerro
            for (Script script : this.scripts) {
242 44340 jjdelcerro
                URL location = script.getURL();
243 43983 jjdelcerro
                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 44009 jjdelcerro
        this.clear();
262
263 43983 jjdelcerro
        this.phrase = state.getString("phrase");
264 43991 jjdelcerro
        String userScript_code = state.getString("userScript_code");
265
        String userScript_language = state.getString("userScript_language");
266 43983 jjdelcerro
        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 43991 jjdelcerro
        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 43983 jjdelcerro
                }
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 43991 jjdelcerro
            definition.addDynFieldString("userScript_code").setMandatory(false);
296
            definition.addDynFieldString("userScript_language").setMandatory(false);
297 44190 jjdelcerro
            definition.addDynFieldList("scripts")
298 44340 jjdelcerro
                    .setClassOfItems(URL.class)
299 44190 jjdelcerro
                    .setMandatory(false);
300 43983 jjdelcerro
        }
301
    }
302
303
    @Override
304 43984 jjdelcerro
    public String toJSON() {
305
        JSONObject expressionJson = new JSONObject();
306
        expressionJson.put("phrase", this.phrase);
307 43991 jjdelcerro
308
        if (this.userScript != null) {
309 43984 jjdelcerro
            JSONObject userScriptJson = new JSONObject();
310
            userScriptJson.put("code", this.userScript.getCode());
311
            userScriptJson.put("language", this.userScript.getTypeName());
312
            expressionJson.put("userScript", userScriptJson);
313 43983 jjdelcerro
        }
314 43991 jjdelcerro
315
        if (this.scripts != null && !this.scripts.isEmpty()) {
316 43984 jjdelcerro
            JSONArray scriptsJson = new JSONArray();
317 43983 jjdelcerro
            for (Script script : this.scripts) {
318 44340 jjdelcerro
                scriptsJson.put(script.getURL());
319 43983 jjdelcerro
            }
320 43984 jjdelcerro
            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 43991 jjdelcerro
        if (expressionJson.has("phrase")) {
332 43984 jjdelcerro
            this.phrase = expressionJson.getString("phrase");
333
        }
334 43991 jjdelcerro
        if (expressionJson.has("userScript")) {
335 43984 jjdelcerro
            String theCode = "";
336
            String theLanguage = "python";
337
            JSONObject userScriptJson = expressionJson.getJSONObject("userScript");
338 43991 jjdelcerro
            if (userScriptJson.has("code")) {
339 43984 jjdelcerro
                theCode = userScriptJson.getString("code");
340 43983 jjdelcerro
            }
341 43991 jjdelcerro
            if (userScriptJson.has("language")) {
342 43984 jjdelcerro
                theCode = userScriptJson.getString("language");
343
            }
344
            this.userScript = scriptMananger.createScript("user", theCode, theLanguage);
345 43983 jjdelcerro
        }
346 43991 jjdelcerro
        if (expressionJson.has("scripts")) {
347 43984 jjdelcerro
            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 43983 jjdelcerro
        }
355
    }
356
357 43984 jjdelcerro
    @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 44215 jjdelcerro
    @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 43983 jjdelcerro
}