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 / TestCase1.java @ 44644
History | View | Annotate | Download (7.97 KB)
1 | 44006 | jjdelcerro | package org.gvsig.expresionevaluator.impl; |
---|---|---|---|
2 | 43128 | jjdelcerro | |
3 | 44006 | jjdelcerro | import junit.framework.TestCase; |
4 | 43128 | jjdelcerro | import org.cresques.cts.IProjection; |
5 | 44006 | jjdelcerro | import org.gvsig.expressionevaluator.Code; |
6 | import org.gvsig.expressionevaluator.LexicalAnalyzer; |
||
7 | import org.gvsig.expressionevaluator.impl.SQLLexicalAnalyzer; |
||
8 | 43128 | jjdelcerro | import org.gvsig.fmap.crs.CRSFactory; |
9 | import org.gvsig.fmap.geom.Geometry; |
||
10 | import org.gvsig.fmap.geom.GeometryLocator; |
||
11 | import org.gvsig.fmap.geom.GeometryManager; |
||
12 | import org.gvsig.fmap.geom.primitive.Point; |
||
13 | 44006 | jjdelcerro | import org.gvsig.fmap.geom.primitive.Polygon; |
14 | import org.gvsig.tools.library.impl.DefaultLibrariesInitializer; |
||
15 | import org.gvsig.expressionevaluator.Compiler; |
||
16 | import org.gvsig.expressionevaluator.ExpressionBuilder.Value; |
||
17 | 44644 | jjdelcerro | import org.gvsig.expressionevaluator.ExpressionUtils; |
18 | import org.gvsig.expressionevaluator.GeometryExpressionBuilder; |
||
19 | import org.gvsig.expressionevaluator.GeometryExpressionBuilderHelper.GeometrySupportType; |
||
20 | import org.gvsig.expressionevaluator.GeometryExpressionUtils; |
||
21 | 44006 | jjdelcerro | import org.gvsig.expressionevaluator.Interpreter; |
22 | import org.gvsig.expressionevaluator.MutableSymbolTable; |
||
23 | import org.gvsig.expressionevaluator.SymbolTable; |
||
24 | import org.gvsig.expressionevaluator.impl.DefaultInterpreter; |
||
25 | import org.gvsig.expressionevaluator.impl.DefaultSymbolTable; |
||
26 | import org.gvsig.fmap.geom.exception.CreateGeometryException; |
||
27 | 43128 | jjdelcerro | |
28 | 44006 | jjdelcerro | public class TestCase1 extends TestCase { |
29 | |||
30 | public TestCase1(String testName) { |
||
31 | super(testName);
|
||
32 | } |
||
33 | |||
34 | 43128 | jjdelcerro | @Override
|
35 | 44006 | jjdelcerro | protected void setUp() throws Exception { |
36 | super.setUp();
|
||
37 | new DefaultLibrariesInitializer().fullInitialize();
|
||
38 | } |
||
39 | 43128 | jjdelcerro | |
40 | 44006 | jjdelcerro | @Override
|
41 | protected void tearDown() throws Exception { |
||
42 | super.tearDown();
|
||
43 | 43128 | jjdelcerro | } |
44 | 44006 | jjdelcerro | |
45 | // TODO add test methods here. The name must begin with 'test'. For example:
|
||
46 | // public void testHello() {}
|
||
47 | 43128 | jjdelcerro | |
48 | 44006 | jjdelcerro | protected LexicalAnalyzer createLexicalAnalyzer() {
|
49 | SQLLexicalAnalyzer lexer = new SQLLexicalAnalyzer();
|
||
50 | return lexer;
|
||
51 | 43128 | jjdelcerro | } |
52 | |||
53 | 44006 | jjdelcerro | protected Compiler createCompiler() { |
54 | 44644 | jjdelcerro | Compiler compiler = ExpressionUtils.createCompiler();
|
55 | 44006 | jjdelcerro | compiler.setLexicalAnalyzer(createLexicalAnalyzer()); |
56 | return compiler;
|
||
57 | 43128 | jjdelcerro | } |
58 | 44006 | jjdelcerro | protected MutableSymbolTable createSymbolTable() {
|
59 | DefaultSymbolTable symbolTable = new DefaultSymbolTable();
|
||
60 | return symbolTable;
|
||
61 | } |
||
62 | 43128 | jjdelcerro | |
63 | 44006 | jjdelcerro | protected Interpreter createInterpreter(SymbolTable symbolTable) {
|
64 | Interpreter interpreter = new DefaultInterpreter();
|
||
65 | interpreter.setSymbolTable(symbolTable); |
||
66 | return interpreter;
|
||
67 | } |
||
68 | |||
69 | private String createExpr1() { |
||
70 | 44644 | jjdelcerro | GeometryExpressionBuilder builder = GeometryExpressionUtils.createExpressionBuilder(); |
71 | 43128 | jjdelcerro | |
72 | 44006 | jjdelcerro | Value expr1 = builder.gt( |
73 | builder.column("campo1"),
|
||
74 | builder.constant(10)
|
||
75 | 43128 | jjdelcerro | ); |
76 | 44006 | jjdelcerro | System.out.println(expr1.toString());
|
77 | 43128 | jjdelcerro | assertEquals( |
78 | 44006 | jjdelcerro | "( (\"campo1\") > (10) )",
|
79 | expr1.toString() |
||
80 | 43128 | jjdelcerro | ); |
81 | 44006 | jjdelcerro | return expr1.toString();
|
82 | 43128 | jjdelcerro | } |
83 | 44006 | jjdelcerro | |
84 | private Polygon createPolygon() throws CreateGeometryException { |
||
85 | GeometryManager geometryManager = GeometryLocator.getGeometryManager(); |
||
86 | IProjection proj = CRSFactory.getCRS("EPSG:4326");
|
||
87 | |||
88 | Polygon polygon = geometryManager.createPolygon(Geometry.SUBTYPES.GEOM2D);
|
||
89 | polygon.addVertex(0, 0); |
||
90 | polygon.addVertex(0, 100); |
||
91 | polygon.addVertex(100, 100); |
||
92 | polygon.addVertex(100, 0); |
||
93 | polygon.addVertex(0, 0); |
||
94 | |||
95 | polygon.setProjection(proj); |
||
96 | return polygon;
|
||
97 | } |
||
98 | 43128 | jjdelcerro | |
99 | 44006 | jjdelcerro | private Geometry createPoint(double x, double y) throws CreateGeometryException { |
100 | 43128 | jjdelcerro | GeometryManager geometryManager = GeometryLocator.getGeometryManager(); |
101 | IProjection proj = CRSFactory.getCRS("EPSG:4326");
|
||
102 | 44006 | jjdelcerro | Point point = geometryManager.createPoint(x, y, Geometry.SUBTYPES.GEOM2D);
|
103 | point.setProjection(proj); |
||
104 | return point;
|
||
105 | } |
||
106 | |||
107 | public void testBuildExpression() throws Exception { |
||
108 | 44644 | jjdelcerro | GeometryExpressionBuilder builder = GeometryExpressionUtils.createExpressionBuilder(); |
109 | 43128 | jjdelcerro | |
110 | 44006 | jjdelcerro | Value expr2 = builder.and( |
111 | builder.custom(this.createExpr1()),
|
||
112 | builder.ST_Intersects( |
||
113 | builder.geometry(createPolygon()), |
||
114 | builder.column("GEOMETRY")
|
||
115 | ) |
||
116 | 43128 | jjdelcerro | ); |
117 | 44198 | jjdelcerro | builder.geometry_support_type(GeometrySupportType.WKT); |
118 | 44006 | jjdelcerro | System.out.println(expr2.toString());
|
119 | 43128 | jjdelcerro | assertEquals( |
120 | 44262 | jjdelcerro | "(( (\"campo1\") > (10) ) AND ST_Intersects((ST_GeomFromText(('POLYGON ((0 0, 0 100, 100 100, 100 0, 0 0))'), (4326))), (\"GEOMETRY\")))",
|
121 | 44006 | jjdelcerro | expr2.toString() |
122 | 43128 | jjdelcerro | ); |
123 | 44198 | jjdelcerro | builder.geometry_support_type(GeometrySupportType.WKB); |
124 | 44006 | jjdelcerro | System.out.println(expr2.toString());
|
125 | 43128 | jjdelcerro | assertEquals( |
126 | 44262 | jjdelcerro | "(( (\"campo1\") > (10) ) AND ST_Intersects((ST_GeomFromWKB((DECODE('000000000300000001000000050000000000000000000000000000000000000000000000004059000000000000405900000000000040590000000000004059000000000000000000000000000000000000000000000000000000000000','hex')), (4326))), (\"GEOMETRY\")))",
|
127 | 44006 | jjdelcerro | expr2.toString() |
128 | 43128 | jjdelcerro | ); |
129 | } |
||
130 | 44006 | jjdelcerro | |
131 | public void testCompileExpression() throws Exception { |
||
132 | 43128 | jjdelcerro | IProjection proj = CRSFactory.getCRS("EPSG:4326");
|
133 | 44644 | jjdelcerro | GeometryExpressionBuilder builder = GeometryExpressionUtils.createExpressionBuilder(); |
134 | 43128 | jjdelcerro | |
135 | 44006 | jjdelcerro | Value expr2 = builder.and( |
136 | builder.custom(this.createExpr1()),
|
||
137 | builder.ST_Intersects( |
||
138 | builder.geometry(createPolygon()), |
||
139 | builder.column("GEOMETRY")
|
||
140 | ) |
||
141 | 43128 | jjdelcerro | ); |
142 | |||
143 | 44006 | jjdelcerro | Compiler compiler = createCompiler();
|
144 | Code code; |
||
145 | |||
146 | 44198 | jjdelcerro | builder.geometry_support_type(GeometrySupportType.WKT); |
147 | 44006 | jjdelcerro | code = compiler.compileExpression(expr2.toString()); |
148 | System.out.println(code.toString());
|
||
149 | 43128 | jjdelcerro | assertEquals( |
150 | 44198 | jjdelcerro | "((\"campo1\" > 10) AND ST_Intersects(ST_GeomFromText('POLYGON ((0 0, 0 100, 100 100, 100 0, 0 0))', 4326), \"GEOMETRY\"))",
|
151 | 44006 | jjdelcerro | code.toString() |
152 | 43128 | jjdelcerro | ); |
153 | 44006 | jjdelcerro | |
154 | 44198 | jjdelcerro | builder.geometry_support_type(GeometrySupportType.WKB); |
155 | 44006 | jjdelcerro | code = compiler.compileExpression(expr2.toString()); |
156 | System.out.println(code.toString());
|
||
157 | 43128 | jjdelcerro | assertEquals( |
158 | 44198 | jjdelcerro | "((\"campo1\" > 10) AND ST_Intersects(ST_GeomFromWKB(DECODE('000000000300000001000000050000000000000000000000000000000000000000000000004059000000000000405900000000000040590000000000004059000000000000000000000000000000000000000000000000000000000000', 'hex'), 4326), \"GEOMETRY\"))",
|
159 | 44006 | jjdelcerro | code.toString() |
160 | 43128 | jjdelcerro | ); |
161 | 44006 | jjdelcerro | |
162 | 43128 | jjdelcerro | } |
163 | 44006 | jjdelcerro | |
164 | public void testRunExpression() throws Exception { |
||
165 | |||
166 | 44644 | jjdelcerro | GeometryExpressionBuilder builder = GeometryExpressionUtils.createExpressionBuilder(); |
167 | 44198 | jjdelcerro | builder.geometry_support_type(GeometrySupportType.WKB); |
168 | 43128 | jjdelcerro | |
169 | 44006 | jjdelcerro | String expr2 = builder.and(
|
170 | builder.custom(this.createExpr1()),
|
||
171 | builder.ST_Intersects( |
||
172 | builder.geometry(createPolygon()), |
||
173 | builder.column("GEOMETRY")
|
||
174 | ) |
||
175 | ).toString(); |
||
176 | 43128 | jjdelcerro | |
177 | 44006 | jjdelcerro | Compiler compiler = createCompiler();
|
178 | Code code = compiler.compileExpression(expr2); |
||
179 | |||
180 | MutableSymbolTable symbolTable = createSymbolTable(); |
||
181 | Interpreter interpreter = createInterpreter(symbolTable); |
||
182 | Object v;
|
||
183 | 43128 | jjdelcerro | |
184 | 44006 | jjdelcerro | symbolTable.setVar("campo1", 11); |
185 | symbolTable.setVar("GEOMETRY", createPoint(50, 50)); |
||
186 | v = interpreter.run(code); |
||
187 | assertEquals(true, ((Boolean)v).booleanValue()); |
||
188 | |||
189 | symbolTable.setVar("campo1", 9); |
||
190 | symbolTable.setVar("GEOMETRY", createPoint(50, 50)); |
||
191 | v = interpreter.run(code); |
||
192 | assertEquals(false, ((Boolean)v).booleanValue()); |
||
193 | |||
194 | symbolTable.setVar("campo1", 11); |
||
195 | symbolTable.setVar("GEOMETRY", createPoint(150, 50)); |
||
196 | v = interpreter.run(code); |
||
197 | assertEquals(false, ((Boolean)v).booleanValue()); |
||
198 | 43128 | jjdelcerro | } |
199 | 44006 | jjdelcerro | |
200 | 43128 | jjdelcerro | } |