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 @ 45366
History | View | Annotate | Download (7.79 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.Set; |
7 |
import org.gvsig.expressionevaluator.Code; |
8 |
import org.gvsig.expressionevaluator.SymbolTable; |
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.impl.symboltable.FeatureSymbolTableImpl; |
14 |
import org.gvsig.expressionevaluator.ExpressionEvaluator; |
15 |
import org.gvsig.expressionevaluator.impl.DefaultExpression; |
16 |
import org.gvsig.expressionevaluator.spi.BaseExpressionEvaluator; |
17 |
import org.gvsig.fmap.dal.expressionevaluator.FeatureSymbolTable; |
18 |
import org.gvsig.fmap.dal.feature.Feature; |
19 |
import org.gvsig.tools.ToolsLocator; |
20 |
import org.gvsig.tools.dynobject.DynStruct; |
21 |
import org.gvsig.tools.evaluator.AbstractEvaluator; |
22 |
import org.gvsig.tools.evaluator.Evaluator; |
23 |
import org.gvsig.tools.evaluator.EvaluatorData; |
24 |
import org.gvsig.tools.evaluator.EvaluatorException; |
25 |
import org.gvsig.tools.evaluator.EvaluatorFieldsInfo; |
26 |
import org.gvsig.tools.exception.BaseException; |
27 |
import org.gvsig.tools.lang.Cloneable; |
28 |
import org.gvsig.tools.persistence.PersistenceManager; |
29 |
import org.gvsig.tools.persistence.PersistentState; |
30 |
import org.gvsig.tools.persistence.exception.PersistenceException; |
31 |
import org.slf4j.Logger; |
32 |
import org.slf4j.LoggerFactory; |
33 |
|
34 |
@SuppressWarnings("UseSpecificCatch") |
35 |
public class DefaultFeatureExpressionEvaluator |
36 |
extends AbstractEvaluator
|
37 |
implements ExpressionEvaluator, Cloneable |
38 |
{ |
39 |
|
40 |
private static final Logger LOGGER = LoggerFactory.getLogger(DefaultFeatureExpressionEvaluator.class); |
41 |
|
42 |
private static class DescriptionAdapter implements Description { |
43 |
|
44 |
Function function; |
45 |
|
46 |
public DescriptionAdapter(Function function) {
|
47 |
this.function = function;
|
48 |
} |
49 |
|
50 |
@Override
|
51 |
public String getName() { |
52 |
return this.function.name(); |
53 |
} |
54 |
|
55 |
@Override
|
56 |
public String getDescription() { |
57 |
return this.function.description(); |
58 |
} |
59 |
|
60 |
@Override
|
61 |
public String getTemplate() { |
62 |
return this.function.template(); |
63 |
} |
64 |
|
65 |
@Override
|
66 |
public int getDataTypeCategories() { |
67 |
switch (this.function.group()) { |
68 |
case Function.GROUP_STRING:
|
69 |
return Description.DATATYPE_CATEGORY_STRING;
|
70 |
case Function.GROUP_BOOLEAN:
|
71 |
return Description.DATATYPE_CATEGORY_BOOLEAN;
|
72 |
case Function.GROUP_DATETIME:
|
73 |
return Description.DATATYPE_CATEGORY_DATETIME;
|
74 |
case Function.GROUP_NUMERIC:
|
75 |
return Description.DATATYPE_CATEGORY_NUMBER;
|
76 |
case Function.GROUP_OGC:
|
77 |
return Description.DATATYPE_CATEGORY_ALL;
|
78 |
default:
|
79 |
return Description.DATATYPE_CATEGORY_ALL;
|
80 |
} |
81 |
} |
82 |
|
83 |
} |
84 |
|
85 |
private FeatureSymbolTable featureSymbolTable;
|
86 |
private SymbolTable symbolTable;
|
87 |
private Expression expression; |
88 |
private Description[] availableOperators; |
89 |
private Description[] availableFunctions; |
90 |
|
91 |
public DefaultFeatureExpressionEvaluator() {
|
92 |
ExpressionEvaluatorManager manager = ExpressionEvaluatorLocator.getManager(); |
93 |
this.symbolTable = manager.createSymbolTable();
|
94 |
this.featureSymbolTable = new FeatureSymbolTableImpl(); |
95 |
this.symbolTable.addSymbolTable(this.featureSymbolTable); |
96 |
this.availableOperators = null; |
97 |
this.availableFunctions = null; |
98 |
} |
99 |
|
100 |
public DefaultFeatureExpressionEvaluator(String source) { |
101 |
this();
|
102 |
ExpressionEvaluatorManager manager = ExpressionEvaluatorLocator.getManager(); |
103 |
this.expression = manager.createExpression();
|
104 |
this.expression.setPhrase(source);
|
105 |
} |
106 |
|
107 |
public DefaultFeatureExpressionEvaluator(Expression expression) { |
108 |
this();
|
109 |
this.expression = expression;
|
110 |
} |
111 |
|
112 |
@Override
|
113 |
public String getName() { |
114 |
return "Genereric expression"; |
115 |
} |
116 |
|
117 |
@Override
|
118 |
public String getDescription() { |
119 |
return this.expression.getPhrase(); |
120 |
} |
121 |
|
122 |
@Override
|
123 |
public String getSQL() { |
124 |
return this.expression.getPhrase(); |
125 |
} |
126 |
|
127 |
@Override
|
128 |
public Expression toExpression() { |
129 |
return expression;
|
130 |
} |
131 |
|
132 |
@Override
|
133 |
public SymbolTable toSymbolTable() {
|
134 |
return this.symbolTable; |
135 |
} |
136 |
|
137 |
@Override
|
138 |
public Object evaluate(EvaluatorData data) throws EvaluatorException { |
139 |
this.featureSymbolTable.setFeature((Feature) data);
|
140 |
Object v = this.expression.execute(this.symbolTable); |
141 |
return v;
|
142 |
} |
143 |
|
144 |
@Override
|
145 |
public EvaluatorFieldsInfo getFieldsInfo() {
|
146 |
final Set<String> names = new HashSet<>(); |
147 |
try {
|
148 |
this.expression.getCode().accept((Object code) -> { |
149 |
if (code instanceof Code.Identifier) { |
150 |
Code.Identifier identifier = (Code.Identifier) code; |
151 |
names.add(identifier.name()); |
152 |
} |
153 |
}); |
154 |
EvaluatorFieldsInfo info = new EvaluatorFieldsInfo();
|
155 |
for (String name : names) { |
156 |
info.addFieldValue(name); |
157 |
} |
158 |
return info;
|
159 |
} catch (BaseException ex) {
|
160 |
throw new RuntimeException("Can't calculate fields information.", ex); |
161 |
} |
162 |
} |
163 |
|
164 |
@Override
|
165 |
public Description[] getAvailableOperators() { |
166 |
if (availableOperators == null) { |
167 |
List<Description> l = new ArrayList<>(); |
168 |
for (Function function : toSymbolTable()) {
|
169 |
if (function.isOperator()) {
|
170 |
l.add(new DescriptionAdapter(function));
|
171 |
} |
172 |
} |
173 |
this.availableOperators = l.toArray(new Description[l.size()]); |
174 |
} |
175 |
return availableOperators;
|
176 |
} |
177 |
|
178 |
@Override
|
179 |
public Description[] getAvailableFunctions() { |
180 |
if (availableFunctions == null) { |
181 |
List<Description> l = new ArrayList<>(); |
182 |
for (Function function : toSymbolTable()) {
|
183 |
if (!function.isOperator()) {
|
184 |
l.add(new DescriptionAdapter(function));
|
185 |
} |
186 |
} |
187 |
this.availableFunctions = l.toArray(new Description[l.size()]); |
188 |
} |
189 |
return availableFunctions;
|
190 |
} |
191 |
|
192 |
@Override
|
193 |
public Evaluator clone() throws CloneNotSupportedException { |
194 |
DefaultFeatureExpressionEvaluator other = (DefaultFeatureExpressionEvaluator) super.clone();
|
195 |
other.expression = this.expression.clone();
|
196 |
other.availableFunctions = null;
|
197 |
other.availableOperators = null;
|
198 |
|
199 |
if (this.featureSymbolTable != null) { |
200 |
other.featureSymbolTable = this.featureSymbolTable.clone();
|
201 |
} |
202 |
if (this.symbolTable != null) { |
203 |
other.symbolTable = this.symbolTable.clone();
|
204 |
} |
205 |
return other;
|
206 |
} |
207 |
|
208 |
@Override
|
209 |
public void saveToState(PersistentState ps) throws PersistenceException { |
210 |
ps.set("expression", expression);
|
211 |
} |
212 |
|
213 |
@Override
|
214 |
public void loadFromState(PersistentState state) throws PersistenceException { |
215 |
this.expression = (Expression) state.get("expression"); |
216 |
} |
217 |
|
218 |
public static void registerPersistence() { |
219 |
PersistenceManager manager = ToolsLocator.getPersistenceManager(); |
220 |
if (manager.getDefinition("DefaultFeatureExpressionEvaluator") == null) { |
221 |
DynStruct definition = manager.addDefinition(DefaultFeatureExpressionEvaluator.class, |
222 |
"DefaultFeatureExpressionEvaluator", "DefaultFeatureExpressionEvaluator persistence definition", null, null); |
223 |
definition.addDynFieldObject("expression").setClassOfValue(DefaultExpression.class).setMandatory(false); |
224 |
} |
225 |
} |
226 |
|
227 |
} |