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