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 / expressionevaluator / impl / DefaultOptimizer.java @ 44338
History | View | Annotate | Download (7.81 KB)
1 | 44009 | jjdelcerro | package org.gvsig.expressionevaluator.impl; |
---|---|---|---|
2 | |||
3 | import org.gvsig.expressionevaluator.Optimizer; |
||
4 | import java.util.Collection; |
||
5 | import java.util.Collections; |
||
6 | import java.util.Iterator; |
||
7 | import org.gvsig.expressionevaluator.Code; |
||
8 | import org.gvsig.expressionevaluator.Code.Caller; |
||
9 | import org.gvsig.expressionevaluator.CodeBuilder; |
||
10 | import org.gvsig.expressionevaluator.Function; |
||
11 | import org.gvsig.expressionevaluator.Interpreter; |
||
12 | import org.gvsig.expressionevaluator.SymbolTable; |
||
13 | 44139 | jjdelcerro | import org.gvsig.expressionevaluator.impl.DefaultCodeBuilder.BaseCodes; |
14 | 44009 | jjdelcerro | import org.gvsig.tools.script.Script; |
15 | |||
16 | /**
|
||
17 | *
|
||
18 | * @author jjdelcerro
|
||
19 | */
|
||
20 | public class DefaultOptimizer implements Optimizer { |
||
21 | |||
22 | private static class OptimizerSymbolTable implements SymbolTable { |
||
23 | |||
24 | private SymbolTable symbolTable;
|
||
25 | |||
26 | public OptimizerSymbolTable() {
|
||
27 | |||
28 | } |
||
29 | |||
30 | public SymbolTable getSymbolTable() {
|
||
31 | return this.symbolTable; |
||
32 | } |
||
33 | |||
34 | public void setSymbolTable(SymbolTable symbolTable) { |
||
35 | this.symbolTable = symbolTable;
|
||
36 | } |
||
37 | |||
38 | @Override
|
||
39 | public String getName() { |
||
40 | return "Optimizer"; |
||
41 | } |
||
42 | |||
43 | @Override
|
||
44 | 44215 | jjdelcerro | public boolean addSymbolTable(SymbolTable symbolTable) { |
45 | 44009 | jjdelcerro | throw new UnsupportedOperationException("Not supported yet."); |
46 | } |
||
47 | |||
48 | @Override
|
||
49 | 44215 | jjdelcerro | public boolean containsSymbolTable(SymbolTable symbolTable) { |
50 | throw new UnsupportedOperationException("Not supported yet."); |
||
51 | } |
||
52 | |||
53 | @Override
|
||
54 | public boolean removeSymbolTable(SymbolTable symbolTable) { |
||
55 | throw new UnsupportedOperationException("Not supported yet."); |
||
56 | } |
||
57 | |||
58 | @Override
|
||
59 | 44009 | jjdelcerro | public Function function(String name) { |
60 | return this.symbolTable.function(name); |
||
61 | } |
||
62 | |||
63 | @Override
|
||
64 | public boolean exists(String name) { |
||
65 | return false; |
||
66 | } |
||
67 | |||
68 | @Override
|
||
69 | public Object value(String name) { |
||
70 | return null; |
||
71 | } |
||
72 | |||
73 | @Override
|
||
74 | 44205 | jjdelcerro | public boolean isSQLCompatible(String name) { |
75 | return false; |
||
76 | } |
||
77 | |||
78 | @Override
|
||
79 | 44009 | jjdelcerro | public Collection<String> variables() { |
80 | return Collections.EMPTY_LIST; |
||
81 | } |
||
82 | |||
83 | @Override
|
||
84 | public Collection<Function> functions() { |
||
85 | return this.symbolTable.functions(); |
||
86 | } |
||
87 | |||
88 | @Override
|
||
89 | 44338 | jjdelcerro | public Collection<Function> localfunctions() { |
90 | return this.symbolTable.functions(); |
||
91 | } |
||
92 | |||
93 | @Override
|
||
94 | 44009 | jjdelcerro | public Collection<Script> scripts() { |
95 | return this.symbolTable.scripts(); |
||
96 | } |
||
97 | |||
98 | @Override
|
||
99 | @SuppressWarnings("CloneDoesntCallSuperClone") |
||
100 | public SymbolTable clone() throws CloneNotSupportedException { |
||
101 | throw new UnsupportedOperationException("Not supported yet."); |
||
102 | } |
||
103 | |||
104 | @Override
|
||
105 | public Iterator<Function> iterator() { |
||
106 | return this.symbolTable.iterator(); |
||
107 | } |
||
108 | |||
109 | } |
||
110 | |||
111 | private final OptimizerSymbolTable symbolTable; |
||
112 | private Interpreter interpreter;
|
||
113 | private CodeBuilder codeBuilder;
|
||
114 | 44010 | jjdelcerro | |
115 | 44009 | jjdelcerro | public DefaultOptimizer() {
|
116 | this.symbolTable = new OptimizerSymbolTable(); |
||
117 | } |
||
118 | |||
119 | public DefaultOptimizer(SymbolTable symbolTable) {
|
||
120 | this();
|
||
121 | this.symbolTable.setSymbolTable(symbolTable);
|
||
122 | } |
||
123 | |||
124 | @Override
|
||
125 | public void setSymbolTable(SymbolTable symbolTable) { |
||
126 | this.symbolTable.setSymbolTable(symbolTable);
|
||
127 | } |
||
128 | |||
129 | 44010 | jjdelcerro | @Override
|
130 | public SymbolTable getSymbolTable() {
|
||
131 | return this.symbolTable; |
||
132 | } |
||
133 | |||
134 | @Override
|
||
135 | public Interpreter getInterpreter() {
|
||
136 | if (this.interpreter == null) { |
||
137 | 44009 | jjdelcerro | this.interpreter = new DefaultInterpreter(); |
138 | this.interpreter.setSymbolTable(this.symbolTable); |
||
139 | } |
||
140 | return this.interpreter; |
||
141 | } |
||
142 | 44010 | jjdelcerro | |
143 | @Override
|
||
144 | public CodeBuilder getCodeBuilder() {
|
||
145 | if (this.codeBuilder == null) { |
||
146 | 44009 | jjdelcerro | this.codeBuilder = new DefaultCodeBuilder(); |
147 | } |
||
148 | return this.codeBuilder; |
||
149 | } |
||
150 | 44010 | jjdelcerro | |
151 | 44009 | jjdelcerro | @Override
|
152 | 44019 | jjdelcerro | public Code optimize(SymbolTable symbolTable, Code code) {
|
153 | this.setSymbolTable(symbolTable);
|
||
154 | return this.optimize(code); |
||
155 | } |
||
156 | |||
157 | @Override
|
||
158 | 44009 | jjdelcerro | public Code optimize(Code code) {
|
159 | code = this.doOptimize(code);
|
||
160 | return code;
|
||
161 | } |
||
162 | 44010 | jjdelcerro | |
163 | 44009 | jjdelcerro | private Code doOptimize(Code code) {
|
164 | switch (code.code()) {
|
||
165 | case Code.CALLER:
|
||
166 | Caller caller = (Caller) code; |
||
167 | 44010 | jjdelcerro | if (caller.function() == null) { |
168 | // resolve and link function
|
||
169 | caller.function(this.getSymbolTable().function(caller.name()));
|
||
170 | } |
||
171 | if (caller.function() instanceof FunctionOptimizer) { |
||
172 | return ((FunctionOptimizer) caller.function()).optimize(this, caller); |
||
173 | } else {
|
||
174 | switch (caller.type()) {
|
||
175 | case Caller.BINARY_OPERATOR: {
|
||
176 | 44198 | jjdelcerro | Code op1 = this.doOptimize(caller.parameters().get(0)); |
177 | Code op2 = this.doOptimize(caller.parameters().get(1)); |
||
178 | 44010 | jjdelcerro | if (caller.function().allowConstantFolding()) {
|
179 | if (op1.code() == Code.CONSTANT && op2.code() == Code.CONSTANT) {
|
||
180 | Object value = this.getInterpreter().run(code); |
||
181 | Code newCode = this.getCodeBuilder().constant(value);
|
||
182 | return newCode;
|
||
183 | } |
||
184 | Code newCode = this.getCodeBuilder().operator(caller.name(), op1, op2);
|
||
185 | return newCode;
|
||
186 | } |
||
187 | 44009 | jjdelcerro | } |
188 | 44010 | jjdelcerro | |
189 | case Code.Caller.UNARY_OPERATOR: {
|
||
190 | 44198 | jjdelcerro | Code op1 = this.doOptimize(caller.parameters().get(0)); |
191 | 44010 | jjdelcerro | if (caller.function().allowConstantFolding()
|
192 | && op1.code() == Code.CONSTANT) { |
||
193 | Object value = this.getInterpreter().run(code); |
||
194 | Code newCode = this.getCodeBuilder().constant(value);
|
||
195 | return newCode;
|
||
196 | } |
||
197 | return code;
|
||
198 | 44009 | jjdelcerro | } |
199 | |||
200 | 44010 | jjdelcerro | case Code.Caller.FUNCTION:
|
201 | default: {
|
||
202 | 44139 | jjdelcerro | BaseCodes newArgs = null;
|
203 | 44010 | jjdelcerro | boolean canOptimize = true; |
204 | 44198 | jjdelcerro | if (caller.parameters() != null) { |
205 | 44139 | jjdelcerro | newArgs = (BaseCodes) this.getCodeBuilder().args();
|
206 | 44198 | jjdelcerro | for (Code arg : caller.parameters()) {
|
207 | 44010 | jjdelcerro | Code newArg = this.doOptimize(arg);
|
208 | newArgs.add(newArg); |
||
209 | if (newArg.code() != Code.CONSTANT) {
|
||
210 | canOptimize = false;
|
||
211 | } |
||
212 | 44009 | jjdelcerro | } |
213 | } |
||
214 | 44010 | jjdelcerro | if (canOptimize && caller.function().allowConstantFolding()) {
|
215 | Object value = this.getInterpreter().run(code); |
||
216 | Code newCode = this.getCodeBuilder().constant(value);
|
||
217 | return newCode;
|
||
218 | } |
||
219 | Code newCode = this.getCodeBuilder().function(
|
||
220 | caller.name(), |
||
221 | caller.type(), |
||
222 | newArgs |
||
223 | ); |
||
224 | 44009 | jjdelcerro | return newCode;
|
225 | } |
||
226 | } |
||
227 | } |
||
228 | 44010 | jjdelcerro | |
229 | 44009 | jjdelcerro | case Code.CONSTANT:
|
230 | case Code.IDENTIFIER:
|
||
231 | case Code.METHOD:
|
||
232 | default:
|
||
233 | return code;
|
||
234 | } |
||
235 | } |
||
236 | } |