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