1 23 24 package org.objectweb.medor.optim.lib; 25 26 import org.objectweb.medor.api.MedorException; 27 import org.objectweb.medor.lib.Log; 28 import org.objectweb.medor.optim.api.QueryRewriter; 29 import org.objectweb.medor.optim.api.RewriteRule; 30 import org.objectweb.medor.optim.api.RuleConfiguration; 31 import org.objectweb.medor.optim.rdb.GroupSameDBRule; 32 import org.objectweb.medor.query.api.QueryTree; 33 import org.objectweb.medor.query.lib.QueryTreePrinter; 34 import org.objectweb.util.monolog.api.BasicLevel; 35 import org.objectweb.util.monolog.api.Logger; 36 37 import java.util.ArrayList ; 38 import java.util.Collection ; 39 import java.util.Iterator ; 40 import java.util.List ; 41 42 49 50 public class BasicQueryRewriter 51 implements QueryRewriter, RuleConfiguration { 52 53 protected List rules = null; 54 55 protected Logger log = null; 56 57 protected boolean debug = false; 58 59 62 public BasicQueryRewriter() { 63 this(new ArrayList ()); 64 } 65 66 71 public BasicQueryRewriter(ArrayList rules) { 72 this.rules = rules; 73 log = Log.loggerFactory.getLogger( 74 Log.MEDOR_PREFIX + ".optim.rewriter.BasicQueryRewriter"); 75 debug = log != null && log.isLoggable(BasicLevel.DEBUG); 76 if (debug){ 77 log.log(BasicLevel.DEBUG, "Creating Query Rewriter"); 78 } 79 80 } 81 82 87 public BasicQueryRewriter(Collection rules) { 88 this.rules = new ArrayList (rules); 89 } 90 91 101 public List getDefaultRules() { 102 ArrayList al = new ArrayList (); 103 al.add(new PushNotInExpressionRule()); 104 al.add(new PushSelectionRule()); 105 al.add(new DropUnusedProjFieldsRule()); 106 al.add(new DropUselessNodeRule()); 107 al.add(new GroupSameDBRule()); 108 al.add(new DropUnusedProjFieldsRule()); 109 return al; 110 } 111 112 115 public void setRuleConfiguration(RuleConfiguration config) { 116 rules = config.getRules(); 117 } 118 119 public QueryTree transform(QueryTree qt) throws MedorException { 120 if (debug){ 121 log.log(BasicLevel.DEBUG, "Entering transformation"); 122 } 123 QueryTree root = qt; 124 for (Iterator it = rules.iterator(); it.hasNext();) { 125 RewriteRule rule = (RewriteRule) it.next(); 126 if (debug) { 127 QueryTreePrinter.printQueryTree(root, log); 128 log.log(BasicLevel.DEBUG, "Run the rule: " + rule.getClass().getName()); 129 } 130 root = rule.rewrite(root, null); 131 } 132 if (debug) QueryTreePrinter.printQueryTree(root, log); 133 return root; 134 } 135 136 public void addRule(RewriteRule rule) { 139 rules.add(rule); 140 } 141 142 public void removeRule(RewriteRule rule) { 143 rules.remove(rule); 144 } 145 146 public List getRules() { 147 return rules; 148 } 149 } 150 | Popular Tags |