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 @ 44243
History | View | Annotate | Download (7.67 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 | public Collection<Script> scripts() { |
||
90 | return this.symbolTable.scripts(); |
||
91 | } |
||
92 | |||
93 | @Override
|
||
94 | @SuppressWarnings("CloneDoesntCallSuperClone") |
||
95 | public SymbolTable clone() throws CloneNotSupportedException { |
||
96 | throw new UnsupportedOperationException("Not supported yet."); |
||
97 | } |
||
98 | |||
99 | @Override
|
||
100 | public Iterator<Function> iterator() { |
||
101 | return this.symbolTable.iterator(); |
||
102 | } |
||
103 | |||
104 | } |
||
105 | |||
106 | private final OptimizerSymbolTable symbolTable; |
||
107 | private Interpreter interpreter;
|
||
108 | private CodeBuilder codeBuilder;
|
||
109 | 44010 | jjdelcerro | |
110 | 44009 | jjdelcerro | public DefaultOptimizer() {
|
111 | this.symbolTable = new OptimizerSymbolTable(); |
||
112 | } |
||
113 | |||
114 | public DefaultOptimizer(SymbolTable symbolTable) {
|
||
115 | this();
|
||
116 | this.symbolTable.setSymbolTable(symbolTable);
|
||
117 | } |
||
118 | |||
119 | @Override
|
||
120 | public void setSymbolTable(SymbolTable symbolTable) { |
||
121 | this.symbolTable.setSymbolTable(symbolTable);
|
||
122 | } |
||
123 | |||
124 | 44010 | jjdelcerro | @Override
|
125 | public SymbolTable getSymbolTable() {
|
||
126 | return this.symbolTable; |
||
127 | } |
||
128 | |||
129 | @Override
|
||
130 | public Interpreter getInterpreter() {
|
||
131 | if (this.interpreter == null) { |
||
132 | 44009 | jjdelcerro | this.interpreter = new DefaultInterpreter(); |
133 | this.interpreter.setSymbolTable(this.symbolTable); |
||
134 | } |
||
135 | return this.interpreter; |
||
136 | } |
||
137 | 44010 | jjdelcerro | |
138 | @Override
|
||
139 | public CodeBuilder getCodeBuilder() {
|
||
140 | if (this.codeBuilder == null) { |
||
141 | 44009 | jjdelcerro | this.codeBuilder = new DefaultCodeBuilder(); |
142 | } |
||
143 | return this.codeBuilder; |
||
144 | } |
||
145 | 44010 | jjdelcerro | |
146 | 44009 | jjdelcerro | @Override
|
147 | 44019 | jjdelcerro | public Code optimize(SymbolTable symbolTable, Code code) {
|
148 | this.setSymbolTable(symbolTable);
|
||
149 | return this.optimize(code); |
||
150 | } |
||
151 | |||
152 | @Override
|
||
153 | 44009 | jjdelcerro | public Code optimize(Code code) {
|
154 | code = this.doOptimize(code);
|
||
155 | return code;
|
||
156 | } |
||
157 | 44010 | jjdelcerro | |
158 | 44009 | jjdelcerro | private Code doOptimize(Code code) {
|
159 | switch (code.code()) {
|
||
160 | case Code.CALLER:
|
||
161 | Caller caller = (Caller) code; |
||
162 | 44010 | jjdelcerro | if (caller.function() == null) { |
163 | // resolve and link function
|
||
164 | caller.function(this.getSymbolTable().function(caller.name()));
|
||
165 | } |
||
166 | if (caller.function() instanceof FunctionOptimizer) { |
||
167 | return ((FunctionOptimizer) caller.function()).optimize(this, caller); |
||
168 | } else {
|
||
169 | switch (caller.type()) {
|
||
170 | case Caller.BINARY_OPERATOR: {
|
||
171 | 44198 | jjdelcerro | Code op1 = this.doOptimize(caller.parameters().get(0)); |
172 | Code op2 = this.doOptimize(caller.parameters().get(1)); |
||
173 | 44010 | jjdelcerro | if (caller.function().allowConstantFolding()) {
|
174 | if (op1.code() == Code.CONSTANT && op2.code() == Code.CONSTANT) {
|
||
175 | Object value = this.getInterpreter().run(code); |
||
176 | Code newCode = this.getCodeBuilder().constant(value);
|
||
177 | return newCode;
|
||
178 | } |
||
179 | Code newCode = this.getCodeBuilder().operator(caller.name(), op1, op2);
|
||
180 | return newCode;
|
||
181 | } |
||
182 | 44009 | jjdelcerro | } |
183 | 44010 | jjdelcerro | |
184 | case Code.Caller.UNARY_OPERATOR: {
|
||
185 | 44198 | jjdelcerro | Code op1 = this.doOptimize(caller.parameters().get(0)); |
186 | 44010 | jjdelcerro | if (caller.function().allowConstantFolding()
|
187 | && op1.code() == Code.CONSTANT) { |
||
188 | Object value = this.getInterpreter().run(code); |
||
189 | Code newCode = this.getCodeBuilder().constant(value);
|
||
190 | return newCode;
|
||
191 | } |
||
192 | return code;
|
||
193 | 44009 | jjdelcerro | } |
194 | |||
195 | 44010 | jjdelcerro | case Code.Caller.FUNCTION:
|
196 | default: {
|
||
197 | 44139 | jjdelcerro | BaseCodes newArgs = null;
|
198 | 44010 | jjdelcerro | boolean canOptimize = true; |
199 | 44198 | jjdelcerro | if (caller.parameters() != null) { |
200 | 44139 | jjdelcerro | newArgs = (BaseCodes) this.getCodeBuilder().args();
|
201 | 44198 | jjdelcerro | for (Code arg : caller.parameters()) {
|
202 | 44010 | jjdelcerro | Code newArg = this.doOptimize(arg);
|
203 | newArgs.add(newArg); |
||
204 | if (newArg.code() != Code.CONSTANT) {
|
||
205 | canOptimize = false;
|
||
206 | } |
||
207 | 44009 | jjdelcerro | } |
208 | } |
||
209 | 44010 | jjdelcerro | if (canOptimize && caller.function().allowConstantFolding()) {
|
210 | Object value = this.getInterpreter().run(code); |
||
211 | Code newCode = this.getCodeBuilder().constant(value);
|
||
212 | return newCode;
|
||
213 | } |
||
214 | Code newCode = this.getCodeBuilder().function(
|
||
215 | caller.name(), |
||
216 | caller.type(), |
||
217 | newArgs |
||
218 | ); |
||
219 | 44009 | jjdelcerro | return newCode;
|
220 | } |
||
221 | } |
||
222 | } |
||
223 | 44010 | jjdelcerro | |
224 | 44009 | jjdelcerro | case Code.CONSTANT:
|
225 | case Code.IDENTIFIER:
|
||
226 | case Code.METHOD:
|
||
227 | default:
|
||
228 | return code;
|
||
229 | } |
||
230 | } |
||
231 | } |