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 / test / java / org / gvsig / expresionevaluator / impl / TestOptimizer.java @ 44211

History | View | Annotate | Download (17.5 KB)

1 43512 jjdelcerro
package org.gvsig.expresionevaluator.impl;
2
3 44009 jjdelcerro
import static junit.framework.Assert.assertEquals;
4 43512 jjdelcerro
import org.gvsig.expressionevaluator.impl.SQLLexicalAnalyzer;
5
import org.gvsig.expressionevaluator.impl.DefaultCompiler;
6
import junit.framework.TestCase;
7 44009 jjdelcerro
import org.cresques.cts.IProjection;
8 43512 jjdelcerro
import org.gvsig.expressionevaluator.LexicalAnalyzer;
9
import org.gvsig.expressionevaluator.Compiler;
10
import org.gvsig.expressionevaluator.Code;
11 44009 jjdelcerro
import org.gvsig.expressionevaluator.ExpressionBuilder;
12
import org.gvsig.expressionevaluator.Interpreter;
13
import org.gvsig.expressionevaluator.MutableSymbolTable;
14
import org.gvsig.expressionevaluator.Optimizer;
15
import org.gvsig.expressionevaluator.SymbolTable;
16
import org.gvsig.expressionevaluator.impl.DefaultExpressionBuilder;
17
import org.gvsig.expressionevaluator.impl.DefaultInterpreter;
18
import org.gvsig.expressionevaluator.impl.DefaultOptimizer;
19
import org.gvsig.expressionevaluator.impl.DefaultSymbolTable;
20
import org.gvsig.fmap.crs.CRSFactory;
21
import org.gvsig.fmap.geom.Geometry;
22
import org.gvsig.fmap.geom.GeometryLocator;
23
import org.gvsig.fmap.geom.GeometryManager;
24
import org.gvsig.fmap.geom.exception.CreateGeometryException;
25
import org.gvsig.fmap.geom.primitive.Point;
26
import org.gvsig.fmap.geom.primitive.Polygon;
27
import org.gvsig.tools.library.impl.DefaultLibrariesInitializer;
28 43512 jjdelcerro
29 44009 jjdelcerro
public class TestOptimizer extends TestCase {
30 43512 jjdelcerro
31 44009 jjdelcerro
    public TestOptimizer(String testName) {
32 43512 jjdelcerro
        super(testName);
33
    }
34 44009 jjdelcerro
35 43512 jjdelcerro
    @Override
36
    protected void setUp() throws Exception {
37
        super.setUp();
38 44009 jjdelcerro
        new DefaultLibrariesInitializer().fullInitialize();
39 43512 jjdelcerro
    }
40 44009 jjdelcerro
41 43512 jjdelcerro
    @Override
42
    protected void tearDown() throws Exception {
43
        super.tearDown();
44
    }
45
46
    // TODO add test methods here. The name must begin with 'test'. For example:
47
    // public void testHello() {}
48
    protected LexicalAnalyzer createLexicalAnalyzer() {
49
        SQLLexicalAnalyzer lexer = new SQLLexicalAnalyzer();
50
        return lexer;
51
    }
52 44009 jjdelcerro
53 43512 jjdelcerro
    protected Compiler createCompiler() {
54
        Compiler compiler = new DefaultCompiler();
55
        compiler.setLexicalAnalyzer(createLexicalAnalyzer());
56
        return compiler;
57
    }
58 44009 jjdelcerro
59
    protected Interpreter createInterpreter(SymbolTable symbolTable) {
60
        Interpreter interpreter = new DefaultInterpreter();
61
        interpreter.setSymbolTable(symbolTable);
62
        return interpreter;
63
    }
64
65
    protected Optimizer createOptimizer(SymbolTable symbolTable) {
66
        Optimizer optimizer = new DefaultOptimizer(symbolTable);
67
        return optimizer;
68
    }
69
70
    protected MutableSymbolTable createSymbolTable() {
71
        DefaultSymbolTable symbolTable = new DefaultSymbolTable();
72
        symbolTable.setVar("precio", 200);
73
        symbolTable.setVar("1990", 0.168873933773767);
74
        return symbolTable;
75
    }
76
77
    private String createExpr1() {
78
        ExpressionBuilder builder = new DefaultExpressionBuilder();
79
80
        ExpressionBuilder.Value expr1 = builder.gt(
81
                builder.column("campo1"),
82
                builder.constant(10)
83
        );
84
        System.out.println(expr1.toString());
85
        assertEquals(
86
                "( (\"campo1\") > (10) )",
87
                expr1.toString()
88
        );
89
        return expr1.toString();
90
    }
91
92
    private Polygon createPolygon() throws CreateGeometryException {
93
        GeometryManager geometryManager = GeometryLocator.getGeometryManager();
94
        IProjection proj = CRSFactory.getCRS("EPSG:4326");
95
96
        Polygon polygon = geometryManager.createPolygon(Geometry.SUBTYPES.GEOM2D);
97
        polygon.addVertex(0, 0);
98
        polygon.addVertex(0, 100);
99
        polygon.addVertex(100, 100);
100
        polygon.addVertex(100, 0);
101
        polygon.addVertex(0, 0);
102
103
        polygon.setProjection(proj);
104
        return polygon;
105
    }
106
107
    private Geometry createPoint(double x, double y) throws CreateGeometryException {
108
        GeometryManager geometryManager = GeometryLocator.getGeometryManager();
109
        IProjection proj = CRSFactory.getCRS("EPSG:4326");
110
        Point point = geometryManager.createPoint(x, y, Geometry.SUBTYPES.GEOM2D);
111
        point.setProjection(proj);
112
        return point;
113
    }
114
115 43512 jjdelcerro
    public void testIdentifier1() {
116
        String source = "precio";
117
118
        Compiler compiler = createCompiler();
119
        Code code = compiler.compileExpression(source);
120 44006 jjdelcerro
        assertEquals("\"precio\"", code.toString());
121 44009 jjdelcerro
122
        SymbolTable symbolTable = createSymbolTable();
123
        Optimizer optimizer = createOptimizer(symbolTable);
124
        Code code2 = optimizer.optimize(code);
125
        assertEquals("\"precio\"", code2.toString());
126 43512 jjdelcerro
    }
127
128
    public void testIdentifier2() {
129 44139 jjdelcerro
        Compiler compiler = createCompiler();
130
        compiler.getLexicalAnalyzer().setUseBracketsForIdentifiers(true);
131
132 43512 jjdelcerro
        String source = "[precio]";
133
134
        Code code = compiler.compileExpression(source);
135 44006 jjdelcerro
        assertEquals("\"precio\"", code.toString());
136 44009 jjdelcerro
137
        SymbolTable symbolTable = createSymbolTable();
138
        Optimizer optimizer = createOptimizer(symbolTable);
139
        Code code2 = optimizer.optimize(code);
140
        assertEquals("\"precio\"", code2.toString());
141 43512 jjdelcerro
    }
142
143
    public void testIdentifier3() {
144
        String source = "\"precio\"";
145
146
        Compiler compiler = createCompiler();
147
        Code code = compiler.compileExpression(source);
148 44006 jjdelcerro
        assertEquals("\"precio\"", code.toString());
149 44009 jjdelcerro
150
        SymbolTable symbolTable = createSymbolTable();
151
        Optimizer optimizer = createOptimizer(symbolTable);
152
        Code code2 = optimizer.optimize(code);
153
        assertEquals("\"precio\"", code2.toString());
154 43512 jjdelcerro
    }
155
156
    public void testTrue() {
157
        String source = "true";
158
159
        Compiler compiler = createCompiler();
160
        Code code = compiler.compileExpression(source);
161 44139 jjdelcerro
        assertEquals("TRUE", code.toString());
162 44009 jjdelcerro
163
        SymbolTable symbolTable = createSymbolTable();
164
        Optimizer optimizer = createOptimizer(symbolTable);
165
        Code code2 = optimizer.optimize(code);
166 44139 jjdelcerro
        assertEquals("TRUE", code2.toString());
167 43512 jjdelcerro
    }
168
169
    public void testFalse() {
170
        String source = "false";
171
172
        Compiler compiler = createCompiler();
173
        Code code = compiler.compileExpression(source);
174 44139 jjdelcerro
        assertEquals("FALSE", code.toString());
175 44009 jjdelcerro
176
        SymbolTable symbolTable = createSymbolTable();
177
        Optimizer optimizer = createOptimizer(symbolTable);
178
        Code code2 = optimizer.optimize(code);
179 44139 jjdelcerro
        assertEquals("FALSE", code2.toString());
180 43512 jjdelcerro
    }
181
182
    public void testNull() {
183
        String source = "null";
184
185
        Compiler compiler = createCompiler();
186
        Code code = compiler.compileExpression(source);
187 44139 jjdelcerro
        assertEquals("NULL", code.toString());
188 44009 jjdelcerro
189
        SymbolTable symbolTable = createSymbolTable();
190
        Optimizer optimizer = createOptimizer(symbolTable);
191
        Code code2 = optimizer.optimize(code);
192 44139 jjdelcerro
        assertEquals("NULL", code2.toString());
193 43512 jjdelcerro
    }
194
195
    public void testNotTrue() {
196
        String source = "not true";
197
198
        Compiler compiler = createCompiler();
199
        Code code = compiler.compileExpression(source);
200 44139 jjdelcerro
        assertEquals("NOT(TRUE)", code.toString());
201 44009 jjdelcerro
202
        SymbolTable symbolTable = createSymbolTable();
203
        Optimizer optimizer = createOptimizer(symbolTable);
204
        Code code2 = optimizer.optimize(code);
205 44139 jjdelcerro
        assertEquals("FALSE", code2.toString());
206 43512 jjdelcerro
    }
207
208
    public void testInteger() {
209
        String source = "23";
210
211
        Compiler compiler = createCompiler();
212
        Code code = compiler.compileExpression(source);
213
        assertEquals("23", code.toString());
214 44009 jjdelcerro
215
        SymbolTable symbolTable = createSymbolTable();
216
        Optimizer optimizer = createOptimizer(symbolTable);
217
        Code code2 = optimizer.optimize(code);
218
        assertEquals("23", code2.toString());
219 43512 jjdelcerro
    }
220
221
    public void operator(String operatorName) {
222
        String source = "precio " + operatorName + " 23";
223
224
        Compiler compiler = createCompiler();
225
        Code code = compiler.compileExpression(source);
226 44198 jjdelcerro
        assertEquals( "(\"precio\" "+operatorName+" 23)", code.toString());
227 44009 jjdelcerro
228
        SymbolTable symbolTable = createSymbolTable();
229
        Optimizer optimizer = createOptimizer(symbolTable);
230
        Code code2 = optimizer.optimize(code);
231 44198 jjdelcerro
        assertEquals( "(\"precio\" "+operatorName+" 23)", code2.toString());
232 43512 jjdelcerro
    }
233 44009 jjdelcerro
234 43512 jjdelcerro
    public void testOperators() {
235
236
        operator("=");
237
        operator("<>");
238
        operator(">");
239
        operator(">=");
240
        operator("<");
241
        operator("<=");
242 44139 jjdelcerro
        operator("LIKE");
243
        operator("ILIKE");
244
        operator("||");
245 43512 jjdelcerro
        operator("+");
246
        operator("-");
247
        operator("*");
248
//        operator("^");
249 44139 jjdelcerro
        operator("OR");
250
        operator("AND");
251 43512 jjdelcerro
        operator("%");
252 44139 jjdelcerro
        operator("IS");
253 43512 jjdelcerro
    }
254
255 44010 jjdelcerro
256 43512 jjdelcerro
    public void testAddMul() {
257
        String source = "precio + 10 * 2 + 20 + 30";
258
259
        Compiler compiler = createCompiler();
260 44009 jjdelcerro
261 43512 jjdelcerro
        Code code = compiler.compileExpression(source);
262 44198 jjdelcerro
        assertEquals("(((\"precio\" + (10 * 2)) + 20) + 30)", code.toString());
263 44009 jjdelcerro
264
        SymbolTable symbolTable = createSymbolTable();
265
        Optimizer optimizer = createOptimizer(symbolTable);
266
        Code code2 = optimizer.optimize(code);
267 44198 jjdelcerro
        assertEquals("(((\"precio\" + 20) + 20) + 30)", code2.toString());
268 43512 jjdelcerro
    }
269 44010 jjdelcerro
270
    public void testMul0() {
271
        String source = "precio + 10 * 0 + 20 + 30";
272 44009 jjdelcerro
273 44010 jjdelcerro
        Compiler compiler = createCompiler();
274
275
        Code code = compiler.compileExpression(source);
276 44198 jjdelcerro
        assertEquals("(((\"precio\" + (10 * 0)) + 20) + 30)", code.toString());
277 44010 jjdelcerro
278
        SymbolTable symbolTable = createSymbolTable();
279
        Optimizer optimizer = createOptimizer(symbolTable);
280
        Code code2 = optimizer.optimize(code);
281 44198 jjdelcerro
        assertEquals("((\"precio\" + 20) + 30)", code2.toString());
282 44010 jjdelcerro
    }
283
284
    public void testMul1() {
285
        String source = "precio + 10 * 1 + 20 + 30";
286
287
        Compiler compiler = createCompiler();
288
289
        Code code = compiler.compileExpression(source);
290 44198 jjdelcerro
        assertEquals("(((\"precio\" + (10 * 1)) + 20) + 30)", code.toString());
291 44010 jjdelcerro
292
        SymbolTable symbolTable = createSymbolTable();
293
        Optimizer optimizer = createOptimizer(symbolTable);
294
        Code code2 = optimizer.optimize(code);
295 44198 jjdelcerro
        assertEquals("(((\"precio\" + 10) + 20) + 30)", code2.toString());
296 44010 jjdelcerro
    }
297
298
    public void testDiv1() {
299
        String source = "precio + 10 / 1 + 20 + 30";
300
301
        Compiler compiler = createCompiler();
302
303
        Code code = compiler.compileExpression(source);
304 44198 jjdelcerro
        assertEquals("(((\"precio\" + (10 / 1)) + 20) + 30)", code.toString());
305 44010 jjdelcerro
306
        SymbolTable symbolTable = createSymbolTable();
307
        Optimizer optimizer = createOptimizer(symbolTable);
308
        Code code2 = optimizer.optimize(code);
309 44198 jjdelcerro
        assertEquals("(((\"precio\" + 10) + 20) + 30)", code2.toString());
310 44010 jjdelcerro
    }
311
312
    public void testDiv0() {
313
        String source = "precio + 0 / 10 + 20 + 30";
314
315
        Compiler compiler = createCompiler();
316
317
        Code code = compiler.compileExpression(source);
318 44198 jjdelcerro
        assertEquals("(((\"precio\" + (0 / 10)) + 20) + 30)", code.toString());
319 44010 jjdelcerro
320
        SymbolTable symbolTable = createSymbolTable();
321
        Optimizer optimizer = createOptimizer(symbolTable);
322
        Code code2 = optimizer.optimize(code);
323 44198 jjdelcerro
        assertEquals("((\"precio\" + 20) + 30)", code2.toString());
324 44010 jjdelcerro
    }
325
326 43519 jjdelcerro
    public void testAddMulPar() {
327
        String source = "(precio + 10) * 2 + 20 + 30";
328
329
        Compiler compiler = createCompiler();
330 44009 jjdelcerro
331 43519 jjdelcerro
        Code code = compiler.compileExpression(source);
332 44198 jjdelcerro
        assertEquals("((((\"precio\" + 10) * 2) + 20) + 30)", code.toString());
333 44009 jjdelcerro
334
        SymbolTable symbolTable = createSymbolTable();
335
        Optimizer optimizer = createOptimizer(symbolTable);
336
        Code code2 = optimizer.optimize(code);
337 44198 jjdelcerro
        assertEquals("((((\"precio\" + 10) * 2) + 20) + 30)", code2.toString());
338 43519 jjdelcerro
    }
339 44009 jjdelcerro
340 43519 jjdelcerro
    public void testAbs() {
341
        String source = "precio + abs(10)";
342
343
        Compiler compiler = createCompiler();
344 44009 jjdelcerro
345 43519 jjdelcerro
        Code code = compiler.compileExpression(source);
346 44198 jjdelcerro
        assertEquals("(\"precio\" + abs(10))", code.toString());
347 44009 jjdelcerro
348
        SymbolTable symbolTable = createSymbolTable();
349
        Optimizer optimizer = createOptimizer(symbolTable);
350
        Code code2 = optimizer.optimize(code);
351 44198 jjdelcerro
        assertEquals("(\"precio\" + 10)", code2.toString());
352 43519 jjdelcerro
    }
353 44009 jjdelcerro
354 43519 jjdelcerro
    public void testAbs2() {
355
        String source = "precio + abs(-10)";
356
357
        Compiler compiler = createCompiler();
358 44009 jjdelcerro
359 43519 jjdelcerro
        Code code = compiler.compileExpression(source);
360 44211 jjdelcerro
        assertEquals("(\"precio\" + abs(-10))", code.toString());
361 44009 jjdelcerro
362
        SymbolTable symbolTable = createSymbolTable();
363
        Optimizer optimizer = createOptimizer(symbolTable);
364
        Code code2 = optimizer.optimize(code);
365 44198 jjdelcerro
        assertEquals("(\"precio\" + 10)", code2.toString());
366 43519 jjdelcerro
    }
367 44009 jjdelcerro
368 43519 jjdelcerro
    public void testPI() {
369
        String source = "precio + PI()";
370
371
        Compiler compiler = createCompiler();
372 44009 jjdelcerro
373 43519 jjdelcerro
        Code code = compiler.compileExpression(source);
374 44198 jjdelcerro
        assertEquals("(\"precio\" + PI())", code.toString());
375 44009 jjdelcerro
376
        SymbolTable symbolTable = createSymbolTable();
377
        Optimizer optimizer = createOptimizer(symbolTable);
378
        Code code2 = optimizer.optimize(code);
379 44198 jjdelcerro
        assertEquals("(\"precio\" + 3.141592653589793)", code2.toString());
380 43519 jjdelcerro
    }
381 44009 jjdelcerro
382 43519 jjdelcerro
    public void testCeil() {
383
        String source = "precio + CEIL(PI())";
384
385
        Compiler compiler = createCompiler();
386 44009 jjdelcerro
387 43519 jjdelcerro
        Code code = compiler.compileExpression(source);
388 44198 jjdelcerro
        assertEquals("(\"precio\" + CEIL(PI()))", code.toString());
389 44009 jjdelcerro
390
        SymbolTable symbolTable = createSymbolTable();
391
        Optimizer optimizer = createOptimizer(symbolTable);
392
        Code code2 = optimizer.optimize(code);
393 44198 jjdelcerro
        assertEquals("(\"precio\" + 4.0)", code2.toString());
394 43519 jjdelcerro
    }
395 44009 jjdelcerro
396 43519 jjdelcerro
    public void testConcat() {
397
        String source = "CONCAT(precio,' euros')";
398
399
        Compiler compiler = createCompiler();
400 44009 jjdelcerro
401 43519 jjdelcerro
        Code code = compiler.compileExpression(source);
402 44006 jjdelcerro
        assertEquals("CONCAT(\"precio\", ' euros')", code.toString());
403 43521 jjdelcerro
404 44009 jjdelcerro
        SymbolTable symbolTable = createSymbolTable();
405
        Optimizer optimizer = createOptimizer(symbolTable);
406
        Code code2 = optimizer.optimize(code);
407
        assertEquals("CONCAT(\"precio\", ' euros')", code2.toString());
408 43521 jjdelcerro
    }
409
410 44139 jjdelcerro
    public void testConcat2() {
411
        String source = "CONCAT(precio,' euros')";
412
413
        Compiler compiler = createCompiler();
414
415
        Code code = compiler.compileExpression(source);
416
        assertEquals("CONCAT(\"precio\", ' euros')", code.toString());
417
418
        SymbolTable symbolTable = createSymbolTable();
419
        Optimizer optimizer = createOptimizer(symbolTable);
420
        Code code2 = optimizer.optimize(code);
421
        assertEquals("CONCAT(\"precio\", ' euros')", code2.toString());
422
    }
423
424 43989 jjdelcerro
    public void testInvokeFunction1() {
425
        String source = "ST_Area(GEOMETRY)";
426
427
        Compiler compiler = createCompiler();
428 44009 jjdelcerro
429 43989 jjdelcerro
        Code code = compiler.compileExpression(source);
430 44006 jjdelcerro
        assertEquals("ST_Area(\"GEOMETRY\")", code.toString());
431 44009 jjdelcerro
432
        SymbolTable symbolTable = createSymbolTable();
433
        Optimizer optimizer = createOptimizer(symbolTable);
434
        Code code2 = optimizer.optimize(code);
435
        assertEquals("ST_Area(\"GEOMETRY\")", code2.toString());
436 43989 jjdelcerro
    }
437
438 43939 jjdelcerro
    public void testInvokeMethod1() {
439
        String source = "'hola'.length()";
440
441
        Compiler compiler = createCompiler();
442 44009 jjdelcerro
443 43939 jjdelcerro
        Code code = compiler.compileExpression(source);
444
        assertEquals("'hola'->length()", code.toString());
445 44009 jjdelcerro
446
        SymbolTable symbolTable = createSymbolTable();
447
        Optimizer optimizer = createOptimizer(symbolTable);
448
        Code code2 = optimizer.optimize(code);
449
        assertEquals("'hola'->length()", code2.toString());
450 43939 jjdelcerro
    }
451 44009 jjdelcerro
452
    public void testSpatialExpression1() throws Exception {
453
        IProjection proj = CRSFactory.getCRS("EPSG:4326");
454
        ExpressionBuilder builder = new DefaultExpressionBuilder();
455 44198 jjdelcerro
        builder.geometry_support_type(ExpressionBuilder.GeometrySupportType.WKB);
456 44009 jjdelcerro
457
        String source = builder.and(
458
                builder.custom(this.createExpr1()),
459
                builder.ST_Intersects(
460
                        builder.geometry(createPolygon()),
461
                        builder.column("GEOMETRY")
462
                )
463
        ).toString();
464
465
        MutableSymbolTable symbolTable = createSymbolTable();
466
        symbolTable.setVar("campo1", 11);
467
        symbolTable.setVar("GEOMETRY", createPoint(50, 50));
468
469
        Interpreter interpreter = createInterpreter(symbolTable);
470 43939 jjdelcerro
        Compiler compiler = createCompiler();
471
        Code code = compiler.compileExpression(source);
472 44009 jjdelcerro
        System.out.println(code.toString());
473
        assertEquals(
474 44198 jjdelcerro
                "((\"campo1\" > 10) AND ST_Intersects(ST_GeomFromWKB(DECODE('000000000300000001000000050000000000000000000000000000000000000000000000004059000000000000405900000000000040590000000000004059000000000000000000000000000000000000000000000000000000000000', 'hex'), 4326), \"GEOMETRY\"))",
475 44009 jjdelcerro
                code.toString()
476
        );
477
478
//        long t1 = System.currentTimeMillis();
479
//        for (int i = 0; i < 1000; i++) {
480
//            interpreter.run(code);
481
//        }
482
//        System.out.println("Sin optimizar: " + (System.currentTimeMillis() - t1));
483
484
//        t1 = System.currentTimeMillis();
485
        Optimizer optimizer = createOptimizer(symbolTable);
486
        Code code2 = optimizer.optimize(code);
487
//        System.out.println("Tiempo de optimizacion: " + (System.currentTimeMillis() - t1));
488
        System.out.println(code2.toString());
489
        assertEquals(
490 44198 jjdelcerro
                "((\"campo1\" > 10) AND ST_Intersects('POLYGON ((0 0, 0 100, 100 100, 100 0, 0 0))'::geometry, \"GEOMETRY\"))",
491 44009 jjdelcerro
                code2.toString()
492
        );
493
494
//        t1 = System.currentTimeMillis();
495
//        for (int i = 0; i < 1000; i++) {
496
//            interpreter.run(code2);
497
//        }
498
//        System.out.println("Optimizado: " + (System.currentTimeMillis() - t1));
499
500 43939 jjdelcerro
    }
501 43983 jjdelcerro
502 43512 jjdelcerro
}