KickJava   Java API By Example, From Geeks To Geeks.

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


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
28 import org.xquark.xquery.normalize.*;
29 import org.xquark.xquery.parser.*;
30
31 public class IsolateWhereVisitor extends DefaultParserVisitor {
32     // **********************************************************************
33
// * VERSIONING
34
// **********************************************************************
35
private static final String JavaDoc RCSRevision = "$Revision: 1.9 $";
36     private static final String JavaDoc RCSName = "$Name: $";
37     // **********************************************************************
38
// * CLASS VARIABLES
39
// **********************************************************************
40
private ArrayList JavaDoc vars = null;
41     private ArrayList JavaDoc varsstr = null;
42     private boolean putvar = false;
43     boolean includenone = true;
44     private GetBoundVariablesVisitor gbv_visitor = null;
45
46     // the expresion that will be generated for child expression
47
private XQueryExpression isolatedExpression = null;
48     private XQueryExpression remainingExpression = null;
49     // the original expression (that will be pruned)
50
// private XQueryExpression root = null;
51
//private TypeVisitor typevisitor = null;
52

53     // **********************************************************************
54
// * INITIALIZATION
55
// **********************************************************************
56
/**
57      *
58      */

59     public IsolateWhereVisitor() {
60         init(null, new GetBoundVariablesVisitor(true, false));
61     }
62     public IsolateWhereVisitor(ArrayList JavaDoc vars) {
63         init(vars, new GetBoundVariablesVisitor(true, false));
64     }
65     public IsolateWhereVisitor(ArrayList JavaDoc vars, GetBoundVariablesVisitor gbv_visitor) {
66         init(vars, gbv_visitor);
67     }
68
69     private void init(ArrayList JavaDoc vars, GetBoundVariablesVisitor gbv_visitor) {
70         this.isolatedExpression = null;
71         this.remainingExpression = null;
72         this.vars = vars;
73         if (this.vars == null)
74             this.vars = new ArrayList JavaDoc();
75         varsstr = new ArrayList JavaDoc(this.vars.size());
76         for (int i = 0; i < this.vars.size(); i++)
77             varsstr.add(((Variable) this.vars.get(i)).getStringValue());
78         this.gbv_visitor = gbv_visitor;
79         gbv_visitor.reset();
80     }
81
82     public void includeNone(boolean includenone) {
83         this.includenone = includenone;
84     }
85
86     public void putVar(boolean putvar) {
87         this.putvar = putvar;
88     }
89     public XQueryExpression getIsolatedExpression() {
90         return isolatedExpression;
91     }
92     public XQueryExpression getRemainingExpression() {
93         return remainingExpression;
94     }
95     public void reset(ArrayList JavaDoc vars) {
96         init(vars, this.gbv_visitor);
97     }
98
99     // **********************************************************************
100
// * VISITATION
101
// **********************************************************************
102
// public void visit(AttributeValuePair arg) throws XQueryException
103

104     public void visit(BinOpANDExpression arg) throws XQueryException {
105         arg.getExpression1().accept(this);
106         //if (isolatedExpression == null && remainingExpression == null) return;
107
XQueryExpression isolated1 = isolatedExpression;
108         XQueryExpression remaining1 = remainingExpression;
109         arg.getExpression2().accept(this);
110         //if (isolatedExpression == null && remainingExpression == null) return;
111
XQueryExpression isolated2 = isolatedExpression;
112         XQueryExpression remaining2 = remainingExpression;
113
114         if (isolated1 == arg.getExpression1() && isolated2 == arg.getExpression2()) {
115             isolatedExpression = arg;
116             remainingExpression = null;
117         } else {
118             if (isolated1 != null)
119                 if (isolated2 != null)
120                     isolatedExpression = new BinOpANDExpression(isolated1, isolated2, null);
121                 else
122                     isolatedExpression = isolated1;
123             else
124                 isolatedExpression = isolated2;
125             if (remaining1 != null)
126                 if (remaining2 != null)
127                     remainingExpression = new BinOpANDExpression(remaining1, remaining2, null);
128                 else
129                     remainingExpression = remaining1;
130             else
131                 remainingExpression = remaining2;
132         }
133     }
134
135     public void visit(BinOpORExpression arg) throws XQueryException {
136
137         arg.getExpression1().accept(this);
138         //if (isolatedExpression == null && remainingExpression == null) return;
139
//XQueryExpression isolated1 = isolatedExpression;
140
XQueryExpression remaining1 = remainingExpression;
141         if (remaining1 != null) {
142             isolatedExpression = null;
143             remainingExpression = arg;
144             return;
145         }
146
147         arg.getExpression2().accept(this);
148         //if (isolatedExpression == null && remainingExpression == null) return;
149
//XQueryExpression isolated2 = isolatedExpression;
150
XQueryExpression remaining2 = remainingExpression;
151
152         if (remaining2 == null) {
153             isolatedExpression = arg;
154             remainingExpression = null;
155         } else {
156             isolatedExpression = null;
157             remainingExpression = arg;
158         }
159
160         // if (isolated1 == arg.getExpression1() && isolated2 == arg.getExpression2()) {
161
// isolatedExpression = arg;
162
// remainingExpression = null;
163
// } else {
164
// if (isolated1 != null)
165
// if (isolated2 != null)
166
// isolatedExpression = new BinOpORExpression(isolated1, isolated2, null);
167
// else
168
// isolatedExpression = isolated1;
169
// else
170
// isolatedExpression = isolated2;
171
// if (remaining1 != null)
172
// if (remaining2 != null)
173
// remainingExpression = new BinOpORExpression(remaining1, remaining2, null);
174
// else
175
// remainingExpression = remaining1;
176
// else
177
// remainingExpression = remaining2;
178
// }
179
}
180
181     // ************************************************************************
182

183     // public void visit(CastTreatExpression arg) throws XQueryException
184
// public void visit(ContextDeclaration arg) throws XQueryException
185
// public void visit(Dereference arg) throws XQueryException
186
// public void visit (Element arg) throws XQueryException
187
// for DepMem.createAlgebra
188
public void visit(FLWRExpression arg) throws XQueryException {
189         ArrayList JavaDoc variables = arg.getVariables();
190         // variable happens somewhere
191
if (putvar)
192             for (int i = 0; i < variables.size(); i++) {
193                 Variable tmpvar = (Variable) variables.get(i);
194                 if (!varsstr.contains(tmpvar.getStringValue())) {
195                     vars.add(tmpvar);
196                     varsstr.add(tmpvar.getStringValue());
197                 }
198             }
199         isolatedExpression = null;
200         XQueryExpression wexp = arg.getWhereClause();
201         if (wexp != null)
202             wexp.accept(this);
203         // if (isolatedExpression == null && remainingExpression == null) return;
204
XQueryExpression newwexp = isolatedExpression;
205
206         // variable happens somewhere
207
XQueryExpression rexp = arg.getReturnClause();
208         rexp.accept(gbv_visitor);
209         XQueryExpression newrexp = new XQueryExpressionSequence(gbv_visitor.getVariables(), null);
210
211         ArrayList JavaDoc flwrvars = arg.getVariables();
212
213         if (putvar)
214             isolatedExpression = new FLWRExpression(variables, newwexp, newrexp, null);
215         else
216             isolatedExpression = new FLWRExpression(vars, newwexp, newrexp, null);
217         isolatedExpression.setParentModule(arg.getParentModule());
218         remainingExpression = null;
219     }
220
221     public void visit(FunctionCall arg) throws XQueryException {
222         if (arg.getArguments() != null) {
223             for (int i = 0; i < arg.getArguments().size(); i++) {
224                 XQueryExpression expi = (XQueryExpression) arg.getArgument(i);
225                 expi.accept(this);
226                 if (isolatedExpression == null && remainingExpression == null)
227                     return;
228                 if (remainingExpression != null) {
229                     isolatedExpression = null;
230                     remainingExpression = arg;
231                     return;
232                 }
233             }
234         }
235         isolatedExpression = arg;
236         remainingExpression = null;
237     }
238
239     // public void visit(FunctionDefinition arg) throws XQueryException
240
// public void visit(InstanceOfExpression arg) throws XQueryException
241
// public void visit(InternalFunctionCall arg) throws XQueryException
242
// public void visit(ITEExpression arg) throws XQueryException
243
// public void visit(LibraryFunctionCall arg) throws XQueryException
244
// public void visit(ListOpAFTERExpression arg) throws XQueryException
245
// public void visit(ListOpArithExpression arg) throws XQueryException
246
// public void visit(ListOpBEFOREExpression arg) throws XQueryException
247
// public void visit(ListOpCompExpression arg) throws XQueryException {
248
// public void visit(ListOpEXCEPTExpression arg) throws XQueryException
249
// public void visit(ListOpINTERSECTExpression arg) throws XQueryException
250
// public void visit(ListOpUNIONExpression arg) throws XQueryException
251
// public void visit(LocatedExpression arg) throws XQueryException {
252
// public void visit(NodeType arg) throws XQueryException
253
// public void visit(Parameter arg) throws XQueryException
254
// public void visit(PrimitiveFunctionCall arg) throws XQueryException
255
// public void visit (QName arg) throws XQueryException
256
public void visit(QuantifiedExpression arg) throws XQueryException {
257         // not supported
258
isolatedExpression = null;
259         remainingExpression = null;
260         /*
261         vars.add(arg.getVariable());
262         makeSimple(arg) ;
263         vars.remove(arg.getVariable());
264         */

265     }
266     // public void visit(RangeExpression arg) throws XQueryException
267
// public void visit(RootNodeFunctionCall arg) throws XQueryException
268
// public void visit(SortedExpression arg) throws XQueryException
269
// public void visit(Step arg) throws XQueryException
270
// public void visit(TypeSwitchExpression arg) throws XQueryException
271
// public void visit(UnOpMinusExpression arg) throws XQueryException
272
// public void visit(Value arg) throws XQueryException
273
// public void visit(ValueBoolean arg) throws XQueryException
274
// public void visit(ValueFloat arg) throws XQueryException
275
// public void visit(ValueInteger arg) throws XQueryException
276
// public void visit(ValueString arg) throws XQueryException
277
public void visit(Variable arg) throws XQueryException {
278         if (varsstr.contains(arg.getStringValue())) {
279             isolatedExpression = arg;
280             remainingExpression = null;
281         } else {
282             isolatedExpression = null;
283             remainingExpression = arg;
284         }
285     }
286
287     // public void visit (XQueryBinaryOperatorExpression arg) throws XQueryException
288
// public void visit(XQueryBooleanOperatorExpression arg) throws XQueryException
289
public void visit(XQueryExpression arg) throws XQueryException {
290         // default behavior
291
gbv_visitor.reset();
292         arg.accept(gbv_visitor);
293         ArrayList JavaDoc boundvars = gbv_visitor.getVariables();
294
295         if (boundvars != null && !boundvars.isEmpty())
296             for (int i = 0; i < boundvars.size(); i++)
297                 if (!varsstr.contains(((Variable) boundvars.get(i)).getStringValue())) {
298                     isolatedExpression = null;
299                     remainingExpression = arg;
300                     return;
301                 }
302         if ((boundvars == null || boundvars.isEmpty()) && !includenone) {
303             isolatedExpression = null;
304             remainingExpression = arg;
305         } else {
306             isolatedExpression = arg;
307             remainingExpression = null;
308         }
309     }
310
311     public void visit(XQueryExpressionSequence arg) throws XQueryException {
312         ArrayList JavaDoc sequence = arg.getSubExpressions();
313         ArrayList JavaDoc alnew = new ArrayList JavaDoc();
314         ArrayList JavaDoc alold = new ArrayList JavaDoc();
315         for (int i = 0; i < sequence.size(); i++) {
316             XQueryExpression expi = (XQueryExpression) sequence.get(i);
317             expi.accept(this);
318             XQueryExpression isolated = isolatedExpression;
319             XQueryExpression remaining = remainingExpression;
320             if (isolated != null)
321                 alnew.add(isolated);
322             if (remaining != null)
323                 alold.add(remaining);
324         }
325
326         if (alnew.isEmpty())
327             isolatedExpression = null;
328         else {
329             isolatedExpression = new XQueryExpressionSequence(alnew, null);
330             isolatedExpression.setParenthesis(true);
331         }
332         if (alold.isEmpty())
333             remainingExpression = null;
334         else {
335             remainingExpression = new XQueryExpressionSequence(alold, null);
336             remainingExpression.setParenthesis(true);
337         }
338     }
339
340     // public void visit(XQueryListBinaryOperatorExpression arg) throws XQueryException {
341
// public void visit(XQueryListUnaryOperatorExpression arg) throws XQueryException
342
// public void visit(XQueryUnaryOperatorExpression arg) throws XQueryException {
343
}
344
Popular Tags