KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > xquark > extractor > algebra > AnalyzePredicateHandUpVisitor


1 /*
2  * This file belongs to the XQuark distribution.
3  * Copyright (C) 2003 Universite de Versailles Saint-Quentin.
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307.
18  * You can also get it at http://www.gnu.org/licenses/lgpl.html
19  *
20  * For more information on this software, see http://www.xquark.org.
21  */

22
23 package org.xquark.extractor.algebra;
24
25 import java.util.*;
26
27 import org.xquark.extractor.common.Debug;
28 import org.xquark.extractor.common.MessageLibrary;
29 import org.xquark.extractor.common.SqlWrapperException;
30
31 public final class AnalyzePredicateHandUpVisitor extends DefaultAlgebraVisitor {
32     private static final String JavaDoc RCSRevision = "$Revision: 1.4 $";
33     private static final String JavaDoc RCSName = "$Name: $";
34
35     List _predicateList = null;
36     Set _referredTI = null;
37     Expression _blockingNode = null;
38
39     public AnalyzePredicateHandUpVisitor() {
40     }
41
42     public void reinit() {
43         _predicateList = null;
44         _referredTI = null;
45         _blockingNode = null;
46     }
47
48     public void setPredicateList(List predicateList) {
49         _predicateList = predicateList;
50     }
51
52     public boolean isBlocked() {
53         return null != _blockingNode;
54     }
55
56     public Expression getBlockingNode() {
57         return _blockingNode;
58     }
59
60     public List getUnresolvedPredicateList() {
61         return _predicateList;
62     }
63
64     public void visit(Expression arg) {
65         visitFather(arg);
66     }
67
68     public void visit(Join arg) throws SqlWrapperException {
69         //Trace.enter(this, "visit(Join arg)");
70

71         Set visibleTI = arg.visibleTableInstances();
72
73         Set referedTI = null;
74         Expression predicate = null;
75         for (int i = _predicateList.size() - 1; i >= 0; i--) {
76             predicate = (Expression) _predicateList.get(i);
77             referedTI = predicate.getReferredTableInstances();
78             if (visibleTI.containsAll(referedTI)) {
79                 _predicateList.remove(predicate);
80                 arg.addPredicate(predicate);
81             }
82         }
83
84         if (!_predicateList.isEmpty()) {
85             visitFather(arg);
86         }
87
88         //Trace.exit(this, "visit(Join arg)");
89
}
90
91     public void visit(RenameRelation arg) throws SqlWrapperException {
92         //Trace.enter(this, "visit(RenameRelation arg)", arg);
93

94         Set visibleTI = arg.visibleTableInstances();
95         Debug.assertTrue(1 == visibleTI.size(), "1 == visibleTI.size()");
96         Map replaceMap = new HashMap();
97
98         Expression ti = null;
99         for (Iterator i = visibleTI.iterator(); i.hasNext();) {
100             ti = (Expression) i.next();
101             if (ti instanceof RenameRelation) {
102                 replaceMap.put(ti, arg);
103             }
104         }
105
106         TopDownReplacer replacer = new TopDownReplacer(replaceMap);
107         Expression predicate = null;
108         for (int i = 0; i < _predicateList.size(); i++) {
109             predicate = (Expression) _predicateList.get(i);
110             predicate.accept(replacer);
111         }
112
113         visitFather(arg);
114
115         //Trace.exit(this, "visit(RenameRelation arg)", arg);
116
}
117
118     public void visit(UnOpProject arg) {
119         //Trace.enter(this, "visit(UnOpProject arg)", arg);
120
/* default implementation for UnOpSort, UnOpGroup, UnOpRestrict */
121         if (arg.getDistinct()) {
122             visitProjectDistinct(arg);
123         } else {
124             visitProject(arg);
125         }
126
127         //Trace.exit(this, "visit(UnOpProject arg)", arg);
128
}
129
130     protected void visitFather(Expression arg) {
131         // Trace.enter(this, "visitFather(Expression arg)");
132
Expression father = arg.getFather();
133         if (father != null) {
134             father.accept(this);
135         } else {
136             throw new SqlWrapperException(MessageLibrary.getMessage("IE_ERR", "Can not arrange predicate " /** @todo + _predicate.pprint() */
137             ), null);
138         }
139
140         // //Trace.exit(this, "visitFather(Expression arg)");
141
}
142     private void visitProjectDistinct(UnOpProject arg) {
143         //Trace.enter(this, "visitProjectDistinct(UnOpProject arg)", arg);
144
_blockingNode = arg;
145         //Trace.exit(this, "visitProjectDistinct(UnOpProject arg)", arg);
146
}
147
148     private void visitProject(UnOpProject arg) {
149         Debug.nyi("visitProject(UnOpProject arg)");
150     }
151
152 }
153
Popular Tags