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 @ 44750
History | View | Annotate | Download (6.55 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.symbolTable = manager.createSymbolTable();
|
97 |
this.featureSymbolTable = new FeatureSymbolTableImpl(); |
98 |
this.symbolTable.addSymbolTable(this.featureSymbolTable); |
99 |
|
100 |
this.expression = expression;
|
101 |
} |
102 |
|
103 |
@Override
|
104 |
public String getName() { |
105 |
return "Genereric expression"; |
106 |
} |
107 |
|
108 |
@Override
|
109 |
public String getDescription() { |
110 |
return this.expression.getPhrase(); |
111 |
} |
112 |
|
113 |
@Override
|
114 |
public String getSQL() { |
115 |
return this.expression.getPhrase(); |
116 |
} |
117 |
|
118 |
@Override
|
119 |
public Expression getExpression() { |
120 |
return expression;
|
121 |
} |
122 |
|
123 |
@Override
|
124 |
public SymbolTable getSymbolTable() {
|
125 |
return this.symbolTable; |
126 |
} |
127 |
|
128 |
@Override
|
129 |
public Object evaluate(EvaluatorData data) throws EvaluatorException { |
130 |
this.featureSymbolTable.setFeature((Feature) data);
|
131 |
Object v = this.expression.execute(this.symbolTable); |
132 |
return v;
|
133 |
} |
134 |
|
135 |
@Override
|
136 |
public EvaluatorFieldsInfo getFieldsInfo() {
|
137 |
final Set<String> names = new HashSet<>(); |
138 |
try {
|
139 |
this.expression.getCode().accept((Object code) -> { |
140 |
if (code instanceof Code.Identifier) { |
141 |
Code.Identifier identifier = (Code.Identifier) code; |
142 |
names.add(identifier.name()); |
143 |
} |
144 |
}); |
145 |
EvaluatorFieldsInfo info = new EvaluatorFieldsInfo();
|
146 |
for (String name : names) { |
147 |
info.addFieldValue(name); |
148 |
} |
149 |
return info;
|
150 |
} catch (BaseException ex) {
|
151 |
throw new RuntimeException("Can't calculate fields information.", ex); |
152 |
} |
153 |
} |
154 |
|
155 |
@Override
|
156 |
public Description[] getAvailableOperators() { |
157 |
if (availableOperators == null) { |
158 |
List<Description> l = new ArrayList<>(); |
159 |
for (Function function : getSymbolTable()) {
|
160 |
if (function.isOperator()) {
|
161 |
l.add(new DescriptionAdapter(function));
|
162 |
} |
163 |
} |
164 |
this.availableOperators = l.toArray(new Description[l.size()]); |
165 |
} |
166 |
return availableOperators;
|
167 |
} |
168 |
|
169 |
@Override
|
170 |
public Description[] getAvailableFunctions() { |
171 |
if (availableFunctions == null) { |
172 |
List<Description> l = new ArrayList<>(); |
173 |
for (Function function : getSymbolTable()) {
|
174 |
if (!function.isOperator()) {
|
175 |
l.add(new DescriptionAdapter(function));
|
176 |
} |
177 |
} |
178 |
this.availableFunctions = l.toArray(new Description[l.size()]); |
179 |
} |
180 |
return availableFunctions;
|
181 |
} |
182 |
|
183 |
@Override
|
184 |
public Evaluator clone() throws CloneNotSupportedException { |
185 |
DefaultExpressionEvaluator other = (DefaultExpressionEvaluator) super.clone();
|
186 |
other.expression = this.expression.clone();
|
187 |
other.availableFunctions = null;
|
188 |
other.availableOperators = null;
|
189 |
|
190 |
if (this.featureSymbolTable != null) { |
191 |
other.featureSymbolTable = this.featureSymbolTable.clone();
|
192 |
} |
193 |
if (this.symbolTable != null) { |
194 |
other.symbolTable = this.symbolTable.clone();
|
195 |
} |
196 |
return other;
|
197 |
} |
198 |
|
199 |
} |