svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.library / org.gvsig.expressionevaluator / org.gvsig.expressionevaluator.lib / org.gvsig.expressionevaluator.lib.api / src / main / java / org / gvsig / expressionevaluator / spi / BaseExpressionEvaluator.java @ 46010
History | View | Annotate | Download (8.92 KB)
1 |
package org.gvsig.expressionevaluator.spi; |
---|---|
2 |
|
3 |
import java.util.ArrayList; |
4 |
import java.util.Collection; |
5 |
import java.util.HashSet; |
6 |
import java.util.List; |
7 |
import java.util.Set; |
8 |
import org.gvsig.expressionevaluator.Code; |
9 |
import org.gvsig.expressionevaluator.Expression; |
10 |
import org.gvsig.expressionevaluator.ExpressionEvaluator; |
11 |
import org.gvsig.expressionevaluator.ExpressionEvaluatorLocator; |
12 |
import org.gvsig.expressionevaluator.ExpressionEvaluatorManager; |
13 |
import org.gvsig.expressionevaluator.Function; |
14 |
import org.gvsig.expressionevaluator.SupportToExpression; |
15 |
import org.gvsig.expressionevaluator.SupportToSymbolTable; |
16 |
import org.gvsig.expressionevaluator.SymbolTable; |
17 |
import org.gvsig.tools.ToolsLocator; |
18 |
import org.gvsig.tools.dynobject.DynStruct; |
19 |
import org.gvsig.tools.evaluator.AbstractEvaluator; |
20 |
import org.gvsig.tools.evaluator.Evaluator; |
21 |
import org.gvsig.tools.evaluator.EvaluatorData; |
22 |
import org.gvsig.tools.evaluator.EvaluatorException; |
23 |
import org.gvsig.tools.evaluator.EvaluatorFieldsInfo; |
24 |
import org.gvsig.tools.exception.BaseException; |
25 |
import org.gvsig.tools.lang.Cloneable; |
26 |
import org.gvsig.tools.persistence.PersistenceManager; |
27 |
import org.gvsig.tools.persistence.PersistentState; |
28 |
import org.gvsig.tools.persistence.exception.PersistenceException; |
29 |
import org.gvsig.tools.util.ListBuilder; |
30 |
import org.slf4j.Logger; |
31 |
import org.slf4j.LoggerFactory; |
32 |
|
33 |
@SuppressWarnings("UseSpecificCatch") |
34 |
public class BaseExpressionEvaluator |
35 |
extends AbstractEvaluator
|
36 |
implements ExpressionEvaluator, Cloneable, SupportToExpression, SupportToSymbolTable |
37 |
{ |
38 |
|
39 |
private static final Logger LOGGER = LoggerFactory.getLogger(BaseExpressionEvaluator.class); |
40 |
|
41 |
private static class DescriptionAdapter implements Description { |
42 |
|
43 |
Function function; |
44 |
|
45 |
public DescriptionAdapter(Function function) {
|
46 |
this.function = function;
|
47 |
} |
48 |
|
49 |
@Override
|
50 |
public String getName() { |
51 |
return this.function.name(); |
52 |
} |
53 |
|
54 |
@Override
|
55 |
public String getDescription() { |
56 |
return this.function.description(); |
57 |
} |
58 |
|
59 |
@Override
|
60 |
public String getTemplate() { |
61 |
return this.function.template(); |
62 |
} |
63 |
|
64 |
@Override
|
65 |
public int getDataTypeCategories() { |
66 |
switch (this.function.group()) { |
67 |
case Function.GROUP_STRING:
|
68 |
return Description.DATATYPE_CATEGORY_STRING;
|
69 |
case Function.GROUP_BOOLEAN:
|
70 |
return Description.DATATYPE_CATEGORY_BOOLEAN;
|
71 |
case Function.GROUP_DATETIME:
|
72 |
return Description.DATATYPE_CATEGORY_DATETIME;
|
73 |
case Function.GROUP_NUMERIC:
|
74 |
return Description.DATATYPE_CATEGORY_NUMBER;
|
75 |
case Function.GROUP_OGC:
|
76 |
return Description.DATATYPE_CATEGORY_ALL;
|
77 |
default:
|
78 |
return Description.DATATYPE_CATEGORY_ALL;
|
79 |
} |
80 |
} |
81 |
|
82 |
} |
83 |
|
84 |
private static class DataSymbolTable extends AbstractSymbolTable { |
85 |
|
86 |
EvaluatorData data; |
87 |
|
88 |
public DataSymbolTable() {
|
89 |
super();
|
90 |
} |
91 |
|
92 |
public void setData(EvaluatorData data) { |
93 |
this.data = data;
|
94 |
} |
95 |
|
96 |
@Override
|
97 |
public boolean exists(String name) { |
98 |
return data.hasDataValue(name);
|
99 |
} |
100 |
|
101 |
@Override
|
102 |
public Object value(String name) { |
103 |
return data.getDataValue(name);
|
104 |
} |
105 |
|
106 |
@Override
|
107 |
public Collection<String> localvariables() { |
108 |
List<String> names = ListBuilder.create(data.getDataNames()); |
109 |
return names;
|
110 |
} |
111 |
|
112 |
@Override
|
113 |
public DataSymbolTable clone() throws CloneNotSupportedException { |
114 |
SymbolTable other = super.clone();
|
115 |
return (DataSymbolTable) other;
|
116 |
} |
117 |
|
118 |
} |
119 |
|
120 |
private DataSymbolTable dataSymbolTable;
|
121 |
private SymbolTable symbolTable;
|
122 |
private Expression expression; |
123 |
private Description[] availableOperators; |
124 |
private Description[] availableFunctions; |
125 |
|
126 |
public BaseExpressionEvaluator(String source) { |
127 |
ExpressionEvaluatorManager manager = ExpressionEvaluatorLocator.getManager(); |
128 |
this.symbolTable = manager.createSymbolTable();
|
129 |
this.dataSymbolTable = new DataSymbolTable(); |
130 |
this.symbolTable.addSymbolTable(this.dataSymbolTable); |
131 |
|
132 |
this.expression = manager.createExpression();
|
133 |
this.expression.setPhrase(source);
|
134 |
} |
135 |
|
136 |
public BaseExpressionEvaluator(Expression expression) { |
137 |
ExpressionEvaluatorManager manager = ExpressionEvaluatorLocator.getManager(); |
138 |
this.symbolTable = manager.createSymbolTable();
|
139 |
this.dataSymbolTable = new DataSymbolTable(); |
140 |
this.symbolTable.addSymbolTable(this.dataSymbolTable); |
141 |
|
142 |
this.expression = expression;
|
143 |
} |
144 |
|
145 |
@Override
|
146 |
public String getName() { |
147 |
return "Genereric expression"; |
148 |
} |
149 |
|
150 |
@Override
|
151 |
public String getDescription() { |
152 |
return this.expression.getPhrase(); |
153 |
} |
154 |
|
155 |
@Override
|
156 |
public String getSQL() { |
157 |
return this.expression.getPhrase(); |
158 |
} |
159 |
|
160 |
@Override
|
161 |
public Expression toExpression() { |
162 |
return expression;
|
163 |
} |
164 |
|
165 |
@Override
|
166 |
public SymbolTable toSymbolTable() {
|
167 |
return this.symbolTable; |
168 |
} |
169 |
|
170 |
@Override
|
171 |
public Object evaluate(EvaluatorData data) throws EvaluatorException { |
172 |
this.dataSymbolTable.setData(data);
|
173 |
Object v = this.expression.execute(this.symbolTable); |
174 |
return v;
|
175 |
} |
176 |
|
177 |
@Override
|
178 |
public EvaluatorFieldsInfo getFieldsInfo() {
|
179 |
final Set<String> names = new HashSet<>(); |
180 |
try {
|
181 |
this.expression.getCode().accept((Object code) -> { |
182 |
if (code instanceof Code.Identifier) { |
183 |
Code.Identifier identifier = (Code.Identifier) code; |
184 |
names.add(identifier.name()); |
185 |
} |
186 |
}); |
187 |
EvaluatorFieldsInfo info = new EvaluatorFieldsInfo();
|
188 |
for (String name : names) { |
189 |
info.addFieldValue(name); |
190 |
} |
191 |
return info;
|
192 |
} catch (BaseException ex) {
|
193 |
throw new RuntimeException("Can't calculate fields information.", ex); |
194 |
} |
195 |
} |
196 |
|
197 |
@Override
|
198 |
public Description[] getAvailableOperators() { |
199 |
if (availableOperators == null) { |
200 |
List<Description> l = new ArrayList<>(); |
201 |
for (Function function : toSymbolTable()) {
|
202 |
if (function.isOperator()) {
|
203 |
l.add(new DescriptionAdapter(function));
|
204 |
} |
205 |
} |
206 |
this.availableOperators = l.toArray(new Description[l.size()]); |
207 |
} |
208 |
return availableOperators;
|
209 |
} |
210 |
|
211 |
@Override
|
212 |
public Description[] getAvailableFunctions() { |
213 |
if (availableFunctions == null) { |
214 |
List<Description> l = new ArrayList<>(); |
215 |
for (Function function : toSymbolTable()) {
|
216 |
if (!function.isOperator()) {
|
217 |
l.add(new DescriptionAdapter(function));
|
218 |
} |
219 |
} |
220 |
this.availableFunctions = l.toArray(new Description[l.size()]); |
221 |
} |
222 |
return availableFunctions;
|
223 |
} |
224 |
|
225 |
@Override
|
226 |
public Evaluator clone() throws CloneNotSupportedException { |
227 |
BaseExpressionEvaluator other = (BaseExpressionEvaluator) super.clone();
|
228 |
other.expression = this.expression.clone();
|
229 |
other.availableFunctions = null;
|
230 |
other.availableOperators = null;
|
231 |
|
232 |
if (this.dataSymbolTable != null) { |
233 |
other.dataSymbolTable = this.dataSymbolTable.clone();
|
234 |
} |
235 |
if (this.symbolTable != null) { |
236 |
other.symbolTable = this.symbolTable.clone();
|
237 |
} |
238 |
return other;
|
239 |
} |
240 |
|
241 |
@Override
|
242 |
public void saveToState(PersistentState state) throws PersistenceException { |
243 |
|
244 |
state.set("expression", expression);
|
245 |
} |
246 |
|
247 |
@Override
|
248 |
public void loadFromState(PersistentState state) throws PersistenceException { |
249 |
expression = (Expression) state.get("expression"); |
250 |
availableFunctions = null;
|
251 |
availableOperators = null;
|
252 |
|
253 |
ExpressionEvaluatorManager manager = ExpressionEvaluatorLocator.getManager(); |
254 |
this.symbolTable = manager.createSymbolTable();
|
255 |
this.dataSymbolTable = new DataSymbolTable(); |
256 |
this.symbolTable.addSymbolTable(this.dataSymbolTable); |
257 |
|
258 |
} |
259 |
|
260 |
|
261 |
public static void registerPersistence() { |
262 |
PersistenceManager manager = ToolsLocator.getPersistenceManager(); |
263 |
if (manager.getDefinition("DefaultExpressionEvaluator") == null) { |
264 |
DynStruct definition = manager.addDefinition(BaseExpressionEvaluator.class, |
265 |
"DefaultExpressionEvaluator", "DefaultExpressionEvaluator persistence definition", null, null); |
266 |
definition.addDynFieldObject("expression").setClassOfValue(Expression.class).setMandatory(false); |
267 |
} |
268 |
} |
269 |
|
270 |
@Override
|
271 |
public void addSymbolTable(SymbolTable symbolTable) { |
272 |
this.symbolTable.addSymbolTable(symbolTable);
|
273 |
} |
274 |
|
275 |
} |