|
1 |
package org.gvsig.expressionevaluator.integration;
|
|
2 |
|
|
3 |
import java.util.ArrayList;
|
|
4 |
import java.util.HashSet;
|
|
5 |
import java.util.List;
|
|
6 |
import java.util.Set;
|
|
7 |
import org.gvsig.expressionevaluator.Code;
|
|
8 |
import org.gvsig.expressionevaluator.Interpreter;
|
|
9 |
import org.gvsig.expressionevaluator.LexicalAnalyzer;
|
|
10 |
import org.gvsig.expressionevaluator.SymbolTable;
|
|
11 |
import org.gvsig.expressionevaluator.Compiler;
|
|
12 |
import org.gvsig.expressionevaluator.ExpressionEvaluator;
|
|
13 |
import org.gvsig.expressionevaluator.ExpressionEvaluatorLocator;
|
|
14 |
import org.gvsig.expressionevaluator.Function;
|
|
15 |
import org.gvsig.expressionevaluator.impl.DefaultCompiler;
|
|
16 |
import org.gvsig.expressionevaluator.impl.DefaultInterpreter;
|
|
17 |
import org.gvsig.expressionevaluator.impl.SQLLexicalAnalyzer;
|
|
18 |
import org.gvsig.expressionevaluator.impl.SQLSymbolTable;
|
|
19 |
import org.gvsig.tools.evaluator.AbstractEvaluator;
|
|
20 |
import org.gvsig.tools.evaluator.EvaluatorData;
|
|
21 |
import org.gvsig.tools.evaluator.EvaluatorException;
|
|
22 |
import org.gvsig.tools.evaluator.EvaluatorFieldsInfo;
|
|
23 |
import org.gvsig.tools.exception.BaseException;
|
|
24 |
import org.gvsig.tools.visitor.VisitCanceledException;
|
|
25 |
import org.gvsig.tools.visitor.Visitor;
|
|
26 |
import org.gvsig.tools.lang.Cloneable;
|
|
27 |
|
|
28 |
public class DefaultExpressionEvaluator extends AbstractEvaluator implements ExpressionEvaluator, Cloneable {
|
|
29 |
|
|
30 |
private static class DescriptionAdapter implements Description {
|
|
31 |
|
|
32 |
Function function;
|
|
33 |
|
|
34 |
public DescriptionAdapter(Function function) {
|
|
35 |
this.function = function;
|
|
36 |
}
|
|
37 |
|
|
38 |
@Override
|
|
39 |
public String getName() {
|
|
40 |
return this.function.name();
|
|
41 |
}
|
|
42 |
|
|
43 |
@Override
|
|
44 |
public String getDescription() {
|
|
45 |
return this.function.description();
|
|
46 |
}
|
|
47 |
|
|
48 |
@Override
|
|
49 |
public String getTemplate() {
|
|
50 |
return this.function.template();
|
|
51 |
}
|
|
52 |
|
|
53 |
@Override
|
|
54 |
public int getDataTypeCategories() {
|
|
55 |
switch(this.function.group()) {
|
|
56 |
case Function.GROUP_STRING:
|
|
57 |
return Description.DATATYPE_CATEGORY_STRING;
|
|
58 |
case Function.GROUP_BOOLEAN:
|
|
59 |
return Description.DATATYPE_CATEGORY_BOOLEAN;
|
|
60 |
case Function.GROUP_DATETIME:
|
|
61 |
return Description.DATATYPE_CATEGORY_DATETIME;
|
|
62 |
case Function.GROUP_NUMERIC:
|
|
63 |
return Description.DATATYPE_CATEGORY_NUMBER;
|
|
64 |
case Function.GROUP_OGC:
|
|
65 |
return Description.DATATYPE_CATEGORY_ALL;
|
|
66 |
default:
|
|
67 |
return Description.DATATYPE_CATEGORY_ALL;
|
|
68 |
}
|
|
69 |
}
|
|
70 |
|
|
71 |
}
|
|
72 |
|
|
73 |
private EvaluatorDataAdapter evaluatorData;
|
|
74 |
private SymbolTable symbolTable;
|
|
75 |
private LexicalAnalyzer lexer;
|
|
76 |
private Compiler compiler;
|
|
77 |
private Interpreter interpreter;
|
|
78 |
private Code code;
|
|
79 |
private String source;
|
|
80 |
private Description[] availableOperators;
|
|
81 |
private Description[] availableFunctions;
|
|
82 |
private Double accuracy;
|
|
83 |
|
|
84 |
public DefaultExpressionEvaluator() {
|
|
85 |
super();
|
|
86 |
this.accuracy = ExpressionEvaluatorLocator.getManager().getAccuracy();
|
|
87 |
}
|
|
88 |
|
|
89 |
@Override
|
|
90 |
public Code getCode() {
|
|
91 |
return this.code;
|
|
92 |
}
|
|
93 |
|
|
94 |
@Override
|
|
95 |
public void compile() {
|
|
96 |
if( this.symbolTable == null ) {
|
|
97 |
this.symbolTable = SQLSymbolTable.getInstance();
|
|
98 |
}
|
|
99 |
if( this.lexer == null ) {
|
|
100 |
this.lexer = new SQLLexicalAnalyzer();
|
|
101 |
}
|
|
102 |
if( this.compiler == null ) {
|
|
103 |
this.compiler = new DefaultCompiler();
|
|
104 |
this.compiler.setLexicalAnalyzer(lexer);
|
|
105 |
}
|
|
106 |
if( this.interpreter == null ) {
|
|
107 |
this.interpreter = new DefaultInterpreter();
|
|
108 |
}
|
|
109 |
this.evaluatorData = new EvaluatorDataAdapter(symbolTable);
|
|
110 |
this.interpreter.setAccuracy(this.accuracy);
|
|
111 |
this.interpreter.setSymbolTable(this.evaluatorData);
|
|
112 |
this.code = this.compiler.compileExpression(source);
|
|
113 |
}
|
|
114 |
|
|
115 |
@Override
|
|
116 |
public Object evaluate(EvaluatorData data) throws EvaluatorException {
|
|
117 |
if( this.code == null ) {
|
|
118 |
this.compile();
|
|
119 |
}
|
|
120 |
this.evaluatorData.setData(data);
|
|
121 |
Object v = this.interpreter.run(code);
|
|
122 |
return v;
|
|
123 |
}
|
|
124 |
|
|
125 |
@Override
|
|
126 |
public String getName() {
|
|
127 |
return "Genereric expression";
|
|
128 |
}
|
|
129 |
|
|
130 |
@Override
|
|
131 |
public EvaluatorFieldsInfo getFieldsInfo() {
|
|
132 |
final Set<String> names = new HashSet<>();
|
|
133 |
try {
|
|
134 |
this.code.accept(new Visitor() {
|
|
135 |
@Override
|
|
136 |
public void visit(Object code) throws VisitCanceledException, BaseException {
|
|
137 |
if( code instanceof Code.Identifier ) {
|
|
138 |
Code.Identifier identifier = (Code.Identifier) code;
|
|
139 |
names.add(identifier.name());
|
|
140 |
}
|
|
141 |
}
|
|
142 |
});
|
|
143 |
EvaluatorFieldsInfo info = new EvaluatorFieldsInfo();
|
|
144 |
for (String name : names) {
|
|
145 |
info.addFieldValue(name);
|
|
146 |
}
|
|
147 |
return info;
|
|
148 |
} catch (BaseException ex) {
|
|
149 |
throw new RuntimeException("Can't calculate fields information.", ex);
|
|
150 |
}
|
|
151 |
}
|
|
152 |
|
|
153 |
@Override
|
|
154 |
public Description[] getAvailableOperators() {
|
|
155 |
if( availableOperators==null ) {
|
|
156 |
List<Description> l = new ArrayList<>();
|
|
157 |
for( Function function : symbolTable) {
|
|
158 |
if( function.isOperator() ) {
|
|
159 |
l.add(new DescriptionAdapter(function));
|
|
160 |
}
|
|
161 |
}
|
|
162 |
this.availableOperators = l.toArray(new Description[l.size()]);
|
|
163 |
}
|
|
164 |
return availableOperators;
|
|
165 |
}
|
|
166 |
|
|
167 |
@Override
|
|
168 |
public Description[] getAvailableFunctions() {
|
|
169 |
if( availableFunctions==null ) {
|
|
170 |
List<Description> l = new ArrayList<>();
|
|
171 |
for( Function function : symbolTable) {
|
|
172 |
if( !function.isOperator() ) {
|
|
173 |
l.add(new DescriptionAdapter(function));
|
|
174 |
}
|
|
175 |
}
|
|
176 |
this.availableFunctions = l.toArray(new Description[l.size()]);
|
|
177 |
}
|
|
178 |
return availableFunctions;
|
|
179 |
}
|
|
180 |
|
|
181 |
@Override
|
|
182 |
public SymbolTable getSymbolTable() {
|
|
183 |
return symbolTable;
|
|
184 |
}
|
|
185 |
|
|
186 |
@Override
|
|
187 |
public void setSymbolTable(SymbolTable symbolTable) {
|
|
188 |
this.symbolTable = symbolTable;
|
|
189 |
}
|
|
190 |
|
|
191 |
@Override
|
|
192 |
public LexicalAnalyzer getLexer() {
|
|
193 |
return lexer;
|
|
194 |
}
|
|
195 |
|
|
196 |
@Override
|
|
197 |
public void setLexer(LexicalAnalyzer lexer) {
|
|
198 |
this.lexer = lexer;
|
|
199 |
}
|
|
200 |
|
|
201 |
@Override
|
|
202 |
public Compiler getCompiler() {
|
|
203 |
return compiler;
|
|
204 |
}
|
|
205 |
|
|
206 |
@Override
|
|
207 |
public void setCompiler(Compiler compiler) {
|
|
208 |
this.compiler = compiler;
|
|
209 |
}
|
|
210 |
|
|
211 |
@Override
|
|
212 |
public Interpreter getInterpreter() {
|
|
213 |
return interpreter;
|
|
214 |
}
|
|
215 |
|
|
216 |
@Override
|
|
217 |
public void setInterpreter(Interpreter interpreter) {
|
|
218 |
this.interpreter = interpreter;
|
|
219 |
}
|
|
220 |
|
|
221 |
@Override
|
|
222 |
public String getSource() {
|
|
223 |
return source;
|
|
224 |
}
|
|
225 |
|
|
226 |
@Override
|
|
227 |
public void setSource(String source) {
|
|
228 |
this.source = source;
|
|
229 |
this.code = null;
|
|
230 |
}
|
|
231 |
|
|
232 |
@Override
|
|
233 |
public Double getAccuracy() {
|
|
234 |
return this.accuracy;
|
|
235 |
}
|
|
236 |
|
|
237 |
@Override
|
|
238 |
public void setAccuracy(Double accuracy) {
|
|
239 |
this.accuracy = accuracy;
|
|
240 |
}
|
|
241 |
|
|
242 |
@Override
|
|
243 |
public ExpressionEvaluator clone() throws CloneNotSupportedException {
|
|
244 |
DefaultExpressionEvaluator other = (DefaultExpressionEvaluator) super.clone();
|
|
245 |
other.code = this.code;
|
|
246 |
other.source = this.source;
|
|
247 |
other.accuracy = this.accuracy;
|
|
248 |
other.availableFunctions = null;
|
|
249 |
other.availableOperators = null;
|
|
250 |
|
|
251 |
if( this.symbolTable!=null ) {
|
|
252 |
other.symbolTable = this.symbolTable.clone();
|
|
253 |
}
|
|
254 |
if( this.lexer!=null ) {
|
|
255 |
other.lexer = this.lexer.clone();
|
|
256 |
}
|
|
257 |
if( this.evaluatorData!=null ) {
|
|
258 |
// evaluatorData don't support clone.
|
|
259 |
other.evaluatorData = new EvaluatorDataAdapter(other.symbolTable);
|
|
260 |
}
|
|
261 |
if( this.compiler!=null ) {
|
|
262 |
other.compiler = this.compiler.clone();
|
|
263 |
other.compiler.setLexicalAnalyzer(other.lexer);
|
|
264 |
}
|
|
265 |
if( this.interpreter!=null ) {
|
|
266 |
other.interpreter = this.interpreter.clone();
|
|
267 |
other.interpreter.setAccuracy(this.accuracy);
|
|
268 |
other.interpreter.setSymbolTable(other.symbolTable);
|
|
269 |
}
|
|
270 |
return other;
|
|
271 |
}
|
|
272 |
|
|
273 |
}
|