KickJava   Java API By Example, From Geeks To Geeks.

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


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.Variable;
34 import org.xquark.xquery.parser.XQueryException;
35 import org.xquark.xquery.parser.XQueryExpression;
36
37 public class OpEval 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 OpEval(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)).toString().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 EvalResultSet(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 EvalResultSet 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 EvalResultSet(OpEval 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     protected void evaluate(boolean non_blocking) throws MediatorException {
200         XQueryExpression condition = ((OpEval) operator).getcondition();
201         Tuple tuple;
202         ArrayList JavaDoc resnodes = DOMUtils.evalNodes(context, expression, null);
203         if (resnodes != null && !resnodes.isEmpty()) {
204             if (operator.isLet()) {
205                 tuple = new Tuple(operator.getSize(), operator.getIdSize());
206                 if (((OpEval) operator).isOnlyVar()) {
207                     tuple.addNodesAtIndex(0, resnodes);
208                     tuple.setParent(buftuples);
209                     if (this.operator.getIdSize() > 0)
210                         tuple.fillIdentifiers();
211                     if (condition == null || DOMUtils.evalCondition(context, condition, tuple))
212                         buftuples.add(tuple);
213                 } else {
214                     boolean doAdd = false;
215                     for (int j = 0; j < paths.size(); j++) {
216                         String JavaDoc pathStrj = paths.get(j).toString();
217                         // test if paths already in tuple
218
if (pathStrj.equals(expression.toString())) {
219                             tuple.addNodesAtIndex(j, resnodes);
220                             doAdd = true;
221                         } else {
222                             for (int i = 0; i < resnodes.size(); i++) {
223                                 ArrayList JavaDoc reslist = DOMUtils.getSteps((TypedNode) resnodes.get(i), (XQueryExpression) paths.get(j));
224                                 if (reslist != null) {
225                                     tuple.addNodesAtIndex(j, reslist);
226                                     doAdd = true;
227                                 }
228                             }
229                         }
230                     }
231                     if (doAdd && (condition == null || DOMUtils.evalCondition(context, condition, tuple))) {
232                         tuple.fillIdentifiers();
233                         buftuples.add(tuple);
234                     }
235                 }
236         
237             } else {
238                 if (((OpEval) operator).isOnlyVar()) {
239                     for (int i = 0; i < resnodes.size(); i++) {
240                         tuple = new Tuple(1, this.operator.getIdSize());
241                         tuple.addNodeAtIndex(0, (TypedNode) resnodes.get(i));
242                         tuple.setParent(buftuples);
243                         if (this.operator.getIdSize() > 0)
244                             tuple.fillIdentifiers();
245                         if (condition == null || DOMUtils.evalCondition(context, condition, tuple))
246                             buftuples.add(tuple);
247                     }
248                 } else {
249                     for (int i = 0; i < resnodes.size(); i++) {
250                         ArrayList JavaDoc nodelist = new ArrayList JavaDoc(1);
251                         nodelist.add(resnodes.get(i));
252                         tuple = buftuples.newTuple();
253                         boolean doAdd = false;
254         
255                         for (int j = 0; j < paths.size(); j++) {
256                             String JavaDoc pathStrj = paths.get(j).toString();
257                             // test if paths already in tuple
258
if (pathStrj.equals(expression.toString())) {
259                                 tuple.addNodesAtIndex(j, resnodes);
260                                 doAdd = true;
261                             } else {
262                                 ArrayList JavaDoc reslist = null;
263                                 try {
264                                     reslist = DOMUtils.getSteps(nodelist, (XQueryExpression) paths.get(j));
265                                 } catch (XQueryException xqe) {
266                                     throw new MediatorException(xqe.getMessage(), xqe);
267                                 }
268                                 if (reslist != null) {
269                                     tuple.addNodesAtIndex(j, reslist);
270                                     doAdd = true;
271                                 }
272                             }
273                         }
274                         if (doAdd && (condition == null || DOMUtils.evalCondition(context, condition, tuple))) {
275                             tuple.fillIdentifiers();
276                             buftuples.add(tuple);
277                         }
278                     }
279                 }
280             }
281         } else
282             throw new MediatorException("OpValue : Could not evaluate expression : " + expression);
283
284         setFinishedWhenEmpty();
285     }
286 }
287
Popular Tags