View Javadoc

    1   /*
    2    * @(#) Animator.java Sep 6, 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 animator.
    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.14 2004-03-26
    28   * @since 1.0
    29   */
    30  public class Animator extends DEVDESSSimulator implements AnimatorInterface
    31  {
    32
    33  	/***
    34  	 * animationDelay refers to the delay in miliseconds between timeSteps
    35  	 *
    36  	 * @uml.property name="animationDelay"
    37  	 */
    38  	protected long animationDelay = 100L;
    39
    40  	/***
    41  	 * @see nl.tudelft.simulation.dsol.simulators.AnimatorInterface
    42  	 *      #getAnimationDelay()
    43  	 *
    44  	 * @uml.property name="animationDelay"
    45  	 */
    46  	public long getAnimationDelay()
    47  	{
    48  		return this.animationDelay;
    49  	}
    50
    51  	/***
    52  	 * @see nl.tudelft.simulation.dsol.simulators.AnimatorInterface
    53  	 *      #setAnimationDelay(long)
    54  	 *
    55  	 * @uml.property name="animationDelay"
    56  	 */
    57  	public void setAnimationDelay(final long animationDelay)
    58  	{
    59  		this.animationDelay = animationDelay;
    60  		Logger.finer(this, "setAnimationDelay", "set the animationDelay to "
    61  				+ animationDelay);
    62  		this.fireEvent(ANIMATION_DELAY_CHANGED_EVENT, animationDelay);
    63  	}
    64
    65  	/***
    66  	 * @see nl.tudelft.simulation.dsol.simulators.DEVSSimulator#run()
    67  	 */
    68  	public void run()
    69  	{
    70  		while (this.isRunning()
    71  				&& !this.eventList.isEmpty()
    72  				&& this.simulatorTime <= this.replication.getRunControl()
    73  						.getRunLength())
    74  		{
    75  			try
    76  			{
    77  				if (this.animationDelay > 0)
    78  				{
    79  					Thread.sleep(this.animationDelay);
    80  				}
    81  			} catch (Exception exception)
    82  			{
    83  				exception = null;
    84  				//Let's neglect this sleep...
    85  			}
    86  			double runUntil = this.simulatorTime + this.timeStep;
    87  			while (!this.eventList.isEmpty()
    88  					&& this.running
    89  					&& runUntil >= this.eventList.first()
    90  							.getAbsoluteExecutionTime())
    91  			{
    92  				synchronized (super.semaphore)
    93  				{
    94  					SimEventInterface event = this.eventList.removeFirst();
    95  					this.simulatorTime = event.getAbsoluteExecutionTime();
    96  					this.fireEvent(SimulatorInterface.TIME_CHANGED_EVENT,
    97  							this.simulatorTime, this.simulatorTime);
    98  					try
    99  					{
    100 						event.execute();
    101 					} catch (Exception exception)
    102 					{
    103 						Logger.severe(this, "run", exception);
    104 					}
    105 				}
    106 			}
    107 			if (this.running)
    108 			{
    109 				this.simulatorTime = runUntil;
    110 			}
    111 			this.fireEvent(SimulatorInterface.TIME_CHANGED_EVENT,
    112 					this.simulatorTime, this.simulatorTime);
    113 			this.fireEvent(AnimatorInterface.UPDATE_ANIMATION_EVENT,
    114 					this.simulatorTime);
    115 		}
    116 	}
    117 }