svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.library / org.gvsig.expressionevaluator / org.gvsig.expressionevaluator.lib / org.gvsig.expressionevaluator.lib.impl / src / main / java / org / gvsig / integration / DefaultExpressionEvaluator.java @ 43521
History | View | Annotate | Download (6.01 KB)
1 |
package org.gvsig.integration; |
---|---|
2 |
|
3 |
import java.util.ArrayList; |
4 |
import java.util.List; |
5 |
import org.gvsig.expressionevaluator.Code; |
6 |
import org.gvsig.expressionevaluator.Interpreter; |
7 |
import org.gvsig.expressionevaluator.LexicalAnalyzer; |
8 |
import org.gvsig.expressionevaluator.SymbolTable; |
9 |
import org.gvsig.expressionevaluator.Compiler; |
10 |
import org.gvsig.expressionevaluator.ExpressionEvaluator; |
11 |
import org.gvsig.expressionevaluator.ExpressionEvaluatorLocator; |
12 |
import org.gvsig.expressionevaluator.Function; |
13 |
import org.gvsig.expressionevaluator.impl.DefaultCompiler; |
14 |
import org.gvsig.expressionevaluator.impl.DefaultInterpreter; |
15 |
import org.gvsig.expressionevaluator.impl.SQLLexicalAnalyzer; |
16 |
import org.gvsig.expressionevaluator.impl.SQLSymbolTable; |
17 |
import org.gvsig.tools.evaluator.AbstractEvaluator; |
18 |
import org.gvsig.tools.evaluator.EvaluatorData; |
19 |
import org.gvsig.tools.evaluator.EvaluatorException; |
20 |
|
21 |
public class DefaultExpressionEvaluator extends AbstractEvaluator implements ExpressionEvaluator { |
22 |
|
23 |
private static class DescriptionAdapter implements Description { |
24 |
|
25 |
Function function; |
26 |
|
27 |
public DescriptionAdapter(Function function) {
|
28 |
this.function = function;
|
29 |
} |
30 |
|
31 |
@Override
|
32 |
public String getName() { |
33 |
return this.function.name(); |
34 |
} |
35 |
|
36 |
@Override
|
37 |
public String getDescription() { |
38 |
return this.function.description(); |
39 |
} |
40 |
|
41 |
@Override
|
42 |
public String getTemplate() { |
43 |
return this.function.template(); |
44 |
} |
45 |
|
46 |
@Override
|
47 |
public int getDataTypeCategories() { |
48 |
switch(this.function.group()) { |
49 |
case Function.GROUP_STRING:
|
50 |
return Description.DATATYPE_CATEGORY_STRING;
|
51 |
case Function.GROUP_BOOLEAN:
|
52 |
return Description.DATATYPE_CATEGORY_BOOLEAN;
|
53 |
case Function.GROUP_DATETIME:
|
54 |
return Description.DATATYPE_CATEGORY_DATETIME;
|
55 |
case Function.GROUP_NUMERIC:
|
56 |
return Description.DATATYPE_CATEGORY_NUMBER;
|
57 |
case Function.GROUP_OGC:
|
58 |
return Description.DATATYPE_CATEGORY_ALL;
|
59 |
default:
|
60 |
return Description.DATATYPE_CATEGORY_ALL;
|
61 |
} |
62 |
} |
63 |
|
64 |
} |
65 |
|
66 |
private EvaluatorDataAdapter evaluatorData;
|
67 |
private SymbolTable symbolTable;
|
68 |
private LexicalAnalyzer lexer;
|
69 |
private Compiler compiler; |
70 |
private Interpreter interpreter;
|
71 |
private Code code;
|
72 |
private String source; |
73 |
private Description[] availableOperators; |
74 |
private Description[] availableFunctions; |
75 |
private Double accuracy; |
76 |
|
77 |
|
78 |
public DefaultExpressionEvaluator() {
|
79 |
super();
|
80 |
this.accuracy = ExpressionEvaluatorLocator.getManager().getAccuracy();
|
81 |
} |
82 |
|
83 |
@Override
|
84 |
public Code getCode() {
|
85 |
return this.code; |
86 |
} |
87 |
|
88 |
@Override
|
89 |
public void compile() { |
90 |
if( this.symbolTable == null ) { |
91 |
this.symbolTable = SQLSymbolTable.getInstance();
|
92 |
} |
93 |
if( this.lexer == null ) { |
94 |
this.lexer = new SQLLexicalAnalyzer(); |
95 |
} |
96 |
if( this.compiler == null ) { |
97 |
this.compiler = new DefaultCompiler(); |
98 |
this.compiler.setLexicalAnalyzer(lexer);
|
99 |
} |
100 |
if( this.interpreter == null ) { |
101 |
this.interpreter = new DefaultInterpreter(); |
102 |
} |
103 |
this.evaluatorData = new EvaluatorDataAdapter(symbolTable); |
104 |
this.interpreter.setAccuracy(this.accuracy); |
105 |
this.interpreter.setSymbolTable(this.evaluatorData); |
106 |
this.code = this.compiler.compileExpression(source); |
107 |
} |
108 |
|
109 |
@Override
|
110 |
public Object evaluate(EvaluatorData data) throws EvaluatorException { |
111 |
if( this.code == null ) { |
112 |
this.compile();
|
113 |
} |
114 |
this.evaluatorData.setData(data);
|
115 |
Object v = this.interpreter.run(code); |
116 |
return v;
|
117 |
} |
118 |
|
119 |
@Override
|
120 |
public String getName() { |
121 |
return "Genereric expression"; |
122 |
} |
123 |
|
124 |
@Override
|
125 |
public Description[] getAvailableOperators() { |
126 |
if( availableOperators==null ) { |
127 |
List<Description> l = new ArrayList<>(); |
128 |
for( Function function : symbolTable) {
|
129 |
if( function.isOperator() ) {
|
130 |
l.add(new DescriptionAdapter(function));
|
131 |
} |
132 |
} |
133 |
this.availableOperators = l.toArray(new Description[l.size()]); |
134 |
} |
135 |
return availableOperators;
|
136 |
} |
137 |
|
138 |
@Override
|
139 |
public Description[] getAvailableFunctions() { |
140 |
if( availableFunctions==null ) { |
141 |
List<Description> l = new ArrayList<>(); |
142 |
for( Function function : symbolTable) {
|
143 |
if( !function.isOperator() ) {
|
144 |
l.add(new DescriptionAdapter(function));
|
145 |
} |
146 |
} |
147 |
this.availableFunctions = l.toArray(new Description[l.size()]); |
148 |
} |
149 |
return availableFunctions;
|
150 |
} |
151 |
|
152 |
@Override
|
153 |
public SymbolTable getSymbolTable() {
|
154 |
return symbolTable;
|
155 |
} |
156 |
|
157 |
@Override
|
158 |
public void setSymbolTable(SymbolTable symbolTable) { |
159 |
this.symbolTable = symbolTable;
|
160 |
} |
161 |
|
162 |
@Override
|
163 |
public LexicalAnalyzer getLexer() {
|
164 |
return lexer;
|
165 |
} |
166 |
|
167 |
@Override
|
168 |
public void setLexer(LexicalAnalyzer lexer) { |
169 |
this.lexer = lexer;
|
170 |
} |
171 |
|
172 |
@Override
|
173 |
public Compiler getCompiler() { |
174 |
return compiler;
|
175 |
} |
176 |
|
177 |
@Override
|
178 |
public void setCompiler(Compiler compiler) { |
179 |
this.compiler = compiler;
|
180 |
} |
181 |
|
182 |
@Override
|
183 |
public Interpreter getInterpreter() {
|
184 |
return interpreter;
|
185 |
} |
186 |
|
187 |
@Override
|
188 |
public void setInterpreter(Interpreter interpreter) { |
189 |
this.interpreter = interpreter;
|
190 |
} |
191 |
|
192 |
@Override
|
193 |
public String getSource() { |
194 |
return source;
|
195 |
} |
196 |
|
197 |
@Override
|
198 |
public void setSource(String source) { |
199 |
this.source = source;
|
200 |
this.code = null; |
201 |
} |
202 |
|
203 |
@Override
|
204 |
public Double getAccuracy() { |
205 |
return this.accuracy; |
206 |
} |
207 |
|
208 |
@Override
|
209 |
public void setAccuracy(Double accuracy) { |
210 |
this.accuracy = accuracy;
|
211 |
} |
212 |
} |