KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > xquark > xquery > typing > QTypePrime


1 /*
2  * This file belongs to the XQuark distribution.
3  * Copyright (C) 2003 XQuark Group.
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.xquery.typing;
24
25 import java.util.ArrayList JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import java.util.List JavaDoc;
28
29 import org.xquark.xquery.parser.XQueryException;
30
31 /*
32 prime(ItemType) = ItemType
33 prime(empty) = none
34 prime(none) = none
35 prime(Type 1 , Type2) = prime(Type 1) | prime(Type 2)
36 prime(Type 1 & Type2) = prime(Type 1) | prime(Type 2)
37 prime(Type 1 | Type2) = prime(Type 1) | prime(Type 2)
38 prime(Type?) = prime(Type)
39 prime(Type*) = prime(Type)
40 prime(Type+) = prime(Type)
41
42 Semantics
43
44 The quantifier function is defined by induction as follows.
45
46 quantifier(ItemType) = 1
47 quantifier(empty) = ?
48 quantifier(none) = 1
49 quantifier(Type 1 , Type2) = quantifier(Type 1) , quantifier(Type 2)
50 quantifier(Type 1 & Type2) = quantifier(Type 1) , quantifier(Type 2)
51 quantifier(Type 1 | Type2) = quantifier(Type 1) | quantifier(Type 2)
52 quantifier(Type?) = quantifier(Type) ?
53 quantifier(Type*) = quantifier(Type) *
54 quantifier(Type+) = quantifier(Type) +
55
56 This definition uses the sum (Occurrence1 , Occurrence2), the choice (Occurrence1 | Occurrence2), and the product (Occurrence1 Occurrence2) of two occurrence indicators Occurrence1, Occurrence2, which are defined by the following tables.
57
58  , 1 ? + *
59  1 + + + +
60  ? + * + *
61  + + + + +
62  * + * + *
63
64  | 1 ? + *
65  1 1 ? + *
66  ? ? ? * *
67  + + * + *
68  * * * * *
69
70   1 ? + *
71  1 1 ? + *
72  ? ? ? * *
73  + + * + *
74  * * * * *
75  
76
77 Examples
78
79 For example, here are the result of applying prime and quantifier on a few simple types.
80
81   prime(element a+) = element a
82   prime(element a | empty) = element a
83   prime(element a?,element b?) = element a | element b
84   prime(element a | element b+, element c*) = element a | element b | element c
85
86   quantifier(element a+) = +
87   quantifier(element a | empty) = ?
88   quantifier(element a?,element b?) = *
89   quantifier(element a | element b+, element d*) = +
90
91 Note that the last occurrence indicator should be '+', since the regular expression is such that there must be at least one element in the sequence (this element being an 'a' element or a 'b' element).
92
93 Note
94
95 Note that prime(Type) quantifier(Type) is always a super type of the original type Type I.e., prime(Type) quantifier(Type) <: Type always holds. Therefore, it is appropriate to used it as an approximation for the type of an expression. This property is required for the soundness of the static type analysis.
96
97 */

98
99 public abstract class QTypePrime extends QType {
100
101     // specific stuff for prime type
102
/**
103      * computed list
104      */

105     protected PrimeList primeList = null;
106     /**
107      * computed occurence
108      */

109     protected byte primeOccurence = OCC_1_1;
110
111     // constructor
112
QTypePrime(List JavaDoc list, byte subclass) {
113         this.subclass = subclass;
114         // compute primelist
115
computePrimeList(list);
116         // compute occurence
117
}
118
119     /**
120      * method to get the list
121      */

122     public List JavaDoc getList() {
123         return primeList;
124     }
125     
126     public List JavaDoc getPrimeList() {
127         return primeList;
128     }
129
130     public byte getOccurence() {
131         return primeOccurence;
132     }
133
134     private void computePrimeList(List JavaDoc list) {
135         primeList = new PrimeList();
136         if (list == null || list.isEmpty())
137             return;
138         //primeOccurence = OCC_0_0;
139
if (list == null || list.isEmpty())
140             return;
141         for (int i = 0; i < list.size(); i++) {
142             QType qtypei = (QType) list.get(i);
143             List JavaDoc listi = ((QType) list.get(i)).getList();
144             if (listi == null) { // QTypeItem
145
primeList.add(qtypei);
146             } else {
147                 // QTypePrime
148
primeList.addAll(listi);
149             }
150             computeOccurence(qtypei.getOccurence());
151         }
152     }
153
154     private void computeOccurence(byte compOccurence) {
155         switch (subclass) {
156             case SEQUENCE :
157                 switch (primeOccurence) {
158                     case OCC_1_1 :
159                     case OCC_1_N :
160                         primeOccurence = OCC_1_N;
161                         break;
162                     case OCC_0_1 :
163                     case OCC_0_N :
164                         switch (compOccurence) {
165                             case OCC_1_1 :
166                             case OCC_1_N :
167                                 primeOccurence = OCC_1_N;
168                                 break;
169                             case OCC_0_1 :
170                             case OCC_0_N :
171                                 primeOccurence = OCC_0_N;
172                                 break;
173                         }
174                         break;
175                 }
176                 break;
177             case UNION :
178                 primeOccurence = computeOccurence(primeOccurence, compOccurence);
179                 break;
180         }
181     }
182
183     // equals
184
public boolean equals(Object JavaDoc qtype) {
185         if (!(qtype instanceof QTypePrime))
186             return false;
187 // if (((QTypePrime) qtype).getOccurence() != occurence)
188
// return false;
189
List JavaDoc list2 = ((QTypePrime) qtype).getPrimeList();
190         int size1 = primeList.size();
191         int size2 = list2.size();
192         if (size1 != size2)
193             return false;
194         for (int i = 0; i < size1; i++) {
195             QType qtype1 = (QType) primeList.get(i);
196             boolean found = false;
197             for (int j = 0; j < size2; j++)
198                 if (found = qtype1.equals((QType) list2.get(j)))
199                     break;
200             if (!found)
201                 return false;
202         }
203         return true;
204     }
205
206     // tells whether qtype is numeric
207
public boolean isNumeric() {
208         Iterator JavaDoc it = primeList.iterator();
209         while (it.hasNext())
210             if (((QType) it.next()).isNumeric())
211                 return true;
212         return false;
213     }
214
215     // tells whether qtype is boolean
216
public boolean isBoolean() {
217         int size = primeList.size();
218         for (int i = 0; i < size; i++)
219             if (((QType) primeList.get(i)).isBoolean())
220                 return true;
221         return false;
222     }
223
224     // tells whether qtype is integer
225
public boolean isInteger() {
226         int size = primeList.size();
227         for (int i = 0; i < size; i++)
228             if (((QType) primeList.get(i)).isInteger())
229                 return true;
230         return false;
231     }
232
233     // tells whether qtype is string
234
public boolean isString() {
235         int size = primeList.size();
236         for (int i = 0; i < size; i++)
237             if (((QType) primeList.get(i)).isString())
238                 return true;
239         return false;
240     }
241
242     // tells whether qtype is qname
243
public boolean isQName() {
244         int size = primeList.size();
245         for (int i = 0; i < size; i++)
246             if (((QType) primeList.get(i)).isQName())
247                 return true;
248         return false;
249     }
250
251     // tells whether qtype is an atom
252
public boolean isAtom() {
253         int size = primeList.size();
254         for (int i = 0; i < size; i++)
255             if (((QType) primeList.get(i)).isAtom())
256                 return true;
257         return false;
258     }
259
260     // tells whether qtype can react to text()
261
public boolean isText() {
262         Iterator JavaDoc it = primeList.iterator();
263         while (it.hasNext())
264             if (((QType) it.next()).isText())
265                 return true;
266         return false;
267     }
268
269     // tells whether qtype is a date or collection of dates
270
public boolean isDate() {
271         Iterator JavaDoc it = primeList.iterator();
272         while (it.hasNext())
273             if (((QType) it.next()).isDate())
274                 return true;
275         return false;
276     }
277
278     // tells whether qtype is a attribte or collection or sequence
279
public boolean isAttribute() {
280         Iterator JavaDoc it = primeList.iterator();
281         while (it.hasNext())
282             if (((QType) it.next()).isAttribute())
283                 return true;
284         return false;
285     }
286
287     // tells whether qtype is a attribte or collection or sequence
288
public boolean isOnlyAttribute() {
289         Iterator JavaDoc it = primeList.iterator();
290         while (it.hasNext())
291             if (!((QType) it.next()).isOnlyAttribute())
292                 return false;
293         return true;
294     }
295
296     // tells whether qtype is a ID, IDREF or IDREFS type
297
public boolean isIDAttribute() {
298         Iterator JavaDoc it = primeList.iterator();
299         while (it.hasNext())
300             if (((QType) it.next()).isIDAttribute())
301                 return true;
302         return false;
303     }
304
305     // tells whether qtype is a node
306
public boolean isNode() {
307         Iterator JavaDoc it = primeList.iterator();
308         while (it.hasNext())
309             if (((QType) it.next()).isNode())
310                 return true;
311         return false;
312     }
313
314     // tells whether qtype is a node with simple type
315
public boolean isSimpleTypeNode() {
316         Iterator JavaDoc it = primeList.iterator();
317         while (it.hasNext())
318             if (((QType) it.next()).isSimpleTypeNode())
319                 return true;
320         return false;
321     }
322
323     // tells whether qtype is a document
324
public boolean isDocument() {
325         Iterator JavaDoc it = primeList.iterator();
326         while (it.hasNext())
327             if (((QType) it.next()).isDocument())
328                 return true;
329         return false;
330     }
331
332     // tells whether qtype is multiple
333
// public boolean isMultiple() {
334
// Iterator it = primeList.iterator();
335
// while (it.hasNext())
336
// if (((QType) it.next()).isMultiple())
337
// return true;
338
// return false;
339
// }
340

341     // tells whether qtype is atomic (atom or element with simple content)
342
public boolean isAtomic() {
343         Iterator JavaDoc it = primeList.iterator();
344         while (it.hasNext())
345             if (((QType) it.next()).isAtomic())
346                 return true;
347         return false;
348     }
349
350     // tells whether qtype is mixed
351
public boolean isMixed() {
352         Iterator JavaDoc it = primeList.iterator();
353         while (it.hasNext())
354             if (((QType) it.next()).isMixed())
355                 return true;
356         return false;
357     }
358
359     // tells whether qtype is element
360
public boolean isElement() {
361         Iterator JavaDoc it = primeList.iterator();
362         while (it.hasNext())
363             if (((QType) it.next()).isElement())
364                 return true;
365         return false;
366     }
367
368     public boolean canBeCastedInto(int castType) {
369         Iterator JavaDoc it = primeList.iterator();
370         while (it.hasNext())
371             if (((QType) it.next()).canBeCastedInto(castType))
372                 return true;
373         return false;
374     }
375
376     public boolean canBeComparedTo(QType compType) {
377         if (compType == null)
378             return false;
379         Iterator JavaDoc it = primeList.iterator();
380         while (it.hasNext())
381             if (((QType) it.next()).canBeComparedTo(compType))
382                 return true;
383         return false;
384     }
385
386     public QType applyDATAFunction() throws TypeException {
387         ArrayList JavaDoc newList = new ArrayList JavaDoc();
388         Iterator JavaDoc it = primeList.iterator();
389         while (it.hasNext()) {
390             QType qtype = (QType) it.next();
391             qtype = qtype.applyDATAFunction();
392             if (qtype != null)
393                 newList.add(qtype);
394         }
395         if (!newList.isEmpty()) {
396             if (newList.size() == 1)
397                 return (QType) newList.get(0);
398             else
399                 return new QTypeUnion(newList);
400         }
401         return null;
402     }
403     
404     public QType changeOccurence(byte occurence) {
405         QTypePrime qtype = null;
406         try {
407             qtype = (QTypePrime) clone();
408         } catch (CloneNotSupportedException JavaDoc cnse) {}
409         qtype.primeOccurence = occurence;
410         return qtype;
411     }
412
413     public void accept(QTypeVisitor visitor) throws XQueryException {
414         visitor.visit(this);
415     }
416
417 }
418
Popular Tags