Revision 44644 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

View differences:

DefaultExpression.java
1 1
package org.gvsig.expressionevaluator.impl;
2 2

  
3
import java.lang.ref.Reference;
4 3
import java.lang.ref.WeakReference;
5 4
import java.util.Objects;
6 5
import org.apache.commons.lang3.StringUtils;
7 6
import org.gvsig.expressionevaluator.Code;
8 7
import org.gvsig.expressionevaluator.Compiler;
9 8
import org.gvsig.expressionevaluator.Expression;
10
import org.gvsig.expressionevaluator.ExpressionEvaluatorLocator;
11 9
import org.gvsig.expressionevaluator.ExpressionEvaluatorManager;
12 10
import org.gvsig.expressionevaluator.ExpressionUtils;
13 11
import org.gvsig.expressionevaluator.Interpreter;
......
29 27
public class DefaultExpression implements Expression, LabeledValue<Expression> {
30 28

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

  
36 31
    private Code code = null;
37 32
    private Interpreter interpreter;
......
39 34
    private SymbolTable mySymbolTable = null;
40 35
    private WeakReference<SymbolTable> lastSymbolTable = null;    
41 36
    private boolean useBracketsForIdentifiers = false;
37
    protected ExpressionEvaluatorManager manager;
42 38

  
43
    public DefaultExpression() {
44

  
39
    public DefaultExpression(ExpressionEvaluatorManager manager) {
40
        this.manager = manager;
45 41
    }
46 42

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

  
108
//    @Override
109
//    public Script getUserScript() {
110
//        return this.userScript;
111
//    }
112
//
113
//    @Override
114
//    public UnmodifiableBasicList<Script> getScripts() {
115
//        if (this.unmodifiableScripts == null) {
116
//            if (this.scripts == null) {
117
//                return null;
118
//            }
119
//            this.unmodifiableScripts = new UnmodifiableBasicListAdapter<>(this.scripts);
120
//        }
121
//        return this.unmodifiableScripts;
122
//    }
123
//
124 98
    @Override
125 99
    public Expression setPhrase(String phrase) {
126 100
        this.phrase = phrase;
......
129 103
        return this;
130 104
    }
131 105

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

  
173 106
    @Override
174 107
    public void clear() {
175 108
        this.phrase = null;
176
//        this.userScript = null;
177
//        this.unmodifiableScripts = null;
178
//        this.scripts = null;
179 109
        this.code = null;
180 110
        this.interpreter = null;
181 111
        this.hasNotBeenOptimized = true;
......
184 114
    @Override
185 115
    public Code getCode() {
186 116
        if (this.code == null) {
187
            ExpressionEvaluatorManager manager = ExpressionEvaluatorLocator.getManager();
188
            Compiler compiler = manager.createCompiler();
117
            Compiler compiler = this.manager.createCompiler();
189 118
            compiler.getLexicalAnalyzer().setUseBracketsForIdentifiers(
190 119
                    this.useBracketsForIdentifiers
191 120
            );
......
206 135

  
207 136
    private Interpreter getInterpreter() {
208 137
        if (this.interpreter == null) {
209
            ExpressionEvaluatorManager manager = ExpressionEvaluatorLocator.getManager();
210
            this.interpreter = manager.createInterpreter();
138
            this.interpreter = this.manager.createInterpreter();
211 139
        }
212 140
        return this.interpreter;
213 141
    }
......
215 143
    @Override
216 144
    public Object execute(SymbolTable symbolTable) {
217 145
        if (this.interpreter == null) {
218
            ExpressionEvaluatorManager manager = ExpressionEvaluatorLocator.getManager();
219
            this.interpreter = manager.createInterpreter();
146
            this.interpreter = this.manager.createInterpreter();
220 147
        }
221 148
        boolean added = this.getSymbolTable().addSymbolTable(symbolTable);
222 149
        if( this.lastSymbolTable==null ) {
......
229 156
            this.interpreter.setSymbolTable(this.mySymbolTable);
230 157
            if( this.hasNotBeenOptimized  ) {
231 158
                try {
232
                    Optimizer optimizer = new DefaultOptimizer(this.getSymbolTable());
159
                    Optimizer optimizer = this.manager.createOptimizer();
160
                    optimizer.setSymbolTable(this.getSymbolTable());
233 161
                    this.code = optimizer.optimize(this.getCode());
234 162
                } catch(Throwable th) {
235 163
                    // Si no es capaz de optimizar la expresion no, peta y la
......
250 178
    @Override
251 179
    public void link(SymbolTable symbolTable) {
252 180
        if (this.interpreter == null) {
253
            ExpressionEvaluatorManager manager = ExpressionEvaluatorLocator.getManager();
254
            this.interpreter = manager.createInterpreter();
181
            this.interpreter = this.manager.createInterpreter();
255 182
        }
256 183
        this.lastSymbolTable = new WeakReference<>(symbolTable);            
257 184
        this.interpreter.setSymbolTable(symbolTable);
258 185
        if( this.hasNotBeenOptimized  ) {
259
            Optimizer optimizer = new DefaultOptimizer(symbolTable);
186
            Optimizer optimizer = new DefaultOptimizer(this.manager, symbolTable);
260 187
            this.code = optimizer.optimize(this.getCode());
261 188
            this.hasNotBeenOptimized = false;
262 189
        }

Also available in: Unified diff