View Javadoc

    1   /*
    2    * @(#)AnimatorInterface.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.simulators;
    11
    12  import nl.tudelft.simulation.dsol.formalisms.devs.SimEventInterface;
    13  import nl.tudelft.simulation.logger.Logger;
    14
    15  /***
    16   * The reference implementation of the DEVDESS simulator.
    17   * <p>
    18   * (c) copyright 2003 <a href="http://www.simulation.tudelft.nl">Delft
    19   * University of Technology </a>, the Netherlands. <br>
    20   * See for project information <a href="http://www.simulation.tudelft.nl">
    21   * www.simulation.tudelft.nl </a> <br>
    22   * License of use: <a href="http://www.gnu.org/copyleft/gpl.html">General Public
    23   * License (GPL) </a>, no warranty <br>
    24   *
    25   * @author <a href="http://www.simulation.tudelft.nl/people/jacobs.html">Peter
    26   *         Jacobs </a>
    27   * @version 1.9 2004-03-18
    28   * @since 1.0
    29   */
    30  public class DEVDESSSimulator extends DEVSSimulator implements
    31  		DEVDESSSimulatorInterface
    32  {
    33
    34  	/***
    35  	 * timeStep represents the DESS timeStep
    36  	 *
    37  	 * @uml.property name="timeStep"
    38  	 */
    39  	protected double timeStep = DEFAULT_TIME_STEP;
    40
    41  	/***
    42  	 * @see nl.tudelft.simulation.dsol.simulators.DESSSimulatorInterface
    43  	 *      #getTimeStep()
    44  	 *
    45  	 * @uml.property name="timeStep"
    46  	 */
    47  	public double getTimeStep()
    48  	{
    49  		return this.timeStep;
    50  	}
    51
    52  	/***
    53  	 * @see nl.tudelft.simulation.dsol.simulators.DESSSimulatorInterface
    54  	 *      #setTimeStep(double)
    55  	 *
    56  	 * @uml.property name="timeStep"
    57  	 */
    58  	public void setTimeStep(final double timeStep)
    59  	{
    60  		synchronized (super.semaphore)
    61  		{
    62  			if (timeStep < 0)
    63  			{
    64  				throw new IllegalArgumentException("timeStep < 0 ?");
    65  			}
    66  			this.timeStep = timeStep;
    67  			this.fireEvent(TIME_STEP_CHANGED_EVENT, timeStep);
    68  			Logger.finer(this, "setTimeStep", timeStep + "");
    69  		}
    70  	}
    71
    72  	/***
    73  	 * @see nl.tudelft.simulation.dsol.simulators.DEVSSimulator#run()
    74  	 */
    75  	public void run()
    76  	{
    77  		while (this.isRunning()
    78  				&& !this.eventList.isEmpty()
    79  				&& this.simulatorTime <= this.replication.getRunControl()
    80  						.getRunLength())
    81  		{
    82  			synchronized (super.semaphore)
    83  			{
    84  				double runUntil = this.simulatorTime + this.timeStep;
    85  				while (!this.eventList.isEmpty()
    86  						&& this.running
    87  						&& runUntil >= (this.eventList.first())
    88  								.getAbsoluteExecutionTime())
    89  				{
    90  					SimEventInterface event = this.eventList.removeFirst();
    91  					this.simulatorTime = event.getAbsoluteExecutionTime();
    92  					this.fireEvent(SimulatorInterface.TIME_CHANGED_EVENT,
    93  							this.simulatorTime, this.simulatorTime);
    94  					try
    95  					{
    96  						event.execute();
    97  					} catch (Exception exception)
    98  					{
    99  						Logger.severe(this, "run", exception);
    100 					}
    101 				}
    102 				if (this.running)
    103 				{
    104 					this.simulatorTime = runUntil;
    105 				}
    106 				this.fireEvent(SimulatorInterface.TIME_CHANGED_EVENT,
    107 						this.simulatorTime, this.simulatorTime);
    108 			}
    109 		}
    110 	}
    111 }