KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > xquark > mediator > plan > Operator


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.mediator.plan;
24
25 import java.util.ArrayList JavaDoc;
26
27 import org.xquark.mediator.algebra.Algebra;
28 import org.xquark.mediator.decomposer.Utils;
29 import org.xquark.mediator.runtime.MediatorException;
30 import org.xquark.schema.SchemaManager;
31 import org.xquark.xquery.parser.XQueryExpression;
32 import org.xquark.xquery.typing.TypeVisitor;
33 public abstract class Operator {
34     // **********************************************************************
35
// * VERSIONING
36
// **********************************************************************
37
private static final String JavaDoc RCSRevision = "$Revision: 1.12 $";
38     private static final String JavaDoc RCSName = "$Name: $";
39
40     // **********************************************************************
41
// * CLASS VARIABLES
42
// **********************************************************************
43
public final static int ORDER_SERIALIZATION = 0;
44     public final static int ORDER_PARALLELIZATION = 1;
45
46     protected XQueryExpression expression = null;
47     protected ExecutionPlan plan = null;
48     protected Operator parentOperator = null;
49     protected ArrayList JavaDoc paths = null;
50     protected ArrayList JavaDoc pathslist = null;
51
52     protected ArrayList JavaDoc sources = null;
53     protected boolean islet = false;
54
55     protected int size = 0;
56     //protected ArrayList equivalences = null;
57
protected int idsize = 0;
58     //protected ArrayList idequivalences = null;
59

60     protected ArrayList JavaDoc indexesSpec = null;
61     protected boolean valueDepends = false;
62     protected boolean whereDepends = false;
63     
64     protected boolean prepared = false;
65     
66     // ***********************************************************************
67
// * INITIALIZATION
68
// ***********************************************************************
69
/**
70      *
71      */

72     public Operator(ExecutionPlan plan, XQueryExpression expression) throws MediatorException {
73         super();
74         this.plan = plan;
75         this.expression = expression;
76         resetPaths();
77     }
78
79     // ***********************************************************************
80
// * GET/SET METHODS
81
// ***********************************************************************
82

83     public XQueryExpression getExpression() {
84         return expression;
85     }
86
87     public void setExpression(XQueryExpression expression) {
88         this.expression = expression;
89     }
90
91     public int getSize() {
92         return size;
93     }
94     public int getIdSize() {
95         return idsize;
96     }
97     public void setDepends(Algebra depnode) {
98         valueDepends = !depnode.getVarsDependingOn().isEmpty();
99         whereDepends = !depnode.getVarsWhereOn().isEmpty();
100     }
101     public boolean getValueDepends() {
102         return valueDepends;
103     }
104     public void setValueDepends(boolean valueDepends) {
105         this.valueDepends = valueDepends;
106     }
107     public boolean getWhereDepends() {
108         return whereDepends;
109     }
110     public void setWhereDepends(boolean whereDepends) {
111         this.whereDepends = whereDepends;
112     }
113     public boolean getDepends() {
114         return valueDepends || whereDepends;
115     }
116     // methods for indexes handling
117
public ArrayList JavaDoc getIndexesSpec() {
118         return this.indexesSpec;
119     }
120     public ArrayList JavaDoc getIndexSpec(int index) {
121         if (indexesSpec == null || indexesSpec.size() <= index)
122             return null;
123         return (ArrayList JavaDoc) this.indexesSpec.get(index);
124     }
125     public int addIndexSpec(ArrayList JavaDoc list) {
126         if (this.indexesSpec == null)
127             this.indexesSpec = new ArrayList JavaDoc();
128         this.indexesSpec.add(list);
129         return this.indexesSpec.size();
130     }
131     public int addIndexSpecs(ArrayList JavaDoc list) {
132         if (this.indexesSpec == null)
133             this.indexesSpec = new ArrayList JavaDoc();
134         this.indexesSpec.addAll(list);
135         return this.indexesSpec.size();
136     }
137     // methods to handle parent operator
138
public void setParentOperator(Operator operator) {
139         this.parentOperator = operator;
140     }
141     public Operator getParentOperator() {
142         return parentOperator;
143     }
144     // methods to handle prepared status
145
public abstract void setPrepared() throws MediatorException;
146     
147 // public boolean getPrepared() {
148
// return prepared;
149
// }
150

151     public OperatorRunnable getOperatorRunnable(DynamicContext ctx) {
152         return new OperatorRunnable(this,ctx);
153     }
154     
155     // ***********************************************************************
156
// * EXECUTE QUERY
157
// ***********************************************************************
158
/**
159      *
160      */

161     protected abstract ResultSet executeQuery(DynamicContext context) throws MediatorException;
162
163     public Operator addCondition(ExecutionPlan plan, XQueryExpression expr) throws MediatorException {
164         return new OpRestrict(plan, expr, this);
165     }
166
167     // **********************************************************************
168
// * DEBUG
169
// **********************************************************************
170
/**
171      *
172      */

173     public String JavaDoc toCompleteString() {
174         return toCompleteString(0);
175     }
176
177     /**
178      *
179      */

180     public String JavaDoc toCompleteString(int indent) {
181         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
182         buf.append(Utils.makeIndent(indent) + "<Class>" + getClass() + "</Class>");
183         return buf.toString();
184     }
185
186     // ***********************************************************************
187
// * METHODS
188
// ***********************************************************************
189
public void resetPaths() {
190         if (this.paths != null)
191             this.paths.clear();
192         else
193             this.paths = new ArrayList JavaDoc();
194         if (this.pathslist != null)
195             this.pathslist.clear();
196         else
197             this.pathslist = new ArrayList JavaDoc();
198     }
199     public ArrayList JavaDoc getPaths() {
200         return paths;
201     }
202     public ArrayList JavaDoc getPathsList() {
203         return pathslist;
204     }
205     public void addPath(XQueryExpression pathi) {
206         if (pathi == null)
207             return;
208         if (!pathslist.contains(pathi.getStringValue())) {
209             paths.add(pathi);
210             pathslist.add(pathi.getStringValue());
211         }
212     }
213     public void addPaths(ArrayList JavaDoc addpaths) {
214         if (addpaths == null || addpaths.isEmpty())
215             return;
216         for (int i = 0; i < addpaths.size(); i++)
217             addPath((XQueryExpression) addpaths.get(i));
218     }
219     public ExecutionPlan getPlan() {
220         return plan;
221     }
222     public TypeVisitor getTypeVisitor() {
223         return plan.getTypeVisitor();
224     }
225     public SchemaManager getSchemaManager() {
226         return plan.getSchemaManager();
227     }
228
229     // **********************************************************************
230
// * OPTIMIZE
231
// **********************************************************************
232
/**
233      *
234      */

235 // public Operator optimize() throws MediatorException {
236
// Operator newnode = this;
237
// Operator worknode = this;
238
//
239
// // make projection as soon as possible
240
// worknode = newnode.optimizeProjection();
241
// if (worknode != null)
242
// newnode = worknode;
243
//
244
// // make clever restriction
245
// worknode = newnode.optimizeRestriction();
246
// if (worknode != null)
247
// newnode = worknode;
248
//
249
// // group by source(s).
250
// // don't work for now
251
// //worknode = newnode.optimizeSource () ;
252
// //if (worknode != null) newnode = worknode ;
253
//
254
// return newnode;
255
// }
256

257     /**
258      * If the *exactly* same set of source is used for an operation,
259      * then the subtree can be passed entierely to the source.
260      * Beware of processing this from the leaf to the node.
261      *
262      * <p>Note: If this method is not overloaded or if it returns null,
263      * it means that _this_ node will not be optimized.</p>
264      */

265 // public Operator optimizeSource() throws MediatorException {
266
// return null;
267
// }
268

269     /**
270      * Add projections on attributes the soonest as possible in order to
271      * manipulate the less data as possible. As soon an attribute isn't
272      * necessary, it must be unselected (by a projection on the complementary
273      * variables).
274      *
275      * <p>Note: If this method is not overloaded or if it returns null,
276      * it means that _this_ node will not be optimized.</p>
277      */

278 // public Operator optimizeProjection() throws MediatorException {
279
// return null;
280
// }
281

282     /**
283      *
284      *
285      * <p>Note: If this method is not overloaded or if it returns null,
286      * it means that _this_ node will not be optimized.</p>
287      */

288 // public Operator optimizeRestriction() throws MediatorException {
289
// return null;
290
// }
291

292     /**
293      * Merge *this* node with the subnode(s). As it should be called
294      * only from source, the returned node must be of type AlgZeroOp.
295      * If it weren't called from a source, it would be a possible case of bug.
296      * As I don't know yet how to test this case, be careful when using
297      * this method.
298      * The return node is a AlgSource where the XQueryExpression is the merging
299      * of all child(ren) nodes' expression.
300      */

301     //protected abstract Algebra mergeWithSub() throws MediatorException ;
302

303     /**
304      * If the set of sources handled by this node are exactly the same (order
305      * not important) then return true.
306      * An AlgZeroOp is alway true.
307      * An AlgUnOp is alway the same value than the value of its children.
308      * An AlgBinValue is true if both of its child are true and IF the
309      * set of sources handled by its children are exactly the same.
310      */

311 // protected abstract boolean shareSameSources();
312

313     /**
314      * Return true if the node is of type source.
315      * (AlgSource or AlgValue ie. all nodes of type AlgZeroOp).
316      */

317     public abstract boolean isSource();
318
319     /**
320      * Return all the sources depending of this node.
321      */

322     public abstract ArrayList JavaDoc getSources();
323
324     public void isLet(boolean islet) {
325         this.islet = islet;
326     }
327     public boolean isLet() {
328         return islet;
329     }
330     public abstract void terminate();
331
332 // protected void addEquivalence(ArrayList equivalences, int i, int j) {
333
// int[] couple = new int[2];
334
// couple[0] = i;
335
// couple[1] = j;
336
// equivalences.add(couple);
337
// }
338
}
339
Popular Tags