KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > medor > optim > lib > SourceCodeGeneratorCompiler


1 /**
2  * MEDOR: Middleware Enabling Distributed Object Requests
3  *
4  * Copyright (C) 2001-2003 France Telecom R&D
5  * Contact: alexandre.lefebvre@rd.francetelecom.com
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20  *
21  * Initial developers: M. Alia, A. Lefebvre
22  */

23
24 /**
25  * Package definition.
26  */

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 JavaDoc;
59 import java.io.FileWriter JavaDoc;
60 import java.util.ArrayList JavaDoc;
61 import java.util.Enumeration JavaDoc;
62 import java.util.HashSet JavaDoc;
63 import java.util.Iterator JavaDoc;
64 import java.util.Stack JavaDoc;
65 import java.util.Vector JavaDoc;
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 JavaDoc sourceDirectory,classDirectory;
73     private HashSet JavaDoc library = new HashSet JavaDoc();
74     ExpressionHelper eHelper = new PostfixExpressionHelper();
75     private int nbrFilter = 0;
76     private int nbrQTType = 0;
77     private static final String JavaDoc javaFilterName = "QueryFilter";
78     private static final String JavaDoc tupleLoaderClassName = "TupleLoader";
79
80     public SourceCodeGeneratorCompiler(String JavaDoc outputSourceDirectory,
81                                        String JavaDoc outputClassdirectory,
82                                        String JavaDoc[] libraryPaths)
83             throws MedorException {
84         try {
85             sourceDirectory = outputSourceDirectory;
86             classDirectory = outputClassdirectory;
87             File JavaDoc tadir;
88             tadir = new File JavaDoc(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 JavaDoc(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 JavaDoc targetDirectory = new File JavaDoc(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 JavaDoc 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         //try {
128
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         //} catch (Exception e) {
139
// throw new MedorException(e.getMessage());
140
//}
141
}
142
143     public QueryTree getQueryTree() {
144         return queryTree;
145     }
146
147     public String JavaDoc[] getOutPutDirectoryName() {
148         String JavaDoc[] dirs = new String JavaDoc[2];
149         dirs[0] = sourceDirectory;
150         dirs[1] = classDirectory;
151
152         return dirs;
153     }
154
155     /**
156      * Generate a typed QueryFilter corresponding to the old QueryTree's
157      * QueryFilter
158      * @throws MedorException if IO error
159      */

160     private void buildAllQueryFilter() throws MedorException {
161         buildFilterSources(queryTree);
162     }
163
164     private void buildAllTupleLoader() throws MedorException {
165         buildTupleLoader(queryTree);
166     }
167
168     /*
169      * This method generate a java class code only for this QueryTree.
170      * @param : QueryTree
171      */

172     private void generateTupleLoader(int[] indexes,
173                                      TupleStructure ts,
174                                      String JavaDoc javaFileName)
175             throws MedorException {
176         try {
177             FileWriter JavaDoc 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 JavaDoc listParams = new ArrayList JavaDoc();
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 JavaDoc[] 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 JavaDoc[] allParameters = new String JavaDoc[listParams.size()];
207             for (int cpt = 0; (cpt < listParams.size()); cpt++) {
208                 allParameters[cpt] = (String JavaDoc) 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                 //bk.writeLine("VariableOperand temp1 = (VariableOperand) temp[i];
229
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                             //TODO handle TupleCollections
263
default:
264                             if (type.getJormName().equals("collection"))
265                                 bk.writeLine("temp[" + i + "].setValue(thisTuple.getTupleCollection(" + indexes[i] + "));");
266                             break;
267                             //default : throw new MedorException(TYPE_1,"Invalid Attribute type " + type);
268
}
269                 } else {
270                     // Loading a CalculatedField
271
Expression calculExp = ((CalculatedField) ts.getField(i + 1)).getExpression();
272                     String JavaDoc 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;"); // Not called when evaluating
280
bk.closeBlock();
281             bk.newLine();
282             bk.openBlock("public TupleStructure getTupleStructure()");
283             bk.writeLine("return null;"); // Not called when evaluating
284
bk.closeBlock();
285             bk.closeBlock();
286             fw.close();
287         } catch (Exception JavaDoc e) {
288             throw new MedorException(e);
289         }
290     }
291
292     /*
293      * This method generate a java class code only for this QueryFilter.
294      * @param filter: PostFixeQueryFilter
295      */

296     private void generateFilterSourceCode(Expression filter,
297                                           String JavaDoc javaFileName) throws MedorException {
298
299         try {
300             FileWriter JavaDoc 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;"); // Not obliged
309
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 JavaDoc[] 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 JavaDoc filterExpression = generateQueryFilterString(eHelper.toStack(filter));
344
345             bk.writeLine("result.setValue( " + filterExpression + ");");
346             bk.closeBlock();
347
348             String JavaDoc 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;"); // Not called when evaluating
385
bk.closeBlock();
386             bk.newLine();
387             bk.openBlock("public Operand getResult()");
388             bk.writeLine("return result;"); // Not called when evaluating
389
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 JavaDoc e) {
402             throw new MedorException(e);
403         }
404     }
405
406     private String JavaDoc generateQueryFilterString(Expression[] regularExp)
407             throws ExpressionException {
408         Object JavaDoc[] polonfpf = regularExp;
409         Stack JavaDoc popBuffer = new Stack JavaDoc();
410         int cpt = 0;
411         //PType currentType = null;
412
Comparator c;
413         BinaryLogicalOperator bLogicop;
414         BinaryArithmeticOperator bArithOp;
415         Object JavaDoc op1,op2;
416         String JavaDoc leftOp,rightOp,opExp = "";
417         String JavaDoc 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                     //currentType = ((Operand) op1).getType();
430
} else
431                     leftOp = ((String JavaDoc) op1);
432
433                 if (op2 instanceof Operand) {
434                     rightOp = generateOperandString((Operand) op2);
435                 } else
436                     rightOp = ((String JavaDoc) 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 JavaDoc) op1);
453
454                 if (op2 instanceof Operand) {
455                     rightOp = generateOperandString((Operand) op2);
456                 } else
457                     rightOp = ((String JavaDoc) 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 JavaDoc) op1);
471
472                 if (op2 instanceof Operand) {
473                     rightOp = generateOperandString((Operand) op2);
474                 } else
475                     rightOp = ((String JavaDoc) 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 JavaDoc) 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 JavaDoc) op1);
498                 if (op2 instanceof Operand) {
499                     rightOp = generateOperandString((Operand) op2);
500                 } else
501                     rightOp = ((String JavaDoc) 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 JavaDoc) op1);
512
513                 opExp = "(" + unArith.getOperatorString() + leftOp + ")";
514                 popBuffer.push(opExp);
515             }
516             cpt++;
517         }
518         return (String JavaDoc) popBuffer.pop();
519     }
520
521     private String JavaDoc generateOperandString(Operand op)
522             throws ExpressionException {
523         String JavaDoc 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                 // first case: TupleCollection
626
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 JavaDoc getSourceFileIterator() {
654         return queryFiltersHolder.iterateFile();
655     }
656
657     /**
658      * In this case we will load the query filters generated class into the vm
659      * and reaffect them to the new QueryTree
660      */

661     public QueryTree transform(QueryTree query) throws MedorException {
662         this.queryTree = query;
663         //TODO ajouter l'appel ŕ la vérification des filter et des
664
//tuplestructure (voir interface QueryIndexGenerator).
665
//parcouring and changing the queryFilters
666
buildAllQueryFilter();
667         buildAllTupleLoader();
668         Vector JavaDoc list = new Vector JavaDoc();
669         Iterator JavaDoc i1 = queryFiltersHolder.iterateFile();
670         Iterator JavaDoc 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) queryTree.clone();
679
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         //i++;
687
return copy;
688     }
689
690     private void compileFilters(QueryTree query, String JavaDoc fileName)
691             throws MedorException {
692         Class JavaDoc c = null;
693         try {
694             //int i = 0;
695
if ((query instanceof QueryNode) &&
696                     (((QueryNode) query).getQueryFilter() != null)) {
697                 nbrFilter++;
698                 c = mcl.loadClass("org.objectweb.medor.filter.lib." + fileName);
699                 Object JavaDoc 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 JavaDoc exp) {
711             throw new MedorException("THIS CLASS NOT EXIST TRY TO COMPILE AT FIRST");
712         } catch (InstantiationException JavaDoc iexo) {
713         } catch (IllegalAccessException JavaDoc e) {
714         }
715     }
716
717     private void compileTupleLoader(QueryTree query, String JavaDoc fileName)
718             throws MedorException {
719         Class JavaDoc c = null;
720         try {
721             //int i = 0;
722
if ((query instanceof QueryNode) &&
723                     ((QueryNode) query).getTupleLoader() != null) {
724                 nbrQTType++;
725                 c = mcl.loadClass("org.objectweb.medor.filter.lib." + fileName);
726                 Object JavaDoc o = c.newInstance();
727                 TupleLoader tLoader = (TupleLoader) o;
728                 ((QueryNode) query).setTupleLoader(tLoader);
729                 //TODO verifier que c'est un noeud
730
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 JavaDoc exp) {
737             throw new MedorException("THIS CLASS May be NOT EXIST TRY TO COMPILE AT FIRST or correct the classpath");
738         } catch (InstantiationException JavaDoc iexo) {
739         } catch (IllegalAccessException JavaDoc e) {
740         }
741     }
742
743     /**
744      * Java compilation of all generated QueryFilter java Sources
745      */

746     private void javac(Enumeration JavaDoc listFile) {
747
748         Runtime JavaDoc rt = Runtime.getRuntime();
749         Process JavaDoc p = null;
750         // Compilation by javac or we can imagine an other compilator
751
try {
752             System.out.println("Launching javac ...");
753             String JavaDoc classpath = "";
754             String JavaDoc sourceFiles = sourceDirectory + File.separator;
755             Iterator JavaDoc i = library.iterator();
756             if (i.hasNext()) classpath = classpath + ((String JavaDoc) i.next());
757             while (i.hasNext()) {
758                 classpath = classpath + File.pathSeparator + ((String JavaDoc) i.next());
759             }
760
761             if (listFile.hasMoreElements()) {
762                 File JavaDoc f = (File JavaDoc) listFile.nextElement();
763                 String JavaDoc s = f.getName();
764                 sourceFiles = sourceFiles + s;
765             }
766
767             while (listFile.hasMoreElements()) {
768                 File JavaDoc f = (File JavaDoc) listFile.nextElement();
769                 String JavaDoc s = f.getName();
770                 sourceFiles = sourceFiles + " " + sourceDirectory + File.separator + s;// Only For Windows classpath
771
}
772
773             String JavaDoc cmd = "javac -classpath " + classpath + " " + "-d " + classDirectory + " " + sourceFiles;
774             System.out.println(cmd);
775             try {
776                 p = rt.exec(cmd);
777             } catch (java.io.IOException JavaDoc e) {
778                 System.err.println(" Compilation failed... " + e.getMessage());
779             }
780             p.waitFor();
781             System.out.println("Compilation finisched ...");
782
783         } catch (InterruptedException JavaDoc e) {
784             System.err.println(" Compilation failed... " + e.getMessage());
785         }
786     }
787
788     public void addLibraryPath(String JavaDoc classpath) throws MedorException {
789
790         File JavaDoc tadir = new File JavaDoc(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 JavaDoc getPathsIterator() {
798         return library.iterator();
799     }
800 }
801
Popular Tags