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