1 23 24 package org.objectweb.medor.optim.jorm; 25 26 import org.objectweb.medor.api.Field; 27 import org.objectweb.medor.api.MedorException; 28 import org.objectweb.medor.filter.api.FieldOperand; 29 import org.objectweb.medor.expression.api.Operator; 30 import org.objectweb.medor.expression.api.Expression; 31 import org.objectweb.medor.optim.api.LeafRewriteRule; 32 import org.objectweb.medor.optim.api.LeafRewriter; 33 import org.objectweb.medor.optim.lib.BasicRule; 34 import org.objectweb.medor.query.api.CalculatedField; 35 import org.objectweb.medor.query.api.FilteredQueryTree; 36 import org.objectweb.medor.query.api.NestedField; 37 import org.objectweb.medor.query.api.PropagatedField; 38 import org.objectweb.medor.query.api.QueryLeaf; 39 import org.objectweb.medor.query.api.QueryNode; 40 import org.objectweb.medor.query.api.QueryTree; 41 import org.objectweb.medor.query.api.QueryTreeField; 42 import org.objectweb.medor.query.jorm.lib.ClassExtent; 43 import org.objectweb.util.monolog.api.BasicLevel; 44 import org.objectweb.util.monolog.api.Logger; 45 import org.objectweb.util.monolog.api.LoggerFactory; 46 47 import java.util.ArrayList ; 48 import java.util.Collection ; 49 import java.util.HashMap ; 50 import java.util.Iterator ; 51 import java.util.Map ; 52 57 public class JormLeafRewriteRule extends BasicRule implements LeafRewriteRule { 58 59 ArrayList leafRewriters = null; 60 61 public JormLeafRewriteRule() { 62 super("JormLeafRewriteRule"); 63 leafRewriters = new ArrayList (); 64 } 65 66 public JormLeafRewriteRule(LeafRewriter lr) { 67 this(); 68 leafRewriters.add(lr); 69 } 70 71 74 public void addLeafRewriter(LeafRewriter lr) { 75 leafRewriters.add(lr); 76 } 77 public void removeLeafRewriter(LeafRewriter lr) { 78 leafRewriters.add(lr); 79 } 80 public Collection getLeafRewriters() { 81 return leafRewriters; 82 } 83 public LeafRewriter getLeafRewriter(QueryLeaf ql) { 84 for (Iterator it=leafRewriters.iterator(); it.hasNext();){ 85 LeafRewriter lr = (LeafRewriter) it.next(); 86 if (lr.canRewrite(ql)) 87 return lr; 88 } 89 if (debug) 90 log.log(BasicLevel.DEBUG, "No LeafRewriter found (among " 91 + leafRewriters.size() + ") for this QueryLeaf: " + ql); 92 return null; 93 } 94 95 98 public void setLoggerFactory(LoggerFactory loggerFactory) { 99 } 100 101 public LoggerFactory getLoggerFactory() { 102 return null; 103 } 104 105 public void setLogger(Logger logger) { 106 this.log = logger; 107 } 108 109 public Logger getLogger() { 110 return log; 111 } 112 113 116 118 public QueryTree rewrite(QueryTree qt, QueryNode _parent) 119 throws MedorException { 120 debug = log != null && log.isLoggable(BasicLevel.DEBUG); 121 122 if (debug) 123 log.log(BasicLevel.DEBUG, "qt=" + qt); 124 if (qt==null) return null; 125 ClassExtent extent = null; 126 if (qt instanceof ClassExtent) { 127 extent = (ClassExtent) qt; 129 return getLeafRewriter(extent).rewrite(extent); 130 } 131 for(Iterator it=getLeafUsers(qt).entrySet().iterator(); it.hasNext();) { 132 Map.Entry entry = (Map.Entry ) it.next(); 133 extent = (ClassExtent) entry.getKey(); 134 ArrayList users = (ArrayList ) entry.getValue(); 135 136 QueryTree neo = getLeafRewriter(extent).rewrite(extent); 138 139 for(Iterator it2 = users.iterator(); it2.hasNext();) { 141 Object o = it2.next(); 142 if (o instanceof FieldOperand) { 143 FieldOperand fo = (FieldOperand) o; 144 fo.setField( 145 neo.getTupleStructure().getField( 146 fo.getField().getName())); 147 } 148 else if (o instanceof PropagatedField) { 149 PropagatedField pf = (PropagatedField) o; 150 Field[] fs = pf.getPreviousFields(); 151 QueryTreeField[] neos = new QueryTreeField[fs.length]; 152 boolean mustReplace = false; 153 for(int i=0; i<fs.length; i++) { 154 if (fs[i] instanceof QueryTreeField 155 && ((QueryTreeField)fs[i]).getQueryTree()==extent) { 156 mustReplace = true; 157 neos[i] = (QueryTreeField) 158 neo.getTupleStructure().getField(fs[i].getName()); 159 } 160 else { 161 neos[i] = (QueryTreeField) fs[i]; 162 } 163 164 } 165 if (mustReplace) 166 ((QueryNode) pf.getQueryTree()) 167 .updatePropagatedField(pf.getName(), neos); 168 } 169 else if (o instanceof NestedField) { 170 NestedField nf = (NestedField) o; 171 Field[] fs = nf.getFields(); 172 QueryTreeField[] neos = new QueryTreeField[fs.length]; 173 boolean mustReplace = false; 174 for(int i=0; i<fs.length; i++) { 175 if (fs[i] instanceof QueryTreeField 176 && ((QueryTreeField)fs[i]).getQueryTree()==extent) { 177 mustReplace = true; 178 neos[i] = (QueryTreeField) 179 neo.getTupleStructure().getField(fs[i].getName()); 180 } 181 else { 182 neos[i] = (QueryTreeField) fs[i]; 183 } 184 } 185 if (mustReplace) 186 ((QueryNode) nf.getQueryTree()) 187 .updatePropagatedField(nf.getName(), neos); 188 } 189 } 190 } 191 return qt; 192 } 193 194 197 200 protected Map getLeafUsers(QueryTree qt) throws MedorException { 201 Map res = new HashMap (); 202 getLeafUsers(qt, res, new ArrayList ()); 203 return res; 204 } 205 206 216 protected void getLeafUsers(QueryTree qt, Map m, ArrayList al) throws MedorException { 217 al.add(qt); 218 if (qt instanceof FilteredQueryTree) { 220 getLeafUsers( 221 ((FilteredQueryTree) qt).getQueryFilter(), m, al); 222 } 223 224 if (!(qt instanceof QueryLeaf)) { 226 Field[] fields = qt.getTupleStructure().getFields(); 227 for(int i=0; i<fields.length; i++) { 228 if (fields[i] instanceof PropagatedField) { 229 getLeafUsers( 230 ((PropagatedField) fields[i]).getPreviousFields(), 231 fields[i], 232 m, al); 233 } 234 else if (fields[i] instanceof CalculatedField) { 235 getLeafUsers( 236 ((CalculatedField) fields[i]).getExpression(), m, al); 237 } 238 else if (fields[i] instanceof NestedField) { 239 getLeafUsers( 240 ((NestedField)fields[i]).getFields(), 241 fields[i], 242 m, al); 243 } 244 } 245 } 246 } 247 248 257 private void getLeafUsers(Expression e, Map m, ArrayList al) throws MedorException { 258 if (e instanceof Operator) { 259 for (int i = 0; i< ((Operator) e).getOperandNumber(); i++) 260 getLeafUsers(((Operator)e).getExpression(i), m, al); 261 } 262 else if (e instanceof FieldOperand) { 263 FieldOperand fo = (FieldOperand) e; 264 QueryTree qt = ((QueryTreeField) fo.getField()).getQueryTree(); 265 if (qt instanceof ClassExtent) { 266 ArrayList users = (ArrayList ) m.get(qt); 267 if (users==null) { 268 users = new ArrayList (); 269 m.put(qt, users); 270 } 271 users.add(fo); 272 } 273 else if (!al.contains(qt)) { 274 getLeafUsers(qt, m, al); 275 } 276 } 277 } 278 279 292 private void getLeafUsers(Field[] usedFields, Object user, Map m, ArrayList al) throws MedorException { 293 for(int i=0; i<usedFields.length; i++) { 294 if (usedFields[i] instanceof QueryTreeField) { 295 QueryTree qt = ((QueryTreeField)usedFields[i]).getQueryTree(); 296 if (qt instanceof ClassExtent) { 297 ArrayList users = (ArrayList ) m.get(qt); 298 if (users==null) { 299 users = new ArrayList (); 300 m.put(qt, users); 301 } 302 users.add(user); 303 } else if (!al.contains(qt)) { 304 getLeafUsers(qt, m, al); 305 } 306 307 } 308 } 309 } 310 } 311 | Popular Tags |