Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
peersimgrid release 1.0
[simgrid.git] / contrib / psg / src / peersim / reports / ConnectivityObserver.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 java.util.Iterator;
22 import java.util.Map;
23 import peersim.config.Configuration;
24 import peersim.util.IncrementalStats;
25
26 /**
27  * Reports statistics about connectivity properties of the network, such as
28  * weakly or strongly connected clusters.
29  */
30 public class ConnectivityObserver extends GraphObserver
31 {
32
33 //--------------------------------------------------------------------------
34 //Parameters
35 //--------------------------------------------------------------------------
36
37 /**
38  * The parameter used to request cluster size statistics instead of the usual
39  * list of clusters. Not set by default.
40  * @config
41  */
42 private static final String PAR_STATS = "stats";
43
44 /**
45  * Defines the types of connected clusters to discover.
46  * Possible values are
47  * <ul>
48  * <li>"wcc": weakly connected clusters</li>
49  * <li>"scc": strongly connected clusters</li>
50  * </ul>
51  * Defaults to "wcc".
52  * @config
53  */
54 private static final String PAR_TYPE = "type";
55
56 //--------------------------------------------------------------------------
57 //Fields
58 //--------------------------------------------------------------------------
59
60 /** {@link #PAR_STATS} */
61 private final boolean sizestats;
62
63 /** {@link #PAR_TYPE} */
64 private final String type;
65
66 //--------------------------------------------------------------------------
67 //Initialization
68 //--------------------------------------------------------------------------
69
70 /**
71  * Standard constructor that reads the configuration parameters.
72  * Invoked by the simulation engine.
73  * @param name the configuration prefix for this class
74  */
75 public ConnectivityObserver(String name)
76 {
77         super(name);
78         sizestats = Configuration.contains(name + "." + PAR_STATS);
79         type = Configuration.getString(name + "." + PAR_TYPE,"wcc");
80 }
81
82 //--------------------------------------------------------------------------
83 //Methods
84 //--------------------------------------------------------------------------
85
86 /**
87 * Prints information about clusters.
88 * If parameter {@value #PAR_STATS} is defined then the output is
89 * produced by {@link IncrementalStats#toString}, over the sizes of the
90 * clusters.
91 * Otherwise one line is printed that contains the string representation of
92 * a map, that holds cluster IDs mapped to cluster sizes.
93 * The meaning of the cluster IDs is not specified, but is printed for
94 * debugging purposes.
95 * @return always false
96 * @see peersim.graph.GraphAlgorithms#tarjan
97 * @see peersim.graph.GraphAlgorithms#weaklyConnectedClusters
98 */
99 public boolean execute()
100 {
101         Map clst;
102         updateGraph();
103         
104         if(type.equals("wcc"))
105                 clst=ga.weaklyConnectedClusters(g);
106         else if(type.equals("scc"))
107                 clst=ga.tarjan(g);
108         else
109                 throw new RuntimeException(
110                 "Unsupported connted cluster type '"+type+"'");
111
112         if (!sizestats) {
113                 System.out.println(name + ": " + clst);
114         } else {
115                 IncrementalStats stats = new IncrementalStats();
116                 Iterator it = clst.values().iterator();
117                 while (it.hasNext()) {
118                         stats.add(((Integer) it.next()).intValue());
119                 }
120                 System.out.println(name + ": " + stats);
121         }
122         return false;
123 }
124
125 }