Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.library / org.gvsig.expressionevaluator / org.gvsig.expressionevaluator.geometry / org.gvsig.expressionevaluator.geometry.lib / org.gvsig.expressionevaluator.geometry.lib.impl / src / test / java / org / gvsig / expresionevaluator / impl / TestExpressionBuilderFormatter.java @ 44748

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