1 23 package org.objectweb.medor.optim.jorm; 24 25 import org.objectweb.jorm.metainfo.api.NameDef; 26 import org.objectweb.jorm.metainfo.api.PrimitiveElement; 27 import org.objectweb.jorm.type.api.PType; 28 import org.objectweb.medor.api.Field; 29 import org.objectweb.medor.api.MedorException; 30 import org.objectweb.medor.filter.api.FieldOperand; 31 import org.objectweb.medor.filter.jorm.lib.CompositePName; 32 import org.objectweb.medor.filter.jorm.lib.SinglePName; 33 import org.objectweb.medor.filter.lib.BasicFieldOperand; 34 import org.objectweb.medor.optim.api.LeafRewriter; 35 import org.objectweb.medor.query.api.QueryLeaf; 36 import org.objectweb.medor.query.api.QueryNode; 37 import org.objectweb.medor.query.api.QueryTree; 38 import org.objectweb.medor.query.api.QueryTreeField; 39 import org.objectweb.medor.query.jorm.api.JormExtent; 40 import org.objectweb.medor.query.jorm.lib.PNameField; 41 import org.objectweb.medor.query.lib.Project; 42 import org.objectweb.medor.expression.api.Expression; 43 import org.objectweb.medor.expression.api.Operand; 44 45 import java.util.Iterator ; 46 import java.util.Map ; 47 48 51 public abstract class JormLeafRewriter extends JormRule 52 implements LeafRewriter { 53 54 public JormLeafRewriter(String suffix) { 55 super(suffix); 56 } 57 58 public abstract boolean canRewrite(QueryLeaf ql); 59 60 public QueryTree rewrite(QueryTree qt, QueryNode parent) 61 throws MedorException { 62 throw new UnsupportedOperationException ("A leaf rewriter only rewrites leaves, not nodes."); 63 } 64 65 public QueryTree rewrite(QueryLeaf ql) 66 throws MedorException { 67 if (!canRewrite(ql)) 68 throw new MedorException( 69 "Impossible to rewrite this QueryLeaf: " + ql); 70 JormExtent extent = (JormExtent) ql; 71 QueryLeaf specificQL = createQueryLeaf(extent); 72 QueryNode qt = new Project(""); 73 74 Field[] oldFields = ql.getTupleStructure().getFields(); 75 for (int i = 0; i < oldFields.length; i++) { 76 if (oldFields[i] instanceof PNameField) { 77 PNameField pnf = (PNameField) oldFields[i]; 78 NameDef nd = pnf.getNameDef(extent); 79 addReference(nd, pnf, qt, specificQL, extent); 80 } else { 81 PrimitiveElement pe = getPrimitiveElement( 82 extent, getShortFieldName(oldFields[i].getName())); 83 QueryTreeField qtf = 84 getField(specificQL, pe, extent); 85 qt.addPropagatedField( 87 qtf.getName(), 88 qtf.getType(), 89 new QueryTreeField[]{qtf}); 90 } 91 } 92 return qt; 93 } 94 95 protected abstract QueryLeaf createQueryLeaf(JormExtent extent) 96 throws MedorException; 97 98 public abstract QueryTreeField addPrimitiveElement(QueryLeaf neo, 99 PrimitiveElement pe, 100 JormExtent extent) 101 throws MedorException; 102 103 public QueryTreeField getField(QueryLeaf neo, 104 PrimitiveElement pe, 105 JormExtent extent) throws MedorException { 106 if (neo.getTupleStructure().contains(pe.getName())) { 107 return (QueryTreeField) 108 neo.getTupleStructure().getField(pe.getName()); 109 } else { 110 return addPrimitiveElement(neo, pe, extent); 111 } 112 } 113 114 protected void addReference(NameDef nd, 115 PNameField pnf, 116 QueryNode qt, 117 QueryLeaf specificQL, 118 JormExtent extent) throws MedorException { 119 Operand o = getPNCOperand(extent, pnf); 121 PType type = pnf.getType(); 122 QueryTreeField qtf = null; 123 if (nd.isFieldName()) { 124 String ndfn = getFieldName(specificQL.getName(), nd.getFieldName()); 125 PrimitiveElement pe = getPrimitiveElement(extent, ndfn); 127 qtf = getField(specificQL, pe, extent); 128 129 Expression e = new SinglePName(new BasicFieldOperand(qtf),o, type); 133 qt.addCalculatedField(pnf.getName(), type, e); 134 } 135 else if (nd.isNameRef()) { 136 Map proj = nd.getNameRef().getProjection(); 137 FieldOperand[] fos = new BasicFieldOperand[proj.size()]; 138 String [] cofns = new String [proj.size()]; 139 int counter = 0; 140 for (Iterator it = proj.entrySet().iterator(); it.hasNext();) { 141 Map.Entry me = (Map.Entry ) it.next(); 142 cofns[counter] = (String ) me.getKey(); 143 String clafn = (String ) me.getValue(); 144 String ndfn = getFieldName(specificQL.getName(), clafn); 145 qtf = getField(specificQL, getPrimitiveElement(extent, ndfn), extent); 146 fos[counter] = new BasicFieldOperand(qtf); 147 counter++; 148 } 149 Expression e = new CompositePName(fos, cofns, o, type); 153 qt.addCalculatedField(pnf.getName(), type, e); 154 } 155 } 156 protected String getFieldName(String prefix, String nodeName) { 157 if (prefix==null || prefix.length()==0) 158 return nodeName; 159 else 160 return prefix + "." + nodeName; 161 } 162 163 protected String getShortFieldName(String fn) { 164 return fn.substring(fn.lastIndexOf(".")+1, fn.length()); 165 } 166 } 167 | Popular Tags |