KickJava   Java API By Example, From Geeks To Geeks.

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


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.EvaluationVisitor;
28 import org.xquark.mediator.DOMUtils.Tuple;
29 import org.xquark.mediator.runtime.MediatorException;
30 import org.xquark.xquery.parser.XQueryException;
31 import org.xquark.xquery.parser.XQueryExpression;
32
33 public class OpFunc extends OpUn {
34     // **********************************************************************
35
// * VERSIONING
36
// **********************************************************************
37
private static final String JavaDoc RCSRevision = "$Revision: 1.7 $";
38     private static final String JavaDoc RCSName = "$Name: $"; // * INITIALIZATION
39
// ***********************************************************************
40
/**
41      *
42      */

43     public OpFunc(ExecutionPlan plan, XQueryExpression expression, Operator childexp) throws MediatorException {
44         super(plan, expression, childexp);
45         size = 1;
46         idsize = 0;
47     }
48
49     // #############################################################################
50
// VISITOR STUFF
51
// #############################################################################
52

53     public void accept(OperatorVisitor visitor) throws MediatorException {
54         visitor.visit(this);
55     }
56
57     // ***********************************************************************
58
// * EXECUTE QUERY
59
// ***********************************************************************
60
/**
61      *
62      */

63     protected ResultSet getResultSet(DynamicContext context, OperatorRunnable child) throws MediatorException {
64         return new FuncResultSet(this, context, child);
65     }
66
67     // **********************************************************************
68
// * OPTIMIZE
69
// **********************************************************************
70
/**
71      * FuncMAX *this* node with the subnode(s). As it should be called
72      * only from source, the returned node must be of type AlgSource.
73      * The return node is a AlgSource where the XQueryExpression is the merging
74      * of all child(ren) nodes' expression.
75      */

76     // protected Algebra mergeWithSub() throws MediatorException { return this ; }
77

78     // **********************************************************************
79
// * DEBUG
80
// **********************************************************************
81
/**
82      *
83      */

84 }
85
86 class FuncResultSet extends UnResultSet {
87     // **********************************************************************
88
// * VERSIONING
89
// **********************************************************************
90
private static final String JavaDoc RCSRevision = "$Revision: 1.7 $";
91     private static final String JavaDoc RCSName = "$Name: $"; // * CLASS VARIABLES
92
// ***********************************************************************
93
EvaluationVisitor visitor = null;
94     // ***********************************************************************
95
// * INITIALIZATION
96
// ***********************************************************************
97
public FuncResultSet(OpFunc operator, DynamicContext context, OperatorRunnable child) throws MediatorException {
98         super(operator, context, child);
99         visitor = new EvaluationVisitor(operator.getPlan().getSchemaManager());
100     }
101
102     // ***********************************************************************
103
// * EVALUATE IMPLEMENTATION
104
// ***********************************************************************
105
/**
106      * In a merge product, we can all do in parallel, so we
107      * must generate all results of both source and then make the merge
108      * product.
109      *
110      * @param non_blocking if the evaluation is blocking, we must
111      * generate all the results before passing.
112      */

113     protected void evaluate(boolean non_blocking) throws MediatorException {
114         //ArrayList childpaths = null ;
115
//Algebra child = ((AlgUnOp) algebra).getChildAlgebra() ;
116
//childpaths = child.getPaths() ;
117

118         //boolean islet = this.algebra.isLet();
119
Tuple newtuple = null;
120         if (resultset == null) {
121             try {
122                 ArrayList JavaDoc tuples = context.getCurrentTuples();
123                 //if (tuples != null && !tuples.isEmpty()) {
124
visitor = new EvaluationVisitor(this.operator.getPlan().getSchemaManager());
125                     visitor.reset(tuples);
126                     visitor.setReturnType(EvaluationVisitor.NODE_TYPE);
127                     if (expression != null) {
128                         expression.accept(visitor);
129                         ArrayList JavaDoc resnodes = visitor.getResNodes();
130                         if (resnodes != null && !resnodes.isEmpty()) {
131                             newtuple = buftuples.newTuple();
132                             newtuple.addNodesAtIndex(0,resnodes);
133                             buftuples.add(newtuple);
134                         }
135                     }
136                 //}
137
} catch (XQueryException e) {
138                 throw new MediatorException("AlgNotSource.evaluate: " + e.getMessage(), e);
139             }
140             setFinishedWhenEmpty();
141         }
142         else {
143             if (!resultset.hasNext()) {
144                 resultset.close();
145                 setFinishedWhenEmpty();
146                 return;
147             }
148             while (true) {
149                 try {
150                     Tuple tuple = resultset.next();
151                     tuple.eraseIdSize();
152                     visitor.reset(tuple);
153                     visitor.setReturnType(EvaluationVisitor.NODE_TYPE);
154                     this.getOperator().getExpression().accept(visitor);
155                     ArrayList JavaDoc resnodes = visitor.getResNodes();
156                     if (resnodes != null && !resnodes.isEmpty()) {
157                         newtuple = buftuples.newTuple();
158                         newtuple.addNodesAtIndex(0,resnodes);
159                         buftuples.add(newtuple);
160                         if (non_blocking)
161                             break;
162                     }
163                 }
164                 catch (XQueryException e) {
165                     throw new MediatorException("Could not evaluate function", e);
166                 }
167             }
168             if (!resultset.hasNext()) {
169                 resultset.close();
170                 setFinishedWhenEmpty();
171             }
172         }
173     }
174 }
175
Popular Tags