SimpleSimulator.java

  1. package org.opentrafficsim.simulationengine;

  2. import java.io.Serializable;

  3. import javax.naming.NamingException;

  4. import org.djunits.unit.TimeUnit;
  5. import org.djunits.value.vdouble.scalar.Duration;
  6. import org.djunits.value.vdouble.scalar.Time;
  7. import org.opentrafficsim.base.modelproperties.PropertyException;
  8. import org.opentrafficsim.core.dsol.OTSModelInterface;
  9. import org.opentrafficsim.core.dsol.OTSReplication;

  10. import nl.tudelft.simulation.dsol.SimRuntimeException;
  11. import nl.tudelft.simulation.dsol.experiment.ReplicationMode;
  12. import nl.tudelft.simulation.dsol.formalisms.eventscheduling.SimEvent;
  13. import nl.tudelft.simulation.dsol.simtime.SimTimeDoubleUnit;
  14. import nl.tudelft.simulation.dsol.simulators.DEVSSimulator;

  15. /**
  16.  * Construct a DSOL DEVSSimulator or DEVSAnimator the easy way.
  17.  * <p>
  18.  * Copyright (c) 2013-2018 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
  19.  * BSD-style license. See <a href="http://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
  20.  * <p>
  21.  * $LastChangedDate: 2018-09-19 13:55:45 +0200 (Wed, 19 Sep 2018) $, @version $Revision: 4006 $, by $Author: averbraeck $,
  22.  * initial version 12 nov. 2014 <br>
  23.  * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
  24.  */
  25. public class SimpleSimulator extends DEVSSimulator.TimeDoubleUnit implements SimpleSimulatorInterface, Serializable
  26. {
  27.     /** */
  28.     private static final long serialVersionUID = 20150510L;

  29.     /** Counter for replication. */
  30.     private int lastReplication = 0;

  31.     /**
  32.      * Create a simulation engine without animation; the easy way. PauseOnError is set to true;
  33.      * @param startTime SimTimeDoubleUnit; the start time of the simulation
  34.      * @param warmupPeriod Duration; the warm up period of the simulation (use new Duration(0, SECOND) if you don't know what
  35.      *            this is)
  36.      * @param runLength Duration; the duration of the simulation
  37.      * @param model OTSModelInterface; the simulation to execute
  38.      * @throws SimRuntimeException on ???
  39.      * @throws NamingException when the context for the replication cannot be created
  40.      */
  41.     public SimpleSimulator(final Time startTime, final Duration warmupPeriod, final Duration runLength,
  42.             final OTSModelInterface model) throws SimRuntimeException, NamingException
  43.     {
  44.         setPauseOnError(true);
  45.         initialize(new OTSReplication("rep" + ++this.lastReplication, new SimTimeDoubleUnit(startTime), warmupPeriod, runLength,
  46.                 model), ReplicationMode.TERMINATING);
  47.     }

  48.     /**
  49.      * Create a simulation engine with animation and prescribed replication number; the easy way. PauseOnError is set to true;
  50.      * @param startTime Time; the start time of the simulation
  51.      * @param warmupPeriod Duration; the warm up period of the simulation (use new Duration(0, SECOND) if you don't know what
  52.      *            this is)
  53.      * @param runLength Duration; the duration of the simulation
  54.      * @param model OTSModelInterface; the simulation to execute
  55.      * @param replication int; the replication number
  56.      * @throws SimRuntimeException on ???
  57.      * @throws NamingException when context for the animation cannot be created
  58.      * @throws PropertyException when one of the user modified properties has the empty string as key
  59.      */
  60.     public SimpleSimulator(final Time startTime, final Duration warmupPeriod, final Duration runLength,
  61.             final OTSModelInterface model, final int replication) throws SimRuntimeException, NamingException, PropertyException
  62.     {
  63.         setPauseOnError(true);
  64.         initialize(new OTSReplication("rep" + replication, new SimTimeDoubleUnit(startTime), warmupPeriod, runLength, model),
  65.                 ReplicationMode.TERMINATING);
  66.     }

  67.     /**
  68.      * {@inheritDoc}
  69.      */
  70.     @Override
  71.     public final SimEvent<SimTimeDoubleUnit> scheduleEvent(final Time executionTime, final short priority, final Object source,
  72.             final Object target, final String method, final Object[] args) throws SimRuntimeException
  73.     {
  74.         SimEvent<SimTimeDoubleUnit> result = new SimEvent<SimTimeDoubleUnit>(
  75.                 new SimTimeDoubleUnit(new Time(executionTime.getSI(), TimeUnit.BASE)), priority, source, target, method, args);
  76.         scheduleEvent(result);
  77.         return result;
  78.     }

  79. }