KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > medor > query > lib > Intersection


1 /**
2  * MEDOR: Middleware Enabling Distributed Object Requests
3  *
4  * Copyright (C) 2001-2004 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 package org.objectweb.medor.query.lib;
25
26 import org.objectweb.medor.api.Field;
27 import org.objectweb.medor.api.MedorException;
28 import org.objectweb.medor.expression.api.Expression;
29 import org.objectweb.medor.expression.lib.And;
30 import org.objectweb.medor.expression.lib.Equal;
31 import org.objectweb.medor.filter.lib.BasicFieldOperand;
32 import org.objectweb.medor.query.api.OperationType;
33 import org.objectweb.medor.query.api.QueryTree;
34 import org.objectweb.medor.query.api.QueryTreeField;
35
36 import java.util.Map JavaDoc;
37
38 /**
39  *
40  * @author Sebastien Chassande-Barrioz
41  */

42 public class Intersection extends BasicQueryNode {
43
44
45     /**
46      * It represents filter associated to the Intersection QueryNode
47      * All elements of this stack have the link to its children. This implies
48      * that the top element stack[stack.length-1] is also the root of the tree which describes the
49      * filter.
50      */

51     private Expression[] stack;
52
53     public Intersection() {
54     }
55
56     public Intersection(QueryTree leftQT,
57                         QueryTree rightQT,
58                         String JavaDoc name)
59         throws MedorException {
60         super(name);
61
62         Field[] lfs = leftQT.getTupleStructure().getFields();
63         Field[] rfs = rightQT.getTupleStructure().getFields();
64         // we test the attributes types and names before performing
65
// the intersection opertation
66
if (lfs.length != rfs.length) {
67             throw new MedorException(
68                 "The intersection must be with the same Tuple Collection");
69         }
70         indexes = new int[lfs.length];
71         stack = new Expression[4 * lfs.length - 1];
72         for(int i=1; i<lfs.length; i++) {
73             if (//fields must have the same name
74
!lfs[i].getName().equalsIgnoreCase(rfs[i].getName())
75                 ||
76                 //the types must be comparable
77
!(lfs[i].getType().isa(rfs[i].getType())
78                     || rfs[i].getType().isa(lfs[i].getType()))) {
79                 throw new MedorException(
80                     "The intersection must be with the same Tuple Collection");
81             }
82
83             //adds the fields from the underlying query trees
84
addPropagatedField(
85                 lfs[i].getName(),
86                 lfs[i].getType(),
87                 new QueryTreeField[] {(QueryTreeField)lfs[i],
88                                       (QueryTreeField)rfs[i]});
89
90             // - build a stack which represents the filter
91
// - Fill the fields list
92
// - fix the index
93
stack[4*(i-1)] = new BasicFieldOperand(lfs[i]);
94             stack[4*(i-1) + 1] = new BasicFieldOperand(rfs[i]);
95             stack[4*(i-1) + 2] = new Equal(stack[4*i-1], stack[4*i]);
96             if (i>1) {
97                 stack[4*(i-1) + 3] = new And(stack[4*i+1], stack[4*i-2]);
98             }
99             indexes[i] = i;
100         }
101     }
102
103     public Object JavaDoc clone(Object JavaDoc clone,
104                         Map JavaDoc obj2clone) throws CloneNotSupportedException JavaDoc {
105         clone = super.clone(clone, obj2clone);
106         Intersection intersec = (Intersection) clone;
107         if (stack != null) {
108             intersec.stack = new Expression[stack.length];
109             for(int i=1; i<stack.length; i++) {
110                 intersec.stack[i] = (Expression) getClone(stack[i], obj2clone);
111             }
112         }
113         return clone;
114     }
115
116     public void setQueryFilter(Expression f) {
117         throw new UnsupportedOperationException JavaDoc("Intersections cannot be assigned a filter.");
118     }
119
120     public Expression getQueryFilter() {
121         return stack[stack.length-1];
122     }
123
124     public short getType() {
125         return OperationType.INTERSECTION;
126     }
127 }
128
Popular Tags