View Javadoc

    1   /*
    2    * @(#)Replication.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.rmi.RemoteException;
    14
    15  import javax.naming.Context;
    16  import javax.naming.InitialContext;
    17
    18  import nl.tudelft.simulation.dsol.simulators.SimulatorInterface;
    19  import nl.tudelft.simulation.event.Event;
    20  import nl.tudelft.simulation.event.EventInterface;
    21  import nl.tudelft.simulation.event.EventListenerInterface;
    22  import nl.tudelft.simulation.event.EventProducer;
    23  import nl.tudelft.simulation.event.EventProducerInterface;
    24  import nl.tudelft.simulation.event.EventType;
    25  import nl.tudelft.simulation.logger.Logger;
    26
    27  /***
    28   * The RunControl specifies all parameters for the simulator. <br>
    29   * (c) copyright 2003 <a href="http://www.simulation.tudelft.nl">Delft
    30   * University of Technology </a>, the Netherlands. <br>
    31   * See for project information <a
    32   * href="http://www.simulation.tudelft.nl">www.simulation.tudelft.nl </a> <br>
    33   * License of use: <a href="http://www.gnu.org/copyleft/gpl.html">General Public
    34   * License (GPL) </a>, no warranty <br>
    35   *
    36   * @version 2.0 21.09.2003 <br>
    37   * @author <a href="http://www.tbm.tudelft.nl/webstaf/peterja/index.htm">Peter
    38   *         Jacobs </a>, <a
    39   *         href="http://www.tbm.tudelft.nl/webstaf/alexandv/index.htm">Alexander
    40   *         Verbraeck </a>
    41   */
    42  public class RunControl extends EventProducer implements Serializable,
    43  		EventListenerInterface
    44  {
    45  	/*** END_OF_RUN_EVENT is fired at the end of a run */
    46  	public static final EventType END_OF_RUN_EVENT = new EventType(
    47  			"END_OF_RUN_EVENT");
    48
    49  	/***
    50  	 * treatment reflects the treatment to which this runcontrol belongs
    51  	 *
    52  	 * @uml.property name="treatment"
    53  	 */
    54  	protected Treatment treatment = null;
    55
    56  	/***
    57  	 * replications are the replications of this runControl
    58  	 *
    59  	 * @uml.property name="replications"
    60  	 */
    61  	private Replication[] replications;
    62
    63  	/***
    64  	 * warmupPeriod is the warmup period
    65  	 *
    66  	 * @uml.property name="warmupPeriod"
    67  	 */
    68  	private double warmupPeriod = 0.0;
    69
    70  	/***
    71  	 * runLength reflects the runLength of the runControl
    72  	 *
    73  	 * @uml.property name="runLength"
    74  	 */
    75  	private double runLength = Double.NaN;
    76
    77
    78  	/*** replication refers to the current replication */
    79  	private int replication = 0;
    80
    81  	/***
    82  	 * Constructs a new RunControl object.
    83  	 *
    84  	 * @param treatment The parent treatment
    85  	 */
    86  	public RunControl(final Treatment treatment)
    87  	{
    88  		super();
    89  		this.treatment = treatment;
    90  		this.addListener(treatment.getRunControl(),
    91  				RunControl.END_OF_RUN_EVENT, false);
    92  	}
    93
    94  	/***
    95  	 * starts the runControl
    96  	 *
    97  	 * @param simulator is the simulator
    98  	 * @throws RemoteException on network failure
    99  	 */
    100 	public void start(final SimulatorInterface simulator)
    101 			throws RemoteException
    102 	{
    103 		//Let's run the runControl by faking the first
    104 		//END_OF_REPLICATION_EVENT
    105 		simulator.addListener(this,
    106 				SimulatorInterface.END_OF_REPLICATION_EVENT,
    107 				EventProducerInterface.LAST_POSITION, false);
    108 		this.replication = 0;
    109 		this.notify(new Event(SimulatorInterface.END_OF_REPLICATION_EVENT,
    110 				simulator, null));
    111 	}
    112
    113 	/***
    114 	 * returns the treatment
    115 	 *
    116 	 * @return treatment
    117 	 *
    118 	 * @uml.property name="treatment"
    119 	 */
    120 	public Treatment getTreatment()
    121 	{
    122 		return this.treatment;
    123 	}
    124
    125 	/***
    126 	 * returns the replications defined in this runControl
    127 	 *
    128 	 * @return Replication the replications
    129 	 *
    130 	 * @uml.property name="replications"
    131 	 */
    132 	public Replication[] getReplications()
    133 	{
    134 		return this.replications;
    135 	}
    136
    137 	/***
    138 	 * returns the runLength
    139 	 *
    140 	 * @return double the runlength
    141 	 *
    142 	 * @uml.property name="runLength"
    143 	 */
    144 	public double getRunLength()
    145 	{
    146 		return this.runLength;
    147 	}
    148
    149 	/***
    150 	 * returns the warmupPeriod
    151 	 *
    152 	 * @return double the warmup period
    153 	 *
    154 	 * @uml.property name="warmupPeriod"
    155 	 */
    156 	public double getWarmupPeriod()
    157 	{
    158 		return this.warmupPeriod;
    159 	}
    160
    161
    162 	/***
    163 	 * @see nl.tudelft.simulation.event.EventListenerInterface
    164 	 *      #notify(nl.tudelft.simulation.event.EventInterface)
    165 	 */
    166 	public void notify(final EventInterface event)
    167 	{
    168 		try
    169 		{
    170 			if (event.getType().equals(
    171 					SimulatorInterface.END_OF_REPLICATION_EVENT))
    172 			{
    173 				SimulatorInterface simulator = (SimulatorInterface) event
    174 						.getSource();
    175 				if (this.replication < this.replications.length)
    176 				{
    177 					Context context = (Context) new InitialContext()
    178 							.lookup(this.treatment.getExperiment().getRun()
    179 									+ "/treatment("
    180 									+ this.treatment.getNumber() + ")");
    181 					context.createSubcontext("replication(" + this.replication
    182 							+ ")");
    183 					context.createSubcontext("replication(" + this.replication
    184 							+ ")/animation");
    185 					context.createSubcontext("replication(" + this.replication
    186 							+ ")/animation/2D");
    187 					context.createSubcontext("replication(" + this.replication
    188 							+ ")/animation/3D");
    189 					simulator.initialize(this.replications[this.replication]);
    190 					this.replication++;
    191 					simulator.start();
    192 				} else
    193 				{
    194 					this.fireEvent(new Event(RunControl.END_OF_RUN_EVENT, this,
    195 							null));
    196 				}
    197 			}
    198 		} catch (Exception exception)
    199 		{
    200 			Logger.warning(this, "notify", exception);
    201 		}
    202 	}
    203
    204 	/***
    205 	 * resets the RunControl
    206 	 */
    207 	public synchronized void reset()
    208 	{
    209 		this.replication = 0;
    210 		for (int i = 0; i < this.replications.length; i++)
    211 		{
    212 			this.replications[i].reset();
    213 		}
    214 	}
    215
    216 	/***
    217 	 * sets the replications
    218 	 *
    219 	 * @param replications the replications of the runControl
    220 	 *
    221 	 * @uml.property name="replications"
    222 	 */
    223 	public void setReplications(final Replication[] replications)
    224 	{
    225 		this.replications = replications;
    226 	}
    227
    228 	/***
    229 	 * sets the runLength
    230 	 *
    231 	 * @param runLength which is the runLength
    232 	 *
    233 	 * @uml.property name="runLength"
    234 	 */
    235 	public void setRunLength(final double runLength)
    236 	{
    237 		this.runLength = runLength;
    238 	}
    239
    240 	/***
    241 	 * sets the warmupPeriod
    242 	 *
    243 	 * @param warmupPeriod reflects the warmupPeriod
    244 	 *
    245 	 * @uml.property name="warmupPeriod"
    246 	 */
    247 	public void setWarmupPeriod(final double warmupPeriod)
    248 	{
    249 		this.warmupPeriod = warmupPeriod;
    250 	}
    251
    252 	/***
    253 	 * @see java.lang.Object#toString()
    254 	 */
    255 	public String toString()
    256 	{
    257 		String result = super.toString() + " ; RL=" + this.getRunLength()
    258 				+ " ; WP=" + this.getWarmupPeriod() + " ; replications=[";
    259 		for (int i = 0; i < this.replications.length; i++)
    260 		{
    261 			result = result + this.replications[i].toString() + " ; ";
    262 		}
    263 		result = result.substring(0, result.length() - 2) + "]";
    264 		return "[" + result;
    265 	}
    266 }