KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > xquark > extractor > algebra > BuildReferredAttributes


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

22
23 package org.xquark.extractor.algebra;
24
25 import java.util.*;
26
27 import org.xquark.extractor.common.SqlWrapperException;
28 import org.xquark.extractor.xfunctions.XFunction;
29
30 /**
31  * Set the {@link org.xquark.extractor.algebra.Expression#_referredAttributes}
32  * field for an algebra tree with the attributes encountered during the subtree
33  * with the exception of {@link Relation#visibleTableInstances()} which are
34  * removed.
35  */

36 public final class BuildReferredAttributes extends DefaultCompleteVisitor {
37
38     private static final String JavaDoc RCSRevision = "$Revision: 1.5 $";
39     private static final String JavaDoc RCSName = "$Name: $";
40
41
42     private Stack _returnStack = new Stack();
43
44     public BuildReferredAttributes() {
45     }
46
47     public void visit(Expression arg) throws SqlWrapperException {
48         _returnStack.push(null);
49     }
50
51     public void visit(BinaryAlgebra arg) throws SqlWrapperException {
52         Set tmpCol = new HashSet();
53
54         arg.getLeftOperand().accept(this);
55         Set popped = (Set)_returnStack.pop();
56         if ( null != popped ) {
57             tmpCol.addAll(popped);
58         }
59
60         arg.getRightOperand().accept(this);
61         popped = (Set)_returnStack.pop();
62         if ( null != popped ) {
63             tmpCol.addAll(popped);
64         }
65
66         arg.setReferredAttributes(tmpCol);
67         _returnStack.push(tmpCol);
68     }
69
70     public void visit(Join arg) throws SqlWrapperException {
71         Set retVal = new HashSet();
72         Set tmpSet = null;
73         List list = arg.getOperandList();
74         Expression expr = null;
75         if (null != list) {
76             for (int i = 0; i < list.size(); i++) {
77                 expr = (Expression)list.get(i);
78                 expr.accept(this);
79                 tmpSet = (Set)_returnStack.pop();
80                 if (null != tmpSet) {
81                     retVal.addAll(tmpSet);
82                 }
83             }
84         }
85
86         list = arg.getPredicateList();
87         if (null != list) {
88             for (int i = 0; i < list.size(); i++) {
89                 expr = (Expression)list.get(i);
90                 expr.accept(this);
91                 tmpSet = (Set)_returnStack.pop();
92                 if (null != tmpSet) {
93                     retVal.addAll(tmpSet);
94                 }
95             }
96         }
97
98         List localAttributeList = new ArrayList();
99         Set localTableInstance = ((Relation)arg).visibleTableInstances();
100         AttributeExpression attr = null;
101         for (Iterator i = retVal.iterator(); i.hasNext(); ) {
102             attr = (AttributeExpression)i.next();
103             if (localTableInstance.contains(attr.getTableInstance())) {
104                 localAttributeList.add(attr);
105             }
106         }
107
108         retVal.removeAll(localAttributeList);
109
110         if (retVal.isEmpty()) {
111             retVal = null;
112         }
113
114         _returnStack.push(retVal);
115     }
116
117
118     public void visit(BinOpOuterJoin arg) throws SqlWrapperException {
119         Set retVal = new HashSet();
120         Set tmpSet = null;
121
122         arg.getLeftOperand().accept(this);
123         tmpSet = (Set)_returnStack.pop();
124         if (null != tmpSet) {
125             retVal.addAll(tmpSet);
126         }
127
128         arg.getRightOperand().accept(this);
129         tmpSet = (Set)_returnStack.pop();
130         if (null != tmpSet) {
131             retVal.addAll(tmpSet);
132         }
133
134
135         List list = arg.getPredicateList();
136         Expression expr = null;
137         if (null != list) {
138             for (int i = 0; i < list.size(); i++) {
139                 expr = (Expression)list.get(i);
140                 expr.accept(this);
141                 tmpSet = (Set) _returnStack.pop();
142                 if (null != tmpSet) {
143                     retVal.addAll(tmpSet);
144                 }
145             }
146         }
147
148         List localAttributeList = new ArrayList();
149         Set localTableInstance = ((Relation)arg).visibleTableInstances();
150         AttributeExpression attr = null;
151         for (Iterator i = retVal.iterator(); i.hasNext(); ) {
152             attr = (AttributeExpression)i.next();
153             if (localTableInstance.contains(attr.getTableInstance())) {
154                 localAttributeList.add(attr);
155             }
156         }
157
158         retVal.removeAll(localAttributeList);
159
160         if (retVal.isEmpty()) {
161             retVal = null;
162         }
163
164         _returnStack.push(retVal);
165     }
166
167     public void visit(UnaryAlgebra arg) throws SqlWrapperException {
168         arg.getOperand().accept(this);
169         Set retVal = (Set)_returnStack.pop();
170
171         if (null == retVal) {
172             retVal = new HashSet();
173         }
174
175         List list = arg.getParameterList();
176         Expression expr = null;
177         if (null != list) {
178             for (int i = 0; i < list.size(); i++) {
179                 expr = (Expression)list.get(i);
180                 expr.accept(this);
181                 Set tmpSet = (Set) _returnStack.pop();
182                 if (null != tmpSet) {
183                     retVal.addAll(tmpSet);
184                 }
185             }
186         }
187
188         List localAttributeList = new ArrayList();
189         Set localTableInstance = ((Relation)arg).visibleTableInstances();
190         AttributeExpression attr = null;
191         for (Iterator i = retVal.iterator(); i.hasNext(); ) {
192             attr = (AttributeExpression)i.next();
193             if (localTableInstance.contains(attr.getTableInstance()) ||
194                 attr.getTableInstance() == null ) {
195                 localAttributeList.add(attr);
196             }
197         }
198
199         retVal.removeAll(localAttributeList);
200
201         _returnStack.push(retVal);
202     }
203
204     public void visit(AttributeExpression arg) throws SqlWrapperException {
205         Set tmpCol = new HashSet();
206         tmpCol.add(arg);
207         arg.setReferredAttributes(tmpCol);
208
209         _returnStack.push(tmpCol);
210     }
211
212     public void visit(BinaryAtomicOp arg) throws SqlWrapperException {
213         Set tmpCol = new HashSet();
214
215         arg.getLeftOperand().accept(this);
216         Set popped = (Set)_returnStack.pop();
217         if ( null != popped ) {
218             tmpCol.addAll(popped);
219         }
220
221         arg.getRightOperand().accept(this);
222         popped = (Set)_returnStack.pop();
223         if ( null != popped ) {
224             tmpCol.addAll(popped);
225         }
226
227         arg.setReferredAttributes(tmpCol);
228         _returnStack.push(tmpCol);
229     }
230
231     public void visit(OuterJoinPredicate arg) throws SqlWrapperException {
232         arg.getPredicate().accept(this);
233
234         Set tmpCol = (Set)_returnStack.peek();
235         arg.setReferredAttributes(tmpCol);
236     }
237
238     public void visit(UnaryAtomicOp arg) throws SqlWrapperException {
239         arg.getOperand().accept(this);
240
241         Set tmpCol = (Set)_returnStack.peek();
242         arg.setReferredAttributes(tmpCol);
243     }
244
245     public void visit(XFunction arg) throws SqlWrapperException
246     {
247         Set retVal = null;
248         List argList = arg.getArguments();
249         if (null != argList){
250             retVal= new HashSet();
251             Object JavaDoc aArg = null;
252             for (int i = 0; i < argList.size(); i++) {
253
254                 aArg = argList.get(i);
255                 if (aArg instanceof Expression) {
256                     ((Expression)aArg).accept(this);
257                     Set popped = (Set)_returnStack.pop();
258                     if ( null != popped ) {
259                         retVal.addAll(popped);
260                     }
261                 }
262             }
263             if (retVal.isEmpty()) {
264                 retVal = null;
265             }
266         }
267
268         arg.setReferredAttributes(retVal);
269         _returnStack.push(retVal);
270     }
271 }
272
Popular Tags