Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Energy, onHostDestruction: ensured ptr existence
[simgrid.git] / contrib / psg / src / peersim / reports / GraphObserver.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.core.*;
22 import peersim.config.Configuration;
23 import peersim.graph.*;
24 import peersim.cdsim.CDState;
25
26 /**
27 * Class that provides functionality for observing graphs.
28 * It can efficiently create an undirected version of the graph, making sure
29 * it is updated only when the simulation has advanced already, and provides
30 * some common parameters.
31 */
32 public abstract class GraphObserver implements Control {
33
34
35 // ===================== fields =======================================
36 // ====================================================================
37
38 /**
39  * The protocol to operate on.
40  * @config
41  */
42 private static final String PAR_PROT = "protocol";
43
44 /**
45  * If defined, the undirected version of the graph will be analyzed. Not defined
46  * by default.
47  * @config
48  */
49 protected static final String PAR_UNDIR = "undir";
50
51 /**
52 * Alias for {@value #PAR_UNDIR}.
53 * @config
54 */
55 private static final String PAR_UNDIR_ALT = "undirected";
56
57 /**
58  * If defined, the undirected version of the graph will be stored using much
59  * more memory but observers will be in general a few times faster. As a
60  * consequence, it will not work with large graphs. Not defined by default. It
61  * is a static property, that is, it affects all graph observers that are used
62  * in a simulation. That is, it is not a parameter of any observer, the name
63  * should be specified as a standalone property.
64  * @config
65  */
66 private static final String PAR_FAST = "graphobserver.fast";
67
68 /** The name of this observer in the configuration */
69 protected final String name;
70
71 protected final int pid;
72
73 protected final boolean undir;
74
75 protected final GraphAlgorithms ga = new GraphAlgorithms();
76
77 protected Graph g;
78
79 // ---------------------------------------------------------------------
80
81 private static int lastpid = -1234;
82
83 private static long time = -1234;
84
85 private static int phase = -1234;
86
87 private static int ctime = -1234;
88
89 private static Graph dirg;
90
91 private static Graph undirg;
92
93 private static boolean fast;
94
95 /** If any instance of some extending class defines undir we need to
96 maintain an undir graph. Note that the graph is stored in a static
97 field so it is common to all instances. */
98 private static boolean needUndir=false;
99
100 // ===================== initialization ================================
101 // =====================================================================
102
103
104 /**
105  * Standard constructor that reads the configuration parameters.
106  * Invoked by the simulation engine.
107  * @param name the configuration prefix for this class
108  */
109 protected GraphObserver(String name) {
110
111         this.name = name;
112         pid = Configuration.getPid(name+"."+PAR_PROT);
113         undir = (Configuration.contains(name + "." + PAR_UNDIR) |
114                 Configuration.contains(name + "." + PAR_UNDIR_ALT));
115         GraphObserver.fast = Configuration.contains(PAR_FAST);
116         GraphObserver.needUndir = (GraphObserver.needUndir || undir);
117 }
118
119
120 // ====================== methods ======================================
121 // =====================================================================
122
123 /**
124 * Sets {@link #g}.
125 * It MUST be called by any implementation of {@link #execute()} before
126 * doing anything else.
127 * Attempts to initialize {@link #g} from a
128 * pre-calculated graph stored in a static field, but first it
129 * checks whether it needs to be updated.
130 * If the simulation time has progressed or it was calculated for a different
131 * protocol, then updates this static graph as well.
132 * The purpose of this mechanism is to save the time of constructing the
133 * graph if many observers are run on the same graph. Time savings can be very
134 * significant if the undirected version of the same graph is observed by many
135 * observers.
136 */
137 protected void updateGraph() {
138         
139         if( CommonState.getTime() != GraphObserver.time ||
140             (CDState.isCD() && (CDState.getCycleT() != GraphObserver.ctime)) ||
141             CommonState.getPhase() != GraphObserver.phase ||
142             pid != GraphObserver.lastpid )
143         {
144                 // we need to update the graphs
145                 
146                 GraphObserver.lastpid = pid;
147                 GraphObserver.time = CommonState.getTime();
148                 if( CDState.isCD() ) GraphObserver.ctime = CDState.getCycleT();
149                 GraphObserver.phase = CommonState.getPhase();
150
151                 GraphObserver.dirg = new OverlayGraph(pid);
152                 if( GraphObserver.needUndir )
153                 {
154                         if( fast )
155                                 GraphObserver.undirg =
156                                 new FastUndirGraph(GraphObserver.dirg);
157                         else
158                                 GraphObserver.undirg =
159                                 new ConstUndirGraph(GraphObserver.dirg);
160                 }
161         }
162         
163         if( undir ) g = GraphObserver.undirg;
164         else g = GraphObserver.dirg;
165 }
166
167 }
168
169
170