Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
peersimgrid release 1.0
[simgrid.git] / contrib / psg / src / peersim / vector / LinearDistribution.java
1 /*
2  * Copyright (c) 2003-2005 The BISON Project
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License version 2 as
6  * published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  * GNU Lesser General Public License for more details.
12  *
13  * You should have received a copy of the GNU Lesser General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
16  *
17  */
18
19 package peersim.vector;
20
21 import peersim.config.*;
22 import peersim.core.*;
23
24 /**
25  * Initializes a protocol vector with values in the range [{@value #PAR_MIN}, 
26  * {@value #PAR_MAX}] (inclusive both ends), linearly increasing.
27  * @see VectControl
28  * @see peersim.vector
29  */
30 public class LinearDistribution extends VectControl
31 {
32
33 //--------------------------------------------------------------------------
34 //Parameters
35 //--------------------------------------------------------------------------
36
37 /**
38  * Upper end of the interval..
39  * @config
40  */
41 private static final String PAR_MAX = "max";
42
43 /**
44  * Lower end of the interval. Defaults to -{@value #PAR_MAX}.
45  * @config
46  */
47 private static final String PAR_MIN = "min";
48
49 // --------------------------------------------------------------------------
50 // Fields
51 // --------------------------------------------------------------------------
52
53 /** Minimum value */
54 private final Number min;
55
56 /** Maximum value */
57 private final Number max;
58
59 /** Step value */
60 private final double step;
61
62 // --------------------------------------------------------------------------
63 // Initialization
64 // --------------------------------------------------------------------------
65
66 /**
67  * Standard constructor that reads the configuration parameters.
68  * Invoked by the simulation engine.
69  * @param prefix the configuration prefix for this class
70  */
71 public LinearDistribution(String prefix)
72 {
73         super(prefix);
74         
75         // Read parameters based on type
76         if (setter.isInteger()) {
77                 max=Long.valueOf(Configuration.getLong(prefix + "." + PAR_MAX));
78                 min=Long.valueOf(Configuration.getLong(prefix + "." + PAR_MIN, 
79                                 -max.longValue()));
80                 step= (max.longValue()-min.longValue())/
81                         ((double)(Network.size()-1));
82         } else { // we know it's double or float
83                 max = new Double(Configuration.getDouble(prefix+"."+PAR_MAX));
84                 min = new Double(Configuration.getDouble(prefix+"."+PAR_MIN, 
85                                 -max.doubleValue()));
86                 step= (max.doubleValue()-min.doubleValue())/(Network.size()-1);
87         }
88 }
89
90 // --------------------------------------------------------------------------
91 // Methods
92 // --------------------------------------------------------------------------
93
94 /**
95  * Initializes a protocol vector with values in the range [{@value #PAR_MIN}, 
96  * {@value #PAR_MAX}] (inclusive both ends), linearly increasing.
97  * @return always false
98  */
99 public boolean execute() {
100         
101         if ( setter.isInteger() )
102         {
103                 for(int i=0; i<Network.size(); ++i)
104                 {
105                         // we avoid the entire expression being cast to double
106                         setter.set(i,Math.round(i*step)+min.longValue());
107                 }
108         }
109         else
110         {
111                 for(int i=0; i<Network.size(); ++i)
112                 {
113                         setter.set(i,i*step+min.doubleValue());
114                 }
115         }
116
117         return false;
118 }
119
120
121 }
122
123