Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge pull request #1 from mquinson/master
[simgrid.git] / contrib / psg / src / peersim / dynamics / WireFromFile.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
22 import java.io.IOException;
23 import java.io.FileReader;
24 import java.io.LineNumberReader;
25 import java.util.StringTokenizer;
26 import peersim.graph.Graph;
27 import peersim.core.*;
28 import peersim.config.Configuration;
29
30 /**
31 * Takes a {@link Linkable} protocol and adds connections that are stored in a
32 * file. Note that no
33 * connections are removed, they are only added. So it can be used in
34 * combination with other initializers.
35 * The format of the file is as follows. Each line begins with a node ID
36 * (IDs start from 0) followed by a list of neighbors, separated by whitespace.
37 * All node IDs larger than the actual network size will be discarded, but
38 * it does not trigger an error. Lines starting with a "#" character and
39 * empty lines are ignored.
40 */
41 public class WireFromFile extends WireGraph {
42
43
44 // ========================= fields =================================
45 // ==================================================================
46
47
48 /** 
49 *  The filename to load links from.
50 *  @config
51 */
52 private static final String PAR_FILE = "file";
53
54 /** 
55 *  The number of neighbors to be read from the file. If unset, the default
56 * behavior is to read all links in the file. If set, then the first k
57 * neighbors will be read only.
58 *  @config
59 */
60 private static final String PAR_K = "k";
61
62 private final String file;
63
64 private final int k;
65
66 // ==================== initialization ==============================
67 // ==================================================================
68
69
70 /**
71  * Standard constructor that reads the configuration parameters.
72  * Invoked by the simulation engine.
73  * @param prefix the configuration prefix for this class
74  */
75 public WireFromFile(String prefix) {
76
77         super(prefix);
78         file = Configuration.getString(prefix+"."+PAR_FILE);
79         k = Configuration.getInt(prefix + "." + PAR_K, Integer.MAX_VALUE);
80 }
81
82
83 // ===================== public methods ==============================
84 // ===================================================================
85
86
87 /**
88 * Wires the graph from a file.
89 * The format of the file is as follows. Each line begins with a node ID
90 * (IDs start from 0) followed by a list of neighbors, separated by whitespace.
91 * All node IDs larger than the actual network size will be discarded, but
92 * it does not trigger an error. Lines starting with a "#" character and
93 * empty lines are ignored.
94 */
95 public void wire(Graph g) {
96 try
97 {
98         FileReader fr = new FileReader(file);
99         LineNumberReader lnr = new LineNumberReader(fr);
100         String line;
101         boolean wasOutOfRange=false;
102         while((line=lnr.readLine()) != null)
103         {
104                 if( line.startsWith("#") ) continue;
105                 StringTokenizer st = new StringTokenizer(line);
106                 if(!st.hasMoreTokens()) continue;
107                 
108                 final int from = Integer.parseInt(st.nextToken());
109                 if( from < 0 || from >= Network.size() )
110                 {
111                         wasOutOfRange = true;
112                         continue;
113                 }
114                 
115                 for(int i=0; i<k && st.hasMoreTokens(); ++i)
116                 {
117                         final int to = Integer.parseInt(st.nextToken());
118                         if( to < 0 || to >= Network.size() )
119                                 wasOutOfRange = true;
120                         else
121                                 g.setEdge(from,to);
122                 }
123         }
124
125         if( wasOutOfRange )
126                 System.err.println("WireFromFile warning: in "+file+" "+
127                         "some nodes were out of range and so ignored.");
128         lnr.close();
129 }
130 catch( IOException e )
131 {
132         throw new RuntimeException(e);
133 }
134 }
135
136 }