KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > medor > optim > jorm > JormLeafRewriter


1 /**
2  * MEDOR: Middleware Enabling Distributed Object Requests
3  *
4  * Copyright (C) 2001-2004 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 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 JavaDoc;
46 import java.util.Map JavaDoc;
47
48 /**
49  * @author S. Chassande-Barrioz
50  */

51 public abstract class JormLeafRewriter extends JormRule
52     implements LeafRewriter {
53
54     public JormLeafRewriter(String JavaDoc 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 JavaDoc("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                 //add a propagated field into the intermediate QueryTree
86
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         //System.out.println("NameDef: " + nd);
120
Operand o = getPNCOperand(extent, pnf);
121         PType type = pnf.getType();
122         QueryTreeField qtf = null;
123         if (nd.isFieldName()) {
124             String JavaDoc ndfn = getFieldName(specificQL.getName(), nd.getFieldName());
125             //Check if the field has not already added on the specific query leaf
126
PrimitiveElement pe = getPrimitiveElement(extent, ndfn);
127             qtf = getField(specificQL, pe, extent);
128
129             //add a CalculatedField into the intermediate QueryTree.
130
// This calculatedField has an expression which is a
131
// SinglePName.
132
Expression e = new SinglePName(new BasicFieldOperand(qtf),o, type);
133             qt.addCalculatedField(pnf.getName(), type, e);
134         }
135         else if (nd.isNameRef()) {
136             Map JavaDoc proj = nd.getNameRef().getProjection();
137             FieldOperand[] fos = new BasicFieldOperand[proj.size()];
138             String JavaDoc[] cofns = new String JavaDoc[proj.size()];
139             int counter = 0;
140             for (Iterator JavaDoc it = proj.entrySet().iterator(); it.hasNext();) {
141                 Map.Entry JavaDoc me = (Map.Entry JavaDoc) it.next();
142                 cofns[counter] = (String JavaDoc) me.getKey();
143                 String JavaDoc clafn = (String JavaDoc) me.getValue();
144                 String JavaDoc ndfn = getFieldName(specificQL.getName(), clafn);
145                 qtf = getField(specificQL, getPrimitiveElement(extent, ndfn), extent);
146                 fos[counter] = new BasicFieldOperand(qtf);
147                 counter++;
148             }
149             //add a CalculatedField into the intermediate QueryTree.
150
// This calculatedField has an expression which is a
151
// CompositePName.
152
Expression e = new CompositePName(fos, cofns, o, type);
153             qt.addCalculatedField(pnf.getName(), type, e);
154         }
155     }
156     protected String JavaDoc getFieldName(String JavaDoc prefix, String JavaDoc nodeName) {
157         if (prefix==null || prefix.length()==0)
158             return nodeName;
159         else
160             return prefix + "." + nodeName;
161     }
162
163     protected String JavaDoc getShortFieldName(String JavaDoc fn) {
164         return fn.substring(fn.lastIndexOf(".")+1, fn.length());
165     }
166 }
167
Popular Tags