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