Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge pull request #1 from mquinson/master
[simgrid.git] / contrib / psg / src / peersim / config / CheckConfig.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.config;
20
21 import java.util.*;
22
23 import peersim.cdsim.*;
24 import peersim.edsim.*;
25 import peersim.util.*;
26
27
28 /**
29  * This is utility tool that checks whether a config file can be loaded
30  * or not, without actually performing the simulation. All the error
31  * messages generated by controls and protocols when initialized are
32  * reported.  This is useful to check all the configuration files in a 
33  * directory.
34  */
35 public class CheckConfig {
36
37 //========================== parameters ================================
38 //======================================================================
39
40 /**
41  * This is the prefix of the config properties whose value vary during
42  * a set of experiments.
43  * @config
44  */
45 private static final String PAR_RANGE = "range";
46
47
48 // ========================== static constants ==========================
49 // ======================================================================
50
51 /** {@link CDSimulator} */
52 protected static final int CDSIM = 0;
53
54 /** {@link EDSimulator} */
55 protected static final int EDSIM = 1;
56
57 protected static final int UNKNOWN = -1;
58
59 /** the class names of simulators used */
60 protected static final String[] simName = {
61         CDSimulator.class.getCanonicalName(),
62         EDSimulator.class.getCanonicalName(),
63 };
64
65
66
67         
68 // ========================== methods ===================================
69 // ======================================================================
70
71 /**
72 * Returns the numeric id of the simulator to invoke. At the moment this can
73 * be {@link #CDSIM}, {@link #EDSIM} or {@link #UNKNOWN}.
74 */
75 protected static int getSimID() {
76         
77         if( CDSimulator.isConfigurationCycleDriven())
78         {
79                 return CDSIM;
80         }
81         else if( EDSimulator.isConfigurationEventDriven() )
82         {       
83                 return EDSIM;
84         }
85         else    return UNKNOWN;
86 }
87
88 // ----------------------------------------------------------------------
89
90 /**
91 * Loads the configuration and checks the configuration files against
92 * simple configuration errors, such as missing classes, missing 
93 * parameters or syntax errors.
94 * <p>
95 * The goal is to provide a mechanism to test a configuration file,
96 * without having to perform the actual simulations (that could be
97 * time-consuming) and without necessarily blocking after the first
98 * error encountered. It may be useful, for example, when a major 
99 * refactoring of your code requires a thorough check on all your 
100 * configuration files.
101 * <p>
102 * Loading the configuration is currently done with the help of 
103 * constructing an instance of {@link ParsedProperties} using the 
104 * constructor {@link ParsedProperties#ParsedProperties(String[])},
105 * in the same way as the normal simulator.
106 * <p>
107 * After loading the configuration, the collection of nodes forming a 
108 * Network is instantiated, together with all protocols forming a
109 * node. Initialization controls are executed, and then the simulation
110 * stops. 
111 * <p>
112 * For each error encountered, a message is printed ons standard error,
113 * and the initialization keeps going without interruption. If multiple 
114 * errors are present, an error message for each of them is printed.
115 * Apart from errors, default choices are also printed as warnings, to 
116 * allow developers to spot subtle configuration errors such as missing
117 * parameters that defaults to standard values.
118
119 * @param args passed on to
120 * {@link ParsedProperties#ParsedProperties(String[])}
121 */
122 public static void main(String[] args)
123   throws Exception
124 {
125         System.setErr(new NullPrintStream());
126         Properties prop = new ParsedProperties(args);
127         Configuration.setConfig( prop, true );
128         parseRanges(prop);
129         
130         final int SIMID = getSimID();
131         if( SIMID == UNKNOWN )
132         {
133                 System.err.println(
134                     "Simulator: unable to identify configuration, exiting.");
135                 return;
136         }
137         
138         try {
139         
140                 // XXX could be done through reflection, but
141                 // this is easier to read.
142                 switch(SIMID)
143                 {
144                 case CDSIM:
145                         // Set cycles to 0, so no simulation is ever performed.
146                         prop.setProperty(CDSimulator.PAR_CYCLES, "0");
147                         CDSimulator.nextExperiment();
148                         break;
149                 case EDSIM:
150                         // Set endtime to 0, so no simulation is ever performed.
151                         prop.setProperty(EDSimulator.PAR_ENDTIME, "0");
152                         EDSimulator.nextExperiment();
153                         break;
154                 }
155         
156         } catch (MissingParameterException e) {
157                 System.out.println(e.getMessage());
158                 System.exit(1);
159         } catch (IllegalParameterException e) {
160                 System.out.println(e.getMessage());
161                 System.exit(1);
162         }       
163 }
164
165 /**
166  * Parses a collection of range specifications, identifies the set
167  * of parameters that will change during the simulation and
168  * instantiates them with the first value of their ranges.
169  */
170 private static void parseRanges(Properties prop)
171 {
172         // Get ranges
173         String[] ranges = Configuration.getNames(PAR_RANGE);
174
175         for (int i = 0; i < ranges.length; i++) {
176                 String[] array = Configuration.getString(ranges[i]).split(";");
177                 if (array.length != 2) {
178                         throw new IllegalParameterException(ranges[i],
179                                         " should be formatted as <parameter>;<value list>");
180                 }
181                 String[] values = StringListParser.parseList(array[1]);
182                 prop.setProperty(array[0], values[0]);
183         }
184 }
185
186 }