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

History | View | Annotate | Download (13.3 KB)

1
package org.gvsig.expressionevaluator.impl;
2

    
3
import java.util.Objects;
4
import org.apache.commons.lang3.StringUtils;
5
import org.gvsig.expressionevaluator.Code;
6
import org.gvsig.expressionevaluator.Compiler;
7
import org.gvsig.expressionevaluator.Expression;
8
import org.gvsig.expressionevaluator.ExpressionEvaluatorLocator;
9
import org.gvsig.expressionevaluator.ExpressionEvaluatorManager;
10
import org.gvsig.expressionevaluator.ExpressionUtils;
11
import org.gvsig.expressionevaluator.Interpreter;
12
import org.gvsig.expressionevaluator.Optimizer;
13
import org.gvsig.expressionevaluator.SymbolTable;
14
import org.gvsig.tools.ToolsLocator;
15
import org.gvsig.tools.dynobject.DynStruct;
16
import org.gvsig.tools.persistence.PersistenceManager;
17
import org.gvsig.tools.persistence.PersistentState;
18
import org.gvsig.tools.persistence.exception.PersistenceException;
19
import org.gvsig.tools.script.ScriptManager;
20
import org.gvsig.tools.util.LabeledValue;
21
import org.json.JSONObject;
22

    
23
/**
24
 *
25
 * @author jjdelcerro
26
 */
27
public class DefaultExpression implements Expression, LabeledValue<Expression> {
28

    
29
    private String phrase = null;
30
//    private Script userScript = null;
31
//    private List<Script> scripts = null;
32
//    private UnmodifiableBasicList<Script> unmodifiableScripts = null;
33

    
34
    private Code code = null;
35
    private Interpreter interpreter;
36
    private boolean hasNotBeenOptimized = true;
37
    private SymbolTable mySymbolTable = null;
38
    private boolean useBracketsForIdentifiers = false;
39

    
40
    public DefaultExpression() {
41

    
42
    }
43

    
44
    @Override
45
    public String getLabel() {
46
        return StringUtils.abbreviate(
47
                StringUtils.normalizeSpace(this.getPhrase()),
48
                35
49
        );
50
    }
51

    
52
    @Override
53
    public Expression getValue() {
54
        return this;
55
    }
56

    
57
    @Override
58
    public boolean equals(Object obj) {
59
        if( obj == null || !(obj instanceof Expression) ) {
60
            return false;
61
        }
62
        String this_s = this.toJSON();
63
        String other_s = ((Expression)obj).toJSON();
64
        return this_s.equals(other_s);
65
    }
66

    
67
    @Override
68
    public int hashCode() {
69
        String this_s = this.toJSON();
70
        return Objects.hashCode(this_s);
71
    }
72
    
73
    @Override
74
    public SymbolTable getSymbolTable() {
75
        if( this.mySymbolTable==null ) {
76
            this.mySymbolTable = ExpressionUtils.createSymbolTable();
77
        }
78
        return this.mySymbolTable;
79
    }
80

    
81
    @Override
82
    public String getPhrase() {
83
        return this.phrase;
84
    }
85

    
86
    @Override
87
    public boolean isPhraseEmpty() {
88
        return StringUtils.isBlank(this.phrase);
89
    }
90

    
91
    @Override
92
    public boolean isEmpty() {
93
        if( !StringUtils.isBlank(this.phrase) ) {
94
            return false;
95
        }
96
//        if( this.scripts!=null && !this.scripts.isEmpty() ) {
97
//            return false;
98
//        }
99
//        if( this.userScript!=null && !StringUtils.isBlank(this.userScript.getCode()) ) {
100
//            return false;
101
//        }
102
        return true;
103
    }
104

    
105
//    @Override
106
//    public Script getUserScript() {
107
//        return this.userScript;
108
//    }
109
//
110
//    @Override
111
//    public UnmodifiableBasicList<Script> getScripts() {
112
//        if (this.unmodifiableScripts == null) {
113
//            if (this.scripts == null) {
114
//                return null;
115
//            }
116
//            this.unmodifiableScripts = new UnmodifiableBasicListAdapter<>(this.scripts);
117
//        }
118
//        return this.unmodifiableScripts;
119
//    }
120
//
121
    @Override
122
    public Expression setPhrase(String phrase) {
123
        this.phrase = phrase;
124
        this.code = null;
125
        this.hasNotBeenOptimized = true;
126
        return this;
127
    }
128

    
129
//    @Override
130
//    public Expression setUserScript(String code, String languaje) {
131
//        if (this.userScript == null) {
132
//            ScriptManager scriptMananger = ToolsLocator.getScriptManager();
133
//            this.userScript = scriptMananger.createScript("user", code, languaje);
134
//        } else if (this.userScript.getTypeName().equalsIgnoreCase(languaje)) {
135
//            this.userScript.setCode(code);
136
//        } else {
137
//            ScriptManager scriptMananger = ToolsLocator.getScriptManager();
138
//            this.userScript = scriptMananger.createScript("user", code, languaje);
139
//        }
140
//        return this;
141
//    }
142
//
143
//    @Override
144
//    public Expression setUserScript(Script script) {
145
//        this.userScript = script;
146
//        return this;
147
//    }
148
//
149
//    @Override
150
//    public Expression setUserScript(String code) {
151
//        this.setUserScript(code, "python");
152
//        return this;
153
//    }
154
//
155
//    @Override
156
//    public void removeAllScripts() {
157
//        this.scripts = null;
158
//        this.unmodifiableScripts = null;
159
//    }
160
//
161
//    @Override
162
//    public Expression addScript(Script script) {
163
//        if (this.scripts == null) {
164
//            this.scripts = new ArrayList<>();
165
//        }
166
//        this.scripts.add(script);
167
//        return this;
168
//    }
169

    
170
    @Override
171
    public void clear() {
172
        this.phrase = null;
173
//        this.userScript = null;
174
//        this.unmodifiableScripts = null;
175
//        this.scripts = null;
176
        this.code = null;
177
        this.interpreter = null;
178
        this.hasNotBeenOptimized = true;
179
    }
180

    
181
    @Override
182
    public Code getCode() {
183
        if (this.code == null) {
184
            ExpressionEvaluatorManager manager = ExpressionEvaluatorLocator.getManager();
185
            Compiler compiler = manager.createCompiler();
186
            compiler.getLexicalAnalyzer().setUseBracketsForIdentifiers(
187
                    this.useBracketsForIdentifiers
188
            );
189
            this.code = compiler.compileExpression(this.phrase);
190
        }
191
        return code;
192
    }
193

    
194
    @Override
195
    public void setSQLCompatible(boolean sqlCompatible) {
196
        this.getInterpreter().setSQLCompatible(sqlCompatible);
197
    }
198

    
199
    @Override
200
    public boolean isSQLCompatible() {
201
        return this.getInterpreter().isSQLCompatible();
202
    }
203

    
204
    private Interpreter getInterpreter() {
205
        if (this.interpreter == null) {
206
            ExpressionEvaluatorManager manager = ExpressionEvaluatorLocator.getManager();
207
            this.interpreter = manager.createInterpreter();
208
        }
209
        return this.interpreter;
210
    }
211
    
212
    @Override
213
    public Object execute(SymbolTable symbolTable) {
214
        if (this.interpreter == null) {
215
            ExpressionEvaluatorManager manager = ExpressionEvaluatorLocator.getManager();
216
            this.interpreter = manager.createInterpreter();
217
        }
218
        boolean added = this.getSymbolTable().addSymbolTable(symbolTable);
219
        try {
220
            this.interpreter.setSymbolTable(this.mySymbolTable);
221
            if( this.hasNotBeenOptimized  ) {
222
                Optimizer optimizer = new DefaultOptimizer(symbolTable);
223
                this.code = optimizer.optimize(this.getCode());
224
                this.hasNotBeenOptimized = false;
225
            }
226
            Object x = this.interpreter.run(this.getCode());
227

    
228
            return x;
229
        } finally {
230
            if( added ) {
231
                this.getSymbolTable().removeSymbolTable(symbolTable);
232
            }
233
        }
234
    }
235

    
236
    @Override
237
    public void link(SymbolTable symbolTable) {
238
        if (this.interpreter == null) {
239
            ExpressionEvaluatorManager manager = ExpressionEvaluatorLocator.getManager();
240
            this.interpreter = manager.createInterpreter();
241
        }
242
        this.interpreter.setSymbolTable(symbolTable);
243
        if( this.hasNotBeenOptimized  ) {
244
            Optimizer optimizer = new DefaultOptimizer(symbolTable);
245
            this.code = optimizer.optimize(this.getCode());
246
            this.hasNotBeenOptimized = false;
247
        }
248
        this.interpreter.link(this.getCode());
249
    }
250

    
251
    @Override
252
    public void saveToState(PersistentState state) throws PersistenceException {
253
        state.set("phrase", this.phrase);
254
//        if (this.userScript == null) {
255
//            state.setNull("userScript_code");
256
//            state.setNull("userScript_language");
257
//        } else {
258
//            state.set("userScript_code", this.userScript.getCode());
259
//            state.set("userScript_language", this.userScript.getTypeName());
260
//        }
261
//        if (this.scripts != null && !this.scripts.isEmpty()) {
262
//            List<URL> l = new ArrayList<>();
263
//            for (Script script : this.scripts) {
264
//                URL location = script.getURL();
265
//                if (location != null) {
266
//                    l.add(location);
267
//                }
268
//            }
269
//            if (l.isEmpty()) {
270
//                state.setNull("scripts");
271
//            } else {
272
//                state.set("scripts", l);
273
//            }
274
//        } else {
275
//            state.setNull("scripts");
276
//        }
277
    }
278

    
279
    @Override
280
    public void loadFromState(PersistentState state) throws PersistenceException {
281
        ScriptManager scriptManager = ToolsLocator.getScriptManager();
282

    
283
        this.clear();
284
        
285
        this.phrase = state.getString("phrase");
286
//        String userScript_code = state.getString("userScript_code");
287
//        String userScript_language = state.getString("userScript_language");
288
//        if (StringUtils.isEmpty(userScript_code)) {
289
//            this.userScript = null;
290
//        } else {
291
//            if (StringUtils.isEmpty(userScript_language)) {
292
//                userScript_language = "python";
293
//            }
294
//            this.userScript = scriptManager.createScript("user", userScript_code, userScript_language);
295
//        }
296
//        Iterator scriptsLocations = state.getIterator("scripts");
297
//        if (scriptsLocations != null) {
298
//            while (scriptsLocations.hasNext()) {
299
//                URI location = (URI) scriptsLocations.next();
300
//                Script script = scriptManager.loadScript(location);
301
//                if (script != null) {
302
//                    if (this.scripts == null) {
303
//                        this.scripts = new ArrayList<>();
304
//                    }
305
//                    this.scripts.add(script);
306
//                }
307
//            }
308
//        }
309
    }
310

    
311
    public static void registerPersistence() {
312
        PersistenceManager manager = ToolsLocator.getPersistenceManager();
313
        if (manager.getDefinition("Expression") == null) {
314
            DynStruct definition = manager.addDefinition(DefaultExpression.class,
315
                    "Expression", "Expression persistence definition", null, null);
316
            definition.addDynFieldString("phrase").setMandatory(false);
317
//            definition.addDynFieldString("userScript_code").setMandatory(false);
318
//            definition.addDynFieldString("userScript_language").setMandatory(false);
319
//            definition.addDynFieldList("scripts")
320
//                    .setClassOfItems(URL.class)
321
//                    .setMandatory(false);
322
        }
323
    }
324

    
325
    @Override
326
    public String toJSON() {
327
        JSONObject expressionJson = new JSONObject();
328
        expressionJson.put("phrase", this.phrase);
329

    
330
//        if (this.userScript != null) {
331
//            JSONObject userScriptJson = new JSONObject();
332
//            userScriptJson.put("code", this.userScript.getCode());
333
//            userScriptJson.put("language", this.userScript.getTypeName());
334
//            expressionJson.put("userScript", userScriptJson);
335
//        }
336
//
337
//        if (this.scripts != null && !this.scripts.isEmpty()) {
338
//            JSONArray scriptsJson = new JSONArray();
339
//            for (Script script : this.scripts) {
340
//                scriptsJson.put(script.getURL());
341
//            }
342
//            expressionJson.put("scripts", scriptsJson);
343
//        }
344
        return expressionJson.toString();
345
    }
346

    
347
    @Override
348
    public void fromJSON(String json) {
349
        this.clear();
350
        ScriptManager scriptMananger = ToolsLocator.getScriptManager();
351

    
352
        JSONObject expressionJson = new JSONObject(json);
353
        if (expressionJson.has("phrase")) {
354
            this.phrase = expressionJson.getString("phrase");
355
        }
356
//        if (expressionJson.has("userScript")) {
357
//            String theCode = "";
358
//            String theLanguage = "python";
359
//            JSONObject userScriptJson = expressionJson.getJSONObject("userScript");
360
//            if (userScriptJson.has("code")) {
361
//                theCode = userScriptJson.getString("code");
362
//            }
363
//            if (userScriptJson.has("language")) {
364
//                theCode = userScriptJson.getString("language");
365
//            }
366
//            this.userScript = scriptMananger.createScript("user", theCode, theLanguage);
367
//        }
368
//        if (expressionJson.has("scripts")) {
369
//            this.scripts = new ArrayList<>();
370
//            JSONArray scriptsJson = expressionJson.getJSONArray("scripts");
371
//            for (Object object : scriptsJson) {
372
//                URI location = (URI) object;
373
//                Script script = scriptMananger.loadScript(location);
374
//                this.scripts.add(script);
375
//            }
376
//        }
377
    }
378

    
379
    @Override
380
    public String toString() {
381
        return this.toJSON();
382
    }
383

    
384
    @Override
385
    public Expression clone() throws CloneNotSupportedException {
386
        Expression other = (Expression) super.clone();
387
        other.fromJSON(this.toJSON());
388
        return other;
389
    }
390

    
391
    @Override
392
    public void setUseBracketsForIdentifiers(boolean useBracketsForIdentifiers) {
393
        this.useBracketsForIdentifiers = useBracketsForIdentifiers;
394
    }
395
    
396
    @Override
397
    public boolean getUseBracketsForIdentifiers() {
398
        return this.useBracketsForIdentifiers;
399
    }
400
    
401
}