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 / TestExpressionBuilderFormatter.java @ 44198

History | View | Annotate | Download (17.3 KB)

1 44006 jjdelcerro
package org.gvsig.expresionevaluator.impl;
2 43128 jjdelcerro
3 44198 jjdelcerro
import java.text.MessageFormat;
4 43128 jjdelcerro
import java.util.ArrayList;
5
import java.util.List;
6 44006 jjdelcerro
import junit.framework.TestCase;
7 43128 jjdelcerro
import org.apache.commons.lang3.ArrayUtils;
8 44198 jjdelcerro
import org.apache.commons.lang3.StringUtils;
9 43128 jjdelcerro
import org.cresques.cts.IProjection;
10 44198 jjdelcerro
import org.gvsig.expressionevaluator.Formatter;
11 44006 jjdelcerro
import org.gvsig.expressionevaluator.ExpressionBuilder;
12 44198 jjdelcerro
import org.gvsig.expressionevaluator.ExpressionBuilder.Constant;
13
import org.gvsig.expressionevaluator.ExpressionBuilder.Function;
14 44006 jjdelcerro
import org.gvsig.expressionevaluator.ExpressionBuilder.GeometrySupportType;
15
import org.gvsig.expressionevaluator.ExpressionBuilder.Parameter;
16 44198 jjdelcerro
import org.gvsig.expressionevaluator.ExpressionBuilder.Value;
17 44006 jjdelcerro
import org.gvsig.expressionevaluator.ExpressionBuilder.Variable;
18
import org.gvsig.expressionevaluator.impl.DefaultExpressionBuilder;
19 43128 jjdelcerro
import org.gvsig.fmap.crs.CRSFactory;
20
import org.gvsig.fmap.geom.Geometry;
21
import org.gvsig.fmap.geom.GeometryLocator;
22
import org.gvsig.fmap.geom.GeometryManager;
23 44198 jjdelcerro
import org.gvsig.fmap.geom.GeometryUtils;
24 43128 jjdelcerro
import org.gvsig.fmap.geom.exception.CreateGeometryException;
25
import org.gvsig.fmap.geom.primitive.Point;
26 44006 jjdelcerro
import org.gvsig.tools.library.impl.DefaultLibrariesInitializer;
27 43128 jjdelcerro
28 44198 jjdelcerro
public class TestExpressionBuilderFormatter extends TestCase {
29 44006 jjdelcerro
30 44198 jjdelcerro
    private static class MyFormatter implements Formatter<Value> {
31
32
        private class Formatter_constant_bytearray implements Formatter<Value> {
33
34
            @Override
35
            public boolean canApply(Value value) {
36
                if( value instanceof Constant ) {
37
                    return ((Constant)value).value() instanceof byte[];
38
                }
39
                return false;
40
            }
41
42
            @Override
43
            public String format(Value constant) {
44
                return builder.bytearray_x((byte[]) ((Constant)constant).value());
45
            }
46
        }
47
48
        private class Formatter_constant_geometry implements Formatter<Value> {
49
50
            @Override
51
            public boolean canApply(Value value) {
52
                if( value instanceof Constant ) {
53
                    return ((Constant)value).value() instanceof Geometry;
54
                }
55
                return false;
56
            }
57
58
            @Override
59
            public String format(Value constant) {
60
                Geometry geometry = (Geometry) ((Constant)constant).value();
61
                switch (builder.geometry_support_type()) {
62
                    case EWKB:
63
                        return MessageFormat.format(
64
                                "ST_GeomFromEWKB(({0}), ({1}))",
65
                                builder.bytearray_x(GeometryUtils.toEWKB(geometry)),
66
                                String.valueOf(builder.srs_id(geometry.getProjection()))
67
                        );
68
                    case WKB:
69
                        return MessageFormat.format(
70
                                "ST_GeomFromWKB(({0}), ({1}))",
71
                                builder.bytearray_x(GeometryUtils.toWKB(geometry)),
72
                                String.valueOf(builder.srs_id(geometry.getProjection()))
73
                        );
74
                    case WKT:
75
                    default:
76
                        return MessageFormat.format(
77
                                "ST_GeomFromText({0}, ({1}))",
78
                                builder.string(GeometryUtils.toWKT(geometry)),
79
                                String.valueOf(builder.srs_id(geometry.getProjection()))
80
                        );
81
                }
82
            }
83
        }
84
85
        private class Formatter_ST_intersects implements Formatter<Value> {
86
87
            @Override
88
            public boolean canApply(Value value) {
89
                if( value instanceof Function ) {
90
                    return StringUtils.equalsIgnoreCase("ST_intersects",((Function)value).name());
91
                }
92
                return false;
93
            }
94
95
            @Override
96
            public String format(Value function) {
97
                List<Value> parameters = ((Function)function).parameters();
98
                String p1 = parameters.get(0).toString(formatter());
99
                String p2 = parameters.get(1).toString(formatter());
100
                String r = MessageFormat.format(
101
                        "( (({0}) && ({1})) AND ST_Intersects(({0}),({1}) ))",
102
                        p1,
103
                        p2
104
                );
105
                return r;
106
            }
107
        }
108
109
        private class Formatter_ST_GeomFromEWKB implements Formatter<Value> {
110
111
            @Override
112
            public boolean canApply(Value value) {
113
                if( value instanceof Function ) {
114
                    return StringUtils.equalsIgnoreCase("ST_GeomFromEWKB",((Function)value).name());
115
                }
116
                return false;
117
            }
118
119
            @Override
120
            public String format(Value function) {
121
                List<Value> parameters = ((Function)function).parameters();
122
                String p1 = parameters.get(0).toString(formatter());
123
                String p2 = parameters.get(1).toString(formatter());
124
                String r = MessageFormat.format(
125
                        "GeomFromWKB({0}, {1})",
126
                        p1,
127
                        p2
128
                );
129
                return r;
130
            }
131
        }
132
133
        private class Formatter_IFNULL implements Formatter<Value> {
134
135
            @Override
136
            public boolean canApply(Value value) {
137
                if( value instanceof Function ) {
138
                    return StringUtils.equalsIgnoreCase("IFNULL",((Function)value).name());
139
                }
140
                return false;
141
            }
142
143
            @Override
144
            public String format(Value function) {
145
                List<Value> parameters = ((Function)function).parameters();
146
                String p1 = parameters.get(0).toString(formatter());
147
                String p2 = parameters.get(1).toString(formatter());
148
                String p3 = parameters.get(2).toString(formatter());
149
                String r = MessageFormat.format(
150
                        "NVL2({0}, {1}, {2})",
151
                        p1,
152
                        p3,
153
                        p2
154
                );
155
                return r;
156
            }
157
        }
158
159
        private class Formatter_ST_AsEWKB implements Formatter<Value> {
160
161
            @Override
162
            public boolean canApply(Value value) {
163
                if( value instanceof Function ) {
164
                    return StringUtils.equalsIgnoreCase("ST_AsEWKB",((Function)value).name());
165
                }
166
                return false;
167
            }
168
169
            public String format(Value function) {
170
                List<Value> parameters = ((Function)function).parameters();
171
                String p1 = parameters.get(0).toString(formatter());
172
                String r = MessageFormat.format(
173
                        "AsWKB(({0}))",
174
                        p1
175
                );
176
                return r;
177
            }
178
        }
179
180
        private class Formatter_NOT_IS_NULL implements Formatter<Value> {
181
182
            @Override
183
            public boolean canApply(Value value) {
184
                if( value instanceof Function ) {
185
                    return StringUtils.equalsIgnoreCase("NOT_IS_NULL",((Function)value).name());
186
                }
187
                return false;
188
            }
189
190
            @Override
191
            public String format(Value function) {
192
                List<Value> parameters = ((Function)function).parameters();
193
                String p1 = parameters.get(0).toString(MyFormatter.this);
194
                String r = MessageFormat.format(
195
                        "( ({0}) IS NOT NULL )",
196
                        p1
197
                );
198
                return r;
199
            }
200
        }
201
202
        private final Formatter<Value>[] formatters;
203
        private final ExpressionBuilder builder;
204
205
        public MyFormatter(ExpressionBuilder builder) {
206
            this.builder = builder;
207
            this.formatters = new Formatter[] {
208
                new Formatter_IFNULL(),
209
                new Formatter_NOT_IS_NULL(),
210
                new Formatter_ST_AsEWKB(),
211
                new Formatter_ST_GeomFromEWKB(),
212
                new Formatter_constant_bytearray(),
213
                new Formatter_constant_geometry(),
214
                new Formatter_ST_intersects()
215
            };
216
        }
217
218
        @Override
219
        public boolean canApply(Value value) {
220
            for (Formatter<Value> formatter : formatters) {
221
                if( formatter.canApply(value) ) {
222
                    return true;
223
                }
224
            }
225
            return false;
226
        }
227
228
        @Override
229
        public String format(Value value) {
230
            for (Formatter<Value> formatter : formatters) {
231
                if( formatter.canApply(value) ) {
232
                    return formatter.format(value);
233
                }
234
            }
235
            return value.toString(this);
236
        }
237
238
        private Formatter formatter() {
239
            return this;
240
        }
241
    }
242
243
    public TestExpressionBuilderFormatter(String testName) {
244 44006 jjdelcerro
        super(testName);
245
    }
246
247 43128 jjdelcerro
    @Override
248 44006 jjdelcerro
    protected void setUp() throws Exception {
249
        super.setUp();
250
        new DefaultLibrariesInitializer().fullInitialize();
251
    }
252 43128 jjdelcerro
253 44006 jjdelcerro
    @Override
254
    protected void tearDown() throws Exception {
255
        super.tearDown();
256 43128 jjdelcerro
    }
257 44006 jjdelcerro
258
    // TODO add test methods here. The name must begin with 'test'. For example:
259
    // public void testHello() {}
260 43128 jjdelcerro
261
    List<String> getVariableNames(ExpressionBuilder builder) {
262
        List<String> vars = new ArrayList<>();
263 44198 jjdelcerro
        for (Variable var : builder.variables()) {
264
            vars.add(var.name());
265 43128 jjdelcerro
        }
266
        return vars;
267
    }
268
269
    List<String> getParameterNames(ExpressionBuilder builder) {
270
        List<String> params = new ArrayList<>();
271 44198 jjdelcerro
        for (Parameter param : builder.parameters()) {
272 43128 jjdelcerro
            String s;
273 44198 jjdelcerro
            switch(param.type()) {
274 43128 jjdelcerro
                case Constant:
275 44198 jjdelcerro
                    Object value = param.value();
276 43128 jjdelcerro
                    if( value==null ) {
277
                        s = "null";
278
                    } else if( value instanceof String ) {
279
                        s = "'" + (String)value + "'";
280
                    } else {
281
                        s = value.toString();
282
                    }
283
                    break;
284
                case Geometry:
285
                case Variable:
286
                default:
287 44198 jjdelcerro
                    s = "\"" + param.name() + "\"";
288 43128 jjdelcerro
            }
289
            params.add(s);
290
        }
291
        return params;
292
    }
293
294
    public void test2() {
295 44006 jjdelcerro
        ExpressionBuilder builder = new DefaultExpressionBuilder();
296 43128 jjdelcerro
297
        builder.and(
298
          builder.eq(
299 44198 jjdelcerro
            builder.lower(builder.variable("colum_name_c")),
300 43128 jjdelcerro
            builder.parameter("colum_name_p")
301
          )
302
        );
303
        builder.and(
304
            builder.group(
305
                builder.or(
306
                    builder.like(
307 44198 jjdelcerro
                        builder.lower( builder.variable("uno")),
308 43128 jjdelcerro
                        builder.constant("%10")
309
                    ),
310
                    builder.lt(
311
                        builder.variable("dos"),
312
                        builder.constant(-3.5)
313
                    )
314
                )
315
            )
316
        );
317
        builder.and(
318
                builder.ST_Intersects(
319
                    builder.variable("geom1"),
320
                    builder.ST_Envelope(
321
                        builder.ST_GeomFromWKB(
322
                                builder.parameter("geom2"),
323
                                builder.parameter().value(4326).as_constant()
324
                        )
325
                    )
326
                )
327
        );
328
        builder.and(
329
            builder.gt(
330
                builder.variable("tres"),
331
                builder.constant(123456789)
332
            )
333
        );
334
        assertEquals(
335 44198 jjdelcerro
                "( (LOWER(\"colum_name_c\")) = (?) ) AND ( ( (LOWER(\"uno\")) LIKE ('%10') ) OR ( (\"dos\") < (-3.5) ) ) AND ST_Intersects((\"geom1\"), (ST_Envelope(ST_GeomFromWKB((?), (?))))) AND ( (\"tres\") > (123456789) )",
336 43128 jjdelcerro
                builder.toString()
337
        );
338
        assertEquals(
339 44198 jjdelcerro
                "( (LOWER(\"colum_name_c\")) = (?) ) AND ( ( (LOWER(\"uno\")) LIKE ('%10') ) OR ( (\"dos\") < (-3.5) ) ) AND ( ((\"geom1\") && (ST_Envelope(ST_GeomFromWKB((?), (?))))) AND ST_Intersects((\"geom1\"),(ST_Envelope(ST_GeomFromWKB((?), (?)))) )) AND ( (\"tres\") > (123456789) )",
340
                builder.toString(new MyFormatter(builder))
341
        );
342
343
        assertEquals(
344 43128 jjdelcerro
                "[colum_name_c, dos, geom1, tres, uno]",
345
                ArrayUtils.toString(getVariableNames(builder))
346
        );
347
        assertEquals(
348
                "[\"colum_name_p\", \"geom2\", 4326]",
349
                ArrayUtils.toString(getParameterNames(builder))
350
        );
351
    }
352
353
    public void test3() throws CreateGeometryException {
354 44006 jjdelcerro
        ExpressionBuilder builder = new DefaultExpressionBuilder();
355 43128 jjdelcerro
356
        GeometryManager geometryManager = GeometryLocator.getGeometryManager();
357
        IProjection proj = CRSFactory.getCRS("EPSG:4326");
358
359
        Point point = geometryManager.createPoint(10, 20, Geometry.SUBTYPES.GEOM2D);
360
        builder.set(
361
            builder.ST_Intersects(
362
              builder.geometry(point,proj),
363
              builder.variable("the_geom")
364
            )
365
        );
366 44198 jjdelcerro
        builder.geometry_support_type(GeometrySupportType.WKT);
367 43128 jjdelcerro
        System.out.println(builder.toString());
368
        assertEquals(
369 44198 jjdelcerro
                "ST_Intersects((ST_GeomFromText(('POINT (10 20)'), (4326))), (\"the_geom\"))",
370 43128 jjdelcerro
                builder.toString()
371
        );
372 44198 jjdelcerro
        assertEquals(
373
                "( ((ST_GeomFromText('POINT (10 20)', (4326))) && (\"the_geom\")) AND ST_Intersects((ST_GeomFromText('POINT (10 20)', (4326))),(\"the_geom\") ))",
374
                builder.toString(new MyFormatter(builder))
375
        );
376
377
        builder.geometry_support_type(GeometrySupportType.WKB);
378 43128 jjdelcerro
        System.out.println(builder.toString());
379
        assertEquals(
380 44006 jjdelcerro
                "ST_Intersects((ST_GeomFromWKB((DECODE('000000000140240000000000004034000000000000','hex')), (4326))), (\"the_geom\"))",
381 43128 jjdelcerro
                builder.toString()
382
        );
383
        assertEquals(
384 44198 jjdelcerro
                "( ((ST_GeomFromWKB((x'000000000140240000000000004034000000000000'), (4326))) && (\"the_geom\")) AND ST_Intersects((ST_GeomFromWKB((x'000000000140240000000000004034000000000000'), (4326))),(\"the_geom\") ))",
385
                builder.toString(new MyFormatter(builder))
386
        );
387
        assertEquals(
388 43128 jjdelcerro
                "[the_geom]",
389
                ArrayUtils.toString(getVariableNames(builder))
390
        );
391
        assertEquals(
392
                "[]",
393
                ArrayUtils.toString(getParameterNames(builder))
394
        );
395
    }
396
397
    public void test4() throws CreateGeometryException {
398 44006 jjdelcerro
        ExpressionBuilder builder = new DefaultExpressionBuilder();
399 43128 jjdelcerro
400
        IProjection proj = CRSFactory.getCRS("EPSG:4326");
401
402
        builder.and(
403
                builder.ST_Intersects(
404
                    builder.variable("geom1"),
405
                    builder.ST_Envelope(
406
                        builder.ST_GeomFromWKB(
407
                                builder.parameter("geom2"),
408
                                builder.parameter().value(proj).as_constant()
409
                        )
410
                    )
411
                )
412
        );
413
414
        System.out.println(builder.toString());
415
        assertEquals(
416
                "ST_Intersects((\"geom1\"), (ST_Envelope(ST_GeomFromWKB((?), (?)))))",
417
                builder.toString()
418
        );
419
        assertEquals(
420 44198 jjdelcerro
                "( ((\"geom1\") && (ST_Envelope(ST_GeomFromWKB((?), (?))))) AND ST_Intersects((\"geom1\"),(ST_Envelope(ST_GeomFromWKB((?), (?)))) ))",
421
                builder.toString(new MyFormatter(builder))
422
        );
423
        assertEquals(
424 43128 jjdelcerro
                "[geom1]",
425
                ArrayUtils.toString(getVariableNames(builder))
426
        );
427
        assertEquals(
428
                "[\"geom2\", 4326]",
429
                ArrayUtils.toString(getParameterNames(builder))
430
        );
431
    }
432
433
    public void test5() throws CreateGeometryException {
434 44006 jjdelcerro
        ExpressionBuilder builder = new DefaultExpressionBuilder();
435 43128 jjdelcerro
436
        IProjection proj = CRSFactory.getCRS("EPSG:4326");
437
438
        builder.and(
439
                builder.eq(
440
                        builder.ST_SRID( builder.variable("geom") ),
441
                        builder.srs(proj)
442
                )
443
        );
444
445
        System.out.println(builder.toString());
446
        assertEquals(
447
                "( (ST_SRID(\"geom\")) = (4326) )",
448
                builder.toString()
449
        );
450
        assertEquals(
451 44198 jjdelcerro
                "( (ST_SRID(\"geom\")) = (4326) )",
452
                builder.toString(new MyFormatter(builder))
453
        );
454
        assertEquals(
455 43128 jjdelcerro
                "[geom]",
456
                ArrayUtils.toString(getVariableNames(builder))
457
        );
458
        assertEquals(
459
                "[]",
460
                ArrayUtils.toString(getParameterNames(builder))
461
        );
462
    }
463
}