KickJava   Java API By Example, From Geeks To Geeks.

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


1 /**
2  * Copyright (C) 2004 France Telecom R&D
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */

18 package org.objectweb.medor.optim.lib;
19
20 import org.objectweb.medor.query.api.QueryTree;
21 import org.objectweb.medor.query.api.FilteredQueryTree;
22 import org.objectweb.medor.query.api.QueryNode;
23 import org.objectweb.medor.query.api.PropagatedField;
24 import org.objectweb.medor.query.api.CalculatedField;
25 import org.objectweb.medor.api.Field;
26 import org.objectweb.medor.expression.api.Expression;
27 import org.objectweb.medor.expression.api.Operator;
28 import org.objectweb.medor.filter.api.FieldOperand;
29
30 /**
31  * This class is an helper class for managing the replacement of a QueryTree in
32  * a medor query represented by another QueryTree.
33  *
34  * @author S.Chassande-Barrioz
35  */

36 public class QueryNodeReplacer {
37
38     /**
39      * Replace a QueryTree by another. this is doing by replacing the usage of
40      * its field in all tree.
41      * @param old is the node to replace
42      * @param neo is the new node replacing the old
43      * @param root is the QueryTree from the search must start.
44      * @return if the root queryTree is the one searched, then the newer is
45      * returned, otherwise the root QueryTree is returned.
46      */

47     public QueryTree replaceNode(QueryTree old, QueryTree neo, QueryTree root) {
48         if (root == old) {
49             return neo;
50         }
51         replaceNode(old.getTupleStructure().getFields(),
52             neo.getTupleStructure().getFields(),
53             root);
54         return root;
55     }
56
57     /**
58      * Replace the references to old field by references to new fields in a
59      * QueryTree. The oldFields must not be attached to the current QueryTree.
60      * (Recursive method on QueryNode)
61      * @param oldFields the list of old field to replace
62      * @param neoFields the list of new field replacing the old
63      * @param current is the current QueryTree to treat
64      */

65     private void replaceNode(Field[] oldFields, Field[] neoFields, QueryTree current) {
66         if (current instanceof FilteredQueryTree) {
67             replaceFields(oldFields, neoFields,
68                 ((FilteredQueryTree) current).getQueryFilter());
69             //Update children from
70
((FilteredQueryTree) current).setQueryFilter(
71                 ((FilteredQueryTree) current).getQueryFilter());
72         }
73         if (current instanceof QueryNode) {
74             QueryNode qn = (QueryNode) current;
75             Field[] currentFields = qn.getTupleStructure().getFields();
76             for (int i = 0; i < currentFields.length; i++) {
77                 if (currentFields[i] instanceof PropagatedField) {
78                     PropagatedField pf = (PropagatedField) currentFields[i];
79                     Field[] previous = pf.getPreviousFields();
80                     boolean found = false;
81                     //Search on previous field
82
for (int j = 0; j < previous.length; j++) {
83                         for (int k = 0; k < oldFields.length; k++) {
84                             if (previous[j] == oldFields[k]) {
85                                 found = true;
86                                 //found ==> replace
87
previous[j] = neoFields[k];
88                                 break;
89                             }
90                         }
91                     }
92                     if (found) {
93                         pf.replacePreviousField(previous);
94                     }
95                 } else if (currentFields[i] instanceof CalculatedField) {
96                     CalculatedField cf = (CalculatedField) currentFields[i];
97                     replaceFields(oldFields, neoFields, cf.getExpression());
98                 }
99             }
100             QueryTree[] children = qn.getChildren();
101             for (int i = 0; i < children.length; i++) {
102                 replaceNode(oldFields, neoFields, children[i]);
103             }
104         }
105
106     }
107
108     /**
109      * Replace the references to old field by references to new fields in an
110      * expression (Recusrive method).
111      * @param oldFields the list of old field to replace
112      * @param neoFields the list of new field replacing the old
113      * @param exp is the current medor expression to treat.
114      */

115     public void replaceFields(Field[] oldFields, Field[] neoFields, Expression exp) {
116         if (exp instanceof Operator) {
117             Operator op = (Operator) exp;
118             for (int i = 0; i < op.getOperandNumber(); i++) {
119                 replaceFields(oldFields, neoFields, op.getExpression(i));
120             }
121         } else if (exp instanceof FieldOperand) {
122             FieldOperand fo = (FieldOperand) exp;
123             Field f = fo.getField();
124             for (int i = 0; i < oldFields.length; i++) {
125                 if (f == oldFields[i]) {
126                     fo.setField(neoFields[i]);
127                     break; //stop the search
128
}
129             }
130         }
131     }
132
133 }
134
Popular Tags