1 23 24 27 28 package org.objectweb.medor.optim.lib; 29 30 import org.objectweb.jorm.type.api.PType; 31 import org.objectweb.jorm.type.api.PTypeSpace; 32 import org.objectweb.medor.api.MedorException; 33 import org.objectweb.medor.api.TupleStructure; 34 import org.objectweb.medor.expression.api.BinaryArithmeticOperator; 35 import org.objectweb.medor.expression.api.BinaryLogicalOperator; 36 import org.objectweb.medor.expression.api.Comparator; 37 import org.objectweb.medor.expression.api.Expression; 38 import org.objectweb.medor.expression.api.ExpressionException; 39 import org.objectweb.medor.expression.api.Operand; 40 import org.objectweb.medor.expression.api.ParameterOperand; 41 import org.objectweb.medor.expression.api.UnaryArithmeticOperator; 42 import org.objectweb.medor.expression.api.UnaryLogicalOperator; 43 import org.objectweb.medor.filter.api.BelongOperator; 44 import org.objectweb.medor.filter.api.ExpressionHelper; 45 import org.objectweb.medor.filter.api.FieldOperand; 46 import org.objectweb.medor.filter.postfix.PostfixExpressionHelper; 47 import org.objectweb.medor.optim.api.ExecPlanGenerator; 48 import org.objectweb.medor.query.api.CalculatedField; 49 import org.objectweb.medor.query.api.QueryNode; 50 import org.objectweb.medor.query.api.QueryTree; 51 import org.objectweb.medor.tuple.api.TupleLoader; 52 import org.objectweb.medor.type.lib.QTypeTuple; 53 import org.objectweb.util.io.api.Block; 54 import org.objectweb.util.io.api.TargetHolder; 55 import org.objectweb.util.io.lib.BlockImpl; 56 import org.objectweb.util.io.lib.JavaFileHolder; 57 58 import java.io.File ; 59 import java.io.FileWriter ; 60 import java.util.ArrayList ; 61 import java.util.Enumeration ; 62 import java.util.HashSet ; 63 import java.util.Iterator ; 64 import java.util.Stack ; 65 import java.util.Vector ; 66 67 public class SourceCodeGeneratorCompiler implements ExecPlanGenerator { 68 private DirsCompileClassLoader mcl; 69 private TargetHolder queryFiltersHolder; 70 private TargetHolder tcTypeHolder; 71 private QueryTree queryTree; 72 private String sourceDirectory,classDirectory; 73 private HashSet library = new HashSet (); 74 ExpressionHelper eHelper = new PostfixExpressionHelper(); 75 private int nbrFilter = 0; 76 private int nbrQTType = 0; 77 private static final String javaFilterName = "QueryFilter"; 78 private static final String tupleLoaderClassName = "TupleLoader"; 79 80 public SourceCodeGeneratorCompiler(String outputSourceDirectory, 81 String outputClassdirectory, 82 String [] libraryPaths) 83 throws MedorException { 84 try { 85 sourceDirectory = outputSourceDirectory; 86 classDirectory = outputClassdirectory; 87 File tadir; 88 tadir = new File (sourceDirectory); 89 if (!tadir.isDirectory()) { 90 throw new MedorException("the output directory parametre must be a directory: " + sourceDirectory); 91 } 92 library.add(sourceDirectory); 93 94 tadir = new File (classDirectory); 95 if (!tadir.isDirectory()) { 96 throw new MedorException("the output directory parametre must be a directory: " + classDirectory); 97 } 98 library.add(classDirectory); 99 100 for (int i = 0; (i < libraryPaths.length); i++) { 101 File targetDirectory = new File (libraryPaths[i]); 102 if (!targetDirectory.exists()) { 103 throw new MedorException("the path library " + libraryPaths[i] + "does not exist"); 104 } 105 library.add(libraryPaths[i]); 106 } 107 queryFiltersHolder = new JavaFileHolder(sourceDirectory); 108 tcTypeHolder = new JavaFileHolder(sourceDirectory); 109 } catch (Exception e) { 110 throw new MedorException(e.getMessage()); 111 } 112 } 113 114 public void buildFilterSources(QueryTree query) throws MedorException { 115 if ((query instanceof QueryNode) 116 && (((QueryNode) query).getQueryFilter() != null)) { 117 QueryNode qn = (QueryNode) query; 118 generateFilterSourceCode(qn.getQueryFilter(), 119 javaFilterName + nbrFilter++); 120 QueryTree[] qts = qn.getChildren(); 121 for (int cpt = 0; (cpt < qts.length); cpt++) 122 buildFilterSources(qts[cpt]); 123 } 124 } 125 126 public void buildTupleLoader(QueryTree query) throws MedorException { 127 if ((query instanceof QueryNode) 129 && ((QueryNode) query).getTupleLoader() != null) { 130 generateTupleLoader( 131 ((QueryNode) query).getTupleLoader().getFieldIndexes(), 132 query.getTupleStructure(), 133 tupleLoaderClassName + nbrQTType++); 134 QueryTree[] qts = ((QueryNode) query).getChildren(); 135 for (int cpt = 0; (cpt < qts.length); cpt++) 136 buildTupleLoader(qts[cpt]); 137 } 138 } 142 143 public QueryTree getQueryTree() { 144 return queryTree; 145 } 146 147 public String [] getOutPutDirectoryName() { 148 String [] dirs = new String [2]; 149 dirs[0] = sourceDirectory; 150 dirs[1] = classDirectory; 151 152 return dirs; 153 } 154 155 160 private void buildAllQueryFilter() throws MedorException { 161 buildFilterSources(queryTree); 162 } 163 164 private void buildAllTupleLoader() throws MedorException { 165 buildTupleLoader(queryTree); 166 } 167 168 172 private void generateTupleLoader(int[] indexes, 173 TupleStructure ts, 174 String javaFileName) 175 throws MedorException { 176 try { 177 FileWriter fw = tcTypeHolder.getFileWriter(javaFileName + ".java"); 178 byte b = 0; 179 Block bk = new BlockImpl(fw, b); 180 bk.writeLine("/**"); 181 bk.writeLine(" * This class is generated by Medor Compiler"); 182 bk.writeLine(" * It represents a TupleLoader Object"); 183 bk.writeLine(" */"); 184 bk.newLine(); 185 bk.writeLine("package org.objectweb.medor.filter.lib;"); 186 bk.newLine(); 187 bk.writeLine("import org.objectweb.medor.api.MedorException;"); 188 bk.writeLine("import org.objectweb.medor.tuple.api.TupleLoader;"); 189 bk.writeLine("import org.objectweb.medor.tuple.api.Tuple;"); 190 bk.writeLine("import org.objectweb.medor.expression.api.ParameterOperand;"); 191 bk.writeLine("import org.objectweb.medor.filter.api.VariableOperand;"); 192 bk.writeLine("import org.objectweb.medor.api.TupleStructure;"); 193 bk.newLine(); 194 ArrayList listParams = new ArrayList (); 195 bk.openBlock("public final class " + javaFileName + " implements TupleLoader"); 196 bk.newLine(); 197 for (int i = 1; (i <= ts.getSize()); i++) { 198 if (ts.getField(i) instanceof CalculatedField) { 199 String [] parameters = eHelper.getParameterNames(((CalculatedField) (ts.getField(i))).getExpression()); 200 for (int cpt = 0; (cpt < parameters.length); cpt++) { 201 bk.writeLine("ParameterOperand " + parameters[cpt] + ";"); 202 listParams.add(parameters[cpt]); 203 } 204 } 205 } 206 String [] allParameters = new String [listParams.size()]; 207 for (int cpt = 0; (cpt < listParams.size()); cpt++) { 208 allParameters[cpt] = (String ) listParams.get(cpt); 209 } 210 if (allParameters.length > 0) { 211 bk.newLine(); 212 bk.openBlock("private int getParamRank(String paramName, ParameterOperand[] parameters) throws MedorException"); 213 bk.writeLine("int i;"); 214 bk.writeLine("for (i=0; (i<parameters.length && !(parameters[i].getName().equalsIgnoreCase(paramName)));i++);"); 215 bk.writeLine("if (i== parameters.length) throw new MedorException (\"Parameter Error\" + paramName);"); 216 bk.writeLine("return i;"); 217 bk.closeBlock(); 218 } 219 bk.newLine(); 220 bk.openBlock("public void loadTuple(Tuple thisTuple, VariableOperand[] temp, ParameterOperand[] parameters) throws MedorException"); 221 for (int cpt = 0; (cpt < allParameters.length); cpt++) { 222 bk.writeLine(allParameters[cpt] + "= parameters[getParamRank(" + " \"" + allParameters[cpt] + "\" ,parameters)];"); 223 } 224 225 PType type; 226 int size = indexes.length; 227 for (int i = 0; (i < size); i++) { 228 type = ts.getField(i + 1).getType(); 230 if (indexes[i] != -1) { 231 switch (type.getTypeCode()) { 232 case QTypeTuple.TYPECODE_INT: 233 bk.writeLine("temp[" + i + "].setValue(thisTuple.getInt(" + indexes[i] + "));"); 234 break; 235 case QTypeTuple.TYPECODE_SHORT: 236 bk.writeLine("temp[" + i + "].setValue(thisTuple.getShort(" + indexes[i] + "));"); 237 break; 238 case QTypeTuple.TYPECODE_BYTE: 239 bk.writeLine("temp[" + i + "].setValue(thisTuple.getByte(" + indexes[i] + "));"); 240 break; 241 case QTypeTuple.TYPECODE_LONG: 242 bk.writeLine("temp[" + i + "].setValue(thisTuple.getLong(" + indexes[i] + "));"); 243 break; 244 case QTypeTuple.TYPECODE_DOUBLE: 245 bk.writeLine("temp[" + i + "].setValue(thisTuple.getDouble(" + indexes[i] + "));"); 246 break; 247 case QTypeTuple.TYPECODE_BOOLEAN: 248 bk.writeLine("temp[" + i + "].setValue(thisTuple.getBoolean(" + indexes[i] + "));"); 249 break; 250 case QTypeTuple.TYPECODE_FLOAT: 251 bk.writeLine("temp[" + i + "].setValue(thisTuple.getFloat(" + indexes[i] + "));"); 252 break; 253 case QTypeTuple.TYPECODE_STRING: 254 bk.writeLine("temp[" + i + "].setValue(thisTuple.getString(" + indexes[i] + "));"); 255 break; 256 case QTypeTuple.TYPECODE_CHAR: 257 bk.writeLine("temp[" + i + "].setValue(thisTuple.getChar(" + indexes[i] + "));"); 258 break; 259 case QTypeTuple.TYPECODE_DATE: 260 bk.writeLine("temp[" + i + "].setValue(thisTuple.getDate(" + indexes[i] + "));"); 261 break; 262 default: 264 if (type.getJormName().equals("collection")) 265 bk.writeLine("temp[" + i + "].setValue(thisTuple.getTupleCollection(" + indexes[i] + "));"); 266 break; 267 } 269 } else { 270 Expression calculExp = ((CalculatedField) ts.getField(i + 1)).getExpression(); 272 String filterExpression = generateQueryFilterString(eHelper.toStack(calculExp)); 273 bk.writeLine("temp[" + i + "].setValue(" + filterExpression + ");"); 274 } 275 } 276 bk.closeBlock(); 277 bk.newLine(); 278 bk.openBlock("public int[] getFieldIndexes()"); 279 bk.writeLine("return null;"); bk.closeBlock(); 281 bk.newLine(); 282 bk.openBlock("public TupleStructure getTupleStructure()"); 283 bk.writeLine("return null;"); bk.closeBlock(); 285 bk.closeBlock(); 286 fw.close(); 287 } catch (Exception e) { 288 throw new MedorException(e); 289 } 290 } 291 292 296 private void generateFilterSourceCode(Expression filter, 297 String javaFileName) throws MedorException { 298 299 try { 300 FileWriter fw = queryFiltersHolder.getFileWriter(javaFileName + ".java"); 301 byte b = 0; 302 Block bk = new BlockImpl(fw, b); 303 bk.writeLine("/**"); 304 bk.writeLine(" * This class is generated by Medor Compiler"); 305 bk.writeLine(" * It represents a compiled Expression"); 306 bk.writeLine(" */"); 307 bk.newLine(); 308 bk.writeLine("package org.objectweb.medor.filter.lib;"); bk.newLine(); 310 bk.writeLine("import org.objectweb.medor.api.MedorException;"); 311 bk.writeLine("import org.objectweb.medor.filter.api.Expression;"); 312 bk.writeLine("import org.objectweb.medor.filter.api.Operand;"); 313 bk.writeLine("import org.objectweb.medor.filter.api.VariableOperand;"); 314 bk.writeLine("import org.objectweb.medor.filter.lib.BasicVariableOperand;"); 315 bk.writeLine("import org.objectweb.medor.expression.api.ParameterOperand;"); 316 bk.writeLine("import org.objectweb.medor.type.lib.PTypeSpaceMedor;"); 317 bk.writeLine("import org.objectweb.jorm.type.api.PType;"); 318 bk.writeLine("import org.objectweb.medor.tuple.api.Tuple;"); 319 bk.newLine(); 320 bk.openBlock("public final class " + javaFileName + " implements Expression"); 321 bk.newLine(); 322 bk.writeLine("VariableOperand result = new BasicVariableOperand(PTypeSpaceMedor.BOOLEAN);"); 323 String [] allParameters = eHelper.getParameterNames(filter); 324 for (int cpt = 0; (cpt < allParameters.length); cpt++) { 325 bk.writeLine("ParameterOperand " + allParameters[cpt] + ";"); 326 } 327 328 if (allParameters.length > 0) { 329 bk.newLine(); 330 bk.openBlock("private int getParamRank(String paramName, ParameterOperand[] parameters) throws MedorException"); 331 bk.writeLine("int i;"); 332 bk.writeLine("for (i=0; (i<parameters.length && !(parameters[i].getName().equalsIgnoreCase(paramName)));i++);"); 333 bk.writeLine("if (i== parameters.length) throw new MedorException (paramName);"); 334 bk.writeLine("return i;"); 335 bk.closeBlock(); 336 } 337 bk.newLine(); 338 bk.openBlock("public void evaluate(ParameterOperand[] parameters,Tuple thisTuple) throws MedorException"); 339 for (int cpt = 0; (cpt < allParameters.length); cpt++) { 340 bk.writeLine(allParameters[cpt] + "= parameters[getParamRank(" + "\"" + allParameters[cpt] + "\" ,parameters)];"); 341 } 342 343 String filterExpression = generateQueryFilterString(eHelper.toStack(filter)); 344 345 bk.writeLine("result.setValue( " + filterExpression + ");"); 346 bk.closeBlock(); 347 348 String ptype = ""; 349 bk.newLine(); 350 bk.openBlock("public PType getType()"); 351 switch (filter.getType().getTypeCode()) { 352 case QTypeTuple.TYPECODE_BYTE: 353 ptype = "PTypeSpaceMedor.BYTE"; 354 break; 355 case QTypeTuple.TYPECODE_DOUBLE: 356 ptype = "PTypeSpaceMedor.DOUBLE"; 357 break; 358 case QTypeTuple.TYPECODE_INT: 359 ptype = "PTypeSpaceMedor.INT"; 360 break; 361 case QTypeTuple.TYPECODE_FLOAT: 362 ptype = "PTypeSpaceMedor.FLOAT"; 363 break; 364 case QTypeTuple.TYPECODE_LONG: 365 ptype = "PTypeSpaceMedor.LONG"; 366 break; 367 case QTypeTuple.TYPECODE_SHORT: 368 ptype = "PTypeSpaceMedor.SHORT"; 369 break; 370 case QTypeTuple.TYPECODE_BOOLEAN: 371 ptype = "PTypeSpaceMedor.BOOLEAN"; 372 break; 373 case QTypeTuple.TYPECODE_CHAR: 374 ptype = "PTypeSpaceMedor.CHAR"; 375 break; 376 case QTypeTuple.TYPECODE_DATE: 377 ptype = "PTypeSpaceMedor.DATE"; 378 break; 379 } 380 bk.writeLine("return " + ptype + ";"); 381 bk.closeBlock(); 382 bk.newLine(); 383 bk.openBlock("public boolean isCompiled()"); 384 bk.writeLine("return true;"); bk.closeBlock(); 386 bk.newLine(); 387 bk.openBlock("public Operand getResult()"); 388 bk.writeLine("return result;"); bk.closeBlock(); 390 bk.newLine(); 391 bk.openBlock("public void compileExpression()"); 392 bk.closeBlock(); 393 bk.newLine(); 394 bk.openBlock("public Object clone()"); 395 bk.writeLine("return this;"); 396 bk.closeBlock(); 397 398 bk.newLine(); 399 bk.closeBlock(); 400 fw.close(); 401 } catch (Exception e) { 402 throw new MedorException(e); 403 } 404 } 405 406 private String generateQueryFilterString(Expression[] regularExp) 407 throws ExpressionException { 408 Object [] polonfpf = regularExp; 409 Stack popBuffer = new Stack (); 410 int cpt = 0; 411 Comparator c; 413 BinaryLogicalOperator bLogicop; 414 BinaryArithmeticOperator bArithOp; 415 Object op1,op2; 416 String leftOp,rightOp,opExp = ""; 417 String operator; 418 419 while (cpt < polonfpf.length) { 420 if (polonfpf[cpt] instanceof Operand) 421 popBuffer.push(polonfpf[cpt]); 422 else if (polonfpf[cpt] instanceof Comparator) { 423 op2 = popBuffer.pop(); 424 op1 = popBuffer.pop(); 425 c = (Comparator) polonfpf[cpt]; 426 operator = c.getOperatorString(); 427 if (op1 instanceof Operand) { 428 leftOp = generateOperandString((Operand) op1); 429 } else 431 leftOp = ((String ) op1); 432 433 if (op2 instanceof Operand) { 434 rightOp = generateOperandString((Operand) op2); 435 } else 436 rightOp = ((String ) op2); 437 438 if (!c.getType().isa(PTypeSpace.STRING)) { 439 opExp = "(" + leftOp + operator + rightOp + ")"; 440 } else { 441 opExp = "(" + leftOp + "compareTo(" + rightOp + ")" + operator + "0)"; 442 } 443 popBuffer.push(opExp); 444 445 } else if (polonfpf[cpt] instanceof BelongOperator) { 446 op2 = popBuffer.pop(); 447 op1 = popBuffer.pop(); 448 449 if (op1 instanceof Operand) { 450 leftOp = generateOperandString((Operand) op1); 451 } else 452 leftOp = ((String ) op1); 453 454 if (op2 instanceof Operand) { 455 rightOp = generateOperandString((Operand) op2); 456 } else 457 rightOp = ((String ) op2); 458 459 opExp = "(new InTupleCollection().evaluate(leftOp , rightOp)"; 460 popBuffer.push(opExp); 461 462 } else if (polonfpf[cpt] instanceof BinaryLogicalOperator) { 463 op2 = popBuffer.pop(); 464 op1 = popBuffer.pop(); 465 bLogicop = (BinaryLogicalOperator) polonfpf[cpt]; 466 operator = bLogicop.getOperatorString(); 467 if (op1 instanceof Operand) { 468 leftOp = generateOperandString((Operand) op1); 469 } else 470 leftOp = ((String ) op1); 471 472 if (op2 instanceof Operand) { 473 rightOp = generateOperandString((Operand) op2); 474 } else 475 rightOp = ((String ) op2); 476 477 opExp = "(" + leftOp + operator + rightOp + ")"; 478 popBuffer.push(opExp); 479 480 } else if (polonfpf[cpt] instanceof UnaryLogicalOperator) { 481 op1 = popBuffer.pop(); 482 if (op1 instanceof Operand) { 483 leftOp = generateOperandString((Operand) op1); 484 } else 485 leftOp = ((String ) op1); 486 opExp = "(!" + leftOp + ")"; 487 popBuffer.push(opExp); 488 489 } else if (polonfpf[cpt] instanceof BinaryArithmeticOperator) { 490 op2 = popBuffer.pop(); 491 op1 = popBuffer.pop(); 492 bArithOp = (BinaryArithmeticOperator) polonfpf[cpt]; 493 operator = bArithOp.getOperatorString() + ""; 494 if (op1 instanceof Operand) { 495 leftOp = generateOperandString((Operand) op1); 496 } else 497 leftOp = ((String ) op1); 498 if (op2 instanceof Operand) { 499 rightOp = generateOperandString((Operand) op2); 500 } else 501 rightOp = ((String ) op2); 502 opExp = "(" + leftOp + operator + rightOp + ")"; 503 popBuffer.push(opExp); 504 505 } else if (polonfpf[cpt] instanceof UnaryArithmeticOperator) { 506 UnaryArithmeticOperator unArith = (UnaryArithmeticOperator) polonfpf[cpt]; 507 op1 = popBuffer.pop(); 508 if (op1 instanceof Operand) { 509 leftOp = generateOperandString((Operand) op1); 510 } else 511 leftOp = ((String ) op1); 512 513 opExp = "(" + unArith.getOperatorString() + leftOp + ")"; 514 popBuffer.push(opExp); 515 } 516 cpt++; 517 } 518 return (String ) popBuffer.pop(); 519 } 520 521 private String generateOperandString(Operand op) 522 throws ExpressionException { 523 String operandExp = ""; 524 int range; 525 PType type = op.getType(); 526 switch (type.getTypeCode()) { 527 case QTypeTuple.TYPECODE_INT: 528 if (op instanceof ParameterOperand) { 529 operandExp = ((ParameterOperand) op).getName() + ".getInt()"; 530 } else if (op instanceof FieldOperand) { 531 range = ((FieldOperand) op).getIndex(); 532 operandExp = "thisTuple.getInt(" + range + ")"; 533 } else 534 operandExp = op.getInt() + ""; 535 break; 536 case QTypeTuple.TYPECODE_SHORT: 537 if (op instanceof ParameterOperand) { 538 operandExp = ((ParameterOperand) op).getName() + ".getShort()"; 539 } else if (op instanceof Operand) 540 operandExp = op.getShort() + ""; 541 else if (op instanceof FieldOperand) { 542 range = ((FieldOperand) op).getIndex(); 543 operandExp = "thisTuple.getShort(" + range + ")"; 544 } 545 break; 546 547 case QTypeTuple.TYPECODE_DATE: 548 if (op instanceof ParameterOperand) { 549 operandExp = ((ParameterOperand) op).getName() + ".getByte()"; 550 } else if (op instanceof Operand) 551 operandExp = op.getByte() + ""; 552 else if (op instanceof FieldOperand) { 553 range = ((FieldOperand) op).getIndex(); 554 operandExp = "thisTuple.getByte(" + range + ")"; 555 } 556 break; 557 558 case QTypeTuple.TYPECODE_LONG: 559 if (op instanceof ParameterOperand) { 560 operandExp = ((ParameterOperand) op).getName() + ".getLong()"; 561 } else if (op instanceof Operand) 562 operandExp = op.getLong() + ""; 563 else if (op instanceof FieldOperand) { 564 range = ((FieldOperand) op).getIndex(); 565 operandExp = "thisTuple.getLong(" + range + ")"; 566 } 567 break; 568 569 case QTypeTuple.TYPECODE_DOUBLE: 570 if (op instanceof ParameterOperand) { 571 operandExp = ((ParameterOperand) op).getName() + ".getDouble()"; 572 } else if (op instanceof Operand) 573 operandExp = op.getDouble() + ""; 574 else if (op instanceof FieldOperand) { 575 range = ((FieldOperand) op).getIndex(); 576 operandExp = "thisTuple.getDouble(" + range + ")"; 577 } 578 break; 579 580 case QTypeTuple.TYPECODE_FLOAT: 581 if (op instanceof ParameterOperand) { 582 operandExp = ((ParameterOperand) op).getName() + ".getFloat()"; 583 } else if (op instanceof Operand) 584 operandExp = op.getFloat() + ""; 585 else if (op instanceof FieldOperand) { 586 range = ((FieldOperand) op).getIndex(); 587 operandExp = "thisTuple.getFloat(" + range + ")"; 588 } 589 break; 590 591 case QTypeTuple.TYPECODE_BOOLEAN: 592 if (op instanceof ParameterOperand) { 593 operandExp = ((ParameterOperand) op).getName() + ".getBoolean()"; 594 } else if (op instanceof Operand) 595 operandExp = op.getBoolean() + ""; 596 else if (op instanceof FieldOperand) { 597 range = ((FieldOperand) op).getIndex(); 598 operandExp = "thisTuple.getBoolean(" + range + ")"; 599 } 600 break; 601 602 case QTypeTuple.TYPECODE_CHAR: 603 if (op instanceof ParameterOperand) { 604 operandExp = ((ParameterOperand) op).getName() + ".getChar()"; 605 } else if (op instanceof Operand) 606 operandExp = op.getChar() + ""; 607 else if (op instanceof FieldOperand) { 608 range = ((FieldOperand) op).getIndex(); 609 operandExp = "thisTuple.getChar(" + range + ")"; 610 } 611 break; 612 613 case QTypeTuple.TYPECODE_STRING: 614 if (op instanceof ParameterOperand) { 615 operandExp = ((ParameterOperand) op).getName() + ".getString()"; 616 } else if (op instanceof Operand) 617 operandExp = op.getString(); 618 else if (op instanceof FieldOperand) { 619 range = ((FieldOperand) op).getIndex(); 620 operandExp = "thisTuple.getString(" + range + ")"; 621 } 622 break; 623 624 default: 625 if (type.getJormName().equals("collection")) { 627 if (op instanceof ParameterOperand) { 628 operandExp = ((ParameterOperand) op).getName() + ".getTupleCollection()"; 629 } else if (op instanceof Operand) 630 operandExp = op.getObject() + ""; 631 else if (op instanceof FieldOperand) { 632 range = ((FieldOperand) op).getIndex(); 633 operandExp = "thisTuple.getTupleCollection(" + range + ")"; 634 } 635 break; 636 } else if (type.getJormName().equals("tuple")) { 637 638 if (op instanceof ParameterOperand) { 639 operandExp = ((ParameterOperand) op).getName() + ".getTuple()"; 640 } else if (op instanceof Operand) 641 operandExp = op.getObject() + ""; 642 else if (op instanceof FieldOperand) { 643 range = ((FieldOperand) op).getIndex(); 644 operandExp = "thisTuple.getTuple(" + range + ")"; 645 } 646 break; 647 } 648 } 649 650 return operandExp; 651 } 652 653 public Iterator getSourceFileIterator() { 654 return queryFiltersHolder.iterateFile(); 655 } 656 657 661 public QueryTree transform(QueryTree query) throws MedorException { 662 this.queryTree = query; 663 buildAllQueryFilter(); 667 buildAllTupleLoader(); 668 Vector list = new Vector (); 669 Iterator i1 = queryFiltersHolder.iterateFile(); 670 Iterator i2 = tcTypeHolder.iterateFile(); 671 while (i1.hasNext()) { 672 list.add(i1.next()); 673 } 674 while (i2.hasNext()) { 675 list.add(i2.next()); 676 } 677 javac(list.elements()); 678 QueryTree copy = queryTree; 680 library.add(sourceDirectory); 681 mcl = new DirsCompileClassLoader(library); 682 nbrFilter = 0; 683 nbrQTType = 0; 684 compileFilters(copy, javaFilterName + nbrFilter); 685 compileTupleLoader(copy, tupleLoaderClassName + nbrQTType); 686 return copy; 688 } 689 690 private void compileFilters(QueryTree query, String fileName) 691 throws MedorException { 692 Class c = null; 693 try { 694 if ((query instanceof QueryNode) && 696 (((QueryNode) query).getQueryFilter() != null)) { 697 nbrFilter++; 698 c = mcl.loadClass("org.objectweb.medor.filter.lib." + fileName); 699 Object o = c.newInstance(); 700 Expression predicate = (Expression) o; 701 ((QueryNode) query).setQueryFilter(predicate); 702 QueryTree[] qts = ((QueryNode) query).getChildren(); 703 704 for (int cpt = 0; (cpt < qts.length); cpt++) { 705 706 compileFilters(qts[cpt], javaFilterName + nbrFilter); 707 708 } 709 } 710 } catch (ClassNotFoundException exp) { 711 throw new MedorException("THIS CLASS NOT EXIST TRY TO COMPILE AT FIRST"); 712 } catch (InstantiationException iexo) { 713 } catch (IllegalAccessException e) { 714 } 715 } 716 717 private void compileTupleLoader(QueryTree query, String fileName) 718 throws MedorException { 719 Class c = null; 720 try { 721 if ((query instanceof QueryNode) && 723 ((QueryNode) query).getTupleLoader() != null) { 724 nbrQTType++; 725 c = mcl.loadClass("org.objectweb.medor.filter.lib." + fileName); 726 Object o = c.newInstance(); 727 TupleLoader tLoader = (TupleLoader) o; 728 ((QueryNode) query).setTupleLoader(tLoader); 729 QueryTree[] qts = ((QueryNode) query).getChildren(); 731 732 for (int cpt = 0; (cpt < qts.length); cpt++) { 733 compileTupleLoader(qts[cpt], tupleLoaderClassName + nbrQTType); 734 } 735 } 736 } catch (ClassNotFoundException exp) { 737 throw new MedorException("THIS CLASS May be NOT EXIST TRY TO COMPILE AT FIRST or correct the classpath"); 738 } catch (InstantiationException iexo) { 739 } catch (IllegalAccessException e) { 740 } 741 } 742 743 746 private void javac(Enumeration listFile) { 747 748 Runtime rt = Runtime.getRuntime(); 749 Process p = null; 750 try { 752 System.out.println("Launching javac ..."); 753 String classpath = ""; 754 String sourceFiles = sourceDirectory + File.separator; 755 Iterator i = library.iterator(); 756 if (i.hasNext()) classpath = classpath + ((String ) i.next()); 757 while (i.hasNext()) { 758 classpath = classpath + File.pathSeparator + ((String ) i.next()); 759 } 760 761 if (listFile.hasMoreElements()) { 762 File f = (File ) listFile.nextElement(); 763 String s = f.getName(); 764 sourceFiles = sourceFiles + s; 765 } 766 767 while (listFile.hasMoreElements()) { 768 File f = (File ) listFile.nextElement(); 769 String s = f.getName(); 770 sourceFiles = sourceFiles + " " + sourceDirectory + File.separator + s; } 772 773 String cmd = "javac -classpath " + classpath + " " + "-d " + classDirectory + " " + sourceFiles; 774 System.out.println(cmd); 775 try { 776 p = rt.exec(cmd); 777 } catch (java.io.IOException e) { 778 System.err.println(" Compilation failed... " + e.getMessage()); 779 } 780 p.waitFor(); 781 System.out.println("Compilation finisched ..."); 782 783 } catch (InterruptedException e) { 784 System.err.println(" Compilation failed... " + e.getMessage()); 785 } 786 } 787 788 public void addLibraryPath(String classpath) throws MedorException { 789 790 File tadir = new File (classpath); 791 if (!tadir.exists()) { 792 throw new MedorException("the path library " + classpath + "does not exist"); 793 } 794 library.add(classpath); 795 } 796 797 public Iterator getPathsIterator() { 798 return library.iterator(); 799 } 800 } 801 | Popular Tags |