KickJava   Java API By Example, From Geeks To Geeks.

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


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.decomposer.Utils;
28 import org.xquark.mediator.runtime.MediatorException;
29 import org.xquark.xquery.parser.XQueryExpression;
30
31 /**
32  * This class is a binary node that is a component of the execution tree.
33  * Execution can be serialized or parallelized.
34  *
35  */

36 public abstract class OpMult extends Operator {
37     // **********************************************************************
38
// * VERSIONING
39
// **********************************************************************
40
private static final String JavaDoc RCSRevision = "$Revision: 1.6 $";
41     private static final String JavaDoc RCSName = "$Name: $";
42
43     // **********************************************************************
44
// * CLASS VARIABLES
45
// **********************************************************************
46

47     protected Operator[] childrenOperator = null;
48     protected int ordertype = ORDER_SERIALIZATION;
49
50     // ***********************************************************************
51
// * INITIALIZATION
52
// ***********************************************************************
53
/**
54      * Construct an operator of the execution plan handling deux childs
55      * expressions. This method must be overrided by descendant, but a
56      * super (...) on this should always be called first by it's descendant
57      * constructor in order to avoid problems.
58      *
59      * @param plan a reference on the general execution plan.
60      * @param expression the expression handled by _this_ operator.
61      * @param leftexp a reference on the left child operator.
62      * @param rightexp a refrence on the right child operator.
63      *
64      * @param plan
65      * @param expression
66      * @param leftexp
67      * @param rightexp
68      * @return
69      */

70     public OpMult(ExecutionPlan plan, XQueryExpression expression, Operator left, Operator right) throws MediatorException {
71         super(plan, expression);
72         childrenOperator = new Operator[2];
73         childrenOperator[0] = left;
74         childrenOperator[1] = right;
75         init();
76     }
77
78     public OpMult(ExecutionPlan plan, XQueryExpression expression, Operator[] operators) throws MediatorException {
79         super(plan, expression);
80         this.childrenOperator = operators;
81         init();
82     }
83
84     private void init() throws MediatorException {
85         // Order is important
86
for (int i = 0; i < childrenOperator.length; i++) {
87             childrenOperator[i].setParentOperator(this);
88             addPaths(childrenOperator[i].getPaths());
89             //algebrai.incParentCount();
90
}
91     }
92
93     // #############################################################################
94
// VISITOR STUFF
95
// #############################################################################
96

97     public void accept(OperatorVisitor visitor) throws MediatorException {
98         visitor.visit(this);
99     }
100
101     // ***********************************************************************
102
// * EXECUTE QUERY
103
// ***********************************************************************
104
/**
105      * Start the execution, as a binary node, it start the execution
106      * of the two children it refers. The execution is parallelized or
107      * serialized depending the value of 'ordertype' field that can be
108      * ORDER_SERIALIZATION or ORDER_PARALLELIZATION.
109      */

110
111     public ResultSet executeQuery(DynamicContext context) throws MediatorException {
112         try {
113             OperatorRunnable[] runnables = new OperatorRunnable[childrenOperator.length];
114             if (ordertype == ORDER_SERIALIZATION) {
115                 for (int i = 0; i < childrenOperator.length; i++) {
116                     OperatorRunnable ori = childrenOperator[i].getOperatorRunnable(context);
117                     runnables[i] = ori;
118                     ori.run();
119                 }
120             } else if (ordertype == ORDER_PARALLELIZATION) {
121                 Thread JavaDoc[] threads = new Thread JavaDoc[childrenOperator.length];
122                 for (int i = 0; i < childrenOperator.length; i++) {
123                     OperatorRunnable ori = childrenOperator[i].getOperatorRunnable((DynamicContext)context.clone());
124                     runnables[i] = ori;
125                     threads[i] = new Thread JavaDoc(ori);
126                     threads[i].start();
127                 }
128                 // wait for all threads to exit
129
for (int i = 0; i < threads.length; i++) {
130                     Thread JavaDoc thread = threads[i];
131                     if (thread.isAlive())
132                         thread.join();
133                 }
134             }
135             return getResultSet(context, runnables);
136         } catch (InterruptedException JavaDoc e) {
137             throw new MediatorException("AlgMultOp.executeQuery: " + e.getMessage(), e);
138         }
139     }
140
141     /**
142      * @param runnables
143      * @return
144      */

145     protected abstract ResultSet getResultSet(DynamicContext context, OperatorRunnable[] runnables) throws MediatorException;
146
147     // ***********************************************************************
148
// * GET/SET METHODS
149
// ***********************************************************************
150
/**
151      * Return a reference on the child at given position expression.
152      *
153      * @return a reference on the child at given position expression.
154      */

155 // public Operator getSubElement(int pos) {
156
// if (pos < 0 || pos >= childrenOperator.length)
157
// return null;
158
// return childrenOperator[pos];
159
// }
160
//
161
// public int getSubElementsSize() {
162
// return childrenOperator.length;
163
// }
164
// **********************************************************************
165
// * OPTIMIZE
166
// **********************************************************************
167
/**
168      * If the *exactly* same set of source is used for an operation,
169      * then the subtree can be passed entierely to the source.
170      * Beware of processing this from the leaf to the node.
171      *
172      * <p>Note: If this method is not overloaded or if it returns null,
173      * it means that _this_ node will not be optimized.</p>
174      * @return
175      */

176     // public Operator optimizeSource() throws MediatorException {
177
// for (int i = 0; i < operatorList.size(); i++)
178
// ((Operator) operatorList.get(i)).optimizeSource();
179
// /*
180
// if (shareSameSources()) {
181
// Algebra node = mergeWithSub() ;
182
// return node ;
183
// }
184
// */
185
// return null;
186
// }
187

188     /**
189      * Add projections on attributes the soonest as possible in order to
190      * manipulate the less data as possible. As soon an attribute isn't
191      * necessary, it must be unselected (by a projection on the complementary
192      * variables).
193      *
194      * <p>Note: If this method is not overloaded or if it returns null,
195      * it means that _this_ node will not be optimized.</p>
196      * @return
197      */

198     public Operator optimizeProjection() throws MediatorException {
199         return null;
200     }
201
202     /**
203      *
204      *
205      * <p>Note: If this method is not overloaded or if it returns null,
206      * it means that _this_ node will not be optimized.</p>
207      * @return
208      */

209     public Operator optimizeRestriction() throws MediatorException {
210         return null;
211     }
212
213     /**
214      * If the set of sources handled by this node are exactly the same (order
215      * not important) then return true.
216      * An AlgBinValue is true if both of its child are true and IF the
217      * set of sources handled by its children are exactly the same.
218      * @return
219      */

220     // protected boolean shareSameSources() {
221
// for (int i = 0; i < operatorList.size(); i++)
222
// if (((Operator) operatorList.get(i)).shareSameSources() == false)
223
// return false;
224
// if (compareSource())
225
// return true;
226
// return false;
227
// }
228

229     /**
230      * Compare set of sources.
231      * @param algebra1
232      * @param algebra2
233      * @return
234      */

235     // private boolean compareSource() {
236
// ArrayList reflist = null;
237
// for (int i = 0; i < operatorList.size(); i++) {
238
// Operator algebrai = (Operator) operatorList.get(i);
239
// if (i == 0)
240
// reflist = algebrai.getSources();
241
// else {
242
// ArrayList tmplist = algebrai.getSources();
243
// if (tmplist.size() != reflist.size() || !reflist.containsAll(tmplist))
244
// return false;
245
// }
246
// }
247
// return true;
248
// }
249

250     /**
251      * Return true if the node is of type source.
252      * AlgBinOp is not a source.
253      *
254      * @return false ;
255      */

256     public boolean isSource() {
257         return false;
258     }
259
260     /**
261      * Return all the sources depending of this node.
262      * @return
263      */

264     public ArrayList JavaDoc getSources() {
265         if (sources == null) {
266             sources = new ArrayList JavaDoc();
267             for (int i = 0; i < childrenOperator.length; i++) {
268                 ArrayList JavaDoc tmplist = childrenOperator[i].getSources();
269                 for (int j = 0; j < tmplist.size(); j++) {
270                     Object JavaDoc sourcej = tmplist.get(j);
271                     if (!sources.contains(sourcej))
272                         sources.add(sourcej);
273                 }
274             }
275         }
276         return sources;
277     }
278
279     public void terminate() {
280         for (int i = 0; i < childrenOperator.length; i++)
281             childrenOperator[i].terminate();
282     }
283
284     public void setPrepared() throws MediatorException {
285         if (prepared) return;
286         prepared = true;
287         for (int i = 0; i < childrenOperator.length; i++)
288             childrenOperator[i].setPrepared();
289     }
290
291     public String JavaDoc toCompleteString(int indent) {
292         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
293         buf.append(Utils.makeIndent(indent) + "<" + getClass().getName() + " size=" + size + " id size=" + idsize + " isLet=\"" + islet + "\" valueDepends=\"" + valueDepends + "\" whereDepends=\"" + whereDepends + "\">\n");
294
295         buf.append(Utils.makeIndent(indent + 1) + "<Expression>\n");
296         buf.append(Utils.makeIndent(indent + 2) + expression + "\n");
297         buf.append(Utils.makeIndent(indent + 1) + "</Expression>\n");
298         // if (variables != null) {
299
// buf.append(Utils.makeIndent(indent + 1) + "<Variables>\n") ;
300
// for (int i = 0 ; i < variables.size() ; i ++) {
301
// buf.append(Utils.makeIndent(indent + 2) + "<Variable>" + variables.get(i) + "</Variable>\n") ;
302
// }
303
// buf.append(Utils.makeIndent(indent + 1) + "</Variables>\n") ;
304
// }
305
if (paths != null) {
306             buf.append(Utils.makeIndent(indent + 1) + "<Paths>\n");
307             for (int i = 0; i < paths.size(); i++) {
308                 buf.append(Utils.makeIndent(indent + 2) + "<Path>" + paths.get(i) + "</Path>\n");
309             }
310             buf.append(Utils.makeIndent(indent + 1) + "</Paths>\n");
311         }
312
313         for (int i = 0; i < childrenOperator.length; i++) {
314             buf.append(Utils.makeIndent(indent + 1) + "<Element" + (i + 1) + ">\n");
315             buf.append(childrenOperator[i].toCompleteString(indent + 2));
316             buf.append(Utils.makeIndent(indent + 1) + "</Element" + (i + 1) + ">\n");
317         }
318         buf.append(Utils.makeIndent(indent) + "</" + getClass().getName() + ">\n");
319         return buf.toString();
320     }
321 }
322
Popular Tags