KickJava   Java API By Example, From Geeks To Geeks.

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


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.DOMUtils.DOMUtils;
28 import org.xquark.mediator.DOMUtils.EvaluationVisitor;
29 import org.xquark.mediator.DOMUtils.Tuple;
30 import org.xquark.mediator.algebra.Algebra;
31 import org.xquark.mediator.runtime.MediatorException;
32 import org.xquark.xpath.datamodel.TypedNode;
33 import org.xquark.xquery.parser.Variable;
34 import org.xquark.xquery.parser.XQueryException;
35 import org.xquark.xquery.parser.XQueryExpression;
36
37 public class OpNotSource extends OpUn {
38     // **********************************************************************
39
// * VERSIONING
40
// **********************************************************************
41
private static final String JavaDoc RCSRevision = "$Revision: 1.12 $";
42     private static final String JavaDoc RCSName = "$Name: $"; // * INITIALIZATION
43
// ***********************************************************************
44
private boolean onlyVar = false;
45
46     public OpNotSource(ExecutionPlan plan, XQueryExpression expression, Variable var, Operator childexp, boolean isLet) throws MediatorException {
47         super(plan, expression, childexp);
48         init(var);
49         this.isLet(isLet);
50         idsize = childOperator.getIdSize();
51         if (plan.getIdVars().contains(var)) {
52             idsize++;
53         }
54     }
55
56     // public AlgNotSource(ExecutionPlan plan, XQueryExpression expression, Variable var, boolean isLet, boolean hasIdentifier) throws MediatorException {
57
// super(plan, expression, null);
58
// init(var);
59
// this.isLet(isLet);
60
// if (hasIdentifier) {
61
// idsize = 1;
62
// }
63
// }
64

65     public OpNotSource(ExecutionPlan plan, Algebra depnode) throws MediatorException {
66         super(plan, depnode.getExpression(), null);
67         if (depnode.getPaths().isEmpty())
68             init((Variable) depnode.getVariables().get(0));
69         else {
70             addPaths(depnode.getPaths());
71             size = paths.size();
72         }
73         this.isLet(depnode.isLet());
74         if (depnode.hasIdentifier()) {
75             idsize = 1;
76         }
77     }
78
79     private void init(Variable var) {
80         onlyVar = true;
81         resetPaths();
82         addPath(var);
83         size = 1;
84     }
85
86     public boolean isOnlyVar() {
87         return onlyVar;
88     }
89
90     // #############################################################################
91
// VISITOR STUFF
92
// #############################################################################
93

94     public void accept(OperatorVisitor visitor) throws MediatorException {
95         visitor.visit(this);
96     }
97
98     // ***********************************************************************
99
// * EXECUTE QUERY
100
// ***********************************************************************
101
/**
102      *
103      */

104     protected ResultSet getResultSet(DynamicContext context, OperatorRunnable child) throws MediatorException {
105         return new NotSourceResultSet(this, context, child);
106     }
107
108     // **********************************************************************
109
// * OPTIMIZE
110
// **********************************************************************
111

112     // **********************************************************************
113
// * DEBUG
114
// **********************************************************************
115
/**
116      *
117      */

118 }
119
120 class NotSourceResultSet extends UnResultSet {
121     // **********************************************************************
122
// * VERSIONING
123
// **********************************************************************
124
private static final String JavaDoc RCSRevision = "$Revision: 1.12 $";
125     private static final String JavaDoc RCSName = "$Name: $"; // * INITIALIZATION
126
// ***********************************************************************
127

128     EvaluationVisitor visitor = null;
129     /**
130      *
131      */

132     public NotSourceResultSet(OpNotSource operator, DynamicContext context, OperatorRunnable child) throws MediatorException {
133         super(operator, context, child);
134     }
135
136     // ***********************************************************************
137
// * EVALUATE IMPLEMENTATION
138
// ***********************************************************************
139
/*
140      * @param non_blocking if the evaluation is blocking, we must
141      * generate all the results before passing.
142      */

143     protected void evaluate(boolean non_blocking) throws MediatorException {
144
145         boolean islet = this.operator.isLet();
146         if (islet)
147             non_blocking = false;
148         Tuple newtuple = null;
149         if (resultset == null) {
150             if (expression != null) {
151                 ArrayList JavaDoc nodes = DOMUtils.evalNodes(context, expression, null);
152                 if (nodes != null) {
153                     if (islet) {
154                         if (((OpNotSource) operator).isOnlyVar()) {
155                             newtuple = buftuples.newTuple();
156                             for (int i = 0; i < nodes.size(); i++) {
157                                 newtuple.addNodeAtIndex(0, (TypedNode) nodes.get(i));
158                             }
159                             newtuple.fillIdentifiers();
160                             buftuples.add(newtuple);
161                         } else {
162                             newtuple = buftuples.newTuple();
163                             for (int i = 0; i < nodes.size(); i++) {
164                                 ArrayList JavaDoc nodelist = new ArrayList JavaDoc(1);
165                                 nodelist.add(nodes.get(i));
166                                 for (int j = 0; j < paths.size(); j++) {
167                                     ArrayList JavaDoc reslist = DOMUtils.getSteps(nodelist, (XQueryExpression) paths.get(j));
168                                     newtuple.addNodesAtIndex(j, reslist);
169                                 }
170                             }
171                             newtuple.fillIdentifiers();
172                             buftuples.add(newtuple);
173                         }
174                     } else {
175                         if (((OpNotSource) operator).isOnlyVar()) {
176                             for (int i = 0; i < nodes.size(); i++) {
177                                 newtuple = buftuples.newTuple();
178                                 newtuple.addNodeAtIndex(0, (TypedNode) nodes.get(i));
179                                 newtuple.fillIdentifiers();
180                                 buftuples.add(newtuple);
181                             }
182                         } else {
183                             for (int i = 0; i < nodes.size(); i++) {
184                                 ArrayList JavaDoc nodelist = new ArrayList JavaDoc(1);
185                                 nodelist.add(nodes.get(i));
186                                 newtuple = buftuples.newTuple();
187                                 for (int j = 0; j < paths.size(); j++) {
188                                     ArrayList JavaDoc reslist = DOMUtils.getSteps(nodelist, (XQueryExpression) paths.get(j));
189                                     newtuple.addNodesAtIndex(j, reslist);
190                                 }
191                                 newtuple.fillIdentifiers();
192                                 buftuples.add(newtuple);
193                             }
194                         }
195                     }
196                 }
197             }
198             setFinishedWhenEmpty();
199         } else {
200             while (true) {
201                 if (resultset.hasNext()) {
202                     Tuple tuple = resultset.next();
203                     if (expression == null) {
204                         if (islet) {
205                             if (newtuple == null) {
206                                 newtuple = tuple.getPart(0, operator.getIdSize());
207                                 buftuples.add(newtuple);
208                             } else
209                                 newtuple.addNodesAtIndex(0, tuple.getNodesAtIndex(0));
210                         } else {
211                             int index = 0;
212                             while ((newtuple = tuple.getPart(0, index++, operator.getIdSize())) != null) {
213                                 buftuples.add(newtuple);
214                             }
215                         }
216                     } else { // expression is not null
217
try {
218                             ArrayList JavaDoc tuples = context.getCurrentTuples();
219                             tuples.add(tuple);
220                             if (visitor == null)
221                                 visitor = new EvaluationVisitor(this.operator.getPlan().getSchemaManager());
222                             visitor.reset(tuples);
223                             visitor.setReturnType(EvaluationVisitor.NODE_TYPE);
224                             expression.accept(visitor);
225                             ArrayList JavaDoc nodes = visitor.getResNodes();
226                             if (nodes != null) {
227                                 if (islet) {
228                                     if (((OpNotSource) operator).isOnlyVar()) {
229                                         if (newtuple == null) {
230                                             newtuple = buftuples.newTuple();
231                                             newtuple.addNodesAtIndex(0, nodes);
232                                             newtuple.fillIdentifiers();
233                                             buftuples.add(newtuple);
234                                         } else
235                                             newtuple.addNodesAtIndex(0, nodes);
236                                     } else {
237                                         if (newtuple == null) {
238                                             newtuple = buftuples.newTuple();
239                                             for (int i = 0; i < nodes.size(); i++) {
240                                                 ArrayList JavaDoc nodelist = new ArrayList JavaDoc(1);
241                                                 nodelist.add(nodes.get(i));
242                                                 for (int j = 0; j < paths.size(); j++) {
243                                                     ArrayList JavaDoc reslist = DOMUtils.getSteps(nodelist, (XQueryExpression) paths.get(j));
244                                                     newtuple.addNodesAtIndex(j, reslist);
245                                                 }
246                                             }
247                                             newtuple.fillIdentifiers();
248                                             buftuples.add(newtuple);
249                                         } else {
250                                             for (int i = 0; i < nodes.size(); i++) {
251                                                 ArrayList JavaDoc nodelist = new ArrayList JavaDoc(1);
252                                                 nodelist.add(nodes.get(i));
253                                                 for (int j = 0; j < paths.size(); j++) {
254                                                     ArrayList JavaDoc reslist = DOMUtils.getSteps(nodelist, (XQueryExpression) paths.get(j));
255                                                     newtuple.addNodesAtIndex(j, reslist);
256                                                 }
257                                             }
258                                         }
259                                     }
260                                 } else {
261                                     if (((OpNotSource) operator).isOnlyVar()) {
262                                         for (int i = 0; i < nodes.size(); i++) {
263                                             newtuple = buftuples.newTuple();
264                                             newtuple.addNodeAtIndex(0, (TypedNode) nodes.get(i));
265                                             newtuple.fillIdentifiers();
266                                             buftuples.add(newtuple);
267                                         }
268                                     } else {
269                                         for (int i = 0; i < nodes.size(); i++) {
270                                             ArrayList JavaDoc nodelist = new ArrayList JavaDoc(1);
271                                             nodelist.add(nodes.get(i));
272                                             newtuple = buftuples.newTuple();
273                                             for (int j = 0; j < paths.size(); j++) {
274                                                 ArrayList JavaDoc reslist = DOMUtils.getSteps(nodelist, (XQueryExpression) paths.get(j));
275                                                 newtuple.addNodesAtIndex(j, reslist);
276                                             }
277                                             newtuple.fillIdentifiers();
278                                             buftuples.add(newtuple);
279                                         }
280                                     }
281                                 }
282                             }
283                             tuples.remove(tuple);
284                         } catch (XQueryException e) {
285                             throw new MediatorException("AlgNotSource.evaluate: " + e.getMessage(), e);
286                         }
287                     }
288                     if (non_blocking) {
289                         // can make higher buffering by modifying this test
290
if ((buftuples != null) && (!buftuples.isEmpty()))
291                             break;
292                     }
293                 } else {
294                     setFinishedWhenEmpty();
295                     resultset.close();
296                     return;
297                 }
298             }
299             if (!resultset.hasNext()) {
300                 setFinishedWhenEmpty();
301                 resultset.close();
302             }
303         }
304     }
305 }
306
Popular Tags