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