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.reports;
21 import peersim.config.*;
22 import peersim.core.*;
23 import peersim.util.*;
26 * Prints several statistics about the node degrees in the graph.
28 public class DegreeStats extends GraphObserver
31 //--------------------------------------------------------------------------
33 //--------------------------------------------------------------------------
36 * The number of nodes to be used for sampling the degree.
37 * Defaults to full size of the graph.
40 private static final String PAR_N = "n";
43 * If defined, then the given number of nodes will be traced. That is, it is
44 * guaranteed that in each call the same nodes will be picked in the same order.
45 * If a node being traced fails, its degree will be considered 0. Not defined by
49 private static final String PAR_TRACE = "trace";
52 * Selects a method to use when printing results. Three methods are known:
53 * "stats" will use {@link IncrementalStats#toString}. "freq" will
54 * use {@link IncrementalFreq#print}. "list" will print the
55 * degrees of the sample nodes one by one in one line, separated by spaces.
59 private static final String PAR_METHOD = "method";
62 * Selects the types of links to print information about. Three methods are
63 * known: "live": links pointing to live nodes, "dead": links pointing to nodes
64 * that are unavailable and "all": both dead and live links summed. "all" and
65 * "dead" require parameter {@value peersim.reports.GraphObserver#PAR_UNDIR}
66 * to be unset (graph must be directed). Default is "live".
69 private static final String PAR_TYPE = "linktype";
71 //--------------------------------------------------------------------------
73 //--------------------------------------------------------------------------
77 private final boolean trace;
79 private Node[] traced = null;
81 private final String method;
83 private final String type;
85 private final RandPermutation rp = new RandPermutation(CommonState.r);
87 private int nextnode = 0;
89 //--------------------------------------------------------------------------
91 //--------------------------------------------------------------------------
94 * Standard constructor that reads the configuration parameters.
95 * Invoked by the simulation engine.
96 * @param name the configuration prefix for this class
98 public DegreeStats(String name)
101 n = Configuration.getInt(name + "." + PAR_N, -1);
102 trace = Configuration.contains(name + "." + PAR_TRACE);
103 method = Configuration.getString(name + "." + PAR_METHOD, "stats");
104 type = Configuration.getString(name + "." + PAR_TYPE, "live");
105 if ((type.equals("all") || type.equals("dead")) && undir) {
106 throw new IllegalParameterException(
107 name + "." + PAR_TYPE, " Parameter "+ name + "." +
108 PAR_UNDIR + " must not be defined if " + name + "."
109 + PAR_TYPE + "=" + type + ".");
113 //--------------------------------------------------------------------------
115 //--------------------------------------------------------------------------
118 * Returns next node to get degree information about.
120 private int nextNodeId()
123 if (traced == null) {
124 int nn = (n < 0 ? Network.size() : n);
125 traced = new Node[nn];
126 for (int j = 0; j < nn; ++j)
127 traced[j] = Network.get(j);
129 return traced[nextnode++].getIndex();
134 // ---------------------------------------------------------------------
137 * Returns degree information about next node.
139 private int nextDegree()
141 final int nodeid = nextNodeId();
142 if (type.equals("live")) {
143 return g.degree(nodeid);
144 } else if (type.equals("all")) {
145 return ((OverlayGraph) g).fullDegree(nodeid);
146 } else if (type.equals("dead")) {
147 return ((OverlayGraph) g).fullDegree(nodeid) - g.degree(nodeid);
149 throw new RuntimeException(name + ": invalid type");
152 // ---------------------------------------------------------------------
155 * Prints statistics about node degree. The format of the output is specified
156 * by {@value #PAR_METHOD}. See also the rest of the configuration parameters.
157 * @return always false
159 public boolean execute()
166 final int nn = (n < 0 ? Network.size() : n);
167 if (method.equals("stats")) {
168 IncrementalStats stats = new IncrementalStats();
169 for (int i = 0; i < nn; ++i)
170 stats.add(nextDegree());
171 System.out.println(name + ": " + stats);
172 } else if (method.equals("freq")) {
173 IncrementalFreq stats = new IncrementalFreq();
174 for (int i = 0; i < nn; ++i)
175 stats.add(nextDegree());
176 stats.print(System.out);
177 System.out.println("\n\n");
178 } else if (method.equals("list")) {
179 System.out.print(name + ": ");
180 for (int i = 0; i < nn; ++i)
181 System.out.print(nextDegree() + " ");
182 System.out.println();