2 * Copyright (c) 2003-2005 The BISON Project
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.
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.
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.
19 package peersim.vector;
21 import peersim.config.*;
22 import peersim.core.*;
26 * Initializes the values so that {@value #PAR_PEAKS} nodes have value
27 * {@value #PAR_VALUE}/{@value #PAR_PEAKS}, the rest {@value #PAR_LVALUE}
32 public class PeakDistribution extends VectControl
35 // --------------------------------------------------------------------------
37 // --------------------------------------------------------------------------
40 * The sum of values in the system, to be equally distributed between peak
44 private static final String PAR_VALUE = "value";
47 * The value for the nodes that are not peaks. This parameter is optional,
48 * by default, the nodes that are
49 * not peaks are set to zero. This value overrides that behavior.
52 private static final String PAR_LVALUE = "background";
55 * The number of peaks in the system. If this value is greater than or equal to
56 * 1, it is interpreted as the actual number of peaks. If it is included in
57 * the range [0, 1] it is interpreted as a percentage with respect to the
58 * current network size. Defaults to 1.
61 private static final String PAR_PEAKS = "peaks";
64 // --------------------------------------------------------------------------
66 // --------------------------------------------------------------------------
69 private final Number lvalue;
72 private final Number value;
74 /** Number of peaks */
75 private final double peaks;
77 // --------------------------------------------------------------------------
79 // --------------------------------------------------------------------------
82 * Standard constructor that reads the configuration parameters.
83 * Invoked by the simulation engine.
84 * @param prefix the configuration prefix for this class
86 public PeakDistribution(String prefix)
90 peaks = Configuration.getDouble(prefix+"."+PAR_PEAKS, 1);
92 if( setter.isInteger() )
94 value=Long.valueOf(Configuration.getLong(prefix+"."+PAR_VALUE));
95 lvalue=Long.valueOf(Configuration.getLong(prefix+"."+PAR_LVALUE,0));
99 value = new Double(Configuration.getDouble(prefix + "." +
101 lvalue = new Double(Configuration.getDouble(prefix + "." +
106 // --------------------------------------------------------------------------
108 // --------------------------------------------------------------------------
111 * Initializes the values so that {@value #PAR_PEAKS} nodes have value
112 * {@value #PAR_VALUE}/{@value #PAR_PEAKS}, the rest zero.
113 * @return always false
115 public boolean execute()
117 int pn = (peaks < 1 ? (int) (peaks*Network.size()) : (int) peaks);
119 if( setter.isInteger() )
121 long v = value.longValue()/pn;
122 long lv = lvalue.longValue();
123 for (int i=0; i < pn; i++) setter.set(i, v);
124 for (int i=pn; i < Network.size(); i++) setter.set(i,lv);
128 double v = value.doubleValue()/pn;
129 double lv = lvalue.doubleValue();
130 for (int i=0; i < pn; i++) setter.set(i, v);
131 for (int i=pn; i < Network.size(); i++) setter.set(i,lv);
137 // --------------------------------------------------------------------------