001: /*
002: * Globus Toolkit Public License (GTPL)
003: *
004: * Copyright (c) 1999 University of Chicago and The University of
005: * Southern California. All Rights Reserved.
006: *
007: * 1) The "Software", below, refers to the Globus Toolkit (in either
008: * source-code, or binary form and accompanying documentation) and a
009: * "work based on the Software" means a work based on either the
010: * Software, on part of the Software, or on any derivative work of
011: * the Software under copyright law: that is, a work containing all
012: * or a portion of the Software either verbatim or with
013: * modifications. Each licensee is addressed as "you" or "Licensee."
014: *
015: * 2) The University of Southern California and the University of
016: * Chicago as Operator of Argonne National Laboratory are copyright
017: * holders in the Software. The copyright holders and their third
018: * party licensors hereby grant Licensee a royalty-free nonexclusive
019: * license, subject to the limitations stated herein and
020: * U.S. Government license rights.
021: *
022: * 3) A copy or copies of the Software may be given to others, if you
023: * meet the following conditions:
024: *
025: * a) Copies in source code must include the copyright notice and
026: * this license.
027: *
028: * b) Copies in binary form must include the copyright notice and
029: * this license in the documentation and/or other materials
030: * provided with the copy.
031: *
032: * 4) All advertising materials, journal articles and documentation
033: * mentioning features derived from or use of the Software must
034: * display the following acknowledgement:
035: *
036: * "This product includes software developed by and/or derived from
037: * the Globus project (http://www.globus.org/)."
038: *
039: * In the event that the product being advertised includes an intact
040: * Globus distribution (with copyright and license included) then
041: * this clause is waived.
042: *
043: * 5) You are encouraged to package modifications to the Software
044: * separately, as patches to the Software.
045: *
046: * 6) You may make modifications to the Software, however, if you
047: * modify a copy or copies of the Software or any portion of it,
048: * thus forming a work based on the Software, and give a copy or
049: * copies of such work to others, either in source code or binary
050: * form, you must meet the following conditions:
051: *
052: * a) The Software must carry prominent notices stating that you
053: * changed specified portions of the Software.
054: *
055: * b) The Software must display the following acknowledgement:
056: *
057: * "This product includes software developed by and/or derived
058: * from the Globus Project (http://www.globus.org/) to which the
059: * U.S. Government retains certain rights."
060: *
061: * 7) You may incorporate the Software or a modified version of the
062: * Software into a commercial product, if you meet the following
063: * conditions:
064: *
065: * a) The commercial product or accompanying documentation must
066: * display the following acknowledgment:
067: *
068: * "This product includes software developed by and/or derived
069: * from the Globus Project (http://www.globus.org/) to which the
070: * U.S. Government retains a paid-up, nonexclusive, irrevocable
071: * worldwide license to reproduce, prepare derivative works, and
072: * perform publicly and display publicly."
073: *
074: * b) The user of the commercial product must be given the following
075: * notice:
076: *
077: * "[Commercial product] was prepared, in part, as an account of
078: * work sponsored by an agency of the United States Government.
079: * Neither the United States, nor the University of Chicago, nor
080: * University of Southern California, nor any contributors to
081: * the Globus Project or Globus Toolkit nor any of their employees,
082: * makes any warranty express or implied, or assumes any legal
083: * liability or responsibility for the accuracy, completeness, or
084: * usefulness of any information, apparatus, product, or process
085: * disclosed, or represents that its use would not infringe
086: * privately owned rights.
087: *
088: * IN NO EVENT WILL THE UNITED STATES, THE UNIVERSITY OF CHICAGO
089: * OR THE UNIVERSITY OF SOUTHERN CALIFORNIA OR ANY CONTRIBUTORS
090: * TO THE GLOBUS PROJECT OR GLOBUS TOOLKIT BE LIABLE FOR ANY
091: * DAMAGES, INCLUDING DIRECT, INCIDENTAL, SPECIAL, OR CONSEQUENTIAL
092: * DAMAGES RESULTING FROM EXERCISE OF THIS LICENSE AGREEMENT OR
093: * THE USE OF THE [COMMERCIAL PRODUCT]."
094: *
095: * 8) LICENSEE AGREES THAT THE EXPORT OF GOODS AND/OR TECHNICAL DATA
096: * FROM THE UNITED STATES MAY REQUIRE SOME FORM OF EXPORT CONTROL
097: * LICENSE FROM THE U.S. GOVERNMENT AND THAT FAILURE TO OBTAIN SUCH
098: * EXPORT CONTROL LICENSE MAY RESULT IN CRIMINAL LIABILITY UNDER U.S.
099: * LAWS.
100: *
101: * 9) Portions of the Software resulted from work developed under a
102: * U.S. Government contract and are subject to the following license:
103: * the Government is granted for itself and others acting on its
104: * behalf a paid-up, nonexclusive, irrevocable worldwide license in
105: * this computer software to reproduce, prepare derivative works, and
106: * perform publicly and display publicly.
107: *
108: * 10) The Software was prepared, in part, as an account of work
109: * sponsored by an agency of the United States Government. Neither
110: * the United States, nor the University of Chicago, nor The
111: * University of Southern California, nor any contributors to the
112: * Globus Project or Globus Toolkit, nor any of their employees,
113: * makes any warranty express or implied, or assumes any legal
114: * liability or responsibility for the accuracy, completeness, or
115: * usefulness of any information, apparatus, product, or process
116: * disclosed, or represents that its use would not infringe privately
117: * owned rights.
118: *
119: * 11) IN NO EVENT WILL THE UNITED STATES, THE UNIVERSITY OF CHICAGO OR
120: * THE UNIVERSITY OF SOUTHERN CALIFORNIA OR ANY CONTRIBUTORS TO THE
121: * GLOBUS PROJECT OR GLOBUS TOOLKIT BE LIABLE FOR ANY DAMAGES,
122: * INCLUDING DIRECT, INCIDENTAL, SPECIAL, OR CONSEQUENTIAL DAMAGES
123: * RESULTING FROM EXERCISE OF THIS LICENSE AGREEMENT OR THE USE OF
124: * THE SOFTWARE.
125: *
126: * END OF LICENSE
127: */
128: package org.griphyn.vdl.annotation;
129:
130: import java.util.*;
131:
132: /**
133: * This class defines the node structure for the tree representation of
134: * the query.
135: *
136: * @author Jens-S. Vöckler
137: * @author Yong Zhao
138: * @version $Revision: 50 $
139: */
140: public class QueryTree {
141: /**
142: * Stores a predicate, as the data element of the tree node
143: */
144: private Predicate m_predicate = null;
145:
146: /**
147: * Stores the left child
148: */
149: private QueryTree m_lchild = null;
150:
151: /**
152: * Stores the right child
153: */
154: private QueryTree m_rchild = null;
155:
156: /**
157: * Constructor
158: */
159: public QueryTree() {
160: }
161:
162: /**
163: * Constructor
164: */
165: public QueryTree(Predicate predicate) {
166: m_predicate = predicate;
167: }
168:
169: /**
170: * Get the data element
171: */
172: public Predicate getData() {
173: return m_predicate;
174: }
175:
176: /**
177: * Set the data element
178: */
179: public void setData(Predicate predicate) {
180: m_predicate = predicate;
181: }
182:
183: /**
184: * Get the data element
185: */
186: public int getPredicate() {
187: if (m_predicate != null)
188: return m_predicate.getPredicate();
189: else
190: return -1;
191: }
192:
193: /**
194: * Add a child
195: */
196: public void addChild(QueryTree node) {
197: if (m_lchild == null)
198: m_lchild = node;
199: else
200: m_rchild = node;
201: }
202:
203: /**
204: * Get the left child
205: */
206: public QueryTree getLchild() {
207: return m_lchild;
208: }
209:
210: /**
211: * Set the left child
212: */
213: public void setLchild(QueryTree node) {
214: m_lchild = node;
215: }
216:
217: /**
218: * Get the right child
219: */
220: public QueryTree getRchild() {
221: return m_rchild;
222: }
223:
224: /**
225: * Set the right child
226: */
227: public void setRchild(QueryTree node) {
228: m_rchild = node;
229: }
230:
231: public void traversal() {
232: traversal(this );
233: }
234:
235: public void traversal(QueryTree root) {
236: if (root != null) {
237: QueryTree lchild = root.getLchild();
238: if (lchild != null) {
239: QueryTree llchild = lchild.getLchild();
240: if (llchild != null) {
241: System.out.print("(");
242: }
243: traversal(lchild);
244: if (llchild != null) {
245: System.out.print(")");
246: }
247: }
248: if (root.getData() != null) {
249: System.out.print(" " + root.getData().toString() + " ");
250: }
251: QueryTree rchild = root.getRchild();
252: if (rchild != null) {
253: QueryTree rrchild = rchild.getRchild();
254: if (rrchild != null) {
255: System.out.print("(");
256: }
257: traversal(rchild);
258: if (rrchild != null) {
259: System.out.print(")");
260: }
261: }
262: }
263: }
264:
265: public String toSQL(int annoClass, Object arg) {
266: return toSQL(this , annoClass, arg) + ";";
267: }
268:
269: public String toSQL(QueryTree root, int annoClass, Object arg) {
270: StringBuffer sb = new StringBuffer();
271: if (root != null) {
272: QueryTree lchild = root.getLchild();
273: if (lchild != null) {
274: QueryTree llchild = lchild.getLchild();
275: if (llchild != null) {
276: sb.append("(");
277: }
278: sb.append(toSQL(lchild, annoClass, arg));
279: if (llchild != null) {
280: sb.append(")");
281: }
282: }
283: if (root.getData() != null) {
284: sb.append(root.getData().toSQL(annoClass, arg));
285: }
286: QueryTree rchild = root.getRchild();
287: if (rchild != null) {
288: QueryTree rrchild = rchild.getRchild();
289: if (rrchild != null
290: || root.getPredicate() == Predicate.NOT) {
291: sb.append("(");
292: }
293: sb.append(toSQL(rchild, annoClass, arg));
294: if (rrchild != null
295: || root.getPredicate() == Predicate.NOT) {
296: sb.append(")");
297: }
298: }
299: }
300: return sb.toString();
301: }
302:
303: public String toXQuery(String var) {
304: return toXQuery(this , var);
305: }
306:
307: public String toXQuery(QueryTree root, String var) {
308: StringBuffer sb = new StringBuffer();
309: if (root != null) {
310: QueryTree lchild = root.getLchild();
311: if (lchild != null) {
312: QueryTree llchild = lchild.getLchild();
313: if (llchild != null) {
314: sb.append("(");
315: }
316: sb.append(toXQuery(lchild, var));
317: if (llchild != null) {
318: sb.append(")");
319: }
320: }
321: if (root.getData() != null) {
322: sb.append(root.getData().toXQuery(var));
323: }
324: QueryTree rchild = root.getRchild();
325: if (rchild != null) {
326: QueryTree rrchild = rchild.getRchild();
327: if (rrchild != null
328: || root.getPredicate() == Predicate.NOT) {
329: sb.append("(");
330: }
331: sb.append(toXQuery(rchild, var));
332: if (rrchild != null
333: || root.getPredicate() == Predicate.NOT) {
334: sb.append(")");
335: }
336: }
337: }
338: return sb.toString();
339: }
340: }
|