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 / DefaultExpressionEvaluatorManager.java @ 44019
History | View | Annotate | Download (5.31 KB)
1 |
package org.gvsig.expressionevaluator.impl; |
---|---|
2 |
|
3 |
import java.util.Collection; |
4 |
import java.util.HashMap; |
5 |
import java.util.HashSet; |
6 |
import java.util.Map; |
7 |
import java.util.Set; |
8 |
import org.gvsig.expressionevaluator.Code; |
9 |
import org.gvsig.expressionevaluator.CodeBuilder; |
10 |
import org.gvsig.expressionevaluator.Compiler; |
11 |
import org.gvsig.expressionevaluator.Expression; |
12 |
import org.gvsig.expressionevaluator.ExpressionBuilder; |
13 |
import org.gvsig.expressionevaluator.ExpressionEvaluatorManager; |
14 |
import org.gvsig.expressionevaluator.Interpreter; |
15 |
import org.gvsig.expressionevaluator.LexicalAnalyzer; |
16 |
import org.gvsig.expressionevaluator.MutableSymbolTable; |
17 |
import org.gvsig.expressionevaluator.Optimizer; |
18 |
import org.gvsig.expressionevaluator.SymbolTable; |
19 |
import org.gvsig.expressionevaluator.impl.symboltable.OGCSymbolTable; |
20 |
import org.gvsig.expressionevaluator.impl.symboltable.SQLSymbolTable; |
21 |
import org.slf4j.Logger; |
22 |
import org.slf4j.LoggerFactory; |
23 |
|
24 |
|
25 |
public class DefaultExpressionEvaluatorManager implements ExpressionEvaluatorManager { |
26 |
|
27 |
private static final Logger LOGGER = LoggerFactory.getLogger(DefaultExpressionEvaluatorManager.class); |
28 |
|
29 |
private Double accuracy; |
30 |
private final Map<String,SymbolTable>symbolTables; |
31 |
private final Set<String>autoloadSymbolTables; |
32 |
|
33 |
public DefaultExpressionEvaluatorManager() {
|
34 |
this.symbolTables = new HashMap<>(); |
35 |
this.autoloadSymbolTables = new HashSet<>(); |
36 |
this.registerSymbolTable(new SQLSymbolTable(), true); |
37 |
this.registerSymbolTable(new OGCSymbolTable(), true); |
38 |
} |
39 |
|
40 |
@Override
|
41 |
public SymbolTable getSymbolTable(String name) { |
42 |
return this.symbolTables.get(name.toUpperCase()); |
43 |
} |
44 |
|
45 |
@Override
|
46 |
public Collection<SymbolTable> getSymbolTables() { |
47 |
return this.symbolTables.values(); |
48 |
} |
49 |
|
50 |
@Override
|
51 |
public final void registerSymbolTable(SymbolTable symbolTable, boolean autoload) { |
52 |
if( symbolTable instanceof MutableSymbolTable ) { |
53 |
LOGGER.warn("Registered a MutableSymbolTable '"+symbolTable.getName()+"'."); |
54 |
} |
55 |
this.symbolTables.put(symbolTable.getName().toUpperCase(),symbolTable);
|
56 |
if( autoload ) {
|
57 |
this.autoloadSymbolTables.add(symbolTable.getName().toUpperCase());
|
58 |
} |
59 |
} |
60 |
|
61 |
@Override
|
62 |
public boolean isAutoload(SymbolTable symbolTable) { |
63 |
return this.autoloadSymbolTables.contains(symbolTable.getName().toUpperCase()); |
64 |
} |
65 |
|
66 |
@Override
|
67 |
public Object evaluate(String source) { |
68 |
DefaultInterpreter interpreter = new DefaultInterpreter();
|
69 |
DefaultCompiler compiler = new DefaultCompiler();
|
70 |
Code code = compiler.compileExpression(source); |
71 |
return interpreter.run(code);
|
72 |
} |
73 |
|
74 |
@Override
|
75 |
public Object evaluate(SymbolTable symbolTable, String source) { |
76 |
DefaultInterpreter interpreter = new DefaultInterpreter();
|
77 |
DefaultCompiler compiler = new DefaultCompiler();
|
78 |
Code code = compiler.compileExpression(source); |
79 |
interpreter.setSymbolTable(symbolTable); |
80 |
return interpreter.run(code);
|
81 |
} |
82 |
|
83 |
@Override
|
84 |
public Object evaluate(SymbolTable symbolTable, Code code) { |
85 |
DefaultInterpreter interpreter = new DefaultInterpreter();
|
86 |
interpreter.setSymbolTable(symbolTable); |
87 |
return interpreter.run(code);
|
88 |
} |
89 |
|
90 |
@Override
|
91 |
public Code compile(String source) { |
92 |
DefaultCompiler compiler = new DefaultCompiler();
|
93 |
return compiler.compileExpression(source);
|
94 |
} |
95 |
|
96 |
@Override
|
97 |
public Code compile(LexicalAnalyzer lex, String source) { |
98 |
DefaultCompiler compiler = new DefaultCompiler();
|
99 |
compiler.setLexicalAnalyzer(lex); |
100 |
return compiler.compileExpression(source);
|
101 |
} |
102 |
|
103 |
@Override
|
104 |
public Code optimize(SymbolTable symbolTable, Code code) {
|
105 |
Optimizer optimizer = this.createOptimizer();
|
106 |
return optimizer.optimize(symbolTable, code);
|
107 |
} |
108 |
|
109 |
@Override
|
110 |
public MutableSymbolTable createSymbolTable() {
|
111 |
DefaultSymbolTable theSymbolTable = new DefaultSymbolTable();
|
112 |
return theSymbolTable;
|
113 |
} |
114 |
|
115 |
public void populateSymbolTable(SymbolTable aSymbolTable) { |
116 |
for (String name : this.autoloadSymbolTables ) { |
117 |
SymbolTable symbolTable = this.symbolTables.get(name);
|
118 |
if( symbolTable!=null ) { |
119 |
aSymbolTable.addSymbolTable(symbolTable); |
120 |
} |
121 |
} |
122 |
} |
123 |
|
124 |
@Override
|
125 |
public LexicalAnalyzer createLexicalAnalyzer() {
|
126 |
return new SQLLexicalAnalyzer(); |
127 |
} |
128 |
|
129 |
@Override
|
130 |
public CodeBuilder createCodeBuilder() {
|
131 |
return new DefaultCodeBuilder(); |
132 |
} |
133 |
|
134 |
@Override
|
135 |
public Compiler createCompiler() { |
136 |
return new DefaultCompiler(); |
137 |
} |
138 |
|
139 |
@Override
|
140 |
public Interpreter createInterpreter() {
|
141 |
return new DefaultInterpreter(); |
142 |
} |
143 |
|
144 |
@Override
|
145 |
public Double getAccuracy() { |
146 |
return this.accuracy; |
147 |
} |
148 |
|
149 |
@Override
|
150 |
public void setAccuracy(Double accuracy) { |
151 |
this.accuracy = accuracy;
|
152 |
} |
153 |
|
154 |
@Override
|
155 |
public Expression createExpression() { |
156 |
DefaultExpression e = new DefaultExpression();
|
157 |
return e;
|
158 |
} |
159 |
|
160 |
@Override
|
161 |
public ExpressionBuilder createExpressionBuilder() {
|
162 |
ExpressionBuilder x = new DefaultExpressionBuilder();
|
163 |
return x;
|
164 |
} |
165 |
|
166 |
@Override
|
167 |
public Optimizer createOptimizer() {
|
168 |
Optimizer x = new DefaultOptimizer();
|
169 |
return x;
|
170 |
} |
171 |
} |
172 |
|