KickJava   Java API By Example, From Geeks To Geeks.

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


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.loadprofile;
24
25 import java.util.Enumeration JavaDoc;
26 import java.util.Vector JavaDoc;
27
28 import org.apache.log4j.Category;
29 import org.eclipse.swt.graphics.Color;
30
31 /**
32  * This class store the description of a load profile A load profile need a
33  * behavior and describe the injection profile of the behavior
34  *
35  * @author JC Meillaud
36  * @author A Peyrard
37  */

38 public class GroupDescription {
39     // logger
40
static Category cat = Category
41             .getInstance(GroupDescription.class.getName());
42
43     // attributes
44
private String JavaDoc behaviorId;
45
46     private Color curveColor;
47
48     private String JavaDoc groupId;
49
50     private Vector JavaDoc ramps;
51
52     private boolean updated;
53     
54     private boolean forceStop ;
55
56     public static final int END = -1;
57
58     ////////////////////////////////////////////////////////
59
// Constructors...
60
////////////////////////////////////////////////////////
61

62     /**
63      * Build a new group description element
64      *
65      * @param id
66      * The group id of the created group
67      */

68     public GroupDescription(String JavaDoc id) {
69         this.groupId = id;
70         this.behaviorId = null;
71         this.curveColor = null;
72         this.ramps = new Vector JavaDoc();
73         this.forceStop = true ;
74     }
75
76     /**
77      * Build a new group description element
78      *
79      * @param id
80      * The group id which will be created
81      * @param behaviorId
82      * The behavior id
83      * @param c
84      * The color of the curve representing this behavior
85      */

86     public GroupDescription(String JavaDoc id, String JavaDoc behaviorId, Color c,
87             Vector JavaDoc subCurves, boolean forceStop) {
88         this.groupId = id;
89         this.behaviorId = behaviorId;
90         this.curveColor = c;
91         this.ramps = subCurves;
92         this.forceStop = forceStop ;
93     }
94
95     /////////////////////////////////////////////////////////
96
// Methods...
97
/////////////////////////////////////////////////////////
98

99     /**
100      * This method adds a point to this group
101      *
102      * @param p
103      * The point to add
104      */

105     public void addPoint(Point p) {
106         // if the group has no ramp, do nothing
107
if (this.ramps.size() == 0)
108             return;
109         // create an int to store the number of the ramp
110
int number = -1;
111         // search the number of the ramp which have X-Axis containing the point
112
for (int i = 0; i < this.ramps.size(); i++) {
113             RampDescription temp = (RampDescription) this.ramps.elementAt(i);
114             if (p.x > temp.getStart().x && p.x < temp.getEnd().x) {
115                 number = i;
116                 break;
117             }
118         }
119         // create a new ramp between the first point of the ramp and the point
120
if (number != -1) {
121             // get the rampdescription
122
RampDescription rampContaining = (RampDescription) this.ramps
123                     .elementAt(number);
124             // get a rampID
125
String JavaDoc rampId = LoadProfileManager.getInstance().rampIdGenerator();
126             RampDescription rd = new RampDescription(rampId, rampContaining
127                     .getType(), rampContaining.getStart(), p, new Point(-1, -1));
128             // change the begining point of the ramp containing
129
rampContaining.setStart(p);
130             // add the new ramp to the current group
131
this.ramps.add(number, rd);
132         }
133         // if the point was not containing by group ramps
134
else {
135             // if the point is before the first point
136
RampDescription first = (RampDescription) this.ramps.elementAt(0);
137             if (p.x < first.getStart().x) {
138                 // generate a new id
139
String JavaDoc rampId = LoadProfileManager.getInstance()
140                         .rampIdGenerator();
141                 RampDescription newOne = new RampDescription(rampId, first
142                         .getType(), p, (Point) first.getStart().clone(),
143                         new Point(-1, -1));
144                 // add it
145
this.ramps.add(0, newOne);
146                 return;
147             }
148             // if the point is after the last ramp
149
RampDescription last = (RampDescription) this.ramps
150                     .elementAt(this.ramps.size() - 1);
151             if (p.x > last.getEnd().x) {
152                 // generate a new id
153
String JavaDoc rampId = LoadProfileManager.getInstance()
154                         .rampIdGenerator();
155                 RampDescription newOne = new RampDescription(rampId, last
156                         .getType(), (Point) last.getEnd().clone(), p,
157                         new Point(-1, -1));
158                 // add it
159
this.ramps.add(newOne);
160                 return;
161             }
162         }
163     }
164
165     /**
166      * This method delete the selcted point, the two curves which contains the
167      * point are moving into a unique one, which type is the type of the first
168      * ramp
169      *
170      * @param p
171      * The point to be deleted
172      */

173     public void deletePoint(Point p) {
174         // if the group has no ramp, do nothing
175
if (this.ramps.size() == 0)
176             return;
177         // create an int to store the number of the ramp
178
int number = -1;
179         // if there is only one ramp in the group, delete this ramp
180
if (this.ramps.size() == 1) {
181             this.ramps.clear();
182             return;
183         }
184         // if the point selected is the first point of the group
185
if (((RampDescription) this.ramps.elementAt(0)).getStart().equals(p)) {
186             this.ramps.remove(0);
187             return;
188         }
189         // if the point selected is the last point of the group
190
if (((RampDescription) this.ramps.elementAt(this.ramps.size() - 1))
191                 .getEnd().equals(p)) {
192             this.ramps.remove(this.ramps.size() - 1);
193             return;
194         }
195         // search the number of the ramp which contains the point
196
for (int i = 1; i < this.ramps.size(); i++) {
197             RampDescription temp = (RampDescription) this.ramps.elementAt(i);
198             if (p.x == temp.getStart().x && p.y == temp.getStart().y) {
199                 number = i;
200                 break;
201             }
202         }
203         // the curve containing the point are the ramp nb 'number' and the ramp
204
// nb 'number-1'
205
RampDescription rdNumber = (RampDescription) this.ramps
206                 .elementAt(number);
207         RampDescription rdNumberMinusOne = (RampDescription) this.ramps
208                 .elementAt(number - 1);
209         rdNumberMinusOne.setEnd((Point) rdNumber.getEnd().clone());
210         // remove the ramp nb 'number'
211
this.ramps.remove(number);
212     }
213
214     /**
215      * Add a new ramp to the group
216      *
217      * @param rd
218      * The ramp to add
219      * @return True if the ramp can be added, else false
220      */

221     public boolean addRamp(RampDescription rd) {
222         // we put the new ramp on the right place in the ramps vector
223
// all the ramp are classed by time increasing
224
this.ramps.add(rd);
225         return false;
226     }
227
228     /**
229      * Clone the ramp description but change the id, if the new id is null, set
230      * the previous one
231      *
232      * @param id
233      * The ew id of the cloned ramp
234      * @return The ramp cloned
235      */

236     public GroupDescription clone(String JavaDoc id) {
237         GroupDescription clone = null;
238         if (id == null)
239             clone = new GroupDescription(this.groupId);
240         else
241             clone = new GroupDescription(id);
242         // set the attribute of the ramp description
243
clone.setBehaviorId(this.behaviorId);
244         clone.setCurveColor(this.curveColor);
245         clone.setRamps((Vector JavaDoc) this.ramps.clone());
246         return clone;
247     }
248
249     /**
250      * Return the curve number of the curve containing the given point
251      *
252      * @param p
253      * The point
254      * @return {nb1, nb2} if the point is between two curves, {-1, 0} if the
255      * point is the begining of the first curve, {nb,-1}, if the point
256      * is the ending point of the last curve
257      */

258     public Point rampsWhichContains(Point p) {
259         // search in all curves
260
if (this.ramps.size() == 0)
261             return null;
262         // test the begining point
263
RampDescription curve = (RampDescription) this.ramps.elementAt(0);
264         if (curve.getStart().equals(p))
265             return new Point(-1, 0);
266         // test all last point of subcurve, which are the first point of the
267
// next curve unless the curve is the last one
268
for (int i = 0; i < this.ramps.size(); i++) {
269             RampDescription tempDesc = (RampDescription) this.ramps
270                     .elementAt(i);
271             if (tempDesc.getEnd().equals(p)) {
272                 // we find the curve, if there is a next curve, return {i, i+1}
273
// else {-1,i}
274
if (this.ramps.size() - 1 == i)
275                     return new Point(i, -1);
276                 else
277                     return new Point(i, i + 1);
278             }
279         }
280         // should never append, because when we use this method we know that the
281
// point is a selectable point of the ramp
282
return null;
283     }
284
285     /**
286      * This method returns the ramp description of this group which have rampId
287      * for ID
288      *
289      * @param rampId
290      * The rampId searched
291      * @return The ramp description found
292      */

293     public RampDescription getRampDescription(String JavaDoc rampId) {
294         // for each ramp test if the ID are equals, than the given ID
295
for (int i = 0; i < this.ramps.size(); i++) {
296             if (((RampDescription) this.ramps.elementAt(i)).getRampId().equals(
297                     rampId))
298                 return (RampDescription) this.ramps.elementAt(i);
299         }
300         // if the ramp have not been found
301
return null;
302     }
303
304     /**
305      * This method return an enumeration of all ramps containing by the groups
306      *
307      * @return The enumeration
308      */

309     public Enumeration JavaDoc getElements() {
310         return this.ramps.elements();
311     }
312
313     /**
314      * Method which delete a ramp description element
315      *
316      * @param rampId
317      * The ramp ID
318      */

319     public void removeRampDescription(String JavaDoc rampId) {
320         int number = -1;
321         // for each ramp test if the ID are equals, than the given ID
322
for (int i = 0; i < this.ramps.size(); i++) {
323             if (((RampDescription) this.ramps.elementAt(i)).getRampId().equals(
324                     rampId))
325                 number = i;
326         }
327         // delete the ramp if it has been found
328
if (number != -1) {
329             this.ramps.remove(number);
330         }
331     }
332
333     /**
334      * Method which delete a ramp description element
335      *
336      * @param rd
337      * The ramp description
338      */

339     public void removeRampDescription(RampDescription rd) {
340         // test if the ramps vector contains the given ramp
341
if (this.ramps.contains(rd))
342             this.ramps.remove(rd);
343     }
344
345     /**
346      * Method which returns the ramps id of this group
347      *
348      * @return The ramps ids
349      */

350     public Vector JavaDoc getRampDescriptionIds() {
351         Vector JavaDoc result = new Vector JavaDoc();
352         // for each ramp add the ID to the ids vector
353
for (int i = 0; i < this.ramps.size(); i++)
354             result.add(((RampDescription) this.ramps.elementAt(i)).getRampId());
355         return result;
356     }
357
358     /**
359      * Method to update the RampDescription Data (coefficient for the the line,
360      * ...)
361      *
362      */

363     public void updateRampDescription() {
364         for (int i = 0; i < this.ramps.size(); i++)
365             ((RampDescription) this.ramps.elementAt(i)).updateRampData();
366     }
367
368     /**
369      * Method wich give the <i>Y </i> value corresponding to <i>X </i>. <br>
370      * The value returned is -1, if the <i>X </i> value doesnt belong to the
371      * GroupDescription
372      *
373      * @param x
374      * the x value
375      * @return the y value
376      */

377     public long getVirtualUserNumber(int x) {
378         int result = END;
379         // search the number of the ramp which have X-Axis containing the point
380
int number = -1;
381         for (int i = 0; i < this.ramps.size(); i++) {
382             RampDescription temp = (RampDescription) this.ramps.elementAt(i);
383             if (x <= temp.getEnd().x) {
384                 result = 0;
385                 if (x >= temp.getStart().x) {
386                     number = i;
387                     break;
388                 }
389             }
390         }
391         if (number != -1) {
392             RampDescription rampContaining = (RampDescription) this.ramps
393                     .elementAt(number);
394             if (x == rampContaining.getStart().x) {
395                 result = rampContaining.getStart().y;
396             } else if (x == rampContaining.getEnd().x) {
397                 result = rampContaining.getEnd().y;
398             } else {
399                 result = rampContaining.getCorrespondingY(x);
400             }
401
402         }
403         return result;
404     }
405
406     ////////////////////////////////////////////////////////////////
407
// Attributes getters and setters
408
////////////////////////////////////////////////////////////////
409

410     /**
411      * @return Returns the behaviorId.
412      */

413     public String JavaDoc getBehaviorId() {
414         return behaviorId;
415     }
416
417     /**
418      * @return Returns the curveColor.
419      */

420     public Color getCurveColor() {
421         return curveColor;
422     }
423
424     /**
425      * @param behaviorId
426      * The behaviorId to set.
427      */

428     public void setBehaviorId(String JavaDoc behaviorId) {
429         this.behaviorId = behaviorId;
430     }
431
432     /**
433      * @param curveColor
434      * The curveColor to set.
435      */

436     public void setCurveColor(Color curveColor) {
437         this.curveColor = curveColor;
438     }
439
440     /**
441      * @return Returns the groupId.
442      */

443     public String JavaDoc getGroupId() {
444         return groupId;
445     }
446
447     /**
448      * @return Returns the ramps.
449      */

450     public Vector JavaDoc getRamps() {
451         return ramps;
452     }
453
454     /**
455      * @param ramps
456      * The ramps to set.
457      */

458     public void setRamps(Vector JavaDoc ramps) {
459         this.ramps = ramps;
460     }
461
462     /**
463      * @return Returns the forceStop.
464      */

465     public boolean isForceStop() {
466         return forceStop;
467     }
468     /**
469      * @param forceStop The forceStop to set.
470      */

471     public void setForceStop(boolean forceStop) {
472         this.forceStop = forceStop;
473     }
474     
475     ////////////////////////////////////
476
// DEBUG METHODS...
477
////////////////////////////////////
478

479     public String JavaDoc toString() {
480         String JavaDoc result = "";
481         for (int i = 0; i < ramps.size(); i++) {
482             result = result.concat(((RampDescription) ramps.elementAt(i))
483                     .toString());
484         }
485         return result;
486     }
487 }
Popular Tags