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 | 43983 | jjdelcerro | package org.gvsig.fmap.dal.impl.expressionevaluator; |
---|---|---|---|
2 | 43521 | jjdelcerro | |
3 | import java.util.ArrayList; |
||
4 | 43809 | jjdelcerro | import java.util.HashSet; |
5 | 43521 | jjdelcerro | import java.util.List; |
6 | 46078 | omartinez | import java.util.Objects; |
7 | 43809 | jjdelcerro | import java.util.Set; |
8 | 46078 | omartinez | import org.apache.commons.lang3.StringUtils; |
9 | 43521 | jjdelcerro | import org.gvsig.expressionevaluator.Code; |
10 | 46078 | omartinez | import org.gvsig.expressionevaluator.Code.Callable; |
11 | 43521 | jjdelcerro | import org.gvsig.expressionevaluator.SymbolTable; |
12 | 43984 | jjdelcerro | import org.gvsig.expressionevaluator.Expression; |
13 | 46078 | omartinez | import org.gvsig.expressionevaluator.ExpressionBuilder; |
14 | 43521 | jjdelcerro | import org.gvsig.expressionevaluator.ExpressionEvaluatorLocator; |
15 | 43983 | jjdelcerro | import org.gvsig.expressionevaluator.ExpressionEvaluatorManager; |
16 | 43521 | jjdelcerro | import org.gvsig.expressionevaluator.Function; |
17 | 44738 | jjdelcerro | import org.gvsig.expressionevaluator.impl.symboltable.FeatureSymbolTableImpl; |
18 | 45366 | omartinez | import org.gvsig.expressionevaluator.ExpressionEvaluator; |
19 | import org.gvsig.expressionevaluator.impl.DefaultExpression; |
||
20 | 43983 | jjdelcerro | import org.gvsig.fmap.dal.expressionevaluator.FeatureSymbolTable; |
21 | import org.gvsig.fmap.dal.feature.Feature; |
||
22 | 45366 | omartinez | import org.gvsig.tools.ToolsLocator; |
23 | import org.gvsig.tools.dynobject.DynStruct; |
||
24 | 43521 | jjdelcerro | import org.gvsig.tools.evaluator.AbstractEvaluator; |
25 | 43983 | jjdelcerro | import org.gvsig.tools.evaluator.Evaluator; |
26 | 43521 | jjdelcerro | import org.gvsig.tools.evaluator.EvaluatorData; |
27 | import org.gvsig.tools.evaluator.EvaluatorException; |
||
28 | 43809 | jjdelcerro | import org.gvsig.tools.evaluator.EvaluatorFieldsInfo; |
29 | import org.gvsig.tools.exception.BaseException; |
||
30 | import org.gvsig.tools.lang.Cloneable; |
||
31 | 45366 | omartinez | import org.gvsig.tools.persistence.PersistenceManager; |
32 | import org.gvsig.tools.persistence.PersistentState; |
||
33 | import org.gvsig.tools.persistence.exception.PersistenceException; |
||
34 | 44191 | jjdelcerro | import org.slf4j.Logger; |
35 | import org.slf4j.LoggerFactory; |
||
36 | 43521 | jjdelcerro | |
37 | 44191 | jjdelcerro | @SuppressWarnings("UseSpecificCatch") |
38 | 46086 | jjdelcerro | public class DefaultFeatureExpressionEvaluator |
39 | extends AbstractEvaluator
|
||
40 | implements ExpressionEvaluator, Cloneable { |
||
41 | 43521 | jjdelcerro | |
42 | 45366 | omartinez | private static final Logger LOGGER = LoggerFactory.getLogger(DefaultFeatureExpressionEvaluator.class); |
43 | 46086 | jjdelcerro | private String storeName; |
44 | 45366 | omartinez | |
45 | 43521 | jjdelcerro | private static class DescriptionAdapter implements Description { |
46 | |||
47 | Function function; |
||
48 | 43983 | jjdelcerro | |
49 | 43521 | jjdelcerro | public DescriptionAdapter(Function function) {
|
50 | this.function = function;
|
||
51 | } |
||
52 | 43983 | jjdelcerro | |
53 | 43521 | jjdelcerro | @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 | 43983 | jjdelcerro | 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 | 43521 | jjdelcerro | } |
84 | } |
||
85 | 43983 | jjdelcerro | |
86 | } |
||
87 | |||
88 | private FeatureSymbolTable featureSymbolTable;
|
||
89 | 44009 | jjdelcerro | private SymbolTable symbolTable;
|
90 | 43984 | jjdelcerro | private Expression expression; |
91 | 43521 | jjdelcerro | private Description[] availableOperators; |
92 | private Description[] availableFunctions; |
||
93 | 46086 | jjdelcerro | |
94 | 45366 | omartinez | public DefaultFeatureExpressionEvaluator() {
|
95 | 44009 | jjdelcerro | ExpressionEvaluatorManager manager = ExpressionEvaluatorLocator.getManager(); |
96 | this.symbolTable = manager.createSymbolTable();
|
||
97 | 44738 | jjdelcerro | this.featureSymbolTable = new FeatureSymbolTableImpl(); |
98 | 44009 | jjdelcerro | this.symbolTable.addSymbolTable(this.featureSymbolTable); |
99 | 45366 | omartinez | this.availableOperators = null; |
100 | this.availableFunctions = null; |
||
101 | } |
||
102 | |||
103 | public DefaultFeatureExpressionEvaluator(String source) { |
||
104 | 46086 | jjdelcerro | this(null, source); |
105 | } |
||
106 | |||
107 | 46078 | omartinez | public DefaultFeatureExpressionEvaluator(String storeName, String source) { |
108 | 45366 | omartinez | this();
|
109 | ExpressionEvaluatorManager manager = ExpressionEvaluatorLocator.getManager(); |
||
110 | 44009 | jjdelcerro | this.expression = manager.createExpression();
|
111 | 43984 | jjdelcerro | this.expression.setPhrase(source);
|
112 | 46086 | jjdelcerro | this.storeName = storeName;
|
113 | 43521 | jjdelcerro | } |
114 | |||
115 | 45366 | omartinez | public DefaultFeatureExpressionEvaluator(Expression expression) { |
116 | 46086 | jjdelcerro | this(null, expression); |
117 | 46078 | omartinez | } |
118 | 46086 | jjdelcerro | |
119 | 46078 | omartinez | public DefaultFeatureExpressionEvaluator(String storeName, Expression expression) { |
120 | 45366 | omartinez | this();
|
121 | 44750 | jjdelcerro | this.expression = expression;
|
122 | 46086 | jjdelcerro | this.storeName = storeName;
|
123 | 43984 | jjdelcerro | |
124 | 46086 | jjdelcerro | } |
125 | |||
126 | 43521 | jjdelcerro | @Override
|
127 | 44191 | jjdelcerro | 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 | 45366 | omartinez | public Expression toExpression() { |
143 | 44191 | jjdelcerro | return expression;
|
144 | } |
||
145 | |||
146 | @Override
|
||
147 | 45366 | omartinez | public SymbolTable toSymbolTable() {
|
148 | 44191 | jjdelcerro | return this.symbolTable; |
149 | } |
||
150 | 46086 | jjdelcerro | |
151 | 44191 | jjdelcerro | @Override
|
152 | 43521 | jjdelcerro | public Object evaluate(EvaluatorData data) throws EvaluatorException { |
153 | 43983 | jjdelcerro | this.featureSymbolTable.setFeature((Feature) data);
|
154 | 44009 | jjdelcerro | Object v = this.expression.execute(this.symbolTable); |
155 | 43521 | jjdelcerro | return v;
|
156 | } |
||
157 | |||
158 | @Override
|
||
159 | 43809 | jjdelcerro | public EvaluatorFieldsInfo getFieldsInfo() {
|
160 | 43983 | jjdelcerro | final Set<String> names = new HashSet<>(); |
161 | 43809 | jjdelcerro | try {
|
162 | 44738 | jjdelcerro | this.expression.getCode().accept((Object code) -> { |
163 | 46086 | jjdelcerro | 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 | 43809 | jjdelcerro | }); |
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 | 43521 | jjdelcerro | public Description[] getAvailableOperators() { |
201 | 43983 | jjdelcerro | if (availableOperators == null) { |
202 | 43521 | jjdelcerro | List<Description> l = new ArrayList<>(); |
203 | 45366 | omartinez | for (Function function : toSymbolTable()) {
|
204 | 43983 | jjdelcerro | if (function.isOperator()) {
|
205 | 43521 | jjdelcerro | l.add(new DescriptionAdapter(function));
|
206 | } |
||
207 | } |
||
208 | this.availableOperators = l.toArray(new Description[l.size()]); |
||
209 | } |
||
210 | return availableOperators;
|
||
211 | } |
||
212 | 43983 | jjdelcerro | |
213 | 43521 | jjdelcerro | @Override
|
214 | public Description[] getAvailableFunctions() { |
||
215 | 43983 | jjdelcerro | if (availableFunctions == null) { |
216 | 43521 | jjdelcerro | List<Description> l = new ArrayList<>(); |
217 | 45366 | omartinez | for (Function function : toSymbolTable()) {
|
218 | 43983 | jjdelcerro | if (!function.isOperator()) {
|
219 | 43521 | jjdelcerro | 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 | 43983 | jjdelcerro | public Evaluator clone() throws CloneNotSupportedException { |
229 | 45366 | omartinez | DefaultFeatureExpressionEvaluator other = (DefaultFeatureExpressionEvaluator) super.clone();
|
230 | 43984 | jjdelcerro | other.expression = this.expression.clone();
|
231 | 43809 | jjdelcerro | other.availableFunctions = null;
|
232 | other.availableOperators = null;
|
||
233 | |||
234 | 43983 | jjdelcerro | if (this.featureSymbolTable != null) { |
235 | other.featureSymbolTable = this.featureSymbolTable.clone();
|
||
236 | 43809 | jjdelcerro | } |
237 | 44009 | jjdelcerro | if (this.symbolTable != null) { |
238 | other.symbolTable = this.symbolTable.clone();
|
||
239 | 43809 | jjdelcerro | } |
240 | return other;
|
||
241 | } |
||
242 | 46086 | jjdelcerro | |
243 | 45366 | omartinez | @Override
|
244 | public void saveToState(PersistentState ps) throws PersistenceException { |
||
245 | ps.set("expression", expression);
|
||
246 | } |
||
247 | 43809 | jjdelcerro | |
248 | 45366 | omartinez | @Override
|
249 | public void loadFromState(PersistentState state) throws PersistenceException { |
||
250 | this.expression = (Expression) state.get("expression"); |
||
251 | } |
||
252 | 46086 | jjdelcerro | |
253 | 45366 | omartinez | 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 | 46010 | jjdelcerro | @Override
|
263 | public void addSymbolTable(SymbolTable symbolTable) { |
||
264 | 46086 | jjdelcerro | if (symbolTable != null) { |
265 | 46010 | jjdelcerro | this.symbolTable.addSymbolTable(symbolTable);
|
266 | } |
||
267 | } |
||
268 | |||
269 | 43521 | jjdelcerro | } |