Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
peersimgrid release 1.0
[simgrid.git] / contrib / psg / src / peersim / dynamics / WireGraph.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.dynamics;
20
21 import peersim.graph.Graph;
22 import peersim.core.*;
23 import peersim.config.Configuration;
24
25 /**
26  * This class is the superclass of classes that
27  * takes a {@link Linkable} protocol or a graph and add edges that define a
28  * certain topology.
29  * Note that no connections are removed, they are only added. So it can be used
30  * in combination with other initializers.
31  */
32 public abstract class WireGraph implements Control
33 {
34
35 // --------------------------------------------------------------------------
36 // Parameters
37 // --------------------------------------------------------------------------
38
39 /**
40  * The {@link Linkable} protocol to operate on. If it is not specified,
41  * then operates on {@link #g}. If {@link #g} is null, {@link #execute} throws
42  * an Exception. Note that if {@link #g} is set, it will be used irrespective
43  * of the setting of the protocol in this field.
44  * @config
45  */
46 private static final String PAR_PROT = "protocol";
47
48 /**
49  * If this config property is defined, method {@link Linkable#pack()} is 
50  * invoked on the specified protocol at the end of the wiring phase. 
51  * Default to false.
52  * @config
53  */
54 private static final String PAR_PACK = "pack";
55
56 /**
57  * If set, the generated graph is undirected. In other words, for each link
58  * (i,j) a link (j,i) will also be added. Defaults to false.
59  * @config
60  */
61 private static final String PAR_UNDIR = "undir";
62
63 /**
64 * Alias for {@value #PAR_UNDIR}.
65 * @config
66 */
67 private static final String PAR_UNDIR_ALT = "undirected";
68
69 // --------------------------------------------------------------------------
70 // Fields
71 // --------------------------------------------------------------------------
72
73 /**
74  * The protocol we want to wire. It is negative if no protocol was set
75  * (in that case, a graph must be specified, see {@link #g}).
76  */
77 protected final int pid;
78
79 /** If true, method pack() is invoked on the initialized protocol */
80 private final boolean pack;
81
82 /** If true, edges are added in an undirected fashion.*/
83 public final boolean undir;
84
85 /**
86 * If set (not null), this is the graph to wire. If null, the current overlay
87 * is wired each time {@link #execute} is called, as specified by {@value
88 * #PAR_PROT}.
89 */
90 public Graph g=null;
91
92 // --------------------------------------------------------------------------
93 // Initialization
94 // --------------------------------------------------------------------------
95
96 /**
97  * Standard constructor that reads the configuration parameters. Normally
98  * invoked by the simulation engine.
99  * @param prefix
100  *          the configuration prefix for this class
101  */
102 protected WireGraph(String prefix) {
103
104         if( Configuration.contains(prefix + "." + PAR_PROT) )
105                 pid = Configuration.getPid(prefix + "." + PAR_PROT);
106         else
107                 pid = -10;
108         pack = Configuration.contains(prefix + "." + PAR_PACK);
109         undir = (Configuration.contains(prefix + "." + PAR_UNDIR) |
110                 Configuration.contains(prefix + "." + PAR_UNDIR_ALT));
111 }
112
113
114 //--------------------------------------------------------------------------
115 //Public methods
116 //--------------------------------------------------------------------------
117
118 /**
119 * Calls method {@link #wire} with the graph {@link #g},
120 * or if null, on the overlay specified by the protocol given by config
121 * parameter {@value #PAR_PROT}. If neither {@link #g}, nor {@value #PAR_PROT}
122 * is set, throws a RuntimException.
123 */
124 public final boolean execute() {
125
126         Graph gr;
127         if(g==null && pid==-10)
128         {
129                 throw new RuntimeException(
130                         "Neither a protocol, nor a graph is specified.");
131         }
132         if(g==null) gr = new OverlayGraph(pid,!undir);
133         else gr=g;
134
135         if(gr.size()==0) return false;
136         wire(gr);
137         
138         if( g==null && pack)
139         {
140                 int size = Network.size();
141                 for (int i = 0; i < size; i++)
142                 {
143                         Linkable link =
144                                 (Linkable) Network.get(i).getProtocol(pid);
145                         link.pack();
146                 }
147         }
148         return false;
149 }
150
151 //--------------------------------------------------------------------------
152
153 /** The method that should wire (add edges to) the given graph. Has to
154 * be implemented by extending classes */
155 public abstract void wire(Graph g);
156
157 }
158