View Javadoc
1   package org.opentrafficsim.core.dsol;
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  
11  import nl.tudelft.simulation.dsol.SimRuntimeException;
12  import nl.tudelft.simulation.dsol.experiment.ReplicationMode;
13  import nl.tudelft.simulation.dsol.formalisms.eventscheduling.SimEvent;
14  import nl.tudelft.simulation.dsol.simtime.SimTimeDoubleUnit;
15  import nl.tudelft.simulation.dsol.simulators.DEVSSimulator;
16  
17  /**
18   * Construct a DSOL DEVSSimulator the easy way.
19   * <p>
20   * Copyright (c) 2013-2019 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
21   * BSD-style license. See <a href="http://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
22   * <p>
23   * $LastChangedDate: 2018-10-30 14:03:57 +0100 (Tue, 30 Oct 2018) $, @version $Revision: 4727 $, by $Author: pknoppers $,
24   * initial version 12 nov. 2014 <br>
25   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
26   */
27  public class OTSSimulator extends DEVSSimulator.TimeDoubleUnit implements OTSSimulatorInterface, Serializable
28  {
29      /** */
30      private static final long serialVersionUID = 20150510L;
31  
32      /** Counter for replication. */
33      private int lastReplication = 0;
34  
35      /**
36       * Construct an OTSSimulator.
37       */
38      public OTSSimulator()
39      {
40          super();
41      }
42  
43      /** {@inheritDoc} */
44      @Override
45      public void initialize(final Time startTime, final Duration warmupPeriod, final Duration runLength,
46              final OTSModelInterface model) throws SimRuntimeException, NamingException
47      {
48          setPauseOnError(true);
49          OTSReplication newReplication =
50                  OTSReplication.create("rep" + ++this.lastReplication, startTime, warmupPeriod, runLength, model);
51          super.initialize(newReplication, ReplicationMode.TERMINATING);
52      }
53  
54      /** {@inheritDoc} */
55      @Override
56      public void initialize(final Time startTime, final Duration warmupPeriod, final Duration runLength,
57              final OTSModelInterface model, final int replicationNr) throws SimRuntimeException, NamingException
58      {
59          setPauseOnError(true);
60          OTSReplication newReplication = OTSReplication.create("rep" + replicationNr, startTime, warmupPeriod, runLength, model);
61          super.initialize(newReplication, ReplicationMode.TERMINATING);
62      }
63  
64      /** {@inheritDoc} */
65      @Override
66      public final SimEvent<SimTimeDoubleUnit> scheduleEvent(final Time executionTime, final short priority, final Object source,
67              final Object target, final String method, final Object[] args) throws SimRuntimeException
68      {
69          SimEvent<SimTimeDoubleUnit> result = new SimEvent<>(
70                  new SimTimeDoubleUnit(new Time(executionTime.getSI(), TimeUnit.BASE)), priority, source, target, method, args);
71          scheduleEvent(result);
72          return result;
73      }
74  
75      /** {@inheritDoc} */
76      @Override
77      public OTSReplication getReplication()
78      {
79          return (OTSReplication) super.getReplication();
80      }
81  
82      /** {@inheritDoc} */
83      @Override
84      public String toString()
85      {
86          return "OTSSimulator [lastReplication=" + this.lastReplication + "]";
87      }
88  
89  }