KickJava   Java API By Example, From Geeks To Geeks.

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


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.Tuple;
29 import org.xquark.mediator.algebra.Algebra;
30 import org.xquark.mediator.decomposer.Utils;
31 import org.xquark.mediator.runtime.MediatorException;
32 import org.xquark.xpath.datamodel.TypedNode;
33 import org.xquark.xquery.parser.Value;
34 import org.xquark.xquery.parser.Variable;
35 import org.xquark.xquery.parser.XQueryExpression;
36
37 public class OpValue extends ZeroOp {
38     // **********************************************************************
39
// * VERSIONING
40
// **********************************************************************
41
private static final String JavaDoc RCSRevision = "$Revision: 1.9 $";
42     private static final String JavaDoc RCSName = "$Name: $";
43
44     private XQueryExpression condition = null;
45     private boolean onlyVar = false;
46     // ***********************************************************************
47
// * INITIALIZATION
48
// ***********************************************************************
49
/**
50      *
51      */

52     public OpValue(ExecutionPlan plan, Algebra algebra, XQueryExpression condition, boolean hasIdentifier) throws MediatorException {
53         super(plan, algebra.getExpression());
54         this.condition = condition;
55         /*
56         this.resetPaths();
57         this.addPath(var);
58         size = 1;
59         */

60         Variable var = (Variable) algebra.getVariables().get(0);
61         if (algebra.getPaths().isEmpty()) {
62             addPath(var);
63             size = 1;
64             onlyVar = true;
65         } else {
66             ArrayList JavaDoc tmpPaths = algebra.getPaths();
67             addPaths(tmpPaths);
68             size = tmpPaths.size();
69             if (tmpPaths.size() == 1 && ((XQueryExpression)tmpPaths.get(0)).getStringValue().equals(var.getStringValue()))
70                 onlyVar = true;
71         }
72         idsize = 0;
73         if (plan.getIdVars().contains(var) || hasIdentifier)
74             idsize++;
75         this.isLet(algebra.isLet());
76     }
77
78     // public OpValue(ExecutionPlan plan, Algebra algebra, XQueryExpression expression, Variable var, XQueryExpression condition) throws MediatorException {
79
// super(plan, expression);
80
// this.condition = condition;
81
// /*
82
// this.resetPaths();
83
// this.addPath(var);
84
// size = 1;
85
// */
86
// idsize = 0;
87
// }
88

89     public XQueryExpression getcondition() {
90         return this.condition;
91     }
92
93     public Operator addCondition(ExecutionPlan plan, XQueryExpression expr) throws MediatorException {
94         if (condition == null) {
95             this.condition = expr;
96             return this;
97         } else
98             return new OpRestrict(plan, expr, this);
99     }
100
101     public boolean isOnlyVar() {
102         return onlyVar;
103     }
104
105     // #############################################################################
106
// VISITOR STUFF
107
// #############################################################################
108

109     public void accept(OperatorVisitor visitor) throws MediatorException {
110         visitor.visit(this);
111     }
112
113     // ***********************************************************************
114
// * EXECUTE QUERY
115
// ***********************************************************************
116
/**
117      *
118      */

119     protected ResultSet getResultSet(DynamicContext context) throws MediatorException {
120         return new ValueResultSet(this, context);
121     }
122
123     // **********************************************************************
124
// * OPTIMIZE
125
// **********************************************************************
126
/**
127      * Return all the sources depending of this node.
128      */

129     public ArrayList JavaDoc getSources() {
130         if (sources == null) {
131             sources = new ArrayList JavaDoc();
132         }
133         return sources;
134     }
135
136     // **********************************************************************
137
// * DEBUG
138
// **********************************************************************
139
/**
140      *
141      */

142     public String JavaDoc toCompleteString(int indent) {
143         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
144         buf.append(Utils.makeIndent(indent) + "<" + getClass().getName() + " isLet=\"" + isLet() + "\">\n");
145         buf.append(Utils.makeIndent(indent + 1) + "<Expression>\n");
146         buf.append(Utils.makeIndent(indent + 2) + expression + "\n");
147         buf.append(Utils.makeIndent(indent + 1) + "</Expression>\n");
148         buf.append(Utils.makeIndent(indent + 1) + "<condition>\n");
149         buf.append(Utils.makeIndent(indent + 2) + condition + "\n");
150         buf.append(Utils.makeIndent(indent + 1) + "</condition>\n");
151         // if (variables != null) {
152
// buf.append(Utils.makeIndent(indent + 1) + "<Variables>\n") ;
153
// for (int i = 0 ; i < variables.size() ; i ++) {
154
// buf.append(Utils.makeIndent(indent + 2) + "<Variable>" + variables.get(i) + "</Variable>\n") ;
155
// }
156
// buf.append(Utils.makeIndent(indent + 1) + "</Variables>\n") ;
157
// }
158
if (paths != null) {
159             buf.append(Utils.makeIndent(indent + 1) + "<Paths>\n");
160             for (int i = 0; i < paths.size(); i++) {
161                 buf.append(Utils.makeIndent(indent + 2) + "<Path>" + paths.get(i) + "</Path>\n");
162             }
163             buf.append(Utils.makeIndent(indent + 1) + "</Paths>\n");
164         }
165         buf.append(Utils.makeIndent(indent) + "</" + getClass().getName() + ">\n");
166         return buf.toString();
167     }
168
169 }
170
171 class ValueResultSet extends ZeroResultSet {
172     // **********************************************************************
173
// * VERSIONING
174
// **********************************************************************
175
private static final String JavaDoc RCSRevision = "$Revision: 1.9 $";
176     private static final String JavaDoc RCSName = "$Name: $";
177     // ***********************************************************************
178
// * INITIALIZATION
179
// ***********************************************************************
180
/**
181      *
182      */

183     public ValueResultSet(OpValue operator, DynamicContext context) throws MediatorException {
184         super(operator, context);
185     }
186
187     // ***********************************************************************
188
// * EVALUATE IMPLEMENTATION
189
// ***********************************************************************
190
/**
191      * In a cartesian product, we can all do in parallel, so we
192      * must generate all results of both source and then make the cartesian
193      * product.
194      *
195      * @param non_blocking if the evaluation is blocking, we must
196      * generate all the results before passing.
197      */

198
199     //private static EvaluationVisitor visitor = null;
200

201     protected void evaluate(boolean non_blocking) throws MediatorException {
202         //EvaluationVisitor visitor = null;
203
XQueryExpression condition = ((OpValue) operator).getcondition();
204         Tuple tuple;
205         if (expression instanceof Value) {
206             if (condition != null) {
207                 if (!DOMUtils.evalCondition(context, condition, null)) {
208                     setFinishedWhenEmpty();
209                     return;
210                 }
211             }
212             tuple = new Tuple(1, this.operator.getIdSize());
213             tuple.addNodeAtIndex(0, DOMUtils.makeTextNode((Value) expression, this.operator.getPlan().getSchemaManager()));
214             if (this.operator.getIdSize() > 0)
215                 tuple.fillIdentifiers();
216             buftuples.add(tuple);
217         } else {
218             ArrayList JavaDoc resnodes = DOMUtils.evalNodes(context, expression, null);
219             if (resnodes != null && !resnodes.isEmpty()) {
220                 if (operator.isLet()) {
221                     tuple = new Tuple(operator.getSize(), operator.getIdSize());
222                     if (((OpValue) operator).isOnlyVar()) {
223                         tuple.addNodesAtIndex(0, resnodes);
224                         tuple.setParent(buftuples);
225                         if (this.operator.getIdSize() > 0)
226                             tuple.fillIdentifiers();
227                         if (condition == null || DOMUtils.evalCondition(context, condition, tuple))
228                             buftuples.add(tuple);
229                     } else {
230                         boolean doAdd = false;
231                         for (int i = 0; i < resnodes.size(); i++) {
232                             ArrayList JavaDoc nodelist = new ArrayList JavaDoc(1);
233                             nodelist.add(resnodes.get(i));
234                             // get paths but test if path is expression !!!
235
for (int j = 0; j < paths.size(); j++) {
236                                 if (paths.get(j).toString().equals(expression.getStringValue())) {
237                                     tuple.addNodeAtIndex(j, (TypedNode) resnodes.get(i));
238                                     doAdd = true;
239                                 }
240                                 else {
241                                     ArrayList JavaDoc reslist = DOMUtils.getSteps(nodelist, (XQueryExpression) paths.get(j));
242                                     if (reslist != null) {
243                                         tuple.addNodesAtIndex(j, reslist);
244                                         doAdd = true;
245                                     }
246                                 }
247                             }
248                         }
249                         if (doAdd && (condition == null || DOMUtils.evalCondition(context, condition, tuple))) {
250                             tuple.fillIdentifiers();
251                             buftuples.add(tuple);
252                         }
253                     }
254             
255                 } else {
256                     if (((OpValue) operator).isOnlyVar()) {
257                         for (int i = 0; i < resnodes.size(); i++) {
258                             tuple = new Tuple(1, this.operator.getIdSize());
259                             tuple.addNodeAtIndex(0, (TypedNode) resnodes.get(i));
260                             tuple.setParent(buftuples);
261                             if (this.operator.getIdSize() > 0)
262                                 tuple.fillIdentifiers();
263                             if (condition == null || DOMUtils.evalCondition(context, condition, tuple))
264                                 buftuples.add(tuple);
265                         }
266                     } else {
267                         for (int i = 0; i < resnodes.size(); i++) {
268                             ArrayList JavaDoc nodelist = new ArrayList JavaDoc(1);
269                             nodelist.add(resnodes.get(i));
270                             tuple = buftuples.newTuple();
271                             boolean doAdd = false;
272                             // get paths but test if path is expression !!!
273
for (int j = 0; j < paths.size(); j++) {
274                                 if (paths.get(j).toString().equals(expression.getStringValue())) {
275                                     tuple.addNodeAtIndex(j, (TypedNode) resnodes.get(i));
276                                     doAdd = true;
277                                 }
278                                 else {
279                                     ArrayList JavaDoc reslist = DOMUtils.getSteps(nodelist, (XQueryExpression) paths.get(j));
280                                     if (reslist != null) {
281                                         tuple.addNodesAtIndex(j, reslist);
282                                         doAdd = true;
283                                     }
284                                 }
285                             }
286                             if (doAdd && (condition == null || DOMUtils.evalCondition(context, condition, tuple))) {
287                                 tuple.fillIdentifiers();
288                                 buftuples.add(tuple);
289                             }
290                         }
291                     }
292                 }
293             } else
294                 throw new MediatorException("OpValue : Could not evaluate expression : " + expression);
295         }
296
297         setFinishedWhenEmpty();
298     }
299 }
300
Popular Tags