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

History | View | Annotate | Download (9.53 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 Script getUserScript() {
53
        return this.userScript;
54
    }
55

    
56
    @Override
57
    public UnmodifiableBasicList<Script> getScripts() {
58
        if (this.unmodifiableScripts == null) {
59
            if (this.scripts == null) {
60
                return null;
61
            }
62
            this.unmodifiableScripts = new UnmodifiableBasicListAdapter<>(this.scripts);
63
        }
64
        return this.unmodifiableScripts;
65
    }
66

    
67
    @Override
68
    public void setPhrase(String phrase) {
69
        this.phrase = phrase;
70
        this.code = null;
71
        this.hasNotBeenOptimized = true;
72
    }
73

    
74
    @Override
75
    public void setUserScript(String code, String languaje) {
76
        if (this.userScript == null) {
77
            ScriptManager scriptMananger = ToolsLocator.getScriptManager();
78
            this.userScript = scriptMananger.createScript("user", code, languaje);
79
        } else if (this.userScript.getTypeName().equalsIgnoreCase(languaje)) {
80
            this.userScript.setCode(code);
81
        } else {
82
            ScriptManager scriptMananger = ToolsLocator.getScriptManager();
83
            this.userScript = scriptMananger.createScript("user", code, languaje);
84
        }
85
    }
86

    
87
    @Override
88
    public void setUserScript(Script script) {
89
        this.userScript = script;
90
    }
91

    
92
    @Override
93
    public void setUserScript(String code) {
94
        this.setUserScript(code, "python");
95
    }
96

    
97
    @Override
98
    public void removeAllScripts() {
99
        this.scripts = null;
100
        this.unmodifiableScripts = null;
101
    }
102

    
103
    @Override
104
    public void addScript(Script script) {
105
        if (this.scripts == null) {
106
            this.scripts = new ArrayList<>();
107
        }
108
        this.scripts.add(script);
109
    }
110

    
111
    @Override
112
    public void clear() {
113
        this.phrase = null;
114
        this.userScript = null;
115
        this.unmodifiableScripts = null;
116
        this.scripts = null;
117
        this.code = null;
118
        this.interpreter = null;
119
        this.hasNotBeenOptimized = true;
120
    }
121

    
122
    @Override
123
    public Code getCode() {
124
        if (this.code == null) {
125
            ExpressionEvaluatorManager manager = ExpressionEvaluatorLocator.getManager();
126
            this.code = manager.compile(this.phrase);
127
        }
128
        return code;
129
    }
130

    
131
    @Override
132
    public Object execute(SymbolTable symbolTable) {
133
        if (this.interpreter == null) {
134
            ExpressionEvaluatorManager manager = ExpressionEvaluatorLocator.getManager();
135
            this.interpreter = manager.createInterpreter();
136
        }
137
        this.interpreter.setSymbolTable(symbolTable);
138
        if( this.hasNotBeenOptimized  ) {
139
            Optimizer optimizer = new DefaultOptimizer(symbolTable);
140
            this.code = optimizer.optimize(this.getCode());
141
            this.hasNotBeenOptimized = false;
142
        }
143
        Object x = this.interpreter.run(this.getCode());
144
        return x;
145
    }
146

    
147
    @Override
148
    public void saveToState(PersistentState state) throws PersistenceException {
149
        state.set("phrase", this.phrase);
150
        if (this.userScript == null) {
151
            state.setNull("userScript_code");
152
            state.setNull("userScript_language");
153
        } else {
154
            state.set("userScript_code", this.userScript.getCode());
155
            state.set("userScript_language", this.userScript.getTypeName());
156
        }
157
        if (this.scripts != null && !this.scripts.isEmpty()) {
158
            List<URI> l = new ArrayList<>();
159
            for (Script script : this.scripts) {
160
                URI location = script.getURI();
161
                if (location != null) {
162
                    l.add(location);
163
                }
164
            }
165
            if (l.isEmpty()) {
166
                state.setNull("scripts");
167
            } else {
168
                state.set("scripts", l);
169
            }
170
        } else {
171
            state.setNull("scripts");
172
        }
173
    }
174

    
175
    @Override
176
    public void loadFromState(PersistentState state) throws PersistenceException {
177
        ScriptManager scriptManager = ToolsLocator.getScriptManager();
178

    
179
        this.clear();
180
        
181
        this.phrase = state.getString("phrase");
182
        String userScript_code = state.getString("userScript_code");
183
        String userScript_language = state.getString("userScript_language");
184
        if (StringUtils.isEmpty(userScript_code)) {
185
            this.userScript = null;
186
        } else {
187
            if (StringUtils.isEmpty(userScript_language)) {
188
                userScript_language = "python";
189
            }
190
            this.userScript = scriptManager.createScript("user", userScript_code, userScript_language);
191
        }
192
        Iterator scriptsLocations = state.getIterator("scripts");
193
        if (scriptsLocations != null) {
194
            while (scriptsLocations.hasNext()) {
195
                URI location = (URI) scriptsLocations.next();
196
                Script script = scriptManager.loadScript(location);
197
                if (script != null) {
198
                    if (this.scripts == null) {
199
                        this.scripts = new ArrayList<>();
200
                    }
201
                    this.scripts.add(script);
202
                }
203
            }
204
        }
205
    }
206

    
207
    public static void registerPersistence() {
208
        PersistenceManager manager = ToolsLocator.getPersistenceManager();
209
        if (manager.getDefinition("Expression") == null) {
210
            DynStruct definition = manager.addDefinition(DefaultExpression.class,
211
                    "Expression", "Expression persistence definition", null, null);
212
            definition.addDynFieldString("phrase").setMandatory(false);
213
            definition.addDynFieldString("userScript_code").setMandatory(false);
214
            definition.addDynFieldString("userScript_language").setMandatory(false);
215
            definition.addDynFieldList("scripts").setMandatory(false);
216
        }
217
    }
218

    
219
    @Override
220
    public String toJSON() {
221
        JSONObject expressionJson = new JSONObject();
222
        expressionJson.put("phrase", this.phrase);
223

    
224
        if (this.userScript != null) {
225
            JSONObject userScriptJson = new JSONObject();
226
            userScriptJson.put("code", this.userScript.getCode());
227
            userScriptJson.put("language", this.userScript.getTypeName());
228
            expressionJson.put("userScript", userScriptJson);
229
        }
230

    
231
        if (this.scripts != null && !this.scripts.isEmpty()) {
232
            JSONArray scriptsJson = new JSONArray();
233
            for (Script script : this.scripts) {
234
                scriptsJson.put(script.getURI());
235
            }
236
            expressionJson.put("scripts", scriptsJson);
237
        }
238
        return expressionJson.toString();
239
    }
240

    
241
    @Override
242
    public void fromJSON(String json) {
243
        this.clear();
244
        ScriptManager scriptMananger = ToolsLocator.getScriptManager();
245

    
246
        JSONObject expressionJson = new JSONObject(json);
247
        if (expressionJson.has("phrase")) {
248
            this.phrase = expressionJson.getString("phrase");
249
        }
250
        if (expressionJson.has("userScript")) {
251
            String theCode = "";
252
            String theLanguage = "python";
253
            JSONObject userScriptJson = expressionJson.getJSONObject("userScript");
254
            if (userScriptJson.has("code")) {
255
                theCode = userScriptJson.getString("code");
256
            }
257
            if (userScriptJson.has("language")) {
258
                theCode = userScriptJson.getString("language");
259
            }
260
            this.userScript = scriptMananger.createScript("user", theCode, theLanguage);
261
        }
262
        if (expressionJson.has("scripts")) {
263
            this.scripts = new ArrayList<>();
264
            JSONArray scriptsJson = expressionJson.getJSONArray("scripts");
265
            for (Object object : scriptsJson) {
266
                URI location = (URI) object;
267
                Script script = scriptMananger.loadScript(location);
268
                this.scripts.add(script);
269
            }
270
        }
271
    }
272

    
273
    @Override
274
    public String toString() {
275
        return this.toJSON();
276
    }
277

    
278
    @Override
279
    public Expression clone() throws CloneNotSupportedException {
280
        Expression other = (Expression) super.clone();
281
        other.fromJSON(this.toJSON());
282
        return other;
283
    }
284

    
285
}