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 |
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 |
import org.gvsig.expressionevaluator.impl.DefaultCodeBuilder.BaseCodes; |
14 |
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 |
public boolean addSymbolTable(SymbolTable symbolTable) { |
45 |
throw new UnsupportedOperationException("Not supported yet."); |
46 |
} |
47 |
|
48 |
@Override
|
49 |
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 |
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 |
public boolean isSQLCompatible(String name) { |
75 |
return false; |
76 |
} |
77 |
|
78 |
@Override
|
79 |
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 |
|
110 |
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 |
@Override
|
125 |
public SymbolTable getSymbolTable() {
|
126 |
return this.symbolTable; |
127 |
} |
128 |
|
129 |
@Override
|
130 |
public Interpreter getInterpreter() {
|
131 |
if (this.interpreter == null) { |
132 |
this.interpreter = new DefaultInterpreter(); |
133 |
this.interpreter.setSymbolTable(this.symbolTable); |
134 |
} |
135 |
return this.interpreter; |
136 |
} |
137 |
|
138 |
@Override
|
139 |
public CodeBuilder getCodeBuilder() {
|
140 |
if (this.codeBuilder == null) { |
141 |
this.codeBuilder = new DefaultCodeBuilder(); |
142 |
} |
143 |
return this.codeBuilder; |
144 |
} |
145 |
|
146 |
@Override
|
147 |
public Code optimize(SymbolTable symbolTable, Code code) {
|
148 |
this.setSymbolTable(symbolTable);
|
149 |
return this.optimize(code); |
150 |
} |
151 |
|
152 |
@Override
|
153 |
public Code optimize(Code code) {
|
154 |
code = this.doOptimize(code);
|
155 |
return code;
|
156 |
} |
157 |
|
158 |
private Code doOptimize(Code code) {
|
159 |
switch (code.code()) {
|
160 |
case Code.CALLER:
|
161 |
Caller caller = (Caller) code; |
162 |
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 |
Code op1 = this.doOptimize(caller.parameters().get(0)); |
172 |
Code op2 = this.doOptimize(caller.parameters().get(1)); |
173 |
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 |
} |
183 |
|
184 |
case Code.Caller.UNARY_OPERATOR: {
|
185 |
Code op1 = this.doOptimize(caller.parameters().get(0)); |
186 |
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 |
} |
194 |
|
195 |
case Code.Caller.FUNCTION:
|
196 |
default: {
|
197 |
BaseCodes newArgs = null;
|
198 |
boolean canOptimize = true; |
199 |
if (caller.parameters() != null) { |
200 |
newArgs = (BaseCodes) this.getCodeBuilder().args();
|
201 |
for (Code arg : caller.parameters()) {
|
202 |
Code newArg = this.doOptimize(arg);
|
203 |
newArgs.add(newArg); |
204 |
if (newArg.code() != Code.CONSTANT) {
|
205 |
canOptimize = false;
|
206 |
} |
207 |
} |
208 |
} |
209 |
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 |
return newCode;
|
220 |
} |
221 |
} |
222 |
} |
223 |
|
224 |
case Code.CONSTANT:
|
225 |
case Code.IDENTIFIER:
|
226 |
case Code.METHOD:
|
227 |
default:
|
228 |
return code;
|
229 |
} |
230 |
} |
231 |
} |