View Javadoc

1   /*
2    * @(#)Experiment.java Aug 18, 2003
3    * 
4    * Copyright (c) 2003 Delft University of Technology Jaffalaan 5, 2628 BX Delft,
5    * the Netherlands All rights reserved.
6    * 
7    * This software is proprietary information of Delft University of Technology
8    * The code is published under the General Public License
9    */
10  package nl.tudelft.simulation.dsol.experiment;
11  
12  import java.io.Serializable;
13  import java.net.URL;
14  import java.text.DateFormat;
15  import java.util.Calendar;
16  import java.util.Properties;
17  
18  import javax.naming.Context;
19  import javax.naming.InitialContext;
20  import javax.naming.NamingException;
21  
22  import nl.tudelft.simulation.dsol.ModelInterface;
23  import nl.tudelft.simulation.dsol.simulators.SimulatorInterface;
24  import nl.tudelft.simulation.event.Event;
25  import nl.tudelft.simulation.event.EventInterface;
26  import nl.tudelft.simulation.event.EventListenerInterface;
27  import nl.tudelft.simulation.event.EventProducer;
28  import nl.tudelft.simulation.event.EventProducerInterface;
29  import nl.tudelft.simulation.event.EventType;
30  import nl.tudelft.simulation.logger.Logger;
31  
32  /***
33   * The Experiment specifies the parameters for a simulation experiment <br>
34   * (c) copyright 2003 <a href="http://www.simulation.tudelft.nl">Delft
35   * University of Technology </a>, the Netherlands. <br>
36   * See for project information <a
37   * href="http://www.simulation.tudelft.nl">www.simulation.tudelft.nl </a> <br>
38   * License of use: <a href="http://www.gnu.org/copyleft/gpl.html">General Public
39   * License (GPL) </a>, no warranty <br>
40   * 
41   * @version 2.0 21.09.2003 <br>
42   * @author <a href="http://www.tbm.tudelft.nl/webstaf/peterja/index.htm">Peter
43   *         Jacobs </a>, <a
44   *         href="http://www.tbm.tudelft.nl/webstaf/alexandv/index.htm">Alexander
45   *         Verbraeck </a>
46   */
47  public class Experiment extends EventProducer implements
48  		EventListenerInterface, Serializable
49  {
50  	/*** END_OF_EXPERIMENT_EVENT is fired when the experiment is ended */
51  	public static final EventType END_OF_EXPERIMENT_EVENT = new EventType(
52  			"END_OF_EXPERIMENT_EVENT");
53  
54  	/*** MODEL_CHANGED_EVENT is fired whenever the model is changed */
55  	public static final EventType MODEL_CHANGED_EVENT = new EventType(
56  			"MODEL_CHANGED_EVENT");
57  
58  	/*** SIMULATOR_CHANGED_EVENT is fired whenever the simulator is changed */
59  	public static final EventType SIMULATOR_CHANGED_EVENT = new EventType(
60  			"SIMULATOR_CHANGED_EVENT");
61  
62  	/*** EXPERIMENT_NAME the name property */
63  	public static final String EXPERIMENT_NAME = "EXPERIMENT_NAME";
64  
65  	/*** EXPERIMENT_ANALYST the analyst name property */
66  	public static final String EXPERIMENT_ANALYST = "EXPERIMENT_ANALYST";
67  
68  	/***
69  	 * treatments represent the treatments of this experiment
70  	 * 
71  	 * @uml.property name="treatments"
72  	 */
73  	private Treatment[] treatments = null;
74  
75  
76  	/*** properties reflect the properties */
77  	private Properties properties = new Properties();
78  
79  	/*** treatment reflects the current treatment */
80  	private int treatment = 0;
81  
82  	/***
83  	 * simulator reflects the simulator
84  	 * 
85  	 * @uml.property name="simulator"
86  	 */
87  	private SimulatorInterface simulator;
88  
89  	/***
90  	 * model reflects the model
91  	 * 
92  	 * @uml.property name="model"
93  	 */
94  	private ModelInterface model;
95  
96  	/***
97  	 * the current run
98  	 * 
99  	 * @uml.property name="run"
100 	 */
101 	private String run;
102 
103 	/***
104 	 * the URL of the Experiment
105 	 * 
106 	 * @uml.property name="url"
107 	 */
108 	private URL url = null;
109 
110 
111 	/***
112 	 * constructs a new Experiment
113 	 */
114 	public Experiment()
115 	{
116 		super();
117 	}
118 
119 	/***
120 	 * sets the simulator
121 	 * 
122 	 * @param simulator the simulator
123 	 * 
124 	 * @uml.property name="simulator"
125 	 */
126 	public synchronized void setSimulator(final SimulatorInterface simulator)
127 	{
128 		this.simulator = simulator;
129 		this.fireEvent(SIMULATOR_CHANGED_EVENT, simulator);
130 	}
131 
132 	/***
133 	 * returns the simulator
134 	 * 
135 	 * @return SimulatorInterface
136 	 * 
137 	 * @uml.property name="simulator"
138 	 */
139 	public SimulatorInterface getSimulator()
140 	{
141 		return this.simulator;
142 	}
143 
144 	/***
145 	 * returns the current run
146 	 * 
147 	 * @return String
148 	 * 
149 	 * @uml.property name="run"
150 	 */
151 	public String getRun()
152 	{
153 		return this.run;
154 	}
155 
156 
157 	/***
158 	 * starts the experiment on a simulator
159 	 */
160 	public synchronized void start()
161 	{
162 		Calendar calendar = Calendar.getInstance();
163 		calendar.setTimeInMillis(System.currentTimeMillis());
164 		this.run = DateFormat.getDateTimeInstance().format(calendar.getTime());
165 		try
166 		{
167 			new InitialContext().createSubcontext(this.run);
168 		} catch (NamingException exception)
169 		{
170 			Logger.warning(this, "start", exception);
171 		}
172 		this.treatment = 0;
173 		this.notify(new Event(RunControl.END_OF_RUN_EVENT, this, null));
174 	}
175 
176 	/***
177 	 * returns a property
178 	 * 
179 	 * @param key properties are stored as key-value
180 	 * @return String the property
181 	 */
182 	public String getProperty(final String key)
183 	{
184 		return this.properties.getProperty(key);
185 	}
186 
187 	/***
188 	 * sets a property
189 	 * 
190 	 * @param key properties are stored in key-value pairs
191 	 * @param value properties are stored in key-value pairs
192 	 */
193 	public void setProperty(final String key, final String value)
194 	{
195 		this.properties.put(key, value);
196 	}
197 
198 	/***
199 	 * returns the model
200 	 * 
201 	 * @return ModelInterface the model
202 	 * 
203 	 * @uml.property name="model"
204 	 */
205 	public ModelInterface getModel()
206 	{
207 		return this.model;
208 	}
209 
210 	/***
211 	 * returns the URL
212 	 * 
213 	 * @return URL
214 	 * 
215 	 * @uml.property name="url"
216 	 */
217 	public URL getUrl()
218 	{
219 		return this.url;
220 	}
221 
222 
223 	/***
224 	 * @see nl.tudelft.simulation.event.EventListenerInterface
225 	 *      #notify(nl.tudelft.simulation.event.EventInterface)
226 	 */
227 	public void notify(final EventInterface event)
228 	{
229 		try
230 		{
231 			Context context = (Context) new InitialContext().lookup(this.run);
232 			if (event.getType().equals(RunControl.END_OF_RUN_EVENT))
233 			{
234 				if (this.treatment < this.treatments.length)
235 				{
236 					context.createSubcontext("treatment(" + this.treatment
237 							+ ")");
238 					this.treatments[this.treatment]
239 							.getRunControl()
240 							.addListener(this, RunControl.END_OF_RUN_EVENT,
241 									EventProducerInterface.LAST_POSITION, false);
242 					this.treatment++;
243 					this.treatments[this.treatment - 1].getRunControl().start(
244 							this.simulator);
245 				} else
246 				{
247 					this.fireEvent(new Event(
248 							Experiment.END_OF_EXPERIMENT_EVENT, this, null));
249 				}
250 			}
251 		} catch (Exception exception)
252 		{
253 			Logger.warning(this, "notify", exception);
254 		}
255 	}
256 
257 	/***
258 	 * resets the experiment
259 	 */
260 	public synchronized void reset()
261 	{
262 		this.treatment = 0;
263 		for (int i = 0; i < this.treatments.length; i++)
264 		{
265 			this.treatments[i].getRunControl().reset();
266 		}
267 		try
268 		{
269 			this.setSimulator((SimulatorInterface) this.simulator.getClass()
270 					.newInstance());
271 		} catch (Exception exception)
272 		{
273 			exception.printStackTrace();
274 		}
275 	}
276 
277 	/***
278 	 * sets the model on the experiment
279 	 * 
280 	 * @param model the simulatormodel
281 	 * 
282 	 * @uml.property name="model"
283 	 */
284 	public synchronized void setModel(final ModelInterface model)
285 	{
286 		this.model = model;
287 		this.fireEvent(MODEL_CHANGED_EVENT, model);
288 	}
289 
290 	/***
291 	 * returns the treatments
292 	 * 
293 	 * @return Treatment
294 	 * 
295 	 * @uml.property name="treatments"
296 	 */
297 	public Treatment[] getTreatments()
298 	{
299 		return this.treatments;
300 	}
301 
302 	/***
303 	 * sets the treatments
304 	 * 
305 	 * @param treatments reflect the treatments
306 	 * 
307 	 * @uml.property name="treatments"
308 	 */
309 	public void setTreatments(final Treatment[] treatments)
310 	{
311 		this.treatments = treatments;
312 	}
313 
314 	/***
315 	 * sets the URL of the experiment
316 	 * 
317 	 * @param url the URL
318 	 * 
319 	 * @uml.property name="url"
320 	 */
321 	public void setUrl(final URL url)
322 	{
323 		this.url = url;
324 	}
325 
326 	/***
327 	 * @see java.lang.Object#toString()
328 	 */
329 	public String toString()
330 	{
331 		String result = "[" + super.toString() + " ; "
332 				+ this.getProperty("dsol.experiment.projectTitle") + " ; "
333 				+ this.getProperty("dsol.experiment.analystName")
334 				+ " ; treatments=";
335 		for (int i = 0; i < this.treatments.length; i++)
336 		{
337 			result = result + this.treatments[i].toString() + " ; ";
338 		}
339 		result = result.substring(0, result.length() - 2) + "]";
340 		return result;
341 	}
342 }