DevDessAnimator.java

  1. package nl.tudelft.simulation.dsol.simulators;

  2. import java.io.Serializable;

  3. import nl.tudelft.simulation.dsol.SimRuntimeException;
  4. import nl.tudelft.simulation.dsol.formalisms.eventscheduling.SimEventInterface;
  5. import nl.tudelft.simulation.dsol.simtime.SimTime;

  6. /**
  7.  * The reference implementation of the animator.
  8.  * <p>
  9.  * Copyright (c) 2002-2025 Delft University of Technology, Jaffalaan 5, 2628 BX Delft, the Netherlands. All rights reserved. See
  10.  * for project information <a href="https://simulation.tudelft.nl/dsol/manual/" target="_blank">DSOL Manual</a>. The DSOL
  11.  * project is distributed under a three-clause BSD-style license, which can be found at
  12.  * <a href="https://simulation.tudelft.nl/dsol/docs/latest/license.html" target="_blank">DSOL License</a>.
  13.  * </p>
  14.  * @author <a href="https://www.linkedin.com/in/peterhmjacobs">Peter Jacobs </a>
  15.  * @author <a href="https://github.com/averbraeck">Alexander Verbraeck</a>
  16.  * @param <T> the time type
  17.  * @since 1.5
  18.  */
  19. public class DevDessAnimator<T extends Number & Comparable<T>> extends DevDessSimulator<T> implements AnimatorInterface
  20. {
  21.     /** */
  22.     private static final long serialVersionUID = 20140804L;

  23.     /**
  24.      * @param initialTimeStep T; the initial time step to use in the integration.
  25.      * @param id the id of the simulator, used in logging and firing of events.
  26.      * @throws SimRuntimeException when initialTimeStep &lt;= 0, NaN, or Infinity
  27.      */
  28.     public DevDessAnimator(final Serializable id, final T initialTimeStep) throws SimRuntimeException
  29.     {
  30.         super(id, initialTimeStep);
  31.     }

  32.     /** AnimationDelay refers to the delay in milliseconds between timeSteps. */
  33.     @SuppressWarnings("checkstyle:visibilitymodifier")
  34.     protected long animationDelay = 100L;

  35.     @Override
  36.     public long getAnimationDelay()
  37.     {
  38.         return this.animationDelay;
  39.     }

  40.     @Override
  41.     public void setAnimationDelay(final long animationDelay)
  42.     {
  43.         this.animationDelay = animationDelay;
  44.         this.fireEvent(ANIMATION_DELAY_CHANGED_EVENT, animationDelay);
  45.     }

  46.     @Override
  47.     public void updateAnimation()
  48.     {
  49.         this.fireTimedEvent(AnimatorInterface.UPDATE_ANIMATION_EVENT, null, this.simulatorTime);
  50.     }

  51.     @Override
  52.     public void run()
  53.     {
  54.         AnimationThread animationThread = new AnimationThread(this);
  55.         animationThread.start();
  56.         // set the run flag semaphore to signal to startImpl() that the run method has started
  57.         this.runflag = true;
  58.         while (!isStoppingOrStopped() && !this.eventList.isEmpty()
  59.                 && this.simulatorTime.compareTo(this.replication.getEndTime()) <= 0)
  60.         {
  61.             try
  62.             {
  63.                 if (this.animationDelay > 0)
  64.                 {
  65.                     Thread.sleep(this.animationDelay);
  66.                 }
  67.             }
  68.             catch (Exception exception)
  69.             {
  70.                 exception = null;
  71.                 // Let's neglect this sleep..
  72.             }
  73.             T runUntil = SimTime.plus(this.simulatorTime, this.timeStep);
  74.             while (!this.eventList.isEmpty() && !isStoppingOrStopped()
  75.                     && runUntil.compareTo(this.eventList.first().getAbsoluteExecutionTime()) >= 0)
  76.             {
  77.                 synchronized (super.semaphore)
  78.                 {
  79.                     int cmp = this.eventList.first().getAbsoluteExecutionTime().compareTo(this.runUntilTime);
  80.                     if ((cmp == 0 && !this.runUntilIncluding) || cmp > 0)
  81.                     {
  82.                         this.simulatorTime = SimTime.copy(this.runUntilTime);
  83.                         this.runState = RunState.STOPPING;
  84.                         break;
  85.                     }

  86.                     SimEventInterface<T> event = this.eventList.removeFirst();
  87.                     if (event.getAbsoluteExecutionTime().compareTo(super.simulatorTime) != 0)
  88.                     {
  89.                         super.fireUnverifiedTimedEvent(SimulatorInterface.TIME_CHANGED_EVENT, null,
  90.                                 event.getAbsoluteExecutionTime());
  91.                     }
  92.                     this.simulatorTime = event.getAbsoluteExecutionTime();
  93.                     try
  94.                     {
  95.                         event.execute();
  96.                         if (this.eventList.isEmpty())
  97.                         {
  98.                             this.simulatorTime = SimTime.copy(this.runUntilTime);
  99.                             this.runState = RunState.STOPPING;
  100.                             break;
  101.                         }
  102.                     }
  103.                     catch (Exception exception)
  104.                     {
  105.                         handleSimulationException(exception);
  106.                     }
  107.                 }
  108.             }
  109.             if (!isStoppingOrStopped())
  110.             {
  111.                 this.simulatorTime = runUntil;
  112.             }
  113.             this.fireTimedEvent(SimulatorInterface.TIME_CHANGED_EVENT, null, this.simulatorTime);
  114.         }
  115.         updateAnimation();
  116.         animationThread.stopAnimation();
  117.     }

  118. }