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

History | View | Annotate | Download (10.1 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( !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 Object execute(SymbolTable symbolTable) {
157
        if (this.interpreter == null) {
158
            ExpressionEvaluatorManager manager = ExpressionEvaluatorLocator.getManager();
159
            this.interpreter = manager.createInterpreter();
160
        }
161
        this.interpreter.setSymbolTable(symbolTable);
162
        if( this.hasNotBeenOptimized  ) {
163
            Optimizer optimizer = new DefaultOptimizer(symbolTable);
164
            this.code = optimizer.optimize(this.getCode());
165
            this.hasNotBeenOptimized = false;
166
        }
167
        Object x = this.interpreter.run(this.getCode());
168
        return x;
169
    }
170

    
171
    @Override
172
    public void saveToState(PersistentState state) throws PersistenceException {
173
        state.set("phrase", this.phrase);
174
        if (this.userScript == null) {
175
            state.setNull("userScript_code");
176
            state.setNull("userScript_language");
177
        } else {
178
            state.set("userScript_code", this.userScript.getCode());
179
            state.set("userScript_language", this.userScript.getTypeName());
180
        }
181
        if (this.scripts != null && !this.scripts.isEmpty()) {
182
            List<URI> l = new ArrayList<>();
183
            for (Script script : this.scripts) {
184
                URI location = script.getURI();
185
                if (location != null) {
186
                    l.add(location);
187
                }
188
            }
189
            if (l.isEmpty()) {
190
                state.setNull("scripts");
191
            } else {
192
                state.set("scripts", l);
193
            }
194
        } else {
195
            state.setNull("scripts");
196
        }
197
    }
198

    
199
    @Override
200
    public void loadFromState(PersistentState state) throws PersistenceException {
201
        ScriptManager scriptManager = ToolsLocator.getScriptManager();
202

    
203
        this.clear();
204
        
205
        this.phrase = state.getString("phrase");
206
        String userScript_code = state.getString("userScript_code");
207
        String userScript_language = state.getString("userScript_language");
208
        if (StringUtils.isEmpty(userScript_code)) {
209
            this.userScript = null;
210
        } else {
211
            if (StringUtils.isEmpty(userScript_language)) {
212
                userScript_language = "python";
213
            }
214
            this.userScript = scriptManager.createScript("user", userScript_code, userScript_language);
215
        }
216
        Iterator scriptsLocations = state.getIterator("scripts");
217
        if (scriptsLocations != null) {
218
            while (scriptsLocations.hasNext()) {
219
                URI location = (URI) scriptsLocations.next();
220
                Script script = scriptManager.loadScript(location);
221
                if (script != null) {
222
                    if (this.scripts == null) {
223
                        this.scripts = new ArrayList<>();
224
                    }
225
                    this.scripts.add(script);
226
                }
227
            }
228
        }
229
    }
230

    
231
    public static void registerPersistence() {
232
        PersistenceManager manager = ToolsLocator.getPersistenceManager();
233
        if (manager.getDefinition("Expression") == null) {
234
            DynStruct definition = manager.addDefinition(DefaultExpression.class,
235
                    "Expression", "Expression persistence definition", null, null);
236
            definition.addDynFieldString("phrase").setMandatory(false);
237
            definition.addDynFieldString("userScript_code").setMandatory(false);
238
            definition.addDynFieldString("userScript_language").setMandatory(false);
239
            definition.addDynFieldList("scripts").setMandatory(false);
240
        }
241
    }
242

    
243
    @Override
244
    public String toJSON() {
245
        JSONObject expressionJson = new JSONObject();
246
        expressionJson.put("phrase", this.phrase);
247

    
248
        if (this.userScript != null) {
249
            JSONObject userScriptJson = new JSONObject();
250
            userScriptJson.put("code", this.userScript.getCode());
251
            userScriptJson.put("language", this.userScript.getTypeName());
252
            expressionJson.put("userScript", userScriptJson);
253
        }
254

    
255
        if (this.scripts != null && !this.scripts.isEmpty()) {
256
            JSONArray scriptsJson = new JSONArray();
257
            for (Script script : this.scripts) {
258
                scriptsJson.put(script.getURI());
259
            }
260
            expressionJson.put("scripts", scriptsJson);
261
        }
262
        return expressionJson.toString();
263
    }
264

    
265
    @Override
266
    public void fromJSON(String json) {
267
        this.clear();
268
        ScriptManager scriptMananger = ToolsLocator.getScriptManager();
269

    
270
        JSONObject expressionJson = new JSONObject(json);
271
        if (expressionJson.has("phrase")) {
272
            this.phrase = expressionJson.getString("phrase");
273
        }
274
        if (expressionJson.has("userScript")) {
275
            String theCode = "";
276
            String theLanguage = "python";
277
            JSONObject userScriptJson = expressionJson.getJSONObject("userScript");
278
            if (userScriptJson.has("code")) {
279
                theCode = userScriptJson.getString("code");
280
            }
281
            if (userScriptJson.has("language")) {
282
                theCode = userScriptJson.getString("language");
283
            }
284
            this.userScript = scriptMananger.createScript("user", theCode, theLanguage);
285
        }
286
        if (expressionJson.has("scripts")) {
287
            this.scripts = new ArrayList<>();
288
            JSONArray scriptsJson = expressionJson.getJSONArray("scripts");
289
            for (Object object : scriptsJson) {
290
                URI location = (URI) object;
291
                Script script = scriptMananger.loadScript(location);
292
                this.scripts.add(script);
293
            }
294
        }
295
    }
296

    
297
    @Override
298
    public String toString() {
299
        return this.toJSON();
300
    }
301

    
302
    @Override
303
    public Expression clone() throws CloneNotSupportedException {
304
        Expression other = (Expression) super.clone();
305
        other.fromJSON(this.toJSON());
306
        return other;
307
    }
308

    
309
}