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