Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge pull request #1 from mquinson/master
[simgrid.git] / contrib / psg / src / peersim / vector / InitVectFromFile.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.vector;
20
21 import java.io.*;
22 import java.util.*;
23 import peersim.config.*;
24 import peersim.core.*;
25
26 /**
27  * Initializes a protocol vector from data read from a file.
28  * The file format is as follows:
29  * lines starting with # or lines that contain only
30  * whitespace are ignored.
31  * From the rest of the lines the first field separated by whitespace is
32  * read. Only the first field is read from each line, the rest of the line
33  * is ignored.
34  * The file can contain more values than necessary but
35  * enough values must be present.
36  * @see VectControl
37  * @see peersim.vector
38  */
39 public class InitVectFromFile extends VectControl
40 {
41
42 // --------------------------------------------------------------------------
43 // Parameter names
44 // --------------------------------------------------------------------------
45
46 /**
47  * The filename to load links from.
48  * @config
49  */
50 private static final String PAR_FILE = "file";
51
52 // --------------------------------------------------------------------------
53 // Fields
54 // --------------------------------------------------------------------------
55
56 /** The file to be read */
57 private final String file;
58
59 // --------------------------------------------------------------------------
60 // Initialization
61 // --------------------------------------------------------------------------
62
63 /**
64  * Standard constructor that reads the configuration parameters.
65  * Invoked by the simulation engine.
66  * @param prefix the configuration prefix for this class
67  */
68 public InitVectFromFile(String prefix)
69 {
70         super(prefix);
71         file = Configuration.getString(prefix + "." + PAR_FILE);
72 }
73
74 // --------------------------------------------------------------------------
75 // Methods
76 // --------------------------------------------------------------------------
77
78 /**
79  * Initializes values from a file.
80  * The file format is as follows:
81  * lines starting with # or lines that contain only
82  * whitespace are ignored.
83  * From the rest of the lines the first field separated by whitespace is
84  * read. Only the first field is read from each line, the rest of the line
85  * is ignored.
86  * The file can contain more values than necessary but
87  * enough values must be present.
88  * @throws RuntimeException if the file cannot be read or contains too few
89  * values
90  * @return always false
91  */
92 public boolean execute() {
93
94         int i = 0;
95
96 try {
97         FileReader fr = new FileReader(file);
98         LineNumberReader lnr = new LineNumberReader(fr);
99         String line;
100         while ((line = lnr.readLine()) != null && i < Network.size()) {
101                 if (line.startsWith("#"))
102                         continue;
103                 StringTokenizer st = new StringTokenizer(line);
104                 if (!st.hasMoreTokens())
105                         continue;
106                 if( setter.isInteger() )
107                         setter.set(i,Long.parseLong(st.nextToken()));
108                 else    setter.set(i,Double.parseDouble(st.nextToken()));
109                 i++;
110         }
111         lnr.close();
112 }
113 catch(IOException e)
114 {
115         throw new RuntimeException("Unable to read file: " + e);
116 }
117         
118         if (i < Network.size())
119                 throw new RuntimeException(
120                 "Too few values in file '" + file + "' (only "
121                 + i + "); we need " + Network.size() + ".");
122         
123         return false;
124 }
125
126 // --------------------------------------------------------------------------
127
128 }