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 @ 46078
History | View | Annotate | Download (9.08 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 |
|
43 |
private static final Logger LOGGER = LoggerFactory.getLogger(DefaultFeatureExpressionEvaluator.class); |
44 |
private String storeName; |
45 |
|
46 |
private static class DescriptionAdapter implements Description { |
47 |
|
48 |
Function function; |
49 |
|
50 |
public DescriptionAdapter(Function function) {
|
51 |
this.function = function;
|
52 |
} |
53 |
|
54 |
@Override
|
55 |
public String getName() { |
56 |
return this.function.name(); |
57 |
} |
58 |
|
59 |
@Override
|
60 |
public String getDescription() { |
61 |
return this.function.description(); |
62 |
} |
63 |
|
64 |
@Override
|
65 |
public String getTemplate() { |
66 |
return this.function.template(); |
67 |
} |
68 |
|
69 |
@Override
|
70 |
public int getDataTypeCategories() { |
71 |
switch (this.function.group()) { |
72 |
case Function.GROUP_STRING:
|
73 |
return Description.DATATYPE_CATEGORY_STRING;
|
74 |
case Function.GROUP_BOOLEAN:
|
75 |
return Description.DATATYPE_CATEGORY_BOOLEAN;
|
76 |
case Function.GROUP_DATETIME:
|
77 |
return Description.DATATYPE_CATEGORY_DATETIME;
|
78 |
case Function.GROUP_NUMERIC:
|
79 |
return Description.DATATYPE_CATEGORY_NUMBER;
|
80 |
case Function.GROUP_OGC:
|
81 |
return Description.DATATYPE_CATEGORY_ALL;
|
82 |
default:
|
83 |
return Description.DATATYPE_CATEGORY_ALL;
|
84 |
} |
85 |
} |
86 |
|
87 |
} |
88 |
|
89 |
private FeatureSymbolTable featureSymbolTable;
|
90 |
private SymbolTable symbolTable;
|
91 |
private Expression expression; |
92 |
private Description[] availableOperators; |
93 |
private Description[] availableFunctions; |
94 |
|
95 |
public DefaultFeatureExpressionEvaluator() {
|
96 |
ExpressionEvaluatorManager manager = ExpressionEvaluatorLocator.getManager(); |
97 |
this.symbolTable = manager.createSymbolTable();
|
98 |
this.featureSymbolTable = new FeatureSymbolTableImpl(); |
99 |
this.symbolTable.addSymbolTable(this.featureSymbolTable); |
100 |
this.availableOperators = null; |
101 |
this.availableFunctions = null; |
102 |
} |
103 |
|
104 |
public DefaultFeatureExpressionEvaluator(String source) { |
105 |
this(null, source); |
106 |
} |
107 |
|
108 |
public DefaultFeatureExpressionEvaluator(String storeName, String source) { |
109 |
this();
|
110 |
ExpressionEvaluatorManager manager = ExpressionEvaluatorLocator.getManager(); |
111 |
this.expression = manager.createExpression();
|
112 |
this.expression.setPhrase(source);
|
113 |
this.storeName = storeName;
|
114 |
} |
115 |
|
116 |
public DefaultFeatureExpressionEvaluator(Expression expression) { |
117 |
this(null, expression); |
118 |
} |
119 |
|
120 |
public DefaultFeatureExpressionEvaluator(String storeName, Expression expression) { |
121 |
this();
|
122 |
this.expression = expression;
|
123 |
this.storeName = storeName;
|
124 |
|
125 |
} |
126 |
|
127 |
@Override
|
128 |
public String getName() { |
129 |
return "Genereric expression"; |
130 |
} |
131 |
|
132 |
@Override
|
133 |
public String getDescription() { |
134 |
return this.expression.getPhrase(); |
135 |
} |
136 |
|
137 |
@Override
|
138 |
public String getSQL() { |
139 |
return this.expression.getPhrase(); |
140 |
} |
141 |
|
142 |
@Override
|
143 |
public Expression toExpression() { |
144 |
return expression;
|
145 |
} |
146 |
|
147 |
@Override
|
148 |
public SymbolTable toSymbolTable() {
|
149 |
return this.symbolTable; |
150 |
} |
151 |
|
152 |
@Override
|
153 |
public Object evaluate(EvaluatorData data) throws EvaluatorException { |
154 |
this.featureSymbolTable.setFeature((Feature) data);
|
155 |
Object v = this.expression.execute(this.symbolTable); |
156 |
return v;
|
157 |
} |
158 |
|
159 |
@Override
|
160 |
public EvaluatorFieldsInfo getFieldsInfo() {
|
161 |
final Set<String> names = new HashSet<>(); |
162 |
try {
|
163 |
this.expression.getCode().accept((Object code) -> { |
164 |
if (code instanceof Code.Identifier) { |
165 |
Code.Identifier identifier = (Code.Identifier) code; |
166 |
if(!StringUtils.equalsIgnoreCase(storeName, identifier.name())) {
|
167 |
names.add(identifier.name()); |
168 |
} |
169 |
} else if (code instanceof Code.Callable) { |
170 |
Callable callable = (Callable) code; |
171 |
if (callable.name().equals(ExpressionBuilder.FUNCTION_GETATTR)) {
|
172 |
Code p0 = callable.parameters().get(0);
|
173 |
if (!(p0 instanceof Code.Identifier)) { |
174 |
return;
|
175 |
} |
176 |
if (!StringUtils.equalsIgnoreCase(storeName, ((Code.Identifier) p0).name())) {
|
177 |
return;
|
178 |
} |
179 |
Code p1 = callable.parameters().get(1);
|
180 |
if (!(p1 instanceof Code.Constant)) { |
181 |
return;
|
182 |
} |
183 |
String s = Objects.toString(((Code.Constant)p1).value(), null); |
184 |
if (s != null) { |
185 |
names.add(s); |
186 |
} |
187 |
} |
188 |
} |
189 |
}); |
190 |
EvaluatorFieldsInfo info = new EvaluatorFieldsInfo();
|
191 |
for (String name : names) { |
192 |
info.addFieldValue(name); |
193 |
} |
194 |
return info;
|
195 |
} catch (BaseException ex) {
|
196 |
throw new RuntimeException("Can't calculate fields information.", ex); |
197 |
} |
198 |
} |
199 |
|
200 |
@Override
|
201 |
public Description[] getAvailableOperators() { |
202 |
if (availableOperators == null) { |
203 |
List<Description> l = new ArrayList<>(); |
204 |
for (Function function : toSymbolTable()) {
|
205 |
if (function.isOperator()) {
|
206 |
l.add(new DescriptionAdapter(function));
|
207 |
} |
208 |
} |
209 |
this.availableOperators = l.toArray(new Description[l.size()]); |
210 |
} |
211 |
return availableOperators;
|
212 |
} |
213 |
|
214 |
@Override
|
215 |
public Description[] getAvailableFunctions() { |
216 |
if (availableFunctions == null) { |
217 |
List<Description> l = new ArrayList<>(); |
218 |
for (Function function : toSymbolTable()) {
|
219 |
if (!function.isOperator()) {
|
220 |
l.add(new DescriptionAdapter(function));
|
221 |
} |
222 |
} |
223 |
this.availableFunctions = l.toArray(new Description[l.size()]); |
224 |
} |
225 |
return availableFunctions;
|
226 |
} |
227 |
|
228 |
@Override
|
229 |
public Evaluator clone() throws CloneNotSupportedException { |
230 |
DefaultFeatureExpressionEvaluator other = (DefaultFeatureExpressionEvaluator) super.clone();
|
231 |
other.expression = this.expression.clone();
|
232 |
other.availableFunctions = null;
|
233 |
other.availableOperators = null;
|
234 |
|
235 |
if (this.featureSymbolTable != null) { |
236 |
other.featureSymbolTable = this.featureSymbolTable.clone();
|
237 |
} |
238 |
if (this.symbolTable != null) { |
239 |
other.symbolTable = this.symbolTable.clone();
|
240 |
} |
241 |
return other;
|
242 |
} |
243 |
|
244 |
@Override
|
245 |
public void saveToState(PersistentState ps) throws PersistenceException { |
246 |
ps.set("expression", expression);
|
247 |
} |
248 |
|
249 |
@Override
|
250 |
public void loadFromState(PersistentState state) throws PersistenceException { |
251 |
this.expression = (Expression) state.get("expression"); |
252 |
} |
253 |
|
254 |
public static void registerPersistence() { |
255 |
PersistenceManager manager = ToolsLocator.getPersistenceManager(); |
256 |
if (manager.getDefinition("DefaultFeatureExpressionEvaluator") == null) { |
257 |
DynStruct definition = manager.addDefinition(DefaultFeatureExpressionEvaluator.class, |
258 |
"DefaultFeatureExpressionEvaluator", "DefaultFeatureExpressionEvaluator persistence definition", null, null); |
259 |
definition.addDynFieldObject("expression").setClassOfValue(DefaultExpression.class).setMandatory(false); |
260 |
} |
261 |
} |
262 |
|
263 |
@Override
|
264 |
public void addSymbolTable(SymbolTable symbolTable) { |
265 |
if( symbolTable!=null ) { |
266 |
this.symbolTable.addSymbolTable(symbolTable);
|
267 |
} |
268 |
} |
269 |
|
270 |
} |