1 22 23 package org.xquark.mediator.algebra; 24 25 import java.util.ArrayList ; 26 import java.util.HashSet ; 27 import java.util.Set ; 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 42 public abstract class Algebra extends Object implements Cloneable { 43 private static final String RCSRevision = "$Revision: 1.12 $"; 47 private static final String RCSName = "$Name: $"; 48 protected ArrayList variables = null; 55 protected XQueryExpression expression = null; 57 protected ArrayList paths = null; 59 protected boolean islet = false; 61 protected ArrayList dependDefinitionVars = null; 63 protected ArrayList dependWhereVars = null; 65 protected ArrayList referenceDefinitionVars = null; 67 protected ArrayList algChildren = null; 69 protected Algebra parent = null; 71 protected AlgebraManager algManager = null; 73 protected int idsize = -1; 81 protected boolean hasIdentifier = false; 83 84 93 94 public Algebra(XQueryExpression expression, ArrayList variables, AlgebraManager depmanager, boolean islet) { 95 this.expression = expression; 96 setVariables(variables); 97 this.islet = islet; 99 this.paths = null; 100 this.dependDefinitionVars = new ArrayList (0); 101 this.dependWhereVars = new ArrayList (0); 102 this.algChildren = null; 103 this.referenceDefinitionVars = new ArrayList (0); 104 this.algManager = depmanager; 105 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 ArrayList varList = new ArrayList (0); 117 if (var != null) varList.add(var); 118 setVariables(varList); 119 this.islet = islet; 121 this.paths = null; 122 this.dependDefinitionVars = new ArrayList (0); 123 this.dependWhereVars = new ArrayList (0); 124 this.algChildren = null; 125 this.referenceDefinitionVars = new ArrayList (0); 126 this.algManager = depmanager; 127 if (depmanager.getIdVars().contains(var)) 129 this.hasIdentifier = true; 130 } 131 132 139 141 147 149 153 public boolean isMonoSource() { 154 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; 165 return (Variable) variables.get(i); 166 } 167 public ArrayList getVariables() { 168 return variables; 169 } 170 public void setVariables(ArrayList variables) { 171 this.variables = variables; 172 } 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 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 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 dependvars) { 232 if (dependvars == null || dependvars.isEmpty()) return; 233 dependWhereVars.addAll(dependvars); 234 } 235 public void addDependSourceVariables(ArrayList 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 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 (); 256 if (!(depnode instanceof AlgSource)) { 257 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 tmplist = (ArrayList ) algManager.getVarPaths().get(var); 266 if (tmplist == null) { 267 if (!this.isMonoSource()) { 268 if (paths == null) { 269 paths = new ArrayList (1); 270 paths.add(var); 271 } else 272 paths.add(var); 273 } 274 } else { 275 if (paths == null) 276 paths = (ArrayList ) 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 (); 290 if (tmpdepmem.getPaths() != null) 291 paths.addAll(tmpdepmem.getPaths()); 292 } 293 } 294 } 295 298 public ArrayList getVarsWhereOn() { 299 return this.dependWhereVars; 300 } 301 public ArrayList getVarsDependingOn() { 302 return this.dependDefinitionVars; 303 } 304 319 public ArrayList getVarsReferencedBy() { 320 return this.referenceDefinitionVars; 321 } 322 324 public Algebra getParent() { 325 return this.parent; 326 } 327 public AlgFLWR getParentDepFLWR() { 328 return parent.getParentDepFLWR(); 329 } 336 337 public ArrayList 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 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 (1); 377 algChildren.add(leaf); 378 leaf.setParent(this); 379 } 380 381 391 public Object clone() throws CloneNotSupportedException { 392 Algebra newobj = (Algebra) super.clone(); 393 398 newobj.expression = (XQueryExpression) this.expression.clone(); 399 newobj.algManager = this.algManager; 400 newobj.islet = this.islet; 401 newobj.paths = this.paths; 402 newobj.variables = (ArrayList ) 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 (); 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 getSources() { 416 Set retVal = null; 417 if (null != variables && !variables.isEmpty()) { 418 retVal = new HashSet (); 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 getUrls() { 431 Set retVal = null; 432 if (null != variables && !variables.isEmpty()) { 433 retVal = new HashSet (); 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 addDependSourceVariables(guvvisitor.getVariables()); 458 } 459 460 public String toCompleteString(int indent) { 461 StringBuffer buf = new StringBuffer (); 462 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 toCompleteString() { 518 return toCompleteString(0); 519 } 520 521 public abstract Operator createOperator(ExecutionPlan plan) throws MediatorException; 522 523 } 524 | Popular Tags |