KickJava   Java API By Example, From Geeks To Geeks.

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


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, S. Chassande-Barrioz, A. Lefebvre
22  */

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 JavaDoc;
38 import java.util.Collection JavaDoc;
39 import java.util.Iterator JavaDoc;
40 import java.util.List JavaDoc;
41
42 /**
43  * This class implements the QueryRewriter interface AND also the
44  * RuleConfiguration interface (the aim is to optimize the number of object).
45  * The addDefaultRiles permits to assign the default rules in the right order.
46  *
47  * @author S. Chassande-Barrioz
48  */

49
50 public class BasicQueryRewriter
51     implements QueryRewriter, RuleConfiguration {
52
53     protected List JavaDoc rules = null;
54
55     protected Logger log = null;
56
57     protected boolean debug = false;
58
59     /**
60      * It builds a BasicQueryRewriter with no rule
61      */

62     public BasicQueryRewriter() {
63         this(new ArrayList JavaDoc());
64     }
65
66     /**
67      * It builds a BasicQueryRewriter with a ArrayList of rules specified in
68      * parameter.
69      * @param rules is the list of rules for this configuration
70      */

71     public BasicQueryRewriter(ArrayList JavaDoc 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     /**
83      * It builds a BasicQueryRewriter with a Collection of rules specified in
84      * parameter.
85      * @param rules is the list of rules for this configuration
86      */

87     public BasicQueryRewriter(Collection JavaDoc rules) {
88         this.rules = new ArrayList JavaDoc(rules);
89     }
90
91     /**
92      * It adds the default Medor rules in this following order
93      * <OL>
94      * <UL>PushNotInExpressionRule</UL>
95      * <UL>PushSelectionRule</UL>
96      * <UL>DropUnusedProjFieldsRule</UL>
97      * <UL>DropUselessNodeRule</UL>
98      * <UL>GroupSameStoreRule</UL>
99      * </OL>
100      */

101     public List JavaDoc getDefaultRules() {
102         ArrayList JavaDoc al = new ArrayList JavaDoc();
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     // IMPLEMENTATION OF THE QueryRewriter INTERFACE //
113
//-----------------------------------------------//
114

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 JavaDoc 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     // IMPLEMENTATION OF THE RuleConfiguration INTERFACE //
137
//-----------------------------------------------//
138
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 JavaDoc getRules() {
147         return rules;
148     }
149 }
150
Popular Tags