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