OTSDEVSSimulator.java

  1. package org.opentrafficsim.core.dsol;

  2. import java.rmi.RemoteException;

  3. import org.djunits.value.vdouble.scalar.Duration;
  4. import org.djunits.value.vdouble.scalar.Time;

  5. import nl.tudelft.simulation.dsol.SimRuntimeException;
  6. import nl.tudelft.simulation.dsol.experiment.Replication;
  7. import nl.tudelft.simulation.dsol.experiment.ReplicationMode;
  8. import nl.tudelft.simulation.dsol.simulators.DEVSSimulator;
  9. import nl.tudelft.simulation.dsol.simulators.SimulatorInterface;

  10. /**
  11.  * Typed extension of the DEVSSimulator without remote exceptions.
  12.  * <p>
  13.  * Copyright (c) 2013-2016 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
  14.  * BSD-style license. See <a href="http://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
  15.  * <p>
  16.  * @version $Revision: 2386 $, $LastChangedDate: 2016-10-16 14:55:54 +0200 (Sun, 16 Oct 2016) $, by $Author: averbraeck $,
  17.  *          initial version Aug 15, 2014 <br>
  18.  * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
  19.  */
  20. public class OTSDEVSSimulator extends DEVSSimulator<Time, Duration, OTSSimTimeDouble> implements OTSDEVSSimulatorInterface
  21. {
  22.     /** */
  23.     private static final long serialVersionUID = 20140815L;

  24.     /** {@inheritDoc} */
  25.     @Override
  26.     public final void initialize(final Replication<Time, Duration, OTSSimTimeDouble> initReplication,
  27.             final ReplicationMode replicationMode) throws SimRuntimeException
  28.     {
  29.         try
  30.         {
  31.             super.initialize(initReplication, replicationMode);
  32.         }
  33.         catch (RemoteException exception)
  34.         {
  35.             throw new SimRuntimeException(exception);
  36.         }
  37.     }

  38.     /** {@inheritDoc} */
  39.     @Override
  40.     public final void runUpTo(final Time when) throws SimRuntimeException
  41.     {
  42.         super.runUpTo(when);
  43.     }

  44.     /** {@inheritDoc} */
  45.     @Override
  46.     @SuppressWarnings("checkstyle:designforextension")
  47.     public void start(final boolean fireStartEvent) throws SimRuntimeException
  48.     {
  49.         if (this.isRunning())
  50.         {
  51.             throw new SimRuntimeException("Cannot start a running simulator");
  52.         }
  53.         if (this.replication == null)
  54.         {
  55.             throw new SimRuntimeException("Cannot start a simulator" + " without replication details");
  56.         }
  57.         if (this.simulatorTime.ge(this.replication.getTreatment().getEndTime()))
  58.         {
  59.             throw new SimRuntimeException("Cannot start simulator : " + "simulatorTime = runLength");
  60.         }
  61.         synchronized (this.semaphore)
  62.         {
  63.             this.running = true;
  64.             if (fireStartEvent)
  65.             {
  66.                 this.fireEvent(START_EVENT);
  67.             }
  68.             this.fireTimedEvent(SimulatorInterface.TIME_CHANGED_EVENT, this.simulatorTime, this.simulatorTime);
  69.             this.worker.interrupt();
  70.         }
  71.     }

  72.     /** {@inheritDoc} */
  73.     @Override
  74.     public final String toString()
  75.     {
  76.         return "OTSDEVSSimulator [time=" + getSimulatorTime().getTime() + "]";
  77.     }

  78. }