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