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