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