svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.fmap.dal / org.gvsig.fmap.dal.impl / src / main / java / org / gvsig / fmap / dal / impl / expressionevaluator / DefaultFeatureExpressionEvaluator.java @ 46086
History | View | Annotate | Download (9.45 KB)
1 |
package org.gvsig.fmap.dal.impl.expressionevaluator; |
---|---|
2 |
|
3 |
import java.util.ArrayList; |
4 |
import java.util.HashSet; |
5 |
import java.util.List; |
6 |
import java.util.Objects; |
7 |
import java.util.Set; |
8 |
import org.apache.commons.lang3.StringUtils; |
9 |
import org.gvsig.expressionevaluator.Code; |
10 |
import org.gvsig.expressionevaluator.Code.Callable; |
11 |
import org.gvsig.expressionevaluator.SymbolTable; |
12 |
import org.gvsig.expressionevaluator.Expression; |
13 |
import org.gvsig.expressionevaluator.ExpressionBuilder; |
14 |
import org.gvsig.expressionevaluator.ExpressionEvaluatorLocator; |
15 |
import org.gvsig.expressionevaluator.ExpressionEvaluatorManager; |
16 |
import org.gvsig.expressionevaluator.Function; |
17 |
import org.gvsig.expressionevaluator.impl.symboltable.FeatureSymbolTableImpl; |
18 |
import org.gvsig.expressionevaluator.ExpressionEvaluator; |
19 |
import org.gvsig.expressionevaluator.impl.DefaultExpression; |
20 |
import org.gvsig.fmap.dal.expressionevaluator.FeatureSymbolTable; |
21 |
import org.gvsig.fmap.dal.feature.Feature; |
22 |
import org.gvsig.tools.ToolsLocator; |
23 |
import org.gvsig.tools.dynobject.DynStruct; |
24 |
import org.gvsig.tools.evaluator.AbstractEvaluator; |
25 |
import org.gvsig.tools.evaluator.Evaluator; |
26 |
import org.gvsig.tools.evaluator.EvaluatorData; |
27 |
import org.gvsig.tools.evaluator.EvaluatorException; |
28 |
import org.gvsig.tools.evaluator.EvaluatorFieldsInfo; |
29 |
import org.gvsig.tools.exception.BaseException; |
30 |
import org.gvsig.tools.lang.Cloneable; |
31 |
import org.gvsig.tools.persistence.PersistenceManager; |
32 |
import org.gvsig.tools.persistence.PersistentState; |
33 |
import org.gvsig.tools.persistence.exception.PersistenceException; |
34 |
import org.slf4j.Logger; |
35 |
import org.slf4j.LoggerFactory; |
36 |
|
37 |
@SuppressWarnings("UseSpecificCatch") |
38 |
public class DefaultFeatureExpressionEvaluator |
39 |
extends AbstractEvaluator
|
40 |
implements ExpressionEvaluator, Cloneable { |
41 |
|
42 |
private static final Logger LOGGER = LoggerFactory.getLogger(DefaultFeatureExpressionEvaluator.class); |
43 |
private String storeName; |
44 |
|
45 |
private static class DescriptionAdapter implements Description { |
46 |
|
47 |
Function function; |
48 |
|
49 |
public DescriptionAdapter(Function function) {
|
50 |
this.function = function;
|
51 |
} |
52 |
|
53 |
@Override
|
54 |
public String getName() { |
55 |
return this.function.name(); |
56 |
} |
57 |
|
58 |
@Override
|
59 |
public String getDescription() { |
60 |
return this.function.description(); |
61 |
} |
62 |
|
63 |
@Override
|
64 |
public String getTemplate() { |
65 |
return this.function.template(); |
66 |
} |
67 |
|
68 |
@Override
|
69 |
public int getDataTypeCategories() { |
70 |
switch (this.function.group()) { |
71 |
case Function.GROUP_STRING:
|
72 |
return Description.DATATYPE_CATEGORY_STRING;
|
73 |
case Function.GROUP_BOOLEAN:
|
74 |
return Description.DATATYPE_CATEGORY_BOOLEAN;
|
75 |
case Function.GROUP_DATETIME:
|
76 |
return Description.DATATYPE_CATEGORY_DATETIME;
|
77 |
case Function.GROUP_NUMERIC:
|
78 |
return Description.DATATYPE_CATEGORY_NUMBER;
|
79 |
case Function.GROUP_OGC:
|
80 |
return Description.DATATYPE_CATEGORY_ALL;
|
81 |
default:
|
82 |
return Description.DATATYPE_CATEGORY_ALL;
|
83 |
} |
84 |
} |
85 |
|
86 |
} |
87 |
|
88 |
private FeatureSymbolTable featureSymbolTable;
|
89 |
private SymbolTable symbolTable;
|
90 |
private Expression expression; |
91 |
private Description[] availableOperators; |
92 |
private Description[] availableFunctions; |
93 |
|
94 |
public DefaultFeatureExpressionEvaluator() {
|
95 |
ExpressionEvaluatorManager manager = ExpressionEvaluatorLocator.getManager(); |
96 |
this.symbolTable = manager.createSymbolTable();
|
97 |
this.featureSymbolTable = new FeatureSymbolTableImpl(); |
98 |
this.symbolTable.addSymbolTable(this.featureSymbolTable); |
99 |
this.availableOperators = null; |
100 |
this.availableFunctions = null; |
101 |
} |
102 |
|
103 |
public DefaultFeatureExpressionEvaluator(String source) { |
104 |
this(null, source); |
105 |
} |
106 |
|
107 |
public DefaultFeatureExpressionEvaluator(String storeName, String source) { |
108 |
this();
|
109 |
ExpressionEvaluatorManager manager = ExpressionEvaluatorLocator.getManager(); |
110 |
this.expression = manager.createExpression();
|
111 |
this.expression.setPhrase(source);
|
112 |
this.storeName = storeName;
|
113 |
} |
114 |
|
115 |
public DefaultFeatureExpressionEvaluator(Expression expression) { |
116 |
this(null, expression); |
117 |
} |
118 |
|
119 |
public DefaultFeatureExpressionEvaluator(String storeName, Expression expression) { |
120 |
this();
|
121 |
this.expression = expression;
|
122 |
this.storeName = storeName;
|
123 |
|
124 |
} |
125 |
|
126 |
@Override
|
127 |
public String getName() { |
128 |
return "Genereric expression"; |
129 |
} |
130 |
|
131 |
@Override
|
132 |
public String getDescription() { |
133 |
return this.expression.getPhrase(); |
134 |
} |
135 |
|
136 |
@Override
|
137 |
public String getSQL() { |
138 |
return this.expression.getPhrase(); |
139 |
} |
140 |
|
141 |
@Override
|
142 |
public Expression toExpression() { |
143 |
return expression;
|
144 |
} |
145 |
|
146 |
@Override
|
147 |
public SymbolTable toSymbolTable() {
|
148 |
return this.symbolTable; |
149 |
} |
150 |
|
151 |
@Override
|
152 |
public Object evaluate(EvaluatorData data) throws EvaluatorException { |
153 |
this.featureSymbolTable.setFeature((Feature) data);
|
154 |
Object v = this.expression.execute(this.symbolTable); |
155 |
return v;
|
156 |
} |
157 |
|
158 |
@Override
|
159 |
public EvaluatorFieldsInfo getFieldsInfo() {
|
160 |
final Set<String> names = new HashSet<>(); |
161 |
try {
|
162 |
this.expression.getCode().accept((Object code) -> { |
163 |
if (code instanceof Code.Identifier) { |
164 |
Code.Identifier identifier = (Code.Identifier) code; |
165 |
if (!StringUtils.equalsIgnoreCase(storeName, identifier.name())) {
|
166 |
names.add(identifier.name()); |
167 |
} |
168 |
} else if (code instanceof Code.Callable) { |
169 |
Callable callable = (Callable) code; |
170 |
if (callable.name().equals(ExpressionBuilder.FUNCTION_GETATTR)) {
|
171 |
Code p0 = callable.parameters().get(0);
|
172 |
if (!(p0 instanceof Code.Identifier)) { |
173 |
return;
|
174 |
} |
175 |
if (!StringUtils.equalsIgnoreCase(storeName, ((Code.Identifier) p0).name())) {
|
176 |
return;
|
177 |
} |
178 |
Code p1 = callable.parameters().get(1);
|
179 |
if (!(p1 instanceof Code.Constant)) { |
180 |
return;
|
181 |
} |
182 |
String s = Objects.toString(((Code.Constant) p1).value(), null); |
183 |
if (s != null) { |
184 |
names.add(s); |
185 |
} |
186 |
} |
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 |
DefaultFeatureExpressionEvaluator other = (DefaultFeatureExpressionEvaluator) super.clone();
|
230 |
other.expression = this.expression.clone();
|
231 |
other.availableFunctions = null;
|
232 |
other.availableOperators = null;
|
233 |
|
234 |
if (this.featureSymbolTable != null) { |
235 |
other.featureSymbolTable = this.featureSymbolTable.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 ps) throws PersistenceException { |
245 |
ps.set("expression", expression);
|
246 |
} |
247 |
|
248 |
@Override
|
249 |
public void loadFromState(PersistentState state) throws PersistenceException { |
250 |
this.expression = (Expression) state.get("expression"); |
251 |
} |
252 |
|
253 |
public static void registerPersistence() { |
254 |
PersistenceManager manager = ToolsLocator.getPersistenceManager(); |
255 |
if (manager.getDefinition("DefaultFeatureExpressionEvaluator") == null) { |
256 |
DynStruct definition = manager.addDefinition(DefaultFeatureExpressionEvaluator.class, |
257 |
"DefaultFeatureExpressionEvaluator", "DefaultFeatureExpressionEvaluator persistence definition", null, null); |
258 |
definition.addDynFieldObject("expression").setClassOfValue(DefaultExpression.class).setMandatory(false); |
259 |
} |
260 |
} |
261 |
|
262 |
@Override
|
263 |
public void addSymbolTable(SymbolTable symbolTable) { |
264 |
if (symbolTable != null) { |
265 |
this.symbolTable.addSymbolTable(symbolTable);
|
266 |
} |
267 |
} |
268 |
|
269 |
} |