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 @ 44191
History | View | Annotate | Download (6.82 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.SymbolTable; |
||
9 | 43984 | jjdelcerro | import org.gvsig.expressionevaluator.Expression; |
10 | 43521 | jjdelcerro | import org.gvsig.expressionevaluator.ExpressionEvaluatorLocator; |
11 | 43983 | jjdelcerro | import org.gvsig.expressionevaluator.ExpressionEvaluatorManager; |
12 | 43521 | jjdelcerro | import org.gvsig.expressionevaluator.Function; |
13 | 44191 | jjdelcerro | import org.gvsig.fmap.dal.expressionevaluator.ExpressionEvaluator; |
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 | 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 | 44191 | jjdelcerro | import org.slf4j.Logger; |
26 | import org.slf4j.LoggerFactory; |
||
27 | 43521 | jjdelcerro | |
28 | 44191 | jjdelcerro | @SuppressWarnings("UseSpecificCatch") |
29 | 43983 | jjdelcerro | public class DefaultExpressionEvaluator |
30 | extends AbstractEvaluator
|
||
31 | 44191 | jjdelcerro | implements ExpressionEvaluator, Cloneable |
32 | 43983 | jjdelcerro | { |
33 | 43521 | jjdelcerro | |
34 | 44191 | jjdelcerro | private static final Logger LOGGER = LoggerFactory.getLogger(DefaultExpressionEvaluator.class); |
35 | |||
36 | 43521 | jjdelcerro | private static class DescriptionAdapter implements Description { |
37 | |||
38 | Function function; |
||
39 | 43983 | jjdelcerro | |
40 | 43521 | jjdelcerro | public DescriptionAdapter(Function function) {
|
41 | this.function = function;
|
||
42 | } |
||
43 | 43983 | jjdelcerro | |
44 | 43521 | jjdelcerro | @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 | 43983 | jjdelcerro | 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 | 43521 | jjdelcerro | } |
75 | } |
||
76 | 43983 | jjdelcerro | |
77 | } |
||
78 | |||
79 | private FeatureSymbolTable featureSymbolTable;
|
||
80 | 44009 | jjdelcerro | private SymbolTable symbolTable;
|
81 | 43984 | jjdelcerro | private Expression expression; |
82 | 43521 | jjdelcerro | private Description[] availableOperators; |
83 | private Description[] availableFunctions; |
||
84 | |||
85 | 43983 | jjdelcerro | public DefaultExpressionEvaluator(String source) { |
86 | 44009 | jjdelcerro | ExpressionEvaluatorManager manager = ExpressionEvaluatorLocator.getManager(); |
87 | this.symbolTable = manager.createSymbolTable();
|
||
88 | 43983 | jjdelcerro | this.featureSymbolTable = new DefaultFeatureSymbolTable(); |
89 | 44009 | jjdelcerro | this.symbolTable.addSymbolTable(this.featureSymbolTable); |
90 | |||
91 | this.expression = manager.createExpression();
|
||
92 | 43984 | jjdelcerro | this.expression.setPhrase(source);
|
93 | 43521 | jjdelcerro | } |
94 | |||
95 | 43984 | jjdelcerro | public DefaultExpressionEvaluator(Expression expression) { |
96 | 44009 | jjdelcerro | ExpressionEvaluatorManager manager = ExpressionEvaluatorLocator.getManager(); |
97 | 43984 | jjdelcerro | this.expression = expression;
|
98 | 44009 | jjdelcerro | this.symbolTable = manager.createSymbolTable();
|
99 | 43984 | jjdelcerro | this.featureSymbolTable = new DefaultFeatureSymbolTable( |
100 | expression.getUserScript(), |
||
101 | expression.getScripts() |
||
102 | ); |
||
103 | 44009 | jjdelcerro | this.symbolTable.addSymbolTable(this.featureSymbolTable); |
104 | |||
105 | 43984 | jjdelcerro | } |
106 | |||
107 | 43521 | jjdelcerro | @Override
|
108 | 44191 | jjdelcerro | 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 | 43521 | jjdelcerro | public Object evaluate(EvaluatorData data) throws EvaluatorException { |
134 | 43983 | jjdelcerro | this.featureSymbolTable.setFeature((Feature) data);
|
135 | 44009 | jjdelcerro | Object v = this.expression.execute(this.symbolTable); |
136 | 43521 | jjdelcerro | return v;
|
137 | } |
||
138 | |||
139 | @Override
|
||
140 | 43809 | jjdelcerro | public EvaluatorFieldsInfo getFieldsInfo() {
|
141 | 43983 | jjdelcerro | final Set<String> names = new HashSet<>(); |
142 | 43809 | jjdelcerro | try {
|
143 | 44009 | jjdelcerro | this.expression.getCode().accept(new Visitor() { |
144 | 43809 | jjdelcerro | @Override
|
145 | public void visit(Object code) throws VisitCanceledException, BaseException { |
||
146 | 43983 | jjdelcerro | if (code instanceof Code.Identifier) { |
147 | 43809 | jjdelcerro | 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 | 43521 | jjdelcerro | public Description[] getAvailableOperators() { |
164 | 43983 | jjdelcerro | if (availableOperators == null) { |
165 | 43521 | jjdelcerro | List<Description> l = new ArrayList<>(); |
166 | 43983 | jjdelcerro | for (Function function : getSymbolTable()) {
|
167 | if (function.isOperator()) {
|
||
168 | 43521 | jjdelcerro | l.add(new DescriptionAdapter(function));
|
169 | } |
||
170 | } |
||
171 | this.availableOperators = l.toArray(new Description[l.size()]); |
||
172 | } |
||
173 | return availableOperators;
|
||
174 | } |
||
175 | 43983 | jjdelcerro | |
176 | 43521 | jjdelcerro | @Override
|
177 | public Description[] getAvailableFunctions() { |
||
178 | 43983 | jjdelcerro | if (availableFunctions == null) { |
179 | 43521 | jjdelcerro | List<Description> l = new ArrayList<>(); |
180 | 43983 | jjdelcerro | for (Function function : getSymbolTable()) {
|
181 | if (!function.isOperator()) {
|
||
182 | 43521 | jjdelcerro | 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 | 43983 | jjdelcerro | public Evaluator clone() throws CloneNotSupportedException { |
192 | 43809 | jjdelcerro | DefaultExpressionEvaluator other = (DefaultExpressionEvaluator) super.clone();
|
193 | 43984 | jjdelcerro | other.expression = this.expression.clone();
|
194 | 43809 | jjdelcerro | other.availableFunctions = null;
|
195 | other.availableOperators = null;
|
||
196 | |||
197 | 43983 | jjdelcerro | if (this.featureSymbolTable != null) { |
198 | other.featureSymbolTable = this.featureSymbolTable.clone();
|
||
199 | 43809 | jjdelcerro | } |
200 | 44009 | jjdelcerro | if (this.symbolTable != null) { |
201 | other.symbolTable = this.symbolTable.clone();
|
||
202 | 43809 | jjdelcerro | } |
203 | return other;
|
||
204 | } |
||
205 | |||
206 | 43521 | jjdelcerro | } |