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 @ 45037
History | View | Annotate | Download (6.55 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 | 44738 | jjdelcerro | import org.gvsig.expressionevaluator.impl.symboltable.FeatureSymbolTableImpl; |
14 | 44191 | jjdelcerro | import org.gvsig.fmap.dal.expressionevaluator.ExpressionEvaluator; |
15 | 43983 | jjdelcerro | import org.gvsig.fmap.dal.expressionevaluator.FeatureSymbolTable; |
16 | import org.gvsig.fmap.dal.feature.Feature; |
||
17 | 43521 | jjdelcerro | import org.gvsig.tools.evaluator.AbstractEvaluator; |
18 | 43983 | jjdelcerro | import org.gvsig.tools.evaluator.Evaluator; |
19 | 43521 | jjdelcerro | import org.gvsig.tools.evaluator.EvaluatorData; |
20 | import org.gvsig.tools.evaluator.EvaluatorException; |
||
21 | 43809 | jjdelcerro | import org.gvsig.tools.evaluator.EvaluatorFieldsInfo; |
22 | import org.gvsig.tools.exception.BaseException; |
||
23 | import org.gvsig.tools.lang.Cloneable; |
||
24 | 44191 | jjdelcerro | import org.slf4j.Logger; |
25 | import org.slf4j.LoggerFactory; |
||
26 | 43521 | jjdelcerro | |
27 | 44191 | jjdelcerro | @SuppressWarnings("UseSpecificCatch") |
28 | 43983 | jjdelcerro | public class DefaultExpressionEvaluator |
29 | extends AbstractEvaluator
|
||
30 | 44191 | jjdelcerro | implements ExpressionEvaluator, Cloneable |
31 | 43983 | jjdelcerro | { |
32 | 43521 | jjdelcerro | |
33 | 44191 | jjdelcerro | private static final Logger LOGGER = LoggerFactory.getLogger(DefaultExpressionEvaluator.class); |
34 | |||
35 | 43521 | jjdelcerro | private static class DescriptionAdapter implements Description { |
36 | |||
37 | Function function; |
||
38 | 43983 | jjdelcerro | |
39 | 43521 | jjdelcerro | public DescriptionAdapter(Function function) {
|
40 | this.function = function;
|
||
41 | } |
||
42 | 43983 | jjdelcerro | |
43 | 43521 | jjdelcerro | @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 | 43983 | jjdelcerro | 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 | 43521 | jjdelcerro | } |
74 | } |
||
75 | 43983 | jjdelcerro | |
76 | } |
||
77 | |||
78 | private FeatureSymbolTable featureSymbolTable;
|
||
79 | 44009 | jjdelcerro | private SymbolTable symbolTable;
|
80 | 43984 | jjdelcerro | private Expression expression; |
81 | 43521 | jjdelcerro | private Description[] availableOperators; |
82 | private Description[] availableFunctions; |
||
83 | |||
84 | 43983 | jjdelcerro | public DefaultExpressionEvaluator(String source) { |
85 | 44009 | jjdelcerro | ExpressionEvaluatorManager manager = ExpressionEvaluatorLocator.getManager(); |
86 | this.symbolTable = manager.createSymbolTable();
|
||
87 | 44738 | jjdelcerro | this.featureSymbolTable = new FeatureSymbolTableImpl(); |
88 | 44009 | jjdelcerro | this.symbolTable.addSymbolTable(this.featureSymbolTable); |
89 | |||
90 | this.expression = manager.createExpression();
|
||
91 | 43984 | jjdelcerro | this.expression.setPhrase(source);
|
92 | 43521 | jjdelcerro | } |
93 | |||
94 | 43984 | jjdelcerro | public DefaultExpressionEvaluator(Expression expression) { |
95 | 44009 | jjdelcerro | ExpressionEvaluatorManager manager = ExpressionEvaluatorLocator.getManager(); |
96 | this.symbolTable = manager.createSymbolTable();
|
||
97 | 44750 | jjdelcerro | this.featureSymbolTable = new FeatureSymbolTableImpl(); |
98 | 44009 | jjdelcerro | this.symbolTable.addSymbolTable(this.featureSymbolTable); |
99 | |||
100 | 44750 | jjdelcerro | this.expression = expression;
|
101 | 43984 | jjdelcerro | } |
102 | |||
103 | 43521 | jjdelcerro | @Override
|
104 | 44191 | jjdelcerro | 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 | 43521 | jjdelcerro | public Object evaluate(EvaluatorData data) throws EvaluatorException { |
130 | 43983 | jjdelcerro | this.featureSymbolTable.setFeature((Feature) data);
|
131 | 44009 | jjdelcerro | Object v = this.expression.execute(this.symbolTable); |
132 | 43521 | jjdelcerro | return v;
|
133 | } |
||
134 | |||
135 | @Override
|
||
136 | 43809 | jjdelcerro | public EvaluatorFieldsInfo getFieldsInfo() {
|
137 | 43983 | jjdelcerro | final Set<String> names = new HashSet<>(); |
138 | 43809 | jjdelcerro | try {
|
139 | 44738 | jjdelcerro | 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 | 43809 | jjdelcerro | }); |
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 | 43521 | jjdelcerro | public Description[] getAvailableOperators() { |
157 | 43983 | jjdelcerro | if (availableOperators == null) { |
158 | 43521 | jjdelcerro | List<Description> l = new ArrayList<>(); |
159 | 43983 | jjdelcerro | for (Function function : getSymbolTable()) {
|
160 | if (function.isOperator()) {
|
||
161 | 43521 | jjdelcerro | l.add(new DescriptionAdapter(function));
|
162 | } |
||
163 | } |
||
164 | this.availableOperators = l.toArray(new Description[l.size()]); |
||
165 | } |
||
166 | return availableOperators;
|
||
167 | } |
||
168 | 43983 | jjdelcerro | |
169 | 43521 | jjdelcerro | @Override
|
170 | public Description[] getAvailableFunctions() { |
||
171 | 43983 | jjdelcerro | if (availableFunctions == null) { |
172 | 43521 | jjdelcerro | List<Description> l = new ArrayList<>(); |
173 | 43983 | jjdelcerro | for (Function function : getSymbolTable()) {
|
174 | if (!function.isOperator()) {
|
||
175 | 43521 | jjdelcerro | 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 | 43983 | jjdelcerro | public Evaluator clone() throws CloneNotSupportedException { |
185 | 43809 | jjdelcerro | DefaultExpressionEvaluator other = (DefaultExpressionEvaluator) super.clone();
|
186 | 43984 | jjdelcerro | other.expression = this.expression.clone();
|
187 | 43809 | jjdelcerro | other.availableFunctions = null;
|
188 | other.availableOperators = null;
|
||
189 | |||
190 | 43983 | jjdelcerro | if (this.featureSymbolTable != null) { |
191 | other.featureSymbolTable = this.featureSymbolTable.clone();
|
||
192 | 43809 | jjdelcerro | } |
193 | 44009 | jjdelcerro | if (this.symbolTable != null) { |
194 | other.symbolTable = this.symbolTable.clone();
|
||
195 | 43809 | jjdelcerro | } |
196 | return other;
|
||
197 | } |
||
198 | |||
199 | 43521 | jjdelcerro | } |