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.dynamics;
21 import peersim.graph.Graph;
22 import peersim.core.*;
23 import peersim.config.Configuration;
26 * This class is the superclass of classes that
27 * takes a {@link Linkable} protocol or a graph and add edges that define a
29 * Note that no connections are removed, they are only added. So it can be used
30 * in combination with other initializers.
32 public abstract class WireGraph implements Control
35 // --------------------------------------------------------------------------
37 // --------------------------------------------------------------------------
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.
46 private static final String PAR_PROT = "protocol";
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.
54 private static final String PAR_PACK = "pack";
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.
61 private static final String PAR_UNDIR = "undir";
64 * Alias for {@value #PAR_UNDIR}.
67 private static final String PAR_UNDIR_ALT = "undirected";
69 // --------------------------------------------------------------------------
71 // --------------------------------------------------------------------------
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}).
77 protected final int pid;
79 /** If true, method pack() is invoked on the initialized protocol */
80 private final boolean pack;
82 /** If true, edges are added in an undirected fashion.*/
83 public final boolean undir;
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
92 // --------------------------------------------------------------------------
94 // --------------------------------------------------------------------------
97 * Standard constructor that reads the configuration parameters. Normally
98 * invoked by the simulation engine.
100 * the configuration prefix for this class
102 protected WireGraph(String prefix) {
104 if( Configuration.contains(prefix + "." + PAR_PROT) )
105 pid = Configuration.getPid(prefix + "." + PAR_PROT);
108 pack = Configuration.contains(prefix + "." + PAR_PACK);
109 undir = (Configuration.contains(prefix + "." + PAR_UNDIR) |
110 Configuration.contains(prefix + "." + PAR_UNDIR_ALT));
114 //--------------------------------------------------------------------------
116 //--------------------------------------------------------------------------
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.
124 public final boolean execute() {
127 if(g==null && pid==-10)
129 throw new RuntimeException(
130 "Neither a protocol, nor a graph is specified.");
132 if(g==null) gr = new OverlayGraph(pid,!undir);
135 if(gr.size()==0) return false;
140 int size = Network.size();
141 for (int i = 0; i < size; i++)
144 (Linkable) Network.get(i).getProtocol(pid);
151 //--------------------------------------------------------------------------
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);