KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > medor > eval > lib > CartesianEvaluatedTC


1 /**
2  * MEDOR: Middleware Enabling Distributed Object Requests
3  *
4  * Copyright (C) 2001-2003 France Telecom R&D
5  * Contact: alexandre.lefebvre@rd.francetelecom.com
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20  *
21  * Initial developers: M. Alia, A. Lefebvre
22  */

23
24 /**
25  * Package definition.
26  */

27 package org.objectweb.medor.eval.lib;
28
29 import org.objectweb.medor.api.MedorException;
30 import org.objectweb.medor.api.TupleStructure;
31 import org.objectweb.medor.eval.api.BinaryEvaluatedTC;
32 import org.objectweb.medor.eval.api.NodeEvaluator;
33 import org.objectweb.medor.expression.api.ParameterOperand;
34 import org.objectweb.medor.expression.api.VariableOperand;
35 import org.objectweb.medor.expression.lib.BasicVariableOperand;
36 import org.objectweb.medor.query.api.QueryNode;
37 import org.objectweb.medor.tuple.api.Tuple;
38 import org.objectweb.medor.tuple.api.TupleCollection;
39 import org.objectweb.medor.tuple.lib.MemoryTuple;
40
41
42 /**
43  * This class is the implementation of a catesian result betwin two TupleCollection. Sub NodeEvaulators allow fetching
44  * data from sub QueryTree.
45  */

46 public class CartesianEvaluatedTC
47         extends BasicBinaryEvalutedTC
48         implements BinaryEvaluatedTC {
49
50     private QueryNode query;
51     private TupleCollection leftTC,rightTC;
52     private ParameterOperand[] parameters;
53     private Tuple currentBuffer;
54     private Tuple leftTuple, rightTuple;
55     private VariableOperand[] operandBuffers;
56     private int cursor,size,leftTCSize,rightTCSize;
57
58     public CartesianEvaluatedTC(QueryNode query, NodeEvaluator leftNodeEvaluator, NodeEvaluator rightNodeEvaluator, ParameterOperand[] parameters) throws MedorException {
59         this.parameters = parameters;
60
61         //get sizes
62
leftTCSize = leftNodeEvaluator.getQueryNode().getTupleStructure().getSize();
63         rightTCSize = rightNodeEvaluator.getQueryNode().getTupleStructure().getSize();
64         size = query.getTupleStructure().getSize();
65
66         //Init Buffers
67
operandBuffers = new BasicVariableOperand[size];
68         currentBuffer = new MemoryTuple(operandBuffers);
69
70         // Fetching left and right TupleCollection Result
71
rightTC = rightNodeEvaluator.fetchData(parameters);
72         leftTC = leftNodeEvaluator.fetchData(parameters);
73         if (isEmpty()) {
74             cursor = -2;
75         } else {
76             init();
77         }
78     }
79
80     public void close() throws MedorException {
81         super.close();
82         if (leftTC != null) {
83             leftTC.close();
84         }
85         if (rightTC != null) {
86             rightTC.close();
87         }
88     }
89
90     public TupleStructure getMetaData() throws MedorException {
91         if (closed) {
92             throw new MedorException("Impossible to use a closed TupleCollection");
93         }
94         return query.getTupleStructure();
95     }
96
97     public boolean isLast() throws MedorException {
98         if (closed) {
99             throw new MedorException("Impossible to use a closed TupleCollection");
100         }
101         return ((rightTC.isLast()) && (leftTC.isLast()));
102     }
103
104     // Getting the first Result if possible
105
private void init() throws MedorException {
106         leftTC.first();
107         rightTC.first();
108         leftTuple = leftTC.getTuple();
109         rightTuple = rightTC.getTuple();
110         fixeTheLeftResult();
111         fixeTheRightResult();
112         cursor = 1;
113     }
114
115     public boolean next() throws MedorException {
116         if (closed) {
117             throw new MedorException("Impossible to use a closed TupleCollection");
118         }
119         boolean moved = false;
120         if (isLast()) {
121             moved = false;
122         } else {
123             moved = true;
124             cursor++;
125             if (rightTC.next()) {
126                 leftTuple = leftTC.getTuple();
127                 rightTuple = rightTC.getTuple();
128                 fixeTheRightResult();
129             } else {
130                 if (leftTC.next()) {
131                     rightTC.first();
132                     rightTuple = rightTC.getTuple();
133                     leftTuple = leftTC.getTuple();
134                     fixeTheLeftResult();
135                     fixeTheRightResult();
136                 }
137             }
138         }
139         return moved;
140     }
141
142     public void first() throws MedorException {
143         if (closed) {
144             throw new MedorException("Impossible to use a closed TupleCollection");
145         }
146         //The Initialization
147
init();
148     }
149
150     public int getRow() throws MedorException {
151         if (closed) {
152             throw new MedorException("Impossible to use a closed TupleCollection");
153         }
154         return cursor;
155     }
156
157     public Tuple getTuple() throws MedorException {
158         if (closed) {
159             throw new MedorException("Impossible to use a closed TupleCollection");
160         }
161         if (!isEmpty() && (getRow() >= 1))
162             return currentBuffer;
163         else
164             throw new MedorException(" No elements fetched in this TupleCollection " + getRow());
165     }
166
167     public synchronized Tuple getTuple(int numTuple) throws MedorException {
168         if (closed) {
169             throw new MedorException("Impossible to use a closed TupleCollection");
170         }
171         //System.out.println(" l'index = " + row(numTuple));
172
return getTuple();
173     }
174
175     public boolean isEmpty() throws MedorException{
176         if (closed) {
177             throw new MedorException("Impossible to use a closed TupleCollection");
178         }
179         return ((leftTC.isEmpty()) && (rightTC.isEmpty()));
180     }
181
182     public boolean row(int numTuple) throws MedorException {
183         if (closed) {
184             throw new MedorException("Impossible to use a closed TupleCollection");
185         }
186         first();
187         int cpt = 1;
188         boolean go = true;
189         while ((cpt <= numTuple) && (cpt > 1) && go) {
190             if (!next()) go = false;
191         }
192         if (!go)
193             return false;
194         else {
195             cursor = numTuple;
196             return true;
197         }
198
199     }
200
201     private void fixeTheLeftResult() throws MedorException {
202         for (int cpt = 0; (cpt < leftTCSize); cpt++) {
203             operandBuffers[cpt] = (VariableOperand)leftTuple.getLikeOperand(cpt + 1);
204         }
205     }
206
207     private void fixeTheRightResult() throws MedorException {
208         for (int cpt = leftTCSize; (cpt < size); cpt++) {
209             operandBuffers[cpt] = (VariableOperand)rightTuple.getLikeOperand(cpt - leftTCSize + 1);
210         }
211     }
212
213     public int getLeftTCCursor() throws MedorException {
214         if (closed) {
215             throw new MedorException("Impossible to use a closed TupleCollection");
216         }
217         return leftTC.getRow();
218     }
219
220     public int getRightTCCursor() throws MedorException {
221         if (closed) {
222             throw new MedorException("Impossible to use a closed TupleCollection");
223         }
224         return rightTC.getRow();
225     }
226 }
227
Popular Tags