KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > xquark > mediator > algebra > AlgSourceEval


1 /*
2  * This file belongs to the XQuark distribution.
3  * Copyright (C) 2003 XQuark Group.
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.mediator.algebra;
24
25 import java.util.ArrayList JavaDoc;
26
27 import org.xquark.mediator.decomposer.GetVariablePathsVisitor;
28 import org.xquark.mediator.decomposer.IsolateWhereVisitor;
29 import org.xquark.mediator.plan.ExecutionPlan;
30 import org.xquark.mediator.plan.OpSourceEval;
31 import org.xquark.mediator.plan.Operator;
32 import org.xquark.mediator.runtime.MediatorException;
33 import org.xquark.xquery.parser.*;
34 import org.xquark.xquery.typing.*;
35 import org.xquark.xquery.typing.QTypeAtom;
36
37 /**
38  * This class represent the terminal leaf of the dependancy tree.
39  * It is generated after atomization (and is resulted from the transformation
40  * of DepSource that disappear).
41  *
42  */

43 public class AlgSourceEval extends Algebra {
44     // **********************************************************************
45
// * VERSIONING
46
// **********************************************************************
47
private static final String JavaDoc RCSRevision = "$Revision: 1.10 $";
48     private static final String JavaDoc RCSName = "$Name: $";
49     // **********************************************************************
50
// * CLASS VARIABLES
51
// **********************************************************************
52
//private boolean hasIdentifier = false;
53
// ************************************************************************
54
// * INITIALIZATION
55
// ************************************************************************
56
/**
57      * Create a node from a variable and an expression.
58      *
59      * To create the node, we take the original expression, and we
60      * extract everything relative to variable given by the leaf.
61      * from that, we create a new flower expression that will be associated
62      * to this DepNodeSource, and the old XQueryExpression that is
63      * referenced by it's DepNode is simplified.
64      * (bon, en gros, ça sert a faire une optimisation en faisant
65      * redescendre les restrictions (et plus tard les projection) vers les
66      * sources.
67      *
68      * @param typevisitor
69      * @param origexp
70      * @param varleaf
71      * @param depmanager
72      */

73     public AlgSourceEval(XQueryExpression origexp, ArrayList JavaDoc variables, AlgebraManager depmanager, boolean islet) throws MediatorException {
74         // to satisfy the super constructor, but expression and variable will be changed in following step.
75
super(origexp, variables, depmanager, islet);
76     }
77
78     public AlgSourceEval(XQueryExpression origexp, Variable varleaf, AlgebraManager depmanager, boolean islet) throws MediatorException {
79         // to satisfy the super constructor, but expression and variable will be changed in following step.
80
super(origexp, varleaf, depmanager, islet);
81     }
82
83     // ******************************************************************
84
// * METHODS
85
// ******************************************************************
86

87     public void ParentHasIdentifier() {
88         hasIdentifier = true;
89     }
90
91     /**
92      * Create the FLWRExpression like this :
93      * - F : is the variable varleaf that we consider with no changes
94      * (even the expression associated to it is kept ;
95      * - L : there is no Let at this point ;
96      * - W : is the major part, we must scan the origexp to see what
97      * to keep
98      * - R : for now, we return just the variable, but later, when
99      * we will care about prjection, we will make good
100      * LocatedExpression with it.
101      * @param origexp
102      * @param varleaf
103      */

104
105     private void createFLWRExpression() throws MediatorException {
106         if (!(expression instanceof FLWRExpression))
107             throw new MediatorException("DepNodeEval : " + expression + " is not a FLWR.");
108
109         try {
110             FLWRExpression origflwr = (FLWRExpression) expression;
111
112             // Variables
113
ArrayList JavaDoc newvariables = (ArrayList JavaDoc)variables.clone();
114             newvariables.addAll(this.parent.getVarsDependingOn());
115             newvariables.addAll(this.getVarsDependingOn());
116             //newvariables.addAll(this.parent.getVarsWhereOn());
117
newvariables.addAll(this.getVarsWhereOn());
118
119             // Where
120
XQueryExpression newWhereClause = null;
121             if (origflwr.getWhereClause() != null) {
122                 newWhereClause = (XQueryExpression) origflwr.getWhereClause().clone();
123                 IsolateWhereVisitor visitor = new IsolateWhereVisitor(newvariables);
124                 newWhereClause.accept(visitor);
125                 newWhereClause = visitor.getIsolatedExpression();
126                 // erase isolated where clause from original where clause
127
((FLWRExpression) expression).setWhereClause(visitor.getRemainingExpression());
128             }
129             
130             // update paths because where clause is not calculate in flwr
131
if (newWhereClause != null) {
132                 GetVariablePathsVisitor gvpv = new GetVariablePathsVisitor(true);
133                 newWhereClause.accept(gvpv);
134                 ArrayList JavaDoc tmplist = gvpv.getPaths();
135                 if (tmplist != null)
136                     for (int i = 0; i < tmplist.size(); i++)
137                         if (!this.algManager.getPathsList().contains(tmplist.get(i).toString()))
138                             paths.add(tmplist.get(i));
139             }
140             // OrderBy
141
ArrayList JavaDoc newOrderbys = null;
142 // if (origflwr.getOrderBy() != null) {
143
// newOrderbys = (ArrayList) origflwr.getOrderBy().clone();
144
// for (int i = 0, j = 0; i < newOrderbys.size(); i++, j++) {
145
// XQueryExpression exp = (XQueryExpression) newOrderbys.get(i);
146
// IsolateWhereVisitor visitor = new IsolateWhereVisitor(variables);
147
// exp.accept(visitor);
148
// exp = visitor.getIsolatedExpression();
149
// if (exp != null) {
150
// newOrderbys.set(i, exp);
151
// // if (visitor.getRemainingExpression() != null)
152
// // origflwr.getOrderBy().set(j,visitor.getRemainingExpression());
153
// // else {
154
// // origflwr.getOrderBy().remove(j);
155
// // j--;
156
// // }
157
// } else {
158
// newOrderbys.remove(i);
159
// i--;
160
// }
161
// }
162
// if (newOrderbys.isEmpty())
163
// newOrderbys = null;
164
// // if (origflwr.getOrderBy().isEmpty())
165
// // origflwr.setOrderBy(null);
166
// //if (origOrderby.isEmpty()) origflwr.setOrderBy(null);
167
// }
168
// create new FLWRExpression
169
// keep only single variable
170
// origflwr.setVariables(vars);
171
// recalculate sources if necessary
172
//if (vars.size() != ((FLWRExpression)expression).getVariables().size()) {
173
//}
174
// make return expression
175
FLWRExpression newFlwr = null;
176 // if (doReturn) {
177
// returncreated = true;
178
if (newWhereClause != null && newWhereClause.getQType() == null)
179                     newWhereClause.setQType(new QTypeAtom(this.algManager.getPlan().getSchemaManager().getBooleanType()));
180                 newFlwr = new FLWRExpression(variables, newOrderbys, newWhereClause, new XQueryVoid(expression.getParentModule()), expression.getParentModule());
181                 newFlwr.setParentModule(origflwr.getParentModule());
182 // } else
183
// newFlwr = new FLWRExpression(variables, newOrderbys, newWhereClause, new XQueryVoid(null), null);
184
// flwrcreated = true;
185
expression = newFlwr;
186         } catch (TypeException te) {
187             throw new MediatorException("DepNodeSource.createFLWRExpression : " + te.getMessage(), te);
188         } catch (CloneNotSupportedException JavaDoc e) {
189             throw new MediatorException("DepNodeSource.createFLWRExpression : " + e.getMessage(), e);
190         } catch (XQueryException e) {
191             throw new MediatorException("DepNodeSource.createFLWRExpression : " + e.getMessage(), e);
192         }
193     }
194
195     // ******************************************************************
196
// * ATOMIZE
197
// ******************************************************************
198
/**
199      *
200      * @return
201      */

202     /*
203     public ArrayList atomize () throws MediatorException
204     {
205             throw new MediatorException ("atomize should never been called at this step of the algorithm") ;
206     }
207      */

208     // ******************************************************************
209
// * INFORMATION
210
// ******************************************************************
211
/**
212      * See what sources are associated to this DepNodeSources
213      */

214
215     // ******************************************************************
216
// * CREATE EXECUTION PLAN
217
// ******************************************************************
218
/**
219      *
220      * @param plan
221      * @return
222      */

223     public Operator createOperator(ExecutionPlan plan) throws MediatorException {
224         createFLWRExpression();
225         Operator algebra = new OpSourceEval(plan, expression, paths, hasIdentifier);
226         algebra.isLet(islet);
227         return algebra;
228     }
229
230     public Object JavaDoc clone() throws CloneNotSupportedException JavaDoc {
231         AlgSourceEval newobj = (AlgSourceEval) super.clone();
232         newobj.hasIdentifier = this.hasIdentifier;
233         return newobj;
234     }
235
236     // ******************************************************************
237
// * GET/SET METHODS
238
// ******************************************************************
239
/**
240      *
241      * @return
242      */

243     //public ArrayList getPaths() { return paths ; }
244

245     /**
246      *
247      * @return
248      */

249     // public ArrayList getVarsThatDepend() throws MediatorException { return varsthatdepend ; }
250

251     // ******************************************************************
252
// * DEBUGGING
253
// ******************************************************************
254
/**
255      *
256      * @return
257      */

258 // public String toCompleteString(int indent) {
259
// StringBuffer buf = new StringBuffer();
260
//
261
// buf.append(Utils.makeIndent(indent) + "<NodeEval variables=\"" + variables + " sources=\"" + getSources() + "\" let=" + isLet() + "\" depends=" + depends + "\" mono source=" + isMonoSource() + "\">\n");
262
// buf.append(Utils.makeIndent(indent + 1) + "<Expression>\n");
263
// buf.append(Utils.makeIndent(indent + 2) + expression + "\n");
264
// buf.append(Utils.makeIndent(indent + 1) + "</Expression>\n");
265
// //buf.append("\t\t<Function>" + funcToString() + "</Function>\n") ;
266
// if (depchildren != null && !depchildren.isEmpty()) {
267
// buf.append(Utils.makeIndent(indent + 1) + "<Leaves>\n");
268
// for (int i = 0; i < depchildren.size(); i++) {
269
// buf.append(Utils.makeIndent(indent + 2) + "<Leave>\n");
270
// DepNode depchild = (DepNode) depchildren.get(i);
271
// buf.append(depchild.toCompleteString(indent + 3));
272
// buf.append(Utils.makeIndent(indent + 2) + "</Leave>\n");
273
// }
274
// buf.append(Utils.makeIndent(indent + 1) + "</Leaves>\n");
275
// }
276
// if (referenceDefinitionVars != null && !referenceDefinitionVars.isEmpty()) {
277
// buf.append(Utils.makeIndent(indent + 1) + "<referenceDefinitionVars>\n");
278
// for (int i = 0; i < referenceDefinitionVars.size(); i++) {
279
// Variable var = (Variable) referenceDefinitionVars.get(i);
280
// buf.append(Utils.makeIndent(indent + 2) + "<Var>" + var + "</Var>\n");
281
// }
282
// buf.append(Utils.makeIndent(indent + 1) + "</referenceDefinitionVars>\n");
283
// }
284
// if (dependDefinitionVars != null && !dependDefinitionVars.isEmpty()) {
285
// buf.append(Utils.makeIndent(indent + 1) + "<DependingOn>\n");
286
// for (int i = 0; i < dependDefinitionVars.size(); i++) {
287
// Variable var = (Variable) dependDefinitionVars.get(i);
288
// buf.append(Utils.makeIndent(indent + 2) + "<Var>" + var + "</Var>\n");
289
// }
290
// buf.append(Utils.makeIndent(indent + 1) + "</dependDefinitionVars>\n");
291
// }
292
// if (paths != null) {
293
// buf.append(Utils.makeIndent(indent + 1) + "<Paths>\n");
294
// for (int i = 0; i < paths.size(); i++)
295
// buf.append(Utils.makeIndent(indent + 2) + "<Path>" + paths.get(i) + "</Path>\n");
296
// buf.append(Utils.makeIndent(indent + 1) + "</Paths>\n");
297
// }
298
// buf.append(Utils.makeIndent(indent) + "</NodeEval>\n");
299
// return buf.toString();
300
// }
301

302     public void execute(ExecutionPlan plan) throws MediatorException {}
303
304 }
305
Popular Tags