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