Revision 44009 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
DefaultExpressionEvaluator.java | ||
---|---|---|
5 | 5 |
import java.util.List; |
6 | 6 |
import java.util.Set; |
7 | 7 |
import org.gvsig.expressionevaluator.Code; |
8 |
import org.gvsig.expressionevaluator.Interpreter; |
|
9 | 8 |
import org.gvsig.expressionevaluator.SymbolTable; |
10 |
import org.gvsig.expressionevaluator.Compiler; |
|
11 | 9 |
import org.gvsig.expressionevaluator.Expression; |
12 | 10 |
import org.gvsig.expressionevaluator.ExpressionEvaluatorLocator; |
13 | 11 |
import org.gvsig.expressionevaluator.ExpressionEvaluatorManager; |
... | ... | |
74 | 72 |
} |
75 | 73 |
|
76 | 74 |
private FeatureSymbolTable featureSymbolTable; |
77 |
private Compiler compiler; |
|
78 |
private Interpreter interpreter; |
|
79 |
private Code code; |
|
75 |
private SymbolTable symbolTable; |
|
80 | 76 |
private Expression expression; |
81 | 77 |
private Description[] availableOperators; |
82 | 78 |
private Description[] availableFunctions; |
83 | 79 |
|
84 | 80 |
public DefaultExpressionEvaluator(String source) { |
81 |
ExpressionEvaluatorManager manager = ExpressionEvaluatorLocator.getManager(); |
|
82 |
this.symbolTable = manager.createSymbolTable(); |
|
85 | 83 |
this.featureSymbolTable = new DefaultFeatureSymbolTable(); |
86 |
this.expression = ExpressionEvaluatorLocator.getManager().createExpression(); |
|
84 |
this.symbolTable.addSymbolTable(this.featureSymbolTable); |
|
85 |
|
|
86 |
this.expression = manager.createExpression(); |
|
87 | 87 |
this.expression.setPhrase(source); |
88 | 88 |
} |
89 | 89 |
|
90 | 90 |
public DefaultExpressionEvaluator(Expression expression) { |
91 |
ExpressionEvaluatorManager manager = ExpressionEvaluatorLocator.getManager(); |
|
91 | 92 |
this.expression = expression; |
93 |
this.symbolTable = manager.createSymbolTable(); |
|
92 | 94 |
this.featureSymbolTable = new DefaultFeatureSymbolTable( |
93 | 95 |
expression.getUserScript(), |
94 | 96 |
expression.getScripts() |
95 | 97 |
); |
98 |
this.symbolTable.addSymbolTable(this.featureSymbolTable); |
|
99 |
|
|
96 | 100 |
} |
97 | 101 |
|
98 | 102 |
@Override |
99 | 103 |
public Object evaluate(EvaluatorData data) throws EvaluatorException { |
100 |
if (this.code == null) { |
|
101 |
this.compile(); |
|
102 |
} |
|
103 |
Interpreter theInterpreter = this.getInterpreter(); |
|
104 |
theInterpreter.getSymbolTable().addSymbolTable(this.featureSymbolTable); |
|
105 |
|
|
106 | 104 |
this.featureSymbolTable.setFeature((Feature) data); |
107 |
Object v = this.interpreter.run(code);
|
|
105 |
Object v = this.expression.execute(this.symbolTable);
|
|
108 | 106 |
return v; |
109 | 107 |
} |
110 | 108 |
|
... | ... | |
117 | 115 |
public EvaluatorFieldsInfo getFieldsInfo() { |
118 | 116 |
final Set<String> names = new HashSet<>(); |
119 | 117 |
try { |
120 |
this.compile().accept(new Visitor() {
|
|
118 |
this.expression.getCode().accept(new Visitor() {
|
|
121 | 119 |
@Override |
122 | 120 |
public void visit(Object code) throws VisitCanceledException, BaseException { |
123 | 121 |
if (code instanceof Code.Identifier) { |
... | ... | |
165 | 163 |
} |
166 | 164 |
|
167 | 165 |
private SymbolTable getSymbolTable() { |
168 |
return this.getInterpreter().getSymbolTable();
|
|
166 |
return this.symbolTable;
|
|
169 | 167 |
} |
170 | 168 |
|
171 |
private Interpreter getInterpreter() { |
|
172 |
if (this.interpreter == null) { |
|
173 |
ExpressionEvaluatorManager manager = ExpressionEvaluatorLocator.getManager(); |
|
174 |
this.interpreter = manager.createInterpreter(); |
|
175 |
this.interpreter.setAccuracy(ExpressionEvaluatorLocator.getManager().getAccuracy()); |
|
176 |
} |
|
177 |
return interpreter; |
|
178 |
} |
|
179 |
|
|
180 |
private Compiler getCompiler() { |
|
181 |
if (this.compiler == null) { |
|
182 |
ExpressionEvaluatorManager manager = ExpressionEvaluatorLocator.getManager(); |
|
183 |
this.compiler = manager.createCompiler(); |
|
184 |
} |
|
185 |
return compiler; |
|
186 |
} |
|
187 |
|
|
188 |
private Code compile() { |
|
189 |
if (this.code == null) { |
|
190 |
ExpressionEvaluatorManager manager = ExpressionEvaluatorLocator.getManager(); |
|
191 |
this.code = this.getCompiler().compileExpression(expression.getPhrase()); |
|
192 |
} |
|
193 |
return code; |
|
194 |
} |
|
195 |
|
|
196 | 169 |
@Override |
197 | 170 |
public Evaluator clone() throws CloneNotSupportedException { |
198 | 171 |
DefaultExpressionEvaluator other = (DefaultExpressionEvaluator) super.clone(); |
199 |
other.code = this.code; |
|
200 | 172 |
other.expression = this.expression.clone(); |
201 | 173 |
other.availableFunctions = null; |
202 | 174 |
other.availableOperators = null; |
... | ... | |
204 | 176 |
if (this.featureSymbolTable != null) { |
205 | 177 |
other.featureSymbolTable = this.featureSymbolTable.clone(); |
206 | 178 |
} |
207 |
if (this.compiler != null) {
|
|
208 |
other.compiler = this.compiler.clone();
|
|
179 |
if (this.symbolTable != null) {
|
|
180 |
other.symbolTable = this.symbolTable.clone();
|
|
209 | 181 |
} |
210 |
if (this.interpreter != null) { |
|
211 |
other.interpreter = this.interpreter.clone(); |
|
212 |
} |
|
213 | 182 |
return other; |
214 | 183 |
} |
215 | 184 |
|
Also available in: Unified diff