KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > xquark > mediator > algebra > Algebra


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.algebra;
24
25 import java.util.ArrayList JavaDoc;
26 import java.util.HashSet JavaDoc;
27 import java.util.Set JavaDoc;
28
29 import org.xquark.mediator.decomposer.GetUsedVariablesVisitor;
30 import org.xquark.mediator.decomposer.Utils;
31 import org.xquark.mediator.plan.ExecutionPlan;
32 import org.xquark.mediator.plan.Operator;
33 import org.xquark.mediator.runtime.MediatorException;
34 import org.xquark.xquery.parser.*;
35 import org.xquark.xquery.typing.*;
36
37 /**
38  * This abstract class represent a NODE of the dependancy tree.
39  * It will be extended either by DepMem either by DepNodeSource.
40  *
41  */

42 public abstract class Algebra extends Object JavaDoc implements Cloneable JavaDoc {
43     // **********************************************************************
44
// * VERSIONING
45
// **********************************************************************
46
private static final String JavaDoc RCSRevision = "$Revision: 1.12 $";
47     private static final String JavaDoc RCSName = "$Name: $";
48     // **********************************************************************
49
// * CLASS VARIABLES
50
// **********************************************************************
51
// variable associated to node
52
// protected Variable var = null ;
53
// variables associated to node
54
protected ArrayList JavaDoc variables = null;
55     // expression associated to variable
56
protected XQueryExpression expression = null;
57     // all paths associated to variable
58
protected ArrayList JavaDoc paths = null;
59     // type of variable binding
60
protected boolean islet = false;
61     // list of variables contained in variable definition
62
protected ArrayList JavaDoc dependDefinitionVars = null;
63     // list of variables contained in variable definition
64
protected ArrayList JavaDoc dependWhereVars = null;
65     // list of variables using this variable in their definition
66
protected ArrayList JavaDoc referenceDefinitionVars = null;
67     // list of child nodes
68
protected ArrayList JavaDoc algChildren = null;
69     // parent depnode
70
protected Algebra parent = null;
71     // stuff
72
protected AlgebraManager algManager = null;
73     //protected TypeVisitor typevisitor = null ;
74
// array list of expression to be ordered
75
//protected ArrayList orderbys = null;
76
// tells whether the node is dependent on other node values
77
// protected boolean valueDepends = false;
78
// protected boolean whereDepends = false;
79
// id size
80
protected int idsize = -1;
81     // tells whether the node is bound on id variable
82
protected boolean hasIdentifier = false;
83
84     // ******************************************************************
85
// * INITIALIZATION
86
// ******************************************************************
87
/**
88      *
89      * @param typevisitor
90      * @param expression
91      * @param depmanager
92      */

93
94     public Algebra(XQueryExpression expression, ArrayList JavaDoc variables, AlgebraManager depmanager, boolean islet) {
95         this.expression = expression;
96         setVariables(variables);
97         //this.typevisitor = typevisitor ;
98
this.islet = islet;
99         this.paths = null;
100         this.dependDefinitionVars = new ArrayList JavaDoc(0);
101         this.dependWhereVars = new ArrayList JavaDoc(0);
102         this.algChildren = null;
103         this.referenceDefinitionVars = new ArrayList JavaDoc(0);
104         this.algManager = depmanager;
105         // verify identifier
106
for (int i = 0; i < variables.size(); i++)
107             if (depmanager.getIdVars().contains((Variable) variables.get(i))) {
108                 this.hasIdentifier = true;
109                 break;
110             }
111     }
112
113     public Algebra(XQueryExpression expression, Variable var, AlgebraManager depmanager, boolean islet) {
114         this.expression = expression;
115         //this.var = var ;
116
ArrayList JavaDoc varList = new ArrayList JavaDoc(0);
117         if (var != null) varList.add(var);
118         setVariables(varList);
119         //this.typevisitor = typevisitor ;
120
this.islet = islet;
121         this.paths = null;
122         this.dependDefinitionVars = new ArrayList JavaDoc(0);
123         this.dependWhereVars = new ArrayList JavaDoc(0);
124         this.algChildren = null;
125         this.referenceDefinitionVars = new ArrayList JavaDoc(0);
126         this.algManager = depmanager;
127         // verify identifier
128
if (depmanager.getIdVars().contains(var))
129             this.hasIdentifier = true;
130     }
131
132     // ******************************************************************
133
// * ATOMIZE
134
// ******************************************************************
135
/**
136      *
137      * @return
138      */

139     // public abstract ArrayList atomize () throws MediatorException ;
140

141     // ******************************************************************
142
// * INFORMATION
143
// ******************************************************************
144
/**
145      * Identify dependencies of variables
146      */

147     //public abstract void makeInformation () throws MediatorException ;
148

149     // ******************************************************************
150
// * METHODS
151
// ******************************************************************
152

153     public boolean isMonoSource() {
154         //return this.parent.isMonoSource();
155
int size = (expression.getSourceNames() == null || expression.getSourceNames().isEmpty())?0:expression.getSourceNames().size();
156         size += (expression.getUrls() == null || expression.getUrls().isEmpty())?0:expression.getUrls().size();
157         if (size == 1)
158             return true;
159         return false;
160     }
161
162     public Variable getVar(int i) {
163         if (variables == null)
164             return null; /** @todo Should a Exception be throwed ? */
165         return (Variable) variables.get(i);
166     }
167     public ArrayList JavaDoc getVariables() {
168         return variables;
169     }
170     public void setVariables(ArrayList JavaDoc variables) {
171         this.variables = variables;
172         // if (null != variables) {
173
// sources.clear();
174
// for (int i = 0; i < variables.size(); i++) {
175
// sources.addAll(((Variable)variables.get(i)).getSources());
176
// }
177
// }
178
}
179     public void setLet(boolean islet) {
180         this.islet = islet;
181     }
182     public boolean isLet() {
183         return islet;
184     }
185     public boolean hasIdentifier() {
186         return hasIdentifier;
187     }
188     public boolean isRootQDB() {
189         return false;
190     }
191     public boolean isQDB() {
192         return false;
193     }
194     public XQueryExpression getExpression() {
195         return expression;
196     }
197     public void setExpression(XQueryExpression expression) {
198         this.expression = expression;
199     }
200
201     public int getIdSize() {
202         if (idsize != -1)
203             return idsize;
204         idsize = 0;
205         if (algChildren != null)
206             for (int i = 0; i < algChildren.size(); i++) {
207                 if (algChildren.get(i) instanceof AlgSource)
208                     idsize++;
209             } else if (this instanceof AlgSource)
210             idsize++;
211         if (isQDB()) {
212             AlgFLWR depmem = (AlgFLWR) this;
213             ArrayList JavaDoc subDepFLWRs = depmem.getDependencyList();
214             if (subDepFLWRs != null)
215                 for (int i = 0; i < subDepFLWRs.size(); i++)
216                     idsize += ((AlgFLWR) subDepFLWRs.get(i)).getIdSize();
217         }
218         return idsize;
219     }
220
221     private void addReferenceVariable(Variable referencevar) {
222         referenceDefinitionVars.add(referencevar);
223     }
224     private void addReferenceVariables(ArrayList JavaDoc referencevars) {
225         referenceDefinitionVars.addAll(referencevars);
226     }
227     public void addDependSourceVariable(Variable dependvar) {
228         dependDefinitionVars.add(dependvar);
229         algManager.getMapNode(dependvar).addReferenceVariables(this.getVariables());
230     }
231     public void addDependWhereVariables(ArrayList JavaDoc dependvars) {
232         if (dependvars == null || dependvars.isEmpty()) return;
233         dependWhereVars.addAll(dependvars);
234     }
235     public void addDependSourceVariables(ArrayList JavaDoc dependvars) {
236         if (dependvars == null || dependvars.isEmpty()) return;
237         dependDefinitionVars.addAll(dependvars);
238         for (int i = 0; i < dependvars.size(); i++) {
239             Variable tmpvar = (Variable) dependvars.get(i);
240             algManager.getMapNode(tmpvar).addReferenceVariables(this.getVariables());
241         }
242     }
243     
244     public ArrayList JavaDoc getPaths() {
245         return paths;
246     }
247     public void setPaths() {
248         if (paths != null)
249             paths.clear();
250         if (algChildren != null) {
251             for (int i = 0; i < algChildren.size(); i++) {
252                 Algebra depnode = (Algebra) algChildren.get(i);
253                 depnode.setPaths();
254                 if (paths == null)
255                     paths = new ArrayList JavaDoc();
256                 if (!(depnode instanceof AlgSource)) {
257 // if (i == 0)
258
paths.addAll(depnode.getVariables());
259                 } else if (depnode.getPaths() != null)
260                     paths.addAll(depnode.getPaths());
261             }
262         } else {
263             for (int i = 0; i < this.variables.size(); i++) {
264                 Variable var = (Variable) variables.get(i);
265                 ArrayList JavaDoc tmplist = (ArrayList JavaDoc) algManager.getVarPaths().get(var);
266                 if (tmplist == null) {
267                     if (!this.isMonoSource()) {
268                         if (paths == null) {
269                             paths = new ArrayList JavaDoc(1);
270                             paths.add(var);
271                         } else
272                             paths.add(var);
273                     }
274                 } else {
275                     if (paths == null)
276                         paths = (ArrayList JavaDoc) tmplist.clone();
277                     else
278                         paths.addAll(tmplist);
279                 }
280             }
281         }
282         if (isQDB()) {
283             AlgFLWR depmem = (AlgFLWR) this;
284             if (depmem.getDependencyList() != null)
285                 for (int i = 0; i < depmem.getDependencyList().size(); i++) {
286                     AlgFLWR tmpdepmem = (AlgFLWR) depmem.getDependencyList().get(i);
287                     tmpdepmem.setPaths();
288                     if (paths == null)
289                         paths = new ArrayList JavaDoc();
290                     if (tmpdepmem.getPaths() != null)
291                         paths.addAll(tmpdepmem.getPaths());
292                 }
293         }
294     }
295     //public void setOrderBys(ArrayList orderbys) { this.orderbys = orderbys; }
296
//public ArrayList getOrderBys() { return orderbys ; }
297

298     public ArrayList JavaDoc getVarsWhereOn() {
299         return this.dependWhereVars;
300     }
301     public ArrayList JavaDoc getVarsDependingOn() {
302         return this.dependDefinitionVars;
303     }
304 // public ArrayList getAllVarsDependingOn() {
305
// //if (varsreferencedby.isEmpty()) return varsdependingon;
306
// ArrayList reslist = (ArrayList) dependDefinitionVars.clone();
307
// for (int i = 0; i < dependDefinitionVars.size(); i++) {
308
// DepNode tmpnode = this.depmanager.getMapNode((Variable) dependDefinitionVars.get(i));
309
// ArrayList tmplist = tmpnode.getAllVarsDependingOn();
310
// for (int j = 0; j < tmplist.size(); j++) {
311
// Variable tmpvar = (Variable) tmplist.get(j);
312
// if (!reslist.contains(tmpvar))
313
// reslist.add(tmpvar);
314
// }
315
// }
316
// return reslist;
317
// }
318

319     public ArrayList JavaDoc getVarsReferencedBy() {
320         return this.referenceDefinitionVars;
321     }
322     //public void setVarsReferencedBy(ArrayList list) { this.varsreferencedby = list; }
323

324     public Algebra getParent() {
325         return this.parent;
326     }
327     public AlgFLWR getParentDepFLWR() {
328         return parent.getParentDepFLWR();
329         // if (this instanceof DepMem)
330
// return (DepMem) this;
331
// if (parent instanceof DepMem)
332
// return (DepMem) parent;
333
// else
334
// return parent.getParentDepMem();
335
}
336
337     public ArrayList JavaDoc getChildren() {
338         return this.algChildren;
339     }
340     public AlgebraManager getAlgebraManager() {
341         return this.algManager;
342     }
343     public TypeVisitor getTypeVisitor() {
344         return this.algManager.getTypeVisitor();
345     }
346
347     public boolean getDepends() {
348         return ((dependWhereVars != null && !dependWhereVars.isEmpty()) || (dependDefinitionVars != null && !dependDefinitionVars.isEmpty()));
349     }
350 // public void setWhereDepends(boolean whereDepends) {
351
// this.whereDepends = whereDepends;
352
// }
353
// public boolean getWhereDepends() {
354
// return whereDepends;
355
// }
356
// public void setValueDepends(boolean valueDepends) {
357
// this.valueDepends = valueDepends;
358
// }
359
// public boolean getValueDepends() {
360
// return valueDepends;
361
// }
362
// public boolean getDepends() {
363
// return valueDepends || whereDepends;
364
// }
365

366     public void setParent(Algebra parent) throws MediatorException {
367         this.parent = parent;
368         if (!this.hasIdentifier && parent.hasIdentifier)
369             ParentHasIdentifier();
370     }
371     
372     public void ParentHasIdentifier() {}
373     
374     public void addLeaf(Algebra leaf) throws MediatorException {
375         if (algChildren == null)
376             algChildren = new ArrayList JavaDoc(1);
377         algChildren.add(leaf);
378         leaf.setParent(this);
379     }
380
381 // public AlgSource getFirstSourceLeafDepNode() {
382
// if (algChildren == null) {
383
// if (this instanceof AlgSource)
384
// return (AlgSource) this;
385
// else
386
// return null;
387
// }
388
// return ((Algebra) algChildren.get(0)).getFirstSourceLeafDepNode();
389
// }
390

391     public Object JavaDoc clone() throws CloneNotSupportedException JavaDoc {
392         Algebra newobj = (Algebra) super.clone();
393         /*
394         HashMap vars = new HashMap(1+this.varsdependingon.size());
395         vars.put(this.var.toString(),this.var);
396         for (int i=0;i<this.varsdependingon.size();i++) vars.put(((Variable)this.varsdependingon.get(i)).toString(),this.varsdependingon.get(i));;
397          */

398         newobj.expression = (XQueryExpression) this.expression.clone();
399         newobj.algManager = this.algManager;
400         newobj.islet = this.islet;
401         newobj.paths = this.paths;
402         // newobj.var = this.var;
403
newobj.variables = (ArrayList JavaDoc) this.variables.clone();
404         newobj.dependDefinitionVars = this.dependDefinitionVars;
405         newobj.referenceDefinitionVars = this.referenceDefinitionVars;
406         newobj.algChildren = null;
407         if (this.algChildren != null) {
408             newobj.algChildren = new ArrayList JavaDoc();
409             for (int i = 0; i < this.algChildren.size(); i++)
410                 newobj.algChildren.add((Algebra) ((Algebra) this.algChildren.get(i)).clone());
411         }
412         return newobj;
413     }
414
415     public Set JavaDoc getSources() {
416         Set JavaDoc retVal = null;
417         if (null != variables && !variables.isEmpty()) {
418             retVal = new HashSet JavaDoc();
419             Variable var = null;
420             for (int i = 0; i < variables.size(); i++) {
421                 var = (Variable) variables.get(i);
422                 if (null != var.getSourceNames()) {
423                     retVal.addAll(var.getSourceNames());
424                 }
425             }
426         }
427         return retVal;
428     }
429
430     public Set JavaDoc getUrls() {
431         Set JavaDoc retVal = null;
432         if (null != variables && !variables.isEmpty()) {
433             retVal = new HashSet JavaDoc();
434             Variable var = null;
435             for (int i = 0; i < variables.size(); i++) {
436                 var = (Variable) variables.get(i);
437                 if (null != var.getUrls()) {
438                     retVal.addAll(var.getUrls());
439                 }
440             }
441         }
442         return retVal;
443     }
444
445     protected GetUsedVariablesVisitor guvvisitor = null;
446     
447     protected void fillVarLists() throws MediatorException {
448         if (guvvisitor == null)
449             guvvisitor = new GetUsedVariablesVisitor();
450         guvvisitor.reset();
451         try {
452             expression.accept(guvvisitor);
453         } catch (XQueryException e) {
454             throw new MediatorException(e.getMessage(), e);
455         }
456         //addDependWhereVariables(guvvisitor.getWhereVariables());
457
addDependSourceVariables(guvvisitor.getVariables());
458     }
459     
460     public String JavaDoc toCompleteString(int indent) {
461         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
462         // buf.append(Utils.makeIndent(indent) + "<" + this.getClass().getName() + " varname=\"" + var + "\" let=" + isLet() + ">\n") ;
463
buf.append(Utils.makeIndent(indent) + "<" + this.getClass().getName() + ">\n");
464         buf.append(Utils.makeIndent(indent + 1) + "<variables> " + variables + " </variables>\n");
465         buf.append(Utils.makeIndent(indent + 1) + "<sources> " + getSources() + " </sources>\n");
466         buf.append(Utils.makeIndent(indent + 1) + "<urls> " + getUrls() + " </urls>\n");
467         buf.append(Utils.makeIndent(indent + 1) + "<isLet> " + islet + " </isLet>\n");
468         buf.append(Utils.makeIndent(indent + 1) + "<monosource> " + isMonoSource() + " </monosource>\n");
469         buf.append(Utils.makeIndent(indent + 1) + "<hasIdentifier> " + hasIdentifier + " </hasIdentifier>\n");
470         buf.append(Utils.makeIndent(indent + 1) + "<Expression type=\"" + expression.getClass() + "\">\n");
471         buf.append(Utils.makeIndent(indent + 2) + expression + "\n");
472         buf.append(Utils.makeIndent(indent + 1) + "</Expression>\n");
473         if (algChildren != null) {
474             buf.append(Utils.makeIndent(indent + 1) + "<Leaves>\n");
475             for (int i = 0; i < algChildren.size(); i++) {
476                 buf.append(Utils.makeIndent(indent + 2) + "<Leave>\n");
477                 Algebra depchild = (Algebra) algChildren.get(i);
478                 buf.append(depchild.toCompleteString(indent + 3));
479                 buf.append(Utils.makeIndent(indent + 2) + "</Leave>\n");
480             }
481             buf.append(Utils.makeIndent(indent + 1) + "</Leaves>\n");
482         }
483         if (referenceDefinitionVars != null && !referenceDefinitionVars.isEmpty()) {
484             buf.append(Utils.makeIndent(indent + 1) + "<referenceDefinitionVars>\n");
485             for (int i = 0; i < referenceDefinitionVars.size(); i++) {
486                 Variable var = (Variable) referenceDefinitionVars.get(i);
487                 buf.append(Utils.makeIndent(indent + 2) + "<Var>" + var + "</Var>\n");
488             }
489             buf.append(Utils.makeIndent(indent + 1) + "</referenceDefinitionVars>\n");
490         }
491         if (dependDefinitionVars != null && !dependDefinitionVars.isEmpty()) {
492             buf.append(Utils.makeIndent(indent + 1) + "<dependDefinitionVars>\n");
493             for (int i = 0; i < dependDefinitionVars.size(); i++) {
494                 Variable var = (Variable) dependDefinitionVars.get(i);
495                 buf.append(Utils.makeIndent(indent + 2) + "<Var>" + var + "</Var>\n");
496             }
497             buf.append(Utils.makeIndent(indent + 1) + "</dependDefinitionVars>\n");
498         }
499         if (dependWhereVars != null && !dependWhereVars.isEmpty()) {
500             buf.append(Utils.makeIndent(indent + 1) + "<dependWhereVars>\n");
501             for (int i = 0; i < dependWhereVars.size(); i++) {
502                 Variable var = (Variable) dependWhereVars.get(i);
503                 buf.append(Utils.makeIndent(indent + 2) + "<Var>" + var + "</Var>\n");
504             }
505             buf.append(Utils.makeIndent(indent + 1) + "</dependWhereVars>\n");
506         }
507         if (paths != null && !paths.isEmpty()) {
508             buf.append(Utils.makeIndent(indent + 1) + "<Paths>\n");
509             for (int i = 0; i < paths.size(); i++)
510                 buf.append(Utils.makeIndent(indent + 2) + "<Path>" + paths.get(i) + "</Path>\n");
511             buf.append(Utils.makeIndent(indent + 1) + "</Paths>\n");
512         }
513         buf.append(Utils.makeIndent(indent) + "</" + this.getClass().getName() + ">\n");
514         return buf.toString();
515     }
516
517     public String JavaDoc toCompleteString() {
518         return toCompleteString(0);
519     }
520
521     public abstract Operator createOperator(ExecutionPlan plan) throws MediatorException;
522
523 }
524
Popular Tags