View Javadoc

1   /*
2    * ScenarioManager.java Created @ Sep 17, 2004
3    * 
4    * 
5    * Copyright (c) 2003-2005 Delft University of Technology, Jaffalaan 5, 2628 BX
6    * Delft, the Netherlands. All rights reserved.
7    * 
8    * See for project information <a href="http://www.simulation.tudelft.nl/">
9    * www.simulation.tudelft.nl </a>.
10   * 
11   * The source code and binary code of this software is proprietary information
12   * of Delft University of Technology.
13   */
14  package org.gscg.experiment.scenario;
15  
16  import java.io.Serializable;
17  import java.rmi.RemoteException;
18  
19  import nl.tudelft.simulation.dsol.SimRuntimeException;
20  import nl.tudelft.simulation.dsol.experiment.TimeUnit;
21  import nl.tudelft.simulation.dsol.experiment.TimeUnitInterface;
22  import nl.tudelft.simulation.dsol.formalisms.devs.SimEvent;
23  import nl.tudelft.simulation.dsol.simulators.DEVSSimulatorInterface;
24  import nl.tudelft.simulation.logger.Logger;
25  
26  /***
27   * The ScenarioManager.
28   * <p>
29   * 
30   * Copyright (c) 2003-2005 Delft University of Technology, Jaffalaan 5, 2628 BX
31   * Delft, the Netherlands. All rights reserved.
32   * 
33   * See for project information <a href="http://www.simulation.tudelft.nl/">
34   * www.simulation.tudelft.nl </a>.
35   * 
36   * The source code and binary code of this software is proprietary information
37   * of Delft University of Technology.
38   * 
39   * @author <a
40   *         href="http://www.tbm.tudelft.nl/webstaf/stijnh/index.htm">Stijn-Pieter
41   *         van Houten </a>
42   * @version $Revision: 1.2 $ $Date: 2005/08/09 15:43:42 $
43   * @since 1.0.0
44   */
45  public class ScenarioManager implements Serializable
46  {
47  	/*** the serial version uid */
48  	private static final long serialVersionUID = 11L;
49  
50  	/*** the scenario */
51  	private Scenario scenario = null;
52  
53  	/*** the simulator to schedule events on */
54  	private DEVSSimulatorInterface simulator = null;
55  
56  	/*** the start time of the simulator */
57  	private long startTime = 0L;
58  
59  	/***
60  	 * constructs a new ScenarioManager
61  	 * 
62  	 * @param scenario the scenario
63  	 * @param simulator the simulator
64  	 */
65  	public ScenarioManager(final Scenario scenario,
66  			final DEVSSimulatorInterface simulator)
67  	{
68  		super();
69  		this.scenario = scenario;
70  		this.simulator = simulator;
71  		try
72  		{
73  			this.startTime = this.simulator.getReplication().getRunControl()
74  					.getTreatment().getStartTime();
75  		} catch (RemoteException remoteException)
76  		{
77  			Logger.severe(this, "ScenarioManager", remoteException);
78  		}
79  	}
80  
81  	/***
82  	 * Method scheduleObjects schedules all the actions part of the scenario
83  	 */
84  	public void scheduleObjects()
85  	{
86  		Event[] actions = this.scenario.getActions();
87  
88  		for (int i = 0; i < actions.length; i++)
89  		{
90  			Event action = actions[i];
91  			long eventStartTime = action.getStartTime();
92  			try
93  			{
94  				eventStartTime = (long) TimeUnit.convert(eventStartTime
95  						- this.startTime, TimeUnitInterface.MILLISECOND,
96  						this.simulator);
97  			} catch (RemoteException remoteException)
98  			{
99  				Logger.severe(this, "scheduleObjects", remoteException);
100 			}
101 
102 			SimEvent event = new SimEvent(eventStartTime, action
103 					.getTargetObject(), action.getTargetObject(), action
104 					.getMethodName(), action.getArgs());
105 			try
106 			{
107 				this.simulator.scheduleEvent(event);
108 			} catch (SimRuntimeException simRunTimeException)
109 			{
110 				Logger.severe(this, "scheduleObjects", simRunTimeException);
111 			} catch (RemoteException remoteException)
112 			{
113 				Logger.severe(this, "scheduleObjects", remoteException);
114 			}
115 		}
116 	}
117 }