Revision 44622

View differences:

trunk/org.gvsig.desktop/org.gvsig.desktop.library/org.gvsig.expressionevaluator/org.gvsig.expressionevaluator.lib/org.gvsig.expressionevaluator.lib.impl/src/test/java/org/gvsig/expresionevaluator/impl/TestExpression.java
110 110
    public void testB() {
111 111

  
112 112
        Expression expression = createExpression();
113
        expression.setPhrase("MYVALUE()");
113
        expression.setPhrase("MYVALUE()*10");
114 114

  
115 115
        MyValueSymbolTable t1 = new MyValueSymbolTable("MyValueSymbolTable1");
116 116
        t1.setValue(2);
117 117
        Object v1 = expression.execute(t1);
118
        assertEquals(2, (int) v1);
118
        assertEquals(20, (int) v1);
119 119

  
120 120
        t1.setValue(3);
121 121
        v1 = expression.execute(t1);
122
        assertEquals(3, (int) v1);
122
        assertEquals(30, (int) v1);
123 123

  
124 124
        MyValueSymbolTable t2 = new MyValueSymbolTable("MyValueSymbolTable2");
125
        expression.link(t2);
126 125
        t2.setValue(4);
127 126
        v1 = expression.execute(t2);
128
        assertEquals(4, (int) v1);
127
        assertEquals(40, (int) v1);
129 128

  
130 129
    }
131 130

  
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/DefaultOptimizer.java
168 168
        switch (code.code()) {
169 169
            case Code.CALLER:
170 170
                Caller caller = (Caller) code;
171
                if (caller.function() == null) {
171
                Function function = caller.function();
172
                if ( function == null) {
172 173
                    // resolve and link function
173
                    caller.function(this.getSymbolTable().function(caller.name()));
174
                    function = this.getSymbolTable().function(caller.name());
175
                    caller.function(function);
174 176
                }
175
                if (caller.function() instanceof FunctionOptimizer) {
177
                if (function instanceof FunctionOptimizer) {
176 178
                    return ((FunctionOptimizer) caller.function()).optimize(this, caller);
177 179
                } else {
178 180
                    switch (caller.type()) {
179 181
                        case Caller.BINARY_OPERATOR: {
180 182
                            Code op1 = this.doOptimize(caller.parameters().get(0));
181 183
                            Code op2 = this.doOptimize(caller.parameters().get(1));
182
                            if (caller.function().allowConstantFolding()) {
184
                            if ( function.allowConstantFolding()) {
183 185
                                if (op1.code() == Code.CONSTANT && op2.code() == Code.CONSTANT) {
184 186
                                    Object value = this.getInterpreter().run(code);
185 187
                                    Code newCode = this.getCodeBuilder().constant(value);
......
192 194

  
193 195
                        case Code.Caller.UNARY_OPERATOR: {
194 196
                            Code op1 = this.doOptimize(caller.parameters().get(0));
195
                            if (caller.function().allowConstantFolding()
197
                            if (function.allowConstantFolding()
196 198
                                    && op1.code() == Code.CONSTANT) {
197 199
                                Object value = this.getInterpreter().run(code);
198 200
                                Code newCode = this.getCodeBuilder().constant(value);
......
215 217
                                    }
216 218
                                }
217 219
                            }
218
                            if (canOptimize && caller.function()!=null && caller.function().allowConstantFolding()) {
220
                            if (canOptimize && function!=null && function.allowConstantFolding()) {
219 221
                                Object value = this.getInterpreter().run(code);
220 222
                                Code newCode = this.getCodeBuilder().constant(value);
221 223
                                return newCode;
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
1 1
package org.gvsig.expressionevaluator.impl;
2 2

  
3
import java.lang.ref.Reference;
4
import java.lang.ref.WeakReference;
3 5
import java.util.Objects;
4 6
import org.apache.commons.lang3.StringUtils;
5 7
import org.gvsig.expressionevaluator.Code;
......
35 37
    private Interpreter interpreter;
36 38
    private boolean hasNotBeenOptimized = true;
37 39
    private SymbolTable mySymbolTable = null;
40
    private WeakReference<SymbolTable> lastSymbolTable = null;    
38 41
    private boolean useBracketsForIdentifiers = false;
39 42

  
40 43
    public DefaultExpression() {
......
216 219
            this.interpreter = manager.createInterpreter();
217 220
        }
218 221
        boolean added = this.getSymbolTable().addSymbolTable(symbolTable);
222
        if( this.lastSymbolTable==null ) {
223
            this.lastSymbolTable = new WeakReference<>(symbolTable);            
224
        } else if( this.lastSymbolTable.get()!=symbolTable ) {
225
            this.link(this.getSymbolTable());
226
            this.lastSymbolTable = new WeakReference<>(symbolTable);
227
            this.hasNotBeenOptimized = true;            
228
        }
219 229
        try {
220 230
            this.interpreter.setSymbolTable(this.mySymbolTable);
221 231
            if( this.hasNotBeenOptimized  ) {
222
                Optimizer optimizer = new DefaultOptimizer(symbolTable);
223
                this.code = optimizer.optimize(this.getCode());
232
                try {
233
                    Optimizer optimizer = new DefaultOptimizer(this.getSymbolTable());
234
                    this.code = optimizer.optimize(this.getCode());
235
                } catch(Throwable th) {
236
                    // Si no es capaz de optimizar la expresion no, peta y la
237
                    // ejecuta tal cual.
238
                }
224 239
                this.hasNotBeenOptimized = false;
225 240
            }
226 241
            Object x = this.interpreter.run(this.getCode());

Also available in: Unified diff