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 | 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 | 45366 | omartinez | public class DefaultFeatureExpressionEvaluator |
39 | 43983 | jjdelcerro | extends AbstractEvaluator
|
40 | 44191 | jjdelcerro | implements ExpressionEvaluator, Cloneable |
41 | 43983 | jjdelcerro | { |
42 | 43521 | jjdelcerro | |
43 | 45366 | omartinez | private static final Logger LOGGER = LoggerFactory.getLogger(DefaultFeatureExpressionEvaluator.class); |
44 | 46078 | omartinez | private String storeName; |
45 | 45366 | omartinez | |
46 | 43521 | jjdelcerro | private static class DescriptionAdapter implements Description { |
47 | |||
48 | Function function; |
||
49 | 43983 | jjdelcerro | |
50 | 43521 | jjdelcerro | public DescriptionAdapter(Function function) {
|
51 | this.function = function;
|
||
52 | } |
||
53 | 43983 | jjdelcerro | |
54 | 43521 | jjdelcerro | @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 | 43983 | jjdelcerro | 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 | 43521 | jjdelcerro | } |
85 | } |
||
86 | 43983 | jjdelcerro | |
87 | } |
||
88 | |||
89 | private FeatureSymbolTable featureSymbolTable;
|
||
90 | 44009 | jjdelcerro | private SymbolTable symbolTable;
|
91 | 43984 | jjdelcerro | private Expression expression; |
92 | 43521 | jjdelcerro | private Description[] availableOperators; |
93 | private Description[] availableFunctions; |
||
94 | 45366 | omartinez | |
95 | public DefaultFeatureExpressionEvaluator() {
|
||
96 | 44009 | jjdelcerro | ExpressionEvaluatorManager manager = ExpressionEvaluatorLocator.getManager(); |
97 | this.symbolTable = manager.createSymbolTable();
|
||
98 | 44738 | jjdelcerro | this.featureSymbolTable = new FeatureSymbolTableImpl(); |
99 | 44009 | jjdelcerro | this.symbolTable.addSymbolTable(this.featureSymbolTable); |
100 | 45366 | omartinez | this.availableOperators = null; |
101 | this.availableFunctions = null; |
||
102 | } |
||
103 | |||
104 | public DefaultFeatureExpressionEvaluator(String source) { |
||
105 | 46078 | omartinez | this(null, source); |
106 | } |
||
107 | |||
108 | public DefaultFeatureExpressionEvaluator(String storeName, String source) { |
||
109 | 45366 | omartinez | this();
|
110 | ExpressionEvaluatorManager manager = ExpressionEvaluatorLocator.getManager(); |
||
111 | 44009 | jjdelcerro | this.expression = manager.createExpression();
|
112 | 43984 | jjdelcerro | this.expression.setPhrase(source);
|
113 | 46078 | omartinez | this.storeName = storeName;
|
114 | 43521 | jjdelcerro | } |
115 | |||
116 | 45366 | omartinez | public DefaultFeatureExpressionEvaluator(Expression expression) { |
117 | 46078 | omartinez | this(null, expression); |
118 | } |
||
119 | |||
120 | public DefaultFeatureExpressionEvaluator(String storeName, Expression expression) { |
||
121 | 45366 | omartinez | this();
|
122 | 44750 | jjdelcerro | this.expression = expression;
|
123 | 46078 | omartinez | this.storeName = storeName;
|
124 | |||
125 | } |
||
126 | 43984 | jjdelcerro | |
127 | 43521 | jjdelcerro | @Override
|
128 | 44191 | jjdelcerro | 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 | 45366 | omartinez | public Expression toExpression() { |
144 | 44191 | jjdelcerro | return expression;
|
145 | } |
||
146 | |||
147 | @Override
|
||
148 | 45366 | omartinez | public SymbolTable toSymbolTable() {
|
149 | 44191 | jjdelcerro | return this.symbolTable; |
150 | } |
||
151 | |||
152 | @Override
|
||
153 | 43521 | jjdelcerro | public Object evaluate(EvaluatorData data) throws EvaluatorException { |
154 | 43983 | jjdelcerro | this.featureSymbolTable.setFeature((Feature) data);
|
155 | 44009 | jjdelcerro | Object v = this.expression.execute(this.symbolTable); |
156 | 43521 | jjdelcerro | return v;
|
157 | } |
||
158 | |||
159 | @Override
|
||
160 | 43809 | jjdelcerro | public EvaluatorFieldsInfo getFieldsInfo() {
|
161 | 43983 | jjdelcerro | final Set<String> names = new HashSet<>(); |
162 | 43809 | jjdelcerro | try {
|
163 | 44738 | jjdelcerro | this.expression.getCode().accept((Object code) -> { |
164 | if (code instanceof Code.Identifier) { |
||
165 | Code.Identifier identifier = (Code.Identifier) code; |
||
166 | 46078 | omartinez | 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 | 43809 | jjdelcerro | }); |
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 | 43521 | jjdelcerro | public Description[] getAvailableOperators() { |
202 | 43983 | jjdelcerro | if (availableOperators == null) { |
203 | 43521 | jjdelcerro | List<Description> l = new ArrayList<>(); |
204 | 45366 | omartinez | for (Function function : toSymbolTable()) {
|
205 | 43983 | jjdelcerro | if (function.isOperator()) {
|
206 | 43521 | jjdelcerro | l.add(new DescriptionAdapter(function));
|
207 | } |
||
208 | } |
||
209 | this.availableOperators = l.toArray(new Description[l.size()]); |
||
210 | } |
||
211 | return availableOperators;
|
||
212 | } |
||
213 | 43983 | jjdelcerro | |
214 | 43521 | jjdelcerro | @Override
|
215 | public Description[] getAvailableFunctions() { |
||
216 | 43983 | jjdelcerro | if (availableFunctions == null) { |
217 | 43521 | jjdelcerro | List<Description> l = new ArrayList<>(); |
218 | 45366 | omartinez | for (Function function : toSymbolTable()) {
|
219 | 43983 | jjdelcerro | if (!function.isOperator()) {
|
220 | 43521 | jjdelcerro | 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 | 43983 | jjdelcerro | public Evaluator clone() throws CloneNotSupportedException { |
230 | 45366 | omartinez | DefaultFeatureExpressionEvaluator other = (DefaultFeatureExpressionEvaluator) super.clone();
|
231 | 43984 | jjdelcerro | other.expression = this.expression.clone();
|
232 | 43809 | jjdelcerro | other.availableFunctions = null;
|
233 | other.availableOperators = null;
|
||
234 | |||
235 | 43983 | jjdelcerro | if (this.featureSymbolTable != null) { |
236 | other.featureSymbolTable = this.featureSymbolTable.clone();
|
||
237 | 43809 | jjdelcerro | } |
238 | 44009 | jjdelcerro | if (this.symbolTable != null) { |
239 | other.symbolTable = this.symbolTable.clone();
|
||
240 | 43809 | jjdelcerro | } |
241 | return other;
|
||
242 | } |
||
243 | 45366 | omartinez | |
244 | @Override
|
||
245 | public void saveToState(PersistentState ps) throws PersistenceException { |
||
246 | ps.set("expression", expression);
|
||
247 | } |
||
248 | 43809 | jjdelcerro | |
249 | 45366 | omartinez | @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 | 46010 | jjdelcerro | @Override
|
264 | public void addSymbolTable(SymbolTable symbolTable) { |
||
265 | if( symbolTable!=null ) { |
||
266 | this.symbolTable.addSymbolTable(symbolTable);
|
||
267 | } |
||
268 | } |
||
269 | |||
270 | 43521 | jjdelcerro | } |