KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > modfact > jmi > ps > MOFPolymorphicIterator


1 package org.objectweb.modfact.jmi.ps;
2 import javax.jmi.reflect.*;
3 import java.util.*;
4
5 import org.objectweb.modfact.jmi.logging.Level;
6 import org.objectweb.modfact.jmi.logging.ModFactLogger;
7 import org.objectweb.modfact.jmi.reflect.*;
8
9 /**
10  * @author gregory
11  *
12  * This class is a tool for traverse any MOF Model based on the design pattern
13  * Iterator. It builds a linear representation of the MOF model i.e put it in a
14  * list. We do a BFS on the containment relationship with an order on types
15  * (Package, Class, Association, and Objects).
16  *
17  *
18  * There's a filter system, i.e you can only browse a certain type of the model.
19  * This latter feature is improvable
20  */

21 public class MOFPolymorphicIterator implements Iterator {
22
23     private Vector allNodes = new Vector();
24     private int index = 0;
25
26     private boolean packageFilter = false;
27     private boolean classFilter = false;
28     private boolean associationFilter = false;
29     private boolean instanceFilter = false;
30     private ModFactLogger logger;
31
32     /**
33      *
34      * @param packageFilter
35      * @param classFilter
36      * @param associationFilter
37      * @param instanceFilter
38      * if one of the parameters is set to true then all elements of
39      * this type will be filtered in the traversal algorithm
40      */

41     public void setFilters(
42         boolean packageFilter,
43         boolean classFilter,
44         boolean associationFilter,
45         boolean instanceFilter) {
46         this.packageFilter = packageFilter;
47         this.classFilter = classFilter;
48         this.associationFilter = associationFilter;
49         this.instanceFilter = instanceFilter;
50
51     }
52
53     /**
54      *
55      * @param o : RefBaseObject
56      * Traverse all the contained objects of o, if o is not an instance
57      */

58     public MOFPolymorphicIterator(RefBaseObject o) {
59
60         Iterator it;
61
62         allNodes.addElement(o);
63         BFS(o);
64
65         //debugCode
66
this.logger = ModFactLogger.getLogger("fr.lip6.modfact.jmi.reflect.ps");
67         logger.log(Level.INFO, "création de l'itérateur");
68
69         it = allNodes.iterator();
70
71         while (it.hasNext()) {
72
73             RefBaseObject oo = (RefBaseObject) it.next();
74
75             if (!(oo instanceof RefObject))
76                 logger.log(
77                     Level.INFO,
78                     (oo.getClass().toString()
79                         + "type de l'objet:"
80                         + (((RefBaseObject) oo).refMetaObject()).refGetValue(
81                             "name"))
82                         + " ");
83             else if (oo instanceof RefObject)
84                 logger.log(
85                     Level.INFO,
86                     (oo.getClass().toString()
87                         + "nom et type de l'objet:"
88                         + ((RefObject) oo).refGetValue("name"))
89                         + "----"
90                         + ((RefObject) oo).refMetaObject().refGetValue("name"));
91             else
92                 logger.log(Level.INFO, oo.getClass().toString() + ":" + "#");
93         }
94
95         index = 0;
96     }
97
98     /**
99      *
100      * @see java.util.Iterator#hasNext()
101      * Implements the design pattern hasNext operation
102      */

103     public boolean hasNext() {
104
105         while (index < allNodes.size()) {
106             if ((allNodes.elementAt(index) instanceof RefPackage
107                 && packageFilter)
108                 || ((allNodes.elementAt(index) instanceof RefClass
109                     && classFilter))
110                 || ((allNodes.elementAt(index) instanceof RefAssociation
111                     && associationFilter))
112                 || ((allNodes.elementAt(index) instanceof RefObject
113                     && instanceFilter)))
114                 index++;
115             else
116                 return true;
117
118         }
119         return false;
120     }
121
122     /**
123      * @see java.util.Iterator#next()
124      * implement the design pattern Iterator next operation
125      */

126
127     public Object JavaDoc next() {
128
129         RefBaseObject o = null;
130
131         while (index < allNodes.size()) {
132             if ((allNodes.elementAt(index) instanceof RefPackage
133                 && packageFilter)
134                 || ((allNodes.elementAt(index) instanceof RefClass
135                     && classFilter))
136                 || ((allNodes.elementAt(index) instanceof RefAssociation
137                     && associationFilter))
138                 || ((allNodes.elementAt(index) instanceof RefObject
139                     && instanceFilter)))
140                 index++;
141             else {
142
143                 return allNodes.elementAt(index++);
144
145             }
146
147         }
148         return o;
149     }
150
151     /**
152      * Return the first element and reset the index
153      * of the list
154      */

155     public Object JavaDoc first() {
156         index = 0;
157         return (RefBaseObject) allNodes.elementAt(index);
158     }
159
160     /**
161      * We can reuse the iterator an browse again the model
162      * by resseting the index of the list
163      */

164     public void reinitialize() {
165         index = 0;
166     }
167
168     /**
169     * @see java.util.Iterator#remove()
170     */

171     public void remove() {
172     }
173
174     /**
175      * Method BFS.
176      * @param current
177      *
178      * Put all elements of the Model in a list by traversing the graph of
179      * containment relationship. We do a breadth first search.
180      */

181     private void BFS(RefBaseObject current) {
182
183         Iterator it;
184
185         index++;
186         if (hasPackages(current)) {
187             it = ((RefPackage) current).refAllPackages().iterator();
188             while (it.hasNext())
189                 allNodes.addElement(it.next());
190
191         }
192         if (hasClasses(current)) {
193             it = ((RefPackage) current).refAllClasses().iterator();
194             while (it.hasNext())
195                 allNodes.addElement(it.next());
196
197         }
198         if (hasAssociation(current)) {
199             it = ((RefPackage) current).refAllAssociations().iterator();
200             while (it.hasNext())
201                 allNodes.addElement(it.next());
202
203         }
204
205         if (hasInstance(current)) {
206             it = ((RefClass) current).refAllOfClass().iterator();
207             while (it.hasNext()) {
208                 allNodes.addElement(it.next());
209             }
210
211         }
212
213         if (index < allNodes.size())
214             BFS((RefBaseObject) allNodes.elementAt(index));
215
216     }
217
218     /**
219      * Method hasChild.
220      * @param p
221      * @return boolean
222      *
223      * The followings helper function should be improved
224      */

225     private boolean hasChild(RefPackageImpl p) {
226
227         if (!p.refAllPackages().isEmpty())
228             return true;
229
230         return false;
231
232     }
233
234     /**
235      * Method hasPackages.
236      * @param o
237      * @return boolean
238      */

239     private boolean hasPackages(RefBaseObject o) {
240         if ((o instanceof RefPackageImpl)
241             && !(((RefPackage) o).refAllPackages().isEmpty()))
242             return true;
243
244         return false;
245     }
246
247     /**
248      * Method hasClasses.
249      * @param o
250      * @return boolean
251      */

252     private boolean hasClasses(RefBaseObject o) {
253         if ((o instanceof RefPackageImpl)
254             && !(((RefPackage) o).refAllClasses().isEmpty()))
255             return true;
256         return false;
257     }
258     
259     /**
260      * Method hasAssociation.
261      * @param o
262      * @return boolean
263      */

264     private boolean hasAssociation(RefBaseObject o) {
265         if ((o instanceof RefPackage)
266             && !(((RefPackage) o).refAllAssociations().isEmpty()))
267             return true;
268         return false;
269     }
270
271     /**
272      * Method hasInstance.
273      * @param o
274      * @return boolean
275      */

276     private boolean hasInstance(RefBaseObject o) {
277         if ((o instanceof RefClass)
278             && !(((RefClass) o).refAllOfClass().isEmpty()))
279             return true;
280         return false;
281     }
282
283 }
284
Popular Tags