View Javadoc
1   package org.opentrafficsim.simulationengine;
2   
3   import java.io.Serializable;
4   
5   import javax.naming.NamingException;
6   
7   import org.djunits.unit.TimeUnit;
8   import org.djunits.value.vdouble.scalar.Duration;
9   import org.djunits.value.vdouble.scalar.Time;
10  import org.opentrafficsim.core.dsol.OTSDEVSSimulator;
11  import org.opentrafficsim.core.dsol.OTSModelInterface;
12  import org.opentrafficsim.core.dsol.OTSReplication;
13  import org.opentrafficsim.core.dsol.OTSSimTimeDouble;
14  
15  import nl.tudelft.simulation.dsol.SimRuntimeException;
16  import nl.tudelft.simulation.dsol.experiment.ReplicationMode;
17  import nl.tudelft.simulation.dsol.formalisms.eventscheduling.SimEvent;
18  
19  /**
20   * Construct a DSOL DEVSSimulator or DEVSAnimator the easy way.
21   * <p>
22   * Copyright (c) 2013-2016 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
23   * BSD-style license. See <a href="http://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
24   * <p>
25   * $LastChangedDate: 2016-10-16 23:21:49 +0200 (Sun, 16 Oct 2016) $, @version $Revision: 2387 $, by $Author: averbraeck $,
26   * initial version 12 nov. 2014 <br>
27   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
28   */
29  public class SimpleSimulator extends OTSDEVSSimulator implements SimpleSimulatorInterface, Serializable
30  {
31      /** */
32      private static final long serialVersionUID = 20150510L;
33  
34      /** Counter for replication. */
35      private int lastReplication = 0;
36  
37      /**
38       * Create a simulation engine without animation; the easy way. PauseOnError is set to true;
39       * @param startTime OTSSimTimeDouble; the start time of the simulation
40       * @param warmupPeriod Duration; the warm up period of the simulation (use new Duration(0, SECOND) if you don't know what
41       *            this is)
42       * @param runLength Duration; the duration of the simulation
43       * @param model OTSModelInterface; the simulation to execute
44       * @throws SimRuntimeException on ???
45       * @throws NamingException when the context for the replication cannot be created
46       */
47      public SimpleSimulator(final Time startTime, final Duration warmupPeriod, final Duration runLength,
48          final OTSModelInterface model) throws SimRuntimeException, NamingException
49      {
50          setPauseOnError(true);
51          initialize(new OTSReplication("rep" + ++this.lastReplication, new OTSSimTimeDouble(startTime), warmupPeriod,
52              runLength, model), ReplicationMode.TERMINATING);
53      }
54  
55      /**
56       * {@inheritDoc}
57       */
58      public final SimEvent<OTSSimTimeDouble> scheduleEvent(final Time executionTime, final short priority,
59          final Object source, final Object target, final String method, final Object[] args) throws SimRuntimeException
60      {
61          SimEvent<OTSSimTimeDouble> result =
62              new SimEvent<OTSSimTimeDouble>(new OTSSimTimeDouble(new Time(executionTime.getSI(), TimeUnit.SECOND)),
63                  priority, source, target, method, args);
64          scheduleEvent(result);
65          return result;
66      }
67  
68  }