KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > xquark > mediator > plan > primitivefunctions > OpFuncDISTINCT_VALUES


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.primitivefunctions;
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.plan.*;
30 import org.xquark.mediator.runtime.MediatorException;
31 import org.xquark.xpath.datamodel.TypedNode;
32 import org.xquark.xquery.parser.*;
33
34 public class OpFuncDISTINCT_VALUES extends OpUn {
35     // **********************************************************************
36
// * VERSIONING
37
// **********************************************************************
38
private static final String JavaDoc RCSRevision = "$Revision: 1.10 $";
39     private static final String JavaDoc RCSName = "$Name: $";
40     // ***********************************************************************
41
// * INITIALIZATION
42
// ***********************************************************************
43
/**
44      *
45      */

46     public OpFuncDISTINCT_VALUES(ExecutionPlan plan, XQueryExpression expression, Operator childexp) throws MediatorException {
47         super(plan, expression, childexp);
48         // the path number can be only one (otherwise distinct values might be incorrect)
49
if (paths != null)
50             paths.clear();
51         // calculate paths
52
if (paths == null)
53             paths = new ArrayList JavaDoc();
54         getRetPath(((FunctionCall) expression).getArgument(0), paths);
55         if (paths.isEmpty() || paths.size() > 1)
56             throw new MediatorException("Could not find paths for distinct-values function");
57         //idsize = childalgebra.getIdSize();
58
size = 1;
59         idsize = 0;
60     }
61
62     // TODO better!!!
63
private void getRetPath(XQueryExpression expr, ArrayList JavaDoc reslist) {
64         if (expr instanceof XQueryExpressionSequence) {
65             for (int i = 0; i < ((XQueryExpressionSequence) expr).getSubExpressions().size(); i++) {
66                 getRetPath((XQueryExpression) ((XQueryExpressionSequence) expr).getSubExpressions().get(i), reslist);
67             }
68         }
69         if (expr instanceof LocatedExpression) {
70             reslist.add(expr);
71         } else if (expr instanceof Variable) {
72             reslist.add(expr);
73         } else if (expr instanceof FLWRExpression) {
74             getRetPath(((FLWRExpression) expr).getReturnClause(), reslist);
75         } else if (expr instanceof FunctionCall) {
76             getRetPath(((FunctionCall) expr).getArgument(0), reslist);
77         }
78     }
79     // ***********************************************************************
80
// * EXECUTE QUERY
81
// ***********************************************************************
82
/**
83      *
84      */

85     protected ResultSet getResultSet(DynamicContext context, OperatorRunnable child) throws MediatorException {
86         return new FuncDISTINCT_VALUESResultSet(this, context, child);
87     }
88
89     // **********************************************************************
90
// * OPTIMIZE
91
// **********************************************************************
92
/**
93      * FuncDISTINCT_VALUES *this* node with the subnode(s). As it should be called
94      * only from source, the returned node must be of type AlgSource.
95      * The return node is a AlgSource where the XQueryExpression is the merging
96      * of all child(ren) nodes' expression.
97      */

98     // protected Algebra mergeWithSub() throws MediatorException { return this ; }
99

100     // **********************************************************************
101
// * DEBUG
102
// **********************************************************************
103
/**
104      *
105      */

106 }
107
108 class FuncDISTINCT_VALUESResultSet extends UnResultSet {
109     // **********************************************************************
110
// * VERSIONING
111
// **********************************************************************
112
private static final String JavaDoc RCSRevision = "$Revision: 1.10 $";
113     private static final String JavaDoc RCSName = "$Name: $";
114     // ***********************************************************************
115
// * CLASS VARIABLES
116
// ***********************************************************************
117

118     // ***********************************************************************
119
// * INITIALIZATION
120
// ***********************************************************************
121
public FuncDISTINCT_VALUESResultSet(OpFuncDISTINCT_VALUES operator, DynamicContext context, OperatorRunnable child) throws MediatorException {
122         super(operator, context, child);
123     }
124
125     // ***********************************************************************
126
// * EVALUATE IMPLEMENTATION
127
// ***********************************************************************
128
/**
129      * In a merge product, we can all do in parallel, so we
130      * must generate all results of both source and then make the merge
131      * product.
132      *
133      * @param non_blocking if the evaluation is blocking, we must
134      * generate all the results before passing.
135      */

136     protected void evaluate(boolean non_blocking) throws MediatorException {
137
138         //non_blocking = false;
139

140         // if no resultset do evaluation visitor
141
if (resultset == null) {
142             try {
143                 ArrayList JavaDoc tuples = context.getCurrentTuples();
144                 if (tuples != null && !tuples.isEmpty()) {
145                     EvaluationVisitor visitor = new EvaluationVisitor(operator.getPlan().getSchemaManager());
146                     visitor.reset(tuples);
147                     visitor.setReturnType(EvaluationVisitor.NODE_TYPE);
148                     if (expression != null) {
149                         expression.accept(visitor);
150                         ArrayList JavaDoc nodes = visitor.getResNodes();
151                         if (nodes != null) {
152                             for (int i = 0; i < nodes.size(); i++) {
153                                 Tuple newtuple = buftuples.newTuple();
154                                 newtuple.addNodeAtIndex(0, (TypedNode) nodes.get(i));
155                                 //newtuple.fillIdentifiers(buftuples.getIdentifier());
156
newtuple.fillIdentifiers();
157                                 buftuples.add(newtuple);
158                             }
159                         }
160                     }
161                 }
162                 setFinishedWhenEmpty();
163                 return;
164             } catch (XQueryException xe) {
165                 throw new MediatorException(xe.getMessage(), xe);
166             }
167         }
168
169         // resultset is not null
170
if (!resultset.hasNext()) {
171             resultset.close();
172             setFinishedWhenEmpty();
173             return;
174         }
175
176         if (!resultset.hasNext()) {
177         } else {
178             String JavaDoc path = ((XQueryExpression) operator.getPaths().get(0)).getStringValue();
179             ArrayList JavaDoc tmpNodeList = new ArrayList JavaDoc();
180             ArrayList JavaDoc tmpValueList = new ArrayList JavaDoc();
181             //al = new BufferTuple(childpaths,this.algebra.getIdSize()) ;
182
while (resultset.hasNext()) {
183                 Tuple tuple = resultset.next();
184                 int index = tuple.getParent().getPathIndex(path);
185                 if (index == -1)
186                     index = 0;
187                 ArrayList JavaDoc list = tuple.getNodesAtIndex(index);
188                 if (list == null || list.size() != 1)
189                     continue;
190
191                 TypedNode node = (TypedNode) list.get(0);
192                 if (node.getTypedValue() == null) {
193                     if (node.getChildNodes().getLength() == 1)
194                         node = (TypedNode) node.getFirstChild();
195                     else
196                         node = null;
197                 }
198                 if (node != null && node.getTypedValue() != null) {
199                     if (!tmpValueList.contains(node.getTypedValue())) {
200                         tmpValueList.add(node.getTypedValue());
201                         tmpNodeList.add(node);
202                     }
203                 }
204             }
205             for (int i = 0; i < tmpNodeList.size(); i++) {
206                 Tuple newtuple = buftuples.newTuple();
207                 newtuple.addNodeAtIndex(0, (TypedNode) tmpNodeList.get(i));
208                 newtuple.fillIdentifiers();
209                 buftuples.add(newtuple);
210             }
211         }
212         resultset.close();
213         setFinishedWhenEmpty();
214     }
215 }
216
Popular Tags