KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > xquark > mediator > decomposer > AtomizeWhereVisitor


1 /*
2  * This file belongs to the XQuark distribution.
3  * Copyright (C) 2003 Universite de Versailles Saint-Quentin.
4  * Copyright (C) 2003 XQuark Group.
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307.
19  * You can also get it at http://www.gnu.org/licenses/lgpl.html
20  *
21  * For more information on this software, see http://www.xquark.org.
22  */

23
24 package org.xquark.mediator.decomposer;
25
26 import java.util.ArrayList JavaDoc;
27 import java.util.HashMap JavaDoc;
28 import java.util.Map JavaDoc;
29
30 import org.xquark.xquery.normalize.*;
31 import org.xquark.xquery.parser.*;
32
33 public class AtomizeWhereVisitor extends DefaultParserVisitor {
34     // **********************************************************************
35
// * VERSIONING
36
// **********************************************************************
37
private static final String JavaDoc RCSRevision = "$Revision: 1.5 $";
38     private static final String JavaDoc RCSName = "$Name: $";
39     // **********************************************************************
40
// * CLASS VARIABLES
41
// **********************************************************************
42
// for each var all simple conditions in which this var appears
43
private HashMap JavaDoc varSimpleConditionsMap = null; // { var1 [c1, c2], var2 [c3, c4] ...
44
// for each var a map of (var,conditions) reprensenting simple joins
45
private HashMap JavaDoc varJoinConditionsMap = null; // { var1 { var2 [c121, c122], var3 [c131, c132] ... }, var2 { var1 [c211, c212] ...
46
// for each var a list of complex conditions in which it appears
47
private HashMap JavaDoc varComplexConditionsMap = null; // { var1 [c1, c2], var2 [c3, c4] ...
48
// for each var all conditions in which this var appears
49
private HashMap JavaDoc varAllConditionsMap = null; // { var1 [c1, c2], var2 [c3, c4] ...
50
// for each var all variables appearing in common conditions
51
private HashMap JavaDoc varAssociatedVariablesMap = null; // { var1 [var2, var3], var2 [var3, var4] ...
52
// for each condition all variables appearing
53
private HashMap JavaDoc condAssociatedVariablesMap = null; // { c1 [var2, var3], c2 [var3, var4] ...
54
// a list of all simple conditions
55
private ArrayList JavaDoc allSimpleConditionsList = null;
56     // a list of all join conditions
57
private ArrayList JavaDoc allJoinConditionsList = null;
58     // a list of all complex conditions
59
private ArrayList JavaDoc allComplexConditionsList = null;
60     // a list of all conditions without variables !!!
61
private ArrayList JavaDoc allNoVariablesConditionsList = null;
62     // a list of all conditions
63
private ArrayList JavaDoc allConditionsList = null;
64     // visitor
65
private GetBoundVariablesVisitor gbv_visitor = null;
66
67     // **********************************************************************
68
// * INITIALIZATION
69
// **********************************************************************
70
public AtomizeWhereVisitor() {
71         gbv_visitor = new GetBoundVariablesVisitor(true, false);
72         varSimpleConditionsMap = new HashMap JavaDoc();
73         varJoinConditionsMap = new HashMap JavaDoc();
74         varComplexConditionsMap = new HashMap JavaDoc();
75         varAllConditionsMap = new HashMap JavaDoc();
76         varAssociatedVariablesMap = new HashMap JavaDoc();
77         condAssociatedVariablesMap = new HashMap JavaDoc();
78         allSimpleConditionsList = new ArrayList JavaDoc();
79         allJoinConditionsList = new ArrayList JavaDoc();
80         allComplexConditionsList = new ArrayList JavaDoc();
81         allNoVariablesConditionsList = new ArrayList JavaDoc();
82         allConditionsList = new ArrayList JavaDoc();
83     }
84
85     public HashMap JavaDoc getVarSimpleConditionsMap() {
86         return varSimpleConditionsMap;
87     }
88     public HashMap JavaDoc getVarJoinConditionsMap() {
89         return varJoinConditionsMap;
90     }
91     public HashMap JavaDoc getVarComplexConditionsMap() {
92         return varComplexConditionsMap;
93     }
94     public HashMap JavaDoc getVarAllConditionsMap() {
95         return varAllConditionsMap;
96     }
97     public HashMap JavaDoc getVarAssociatedVariablesMap() {
98         return varAssociatedVariablesMap;
99     }
100     public HashMap JavaDoc getCondAssociatedVariablesMap() {
101         return condAssociatedVariablesMap;
102     }
103     public ArrayList JavaDoc getAllSimpleConditionsList() {
104         return allSimpleConditionsList;
105     }
106     public ArrayList JavaDoc getAllJoinConditionsList() {
107         return allJoinConditionsList;
108     }
109     public ArrayList JavaDoc getAllComplexConditionsList() {
110         return allComplexConditionsList;
111     }
112     public ArrayList JavaDoc getAllNoVariablesConditionsList() {
113         return allNoVariablesConditionsList;
114     }
115     public ArrayList JavaDoc getAllConditionsList() {
116         return allConditionsList;
117     }
118
119     // public void reset() {
120
// varSimpleConditionsMap.clear();
121
// varJoinConditionsMap.clear();
122
// varComplexConditionsMap.clear();
123
// varAllConditionsMap.clear();
124
// allSimpleConditionsList.clear();
125
// allJoinConditionsList.clear();
126
// allComplexConditionsList.clear();
127
// allConditionsList.clear();
128
// }
129
// **********************************************************************
130
// * VISITATION
131
// **********************************************************************
132
// public void visit(AttributeValuePair arg) throws XQueryException
133

134     public void visit(BinOpANDExpression arg) throws XQueryException {
135         arg.getExpression1().accept(this);
136         arg.getExpression2().accept(this);
137     }
138
139     public void visit(ListOpCompExpression arg) throws XQueryException {
140         allConditionsList.add(arg);
141         gbv_visitor.reset();
142         arg.accept(gbv_visitor);
143         ArrayList JavaDoc vars = gbv_visitor.getAllVariables();
144         fillVarAssociatedMap(vars);
145         condAssociatedVariablesMap.put(arg,vars);
146         if (vars.isEmpty()) {
147             allNoVariablesConditionsList.add(arg);
148         } else if (vars.size() == 1) {
149             allSimpleConditionsList.add(arg);
150             Variable var0 = (Variable) vars.get(0);
151             addExprToVarMap(var0,arg,varAllConditionsMap);
152             addExprToVarMap(var0,arg,varSimpleConditionsMap);
153         } else if (vars.size() == 2) {
154             gbv_visitor.reset();
155             arg.getExpression1().accept(gbv_visitor);
156             ArrayList JavaDoc vars1 = gbv_visitor.getAllVariables();
157             if (vars1.size() != 1) {
158                 addExprToVarsMap(vars,arg,varAllConditionsMap);
159                 allComplexConditionsList.add(arg);
160                 // TODO add to complex map for each variable
161
} else { // (vars1.size() == 1)
162
gbv_visitor.reset();
163                 arg.getExpression2().accept(gbv_visitor);
164                 ArrayList JavaDoc vars2 = gbv_visitor.getAllVariables();
165                 if (vars2.size() != 1) {
166                     addExprToVarsMap(vars,arg,varAllConditionsMap);
167                     allComplexConditionsList.add(arg);
168                     // TODO add to complex map for each variable
169
} else { // vars2.size() == 1
170
allJoinConditionsList.add(arg);
171                     Map JavaDoc tmpmap = (Map JavaDoc)varJoinConditionsMap.get(vars1.get(0));
172                     if (tmpmap == null) {
173                         tmpmap = new HashMap JavaDoc();
174                         varJoinConditionsMap.put(vars1.get(0),tmpmap);
175                     }
176                     ArrayList JavaDoc tmplist = (ArrayList JavaDoc) tmpmap.get(vars2.get(0));
177                     if (tmplist == null) {
178                         tmplist = new ArrayList JavaDoc(1);
179                         tmpmap.put(vars2.get(0), tmplist);
180                     }
181                     tmplist.add(arg);
182                     tmpmap = (Map JavaDoc)varJoinConditionsMap.get(vars2.get(0));
183                     if (tmpmap == null) {
184                         tmpmap = new HashMap JavaDoc();
185                         varJoinConditionsMap.put(vars2.get(0),tmpmap);
186                     }
187                     tmplist = (ArrayList JavaDoc) tmpmap.get(vars1.get(0));
188                     if (tmplist == null) {
189                         tmplist = new ArrayList JavaDoc(1);
190                         tmpmap.put(vars1.get(0), tmplist);
191                     }
192                     tmplist.add(arg);
193                 }
194             }
195         } else { // vars.size() > 2
196
for (int i = 0; i < vars.size(); i++) {
197                 Variable var0 = (Variable) vars.get(0);
198                 ArrayList JavaDoc tmplist = (ArrayList JavaDoc) varAllConditionsMap.get(var0);
199                 if (tmplist == null) {
200                     tmplist = new ArrayList JavaDoc(1);
201                     varAllConditionsMap.put(var0, tmplist);
202                 }
203                 tmplist.add(arg);
204             }
205             allComplexConditionsList.add(arg);
206             // TODO add to complex map for each variable
207
}
208
209         // if (vars.isEmpty())
210
// return;
211
// if (vars.size() > 1) {
212
// allComplexConditionsList.add(arg);
213
// }
214
// else if (vars.size() == 1) {
215
// allSimpleConditionsList.add(arg);
216
// }
217
// for (int i = 0; i < vars.size(); i++)
218
// if (!varVarMap.containsKey(vars.get(i)))
219
// varVarMap.put(vars.get(i), null);
220
// if (vars.size() < 3) {
221
// Set values = varVarMap.keySet();
222
// Variable var1 = (Variable) vars.get(0);
223
// Variable var2 = (Variable) vars.get((vars.size()>1)?1:0);
224
// HashMap varMap = (HashMap) varVarMap.get(var1);
225
// if (varMap == null) {
226
// varMap = new HashMap();
227
// ArrayList tmpList = new ArrayList(1);
228
// tmpList.add(arg);
229
// varMap.put(var2, tmpList);
230
// varVarMap.put(var1,varMap);
231
// } else {
232
// ArrayList tmpList = (ArrayList) varMap.get(var2);
233
// if (tmpList == null) {
234
// tmpList = new ArrayList(1);
235
// varMap.put(var2,tmpList);
236
// }
237
// tmpList.add(arg);
238
// }
239
// if (vars.size() > 1) {
240
// varMap = (HashMap) varVarMap.get(var2);
241
// if (varMap == null) {
242
// varMap = new HashMap();
243
// ArrayList tmpList = new ArrayList(1);
244
// tmpList.add(arg);
245
// varMap.put(var1, tmpList);
246
// varVarMap.put(var2,varMap);
247
// } else {
248
// ArrayList tmpList = (ArrayList) varMap.get(var1);
249
// if (tmpList == null) {
250
// tmpList = new ArrayList(1);
251
// varMap.put(var1,tmpList);
252
// }
253
// tmpList.add(arg);
254
// }
255
// }
256
// }
257
}
258
259     public void visit(XQueryExpression arg) throws XQueryException {
260         allConditionsList.add(arg);
261         gbv_visitor.reset();
262         arg.accept(gbv_visitor);
263         ArrayList JavaDoc vars = gbv_visitor.getAllVariables();
264         fillVarAssociatedMap(vars);
265         condAssociatedVariablesMap.put(arg,vars);
266         if (vars.isEmpty()) {
267             allNoVariablesConditionsList.add(arg);
268         } else if (vars.size() == 1) {
269             allSimpleConditionsList.add(arg);
270             Variable var0 = (Variable) vars.get(0);
271             addExprToVarMap(var0,arg,varAllConditionsMap);
272             addExprToVarMap(var0,arg,varSimpleConditionsMap);
273         } else { //vars.size() > 1)
274
allComplexConditionsList.add(arg);
275             addExprToVarsMap(vars,arg,varAllConditionsMap);
276             addExprToVarsMap(vars,arg,varComplexConditionsMap);
277         }
278     }
279
280     private void addExprToVarMap(Variable var, XQueryExpression expr, Map JavaDoc map) {
281         ArrayList JavaDoc tmplist = (ArrayList JavaDoc) map.get(var);
282         if (tmplist == null) {
283             tmplist = new ArrayList JavaDoc(1);
284             map.put(var, tmplist);
285         }
286         tmplist.add(expr);
287     }
288     private void addExprToVarsMap(ArrayList JavaDoc vars, XQueryExpression expr, Map JavaDoc map) {
289         for (int i = 0; i < vars.size(); i++) {
290             addExprToVarMap((Variable) vars.get(i), expr,map);
291         }
292     }
293
294     private void fillVarAssociatedMap(ArrayList JavaDoc vars) {
295         if (vars.size() < 2) return;
296         for (int i = 0; i < vars.size(); i++) {
297             ArrayList JavaDoc tmplist = (ArrayList JavaDoc) varAssociatedVariablesMap.get(vars.get(i));
298             if (tmplist == null) {
299                 tmplist = (ArrayList JavaDoc)vars.clone();
300                 tmplist.remove(i);
301                 varAssociatedVariablesMap.put(vars.get(i), tmplist);
302             }
303             else {
304                 for (int j = 0; j < vars.size(); j++) {
305                     if (j == i) continue;
306                     if (!tmplist.contains(vars.get(j)))
307                         tmplist.add(vars.get(j));
308                 }
309             }
310         }
311     }
312
313 }
314
Popular Tags