Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
kill another out of date script
[simgrid.git] / contrib / psg / src / peersim / reports / DegreeStats.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.reports;
20
21 import peersim.config.*;
22 import peersim.core.*;
23 import peersim.util.*;
24
25 /**
26  * Prints several statistics about the node degrees in the graph.
27  */
28 public class DegreeStats extends GraphObserver
29 {
30
31 //--------------------------------------------------------------------------
32 //Parameter
33 //--------------------------------------------------------------------------
34
35 /**
36  * The number of nodes to be used for sampling the degree. 
37  * Defaults to full size of the graph.
38  * @config
39  */
40 private static final String PAR_N = "n";
41
42 /**
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
46  * default.
47  * @config
48  */
49 private static final String PAR_TRACE = "trace";
50
51 /**
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.
56  * Default is "stats".
57  * @config
58  */
59 private static final String PAR_METHOD = "method";
60
61 /**
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".
67  * @config
68  */
69 private static final String PAR_TYPE = "linktype";
70
71 //--------------------------------------------------------------------------
72 //Parameter
73 //--------------------------------------------------------------------------
74
75 private final int n;
76
77 private final boolean trace;
78
79 private Node[] traced = null;
80
81 private final String method;
82
83 private final String type;
84
85 private final RandPermutation rp = new RandPermutation(CommonState.r);
86
87 private int nextnode = 0;
88
89 //--------------------------------------------------------------------------
90 //Initialization
91 //--------------------------------------------------------------------------
92
93 /**
94  * Standard constructor that reads the configuration parameters.
95  * Invoked by the simulation engine.
96  * @param name the configuration prefix for this class
97  */
98 public DegreeStats(String name)
99 {
100         super(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 + ".");
110         }
111 }
112
113 //--------------------------------------------------------------------------
114 //Methods
115 //--------------------------------------------------------------------------
116
117 /**
118  * Returns next node to get degree information about.
119  */
120 private int nextNodeId()
121 {
122         if (trace) {
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);
128                 }
129                 return traced[nextnode++].getIndex();
130         } else
131                 return rp.next();
132 }
133
134 // ---------------------------------------------------------------------
135
136 /**
137  * Returns degree information about next node.
138  */
139 private int nextDegree()
140 {
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);
148         } else
149                 throw new RuntimeException(name + ": invalid type");
150 }
151
152 // ---------------------------------------------------------------------
153
154 /**
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
158  */
159 public boolean execute()
160 {
161         updateGraph();
162         if (!trace)
163                 rp.reset(g.size());
164         else
165                 nextnode = 0;
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();
183         }
184         return false;
185 }
186
187 }