KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > clif > scenario > util > isac > engine > loadprofile > GroupDescriptionManager


1 /*
2  * CLIF is a Load Injection Framework
3  * Copyright (C) 2004 France Telecom R&D
4  *
5  * This library 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 of the License, or (at your option) any later version.
9  *
10  * This library 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 library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  *
19  * CLIF
20  *
21  * Contact: clif@objectweb.org
22  */

23 package org.objectweb.clif.scenario.util.isac.engine.loadprofile;
24
25 import java.util.Enumeration JavaDoc;
26 import java.util.Hashtable JavaDoc;
27 import java.util.Vector JavaDoc;
28
29 import org.objectweb.clif.scenario.util.isac.loadprofile.GroupDescription;
30 import org.objectweb.clif.scenario.util.isac.loadprofile.Point;
31 import org.objectweb.clif.scenario.util.isac.loadprofile.RampDescription;
32 import org.objectweb.clif.scenario.util.isac.util.IntHolder;
33 import org.w3c.dom.Element JavaDoc;
34 import org.w3c.dom.NamedNodeMap JavaDoc;
35 import org.w3c.dom.Node JavaDoc;
36 import org.w3c.dom.NodeList JavaDoc;
37
38 /**
39  * org.objectweb.clif.scenario.util.isac.engine.loadprofile.GroupDescriptionManager
40  * This class is in charge of the management of the groups. A group is a set of
41  * ramps defining a load profile. All the groups which can be used during the
42  * execution are analysed.
43  *
44  * @author JC Meillaud
45  * @author A Peyrard
46  */

47 public class GroupDescriptionManager {
48
49     /**
50      * attributes
51      */

52     // The table wich contains the groupdescription
53
private Hashtable JavaDoc groupDescriptionTable;
54
55     // The table wich contains the running threads for a group
56
private Hashtable JavaDoc groupRunningBehaviorsTable;
57
58     private Hashtable JavaDoc threadWaitingNumberTable;
59     
60     private Hashtable JavaDoc threadRunningRequiredTable ;
61     
62     private Hashtable JavaDoc maximumsOfBehaviorsTable ;
63     
64     // The current analysed group
65
private GroupDescription analysedGroup;
66
67     // The courant analysed ramp
68
private RampDescription analysedRamp;
69
70     private Point currentEnd;
71
72     private Point currentStart;
73
74     private Point lastEnd;
75     
76     private int analyzeMax ;
77     
78
79
80     /**
81      *
82      */

83     public GroupDescriptionManager() {
84         groupDescriptionTable = new Hashtable JavaDoc();
85         groupRunningBehaviorsTable = new Hashtable JavaDoc();
86         threadWaitingNumberTable = new Hashtable JavaDoc();
87         threadRunningRequiredTable = new Hashtable JavaDoc() ;
88         maximumsOfBehaviorsTable = new Hashtable JavaDoc() ;
89         currentEnd = null;
90         currentStart = null;
91         lastEnd = null;
92     }
93
94     /**
95      * This method build a new GroupDescription from the node given in
96      * parameter.
97      *
98      * @param node
99      * a node representing the root of a tree describing a group
100      */

101     public void addGroupDescription(Node JavaDoc node) {
102         analysedGroup = new GroupDescription(null);
103         this.analyzeMax = 0 ;
104         Vector JavaDoc errors = new Vector JavaDoc();
105         visitGroupDescriptionNode(node, errors);
106         analysedGroup.updateRampDescription();
107         Integer JavaDoc identifiant = generateId(analysedGroup);
108         groupDescriptionTable.put(identifiant, analysedGroup);
109         groupRunningBehaviorsTable.put(identifiant, new Vector JavaDoc());
110         threadWaitingNumberTable.put(identifiant, new IntHolder(0));
111         threadRunningRequiredTable.put(identifiant, new IntHolder(0)) ;
112         this.maximumsOfBehaviorsTable.put(identifiant, new Integer JavaDoc(this.analyzeMax)) ;
113     }
114
115     /**
116      * This method generate an unique id to identify the GroupDescription.
117      *
118      * @param groupDescription
119      * the GroupDescription from wich we have to generate an unique
120      * id
121      * @return An Integer Object representing the id
122      */

123     private Integer JavaDoc generateId(GroupDescription groupDescription) {
124         int keyInt = groupDescription.hashCode();
125         // check in the table if the key already exist
126
while (this.groupDescriptionTable.containsKey(new Integer JavaDoc(keyInt)))
127             keyInt++;
128         return new Integer JavaDoc(keyInt);
129     }
130
131     /**
132      * This method is used to analyse the tree nodes representing the group
133      *
134      * @param node
135      * a node representing the root of a tree describing a group
136      * @param errors
137      * A vector for the errors and warning
138      */

139     private void visitGroupDescriptionNode(Node JavaDoc node, Vector JavaDoc errors) {
140         String JavaDoc tagName = null;
141         switch (node.getNodeType()) {
142             case Node.ELEMENT_NODE :
143                 tagName = ((Element JavaDoc) node).getTagName();
144                 if (org.objectweb.clif.scenario.util.isac.util.tree.Node.RAMP
145                         .equals(tagName)) {
146                     NamedNodeMap JavaDoc attributes = node.getAttributes();
147                     String JavaDoc style = attributes.getNamedItem("style").getNodeValue();
148                     // test with the defined style
149
if (style.equals(RampDescription.LINE_STRING)) {
150                         analysedRamp = new RampDescription(null,
151                                 RampDescription.LINE);
152                     } else if (style.equals(RampDescription.ARC_STRING)) {
153                         analysedRamp = new RampDescription(null,
154                                 RampDescription.ARC);
155                     } else if (style.equals(RampDescription.CRENEL_VH_STRING)) {
156                         analysedRamp = new RampDescription(null,
157                                 RampDescription.CRENEL_VH);
158                     } else if (style.equals(RampDescription.CRENEL_HV_STRING)) {
159                         analysedRamp = new RampDescription(null,
160                                 RampDescription.CRENEL_HV);
161                     }
162                 } else if (org.objectweb.clif.scenario.util.isac.util.tree.Node.GROUP
163                         .equals(tagName)) {
164                     NamedNodeMap JavaDoc attributs = node.getAttributes();
165                     String JavaDoc behaviorId = new String JavaDoc(attributs.getNamedItem("behavior").getNodeValue());
166                     // set the behavior id in the description
167
this.analysedGroup.setBehaviorId(behaviorId) ;
168                     
169                     Node JavaDoc forceStopAttribute = attributs.getNamedItem("forceStop") ;
170                     // if the user define a 'forceStop' attribute
171
if (forceStopAttribute != null) {
172                         String JavaDoc forceStopValue = forceStopAttribute.getNodeValue() ;
173                         if (forceStopValue.equals("false")) {
174                             // set the value of force stop in the group desc
175
this.analysedGroup.setForceStop(false) ;
176                         }
177                         // else true, but true is the default value, so do nothing
178
}
179                 } else if (org.objectweb.clif.scenario.util.isac.util.tree.Node.POINTS
180                         .equals(tagName)) {
181                     // set the current end point as the last end point because
182
// we are describing a new ramp
183
currentEnd = null;
184                     currentStart = null;
185                 } else if (org.objectweb.clif.scenario.util.isac.util.tree.Node.POINT
186                         .equals(tagName)) {
187                     // check if we are in a ramp description
188
if (analysedRamp == null) {
189                         errors
190                                 .add("WARNING : There is a point defined outside a ramp description, ignore it...");
191                     } else {
192                         // get the point
193
NamedNodeMap JavaDoc attributes = node.getAttributes();
194                         String JavaDoc name = attributes.getNamedItem("name").getNodeValue();
195                         String JavaDoc xAxis = attributes.getNamedItem("x").getNodeValue();
196                         String JavaDoc yAxis = attributes.getNamedItem("y").getNodeValue();
197                         if (xAxis != null && yAxis != null && name != null) {
198                             Integer JavaDoc xAxisInteger = new Integer JavaDoc(xAxis);
199                             Integer JavaDoc yAxisInteger = new Integer JavaDoc(yAxis);
200                             // if the yAxis is the maximum store it
201
if (this.analyzeMax < (int)yAxisInteger.intValue()) {
202                                 this.analyzeMax = (int)yAxisInteger.intValue() ;
203                             }
204                             // analyse the point name
205
if (name.equals(RampDescription.POINT_START)) {
206                                 // start point
207
// verify that the start point of this ramp is
208
// the same than the last end ramp point
209
currentStart = new Point(xAxisInteger.intValue(),
210                                         yAxisInteger.intValue());
211
212                                 // if we are not the starting ramp of the group
213
if (lastEnd != null) {
214                                     if (!lastEnd.equals(currentStart)) {
215                                         errors
216                                                 .add("ERROR : A ramp have a starting point which is not the same than the previous ramp ending point !");
217                                     }
218                                 }
219                                 if (currentEnd != null) {
220                                     // test if the starting point is before the
221
// ending point in the time scale
222
if (currentStart.x >= currentEnd.x) {
223                                         errors
224                                                 .add("WARNING : The starting point is after the ending point in the time scale, ignore the ramp !");
225                                     }
226                                     // the starting and the ending point
227
// have been defined we can add the ramp
228
analysedRamp.setStart(currentStart);
229                                     analysedRamp.setEnd(currentEnd);
230                                     // add the ramp to the load profile
231
// manager
232
analysedGroup.addRamp(analysedRamp);
233                                 }
234                             } else if (name.equals(RampDescription.POINT_END)) {
235                                 // end point
236
currentEnd = new Point(xAxisInteger.intValue(),
237                                         yAxisInteger.intValue());
238                                 if (currentStart != null) {
239                                     // test if the starting point is before the
240
// ending point in the time scale
241
if (currentStart.x >= currentEnd.x) {
242                                         errors
243                                                 .add("WARNING : The starting point is after the ending point in the time scale, ignore the ramp !");
244                                     }
245                                     // the starting and the ending point
246
// have been defined we can add the ramp
247
analysedRamp.setStart(currentStart);
248                                     analysedRamp.setEnd(currentEnd);
249                                     // add the ramp to the group description
250
// manager
251
analysedGroup.addRamp(analysedRamp);
252                                 }
253
254                             } else if (name.equals(RampDescription.POINT_ANGLE)) {
255                                 // angle point
256
if (analysedRamp.getType() == RampDescription.LINE) {
257                                     errors
258                                             .add("WARNING : The ramp is a line and does not need any angle point, so ignore it !");
259                                 } else {
260                                     analysedRamp
261                                             .setAngle(new Point(xAxisInteger
262                                                     .intValue(), yAxisInteger
263                                                     .intValue()));
264                                 }
265                             }
266                         } else {
267                             errors
268                                     .add("ERROR : One of the point parameter have not been defined, you must define : name, x, y !");
269                         }
270                     }
271                 }
272
273                 break;
274             default :
275         // do nothing
276
}
277         if (node.hasChildNodes()) {
278             NodeList JavaDoc fils = node.getChildNodes();
279             for (int i = 0; i < fils.getLength(); i++) {
280                 Node JavaDoc tempNode = fils.item(i);
281                 visitGroupDescriptionNode(fils.item(i), errors);
282             }
283         }
284     }
285
286     /**
287      * This method give the Group Description corresponding to the groupid
288      * parameter from the Group Description Table
289      *
290      * @param groupid
291      * A String representing the id of the wanted Group Description
292      * @return The Group Description
293      */

294     public GroupDescription getGroupDescription(Integer JavaDoc groupid) {
295         return (GroupDescription) groupDescriptionTable
296                 .get(groupid);
297     }
298
299     /**
300      * This method is used to get all the Group Description keys of the Group
301      * Decription Table
302      *
303      * @return An Enumeration of all the keys containing in the Group
304      * Description Table
305      */

306     public Enumeration JavaDoc getGroupsIds() {
307         return groupDescriptionTable.keys();
308     }
309
310     /**
311      * Method used to get the vector containing all the behavior threads from
312      * the Group Description identified by the id.
313      *
314      * @param id
315      * The id of the Group Description
316      * @return A vector containing all the behaviors threads of this Group
317      * Description
318      */

319     public Vector JavaDoc getGroupRunningBehaviors(Integer JavaDoc id) {
320         return (Vector JavaDoc) groupRunningBehaviorsTable.get(id);
321     }
322
323     /**
324      * Method used to remove a Group
325      *
326      * @param id
327      * The id of the GroupDescription
328      */

329     public void removeGroup(Integer JavaDoc id) {
330         groupDescriptionTable.remove(id) ;
331         groupRunningBehaviorsTable.remove(id);
332         threadWaitingNumberTable.remove(id) ;
333     }
334
335     public IntHolder getNumberThreadWaitingForAGroup(Integer JavaDoc id) {
336         return (IntHolder) this.threadWaitingNumberTable.get(id);
337     }
338     
339     public IntHolder getNumberThreadRunningRequiredForAGroup(Integer JavaDoc id) {
340         return (IntHolder) this.threadRunningRequiredTable.get(id);
341     }
342     
343     public int getTotalNumberThreadWaiting() {
344         int result = 0;
345         Enumeration JavaDoc e = this.threadWaitingNumberTable.elements();
346         while (e.hasMoreElements()) {
347             IntHolder temp = (IntHolder) e.nextElement();
348             result += temp.getIntValue();
349         }
350         return result;
351     }
352
353     public int getTotalNumberThreadRunning() {
354         int result = 0;
355         Enumeration JavaDoc e = this.groupRunningBehaviorsTable.elements();
356         while (e.hasMoreElements()) {
357             Vector JavaDoc temp = (Vector JavaDoc) e.nextElement();
358             result += temp.size();
359         }
360         return result;
361     }
362     
363     /**
364      * Method which permit to get the maximum number of behaviors running in this group
365      * @param currentId The goup id
366      * @return The number max
367      */

368     public int getMaximumBehaviorsForAGroup(Integer JavaDoc currentId) {
369         return ((Integer JavaDoc)this.maximumsOfBehaviorsTable.get(currentId)).intValue() ;
370     }
371
372 }
Popular Tags