KickJava   Java API By Example, From Geeks To Geeks.

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


1 /**
2  * MEDOR: Middleware Enabling Distributed Object Requests
3  *
4  * Copyright (C) 2001-2005 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, S. Chassande-Barrioz
22  */

23 package org.objectweb.medor.optim.lib;
24
25 import org.objectweb.medor.api.Field;
26 import org.objectweb.medor.api.MedorException;
27 import org.objectweb.medor.expression.api.Expression;
28 import org.objectweb.medor.expression.api.Operator;
29 import org.objectweb.medor.filter.api.FieldOperand;
30 import org.objectweb.medor.filter.lib.ExpressionPrinter;
31 import org.objectweb.medor.lib.Log;
32 import org.objectweb.medor.optim.api.RewriteRule;
33 import org.objectweb.medor.query.api.CalculatedField;
34 import org.objectweb.medor.query.api.NestQueryNode;
35 import org.objectweb.medor.query.api.NestedField;
36 import org.objectweb.medor.query.api.PropagatedField;
37 import org.objectweb.medor.query.api.QueryNode;
38 import org.objectweb.medor.query.api.QueryTree;
39 import org.objectweb.medor.query.api.QueryTreeField;
40 import org.objectweb.medor.query.lib.Nest;
41 import org.objectweb.util.monolog.api.BasicLevel;
42 import org.objectweb.util.monolog.api.Logger;
43
44 import java.util.ArrayList JavaDoc;
45 import java.util.Map JavaDoc;
46
47 /**
48  * @author S. Chassande-Barrioz
49  */

50 public abstract class BasicRule implements RewriteRule {
51
52     /**
53      * A ModifiedExpression is a structure containing an Expression and a
54      * boolean indicating whether the Expression has been modified or not.
55      * <p>
56      * It is used by RewriteRules to check whether an Expression has been
57      * modified, in order to propagate the changes.
58      */

59     public class ModifiedExpression {
60         public boolean isModified;
61         public Expression e;
62
63         public ModifiedExpression() {
64         }
65
66         public ModifiedExpression(Expression ex, boolean ism) {
67             e = ex;
68             isModified = ism;
69         }
70     }
71
72     protected Logger log = null;
73
74     protected boolean debug = false;
75
76     public BasicRule() {
77         log = Log.loggerFactory.getLogger(
78                 Log.MEDOR_PREFIX + ".optim.rule");
79         debug = log != null && log.isLoggable(BasicLevel.DEBUG);
80     }
81
82     public BasicRule(String JavaDoc suffix) {
83         log = Log.loggerFactory.getLogger(
84                 Log.MEDOR_PREFIX + ".optim.rule." + suffix);
85         debug = log != null && log.isLoggable(BasicLevel.DEBUG);
86     }
87
88     public QueryTree rewrite(QueryTree qt) throws MedorException {
89         return rewrite(qt, null);
90     }
91
92     private boolean replace(Expression e, Map JavaDoc old2neo) {
93         log.log(BasicLevel.DEBUG, "ReplaceUsage: Replacing expression " + ExpressionPrinter.e2str(e));
94         if (e instanceof Operator) {
95             int size = ((Operator) e).getOperandNumber();
96             boolean res = false;
97             for (int i = 0; i < size; i++) {
98                 res |= replace(((Operator) e).getExpression(i), old2neo);
99             }
100             return res;
101         } else if (e instanceof FieldOperand) {
102             FieldOperand fo = (FieldOperand) e;
103             Field f = (Field) old2neo.get(fo.getField());
104             if (f != null) {
105                 log.log(BasicLevel.DEBUG, "ReplaceUsage: Found new Field " + f);
106                 fo.setField(f);
107                 return true;
108             }
109         }
110         return false;
111     }
112
113     protected void replaceUsage(QueryNode qn, Map JavaDoc old2neo)
114             throws MedorException {
115         if (qn == null) {
116             return;
117         }
118         Field[] fs = qn.getTupleStructure().getFields();
119         for (int i = 0; i < fs.length; i++) {
120             if (fs[i] instanceof PropagatedField) {
121                 PropagatedField pf = (PropagatedField) fs[i];
122                 QueryTreeField parent = (QueryTreeField)
123                         pf.getPreviousFields()[0];
124                 log.log(BasicLevel.DEBUG, "ReplaceUsage: Replacing PropagatedField " + pf + " (" + parent + ")");
125                 QueryTreeField neo = (QueryTreeField) old2neo.get(parent);
126                 if (neo != null) {
127                     log.log(BasicLevel.DEBUG, "ReplaceUsage: with " + neo);
128                     qn.updatePropagatedField(fs[i].getName(),
129                             new QueryTreeField[]{neo});
130                 }
131             } else if (fs[i] instanceof CalculatedField) {
132                 CalculatedField cf = (CalculatedField) fs[i];
133                 if (replace(cf.getExpression(), old2neo)) {
134                     qn.updateCalculatedField(
135                             cf.getName(), cf.getExpression());
136                 }
137             } else if (fs[i] instanceof NestedField) {
138                 NestedField nf = (NestedField) fs[i];
139                 ArrayList JavaDoc newFields = new ArrayList JavaDoc();
140                 boolean changedNested = false;
141                 for (int j = 0; j < nf.getSize(); j++) {
142                     log.log(BasicLevel.DEBUG, "ReplaceUsage: Replacing NestedField field " + nf.getField(j+1) + " " + nf.getField(j+1).getName());
143                     QueryTreeField neo =
144                             (QueryTreeField) old2neo.get(nf.getField(j+1));
145                     if (neo != null) {
146                         log.log(BasicLevel.DEBUG, "Found as " + neo + " " + neo.getName() + " of node " + neo.getQueryTree());
147                         changedNested = true;
148                         if (! newFields.contains(neo)) {
149                             newFields.add(neo);
150                         }
151                     }
152                     else {
153                         log.log(BasicLevel.DEBUG, "Keeping old field");
154                         if (! newFields.contains(nf.getField(j+1))) {
155                             newFields.add(nf.getField(j+1));
156                         }
157                     }
158                 }
159                 if (changedNested) {
160                     log.log(BasicLevel.DEBUG, "Something changed");
161                     ((NestQueryNode)qn).replaceNestedField(fs[i].getName(), (Field[]) newFields.toArray(new Field[] {}));
162                     }
163             } else throw new MedorException("Unmanaged field: " + fs[i].getName());
164         }
165         if (qn instanceof Nest) {
166             log.log(BasicLevel.DEBUG, "Replacing GROUP BY fields:");
167             //also replace the GroupingFields
168
QueryTreeField[] qfs = ((Nest) qn).getNestingFields();
169             for (int i = 0; i < qfs.length; i++) {
170                 QueryTreeField neo = (QueryTreeField) old2neo.get(qfs[i]);
171                 if (neo != null) {
172                     log.log(BasicLevel.DEBUG, "Replacing " + qfs[i] + " with " + neo);
173                     ((Nest)qn).replaceNestingField(qfs[i], neo);
174                 }
175                 else {
176                     log.log(BasicLevel.DEBUG, "Keeping old GROUP BY field");
177                 }
178             }
179
180         }
181     }
182 }
183
Popular Tags