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.base.modelproperties.PropertyException;
11  import org.opentrafficsim.core.dsol.OTSModelInterface;
12  import org.opentrafficsim.core.dsol.OTSReplication;
13  
14  import nl.tudelft.simulation.dsol.SimRuntimeException;
15  import nl.tudelft.simulation.dsol.experiment.ReplicationMode;
16  import nl.tudelft.simulation.dsol.formalisms.eventscheduling.SimEvent;
17  import nl.tudelft.simulation.dsol.simtime.SimTimeDoubleUnit;
18  import nl.tudelft.simulation.dsol.simulators.DEVSSimulator;
19  
20  /**
21   * Construct a DSOL DEVSSimulator or DEVSAnimator the easy way.
22   * <p>
23   * Copyright (c) 2013-2018 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
24   * BSD-style license. See <a href="http://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
25   * <p>
26   * $LastChangedDate: 2018-09-19 13:55:45 +0200 (Wed, 19 Sep 2018) $, @version $Revision: 4006 $, by $Author: averbraeck $,
27   * initial version 12 nov. 2014 <br>
28   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
29   */
30  public class SimpleSimulator extends DEVSSimulator.TimeDoubleUnit implements SimpleSimulatorInterface, Serializable
31  {
32      /** */
33      private static final long serialVersionUID = 20150510L;
34  
35      /** Counter for replication. */
36      private int lastReplication = 0;
37  
38      /**
39       * Create a simulation engine without animation; the easy way. PauseOnError is set to true;
40       * @param startTime SimTimeDoubleUnit; the start time of the simulation
41       * @param warmupPeriod Duration; the warm up period of the simulation (use new Duration(0, SECOND) if you don't know what
42       *            this is)
43       * @param runLength Duration; the duration of the simulation
44       * @param model OTSModelInterface; the simulation to execute
45       * @throws SimRuntimeException on ???
46       * @throws NamingException when the context for the replication cannot be created
47       */
48      public SimpleSimulator(final Time startTime, final Duration warmupPeriod, final Duration runLength,
49              final OTSModelInterface model) throws SimRuntimeException, NamingException
50      {
51          setPauseOnError(true);
52          initialize(new OTSReplication("rep" + ++this.lastReplication, new SimTimeDoubleUnit(startTime), warmupPeriod, runLength,
53                  model), ReplicationMode.TERMINATING);
54      }
55  
56      /**
57       * Create a simulation engine with animation and prescribed replication number; the easy way. PauseOnError is set to true;
58       * @param startTime Time; the start time of the simulation
59       * @param warmupPeriod Duration; the warm up period of the simulation (use new Duration(0, SECOND) if you don't know what
60       *            this is)
61       * @param runLength Duration; the duration of the simulation
62       * @param model OTSModelInterface; the simulation to execute
63       * @param replication int; the replication number
64       * @throws SimRuntimeException on ???
65       * @throws NamingException when context for the animation cannot be created
66       * @throws PropertyException when one of the user modified properties has the empty string as key
67       */
68      public SimpleSimulator(final Time startTime, final Duration warmupPeriod, final Duration runLength,
69              final OTSModelInterface model, final int replication) throws SimRuntimeException, NamingException, PropertyException
70      {
71          setPauseOnError(true);
72          initialize(new OTSReplication("rep" + replication, new SimTimeDoubleUnit(startTime), warmupPeriod, runLength, model),
73                  ReplicationMode.TERMINATING);
74      }
75  
76      /**
77       * {@inheritDoc}
78       */
79      @Override
80      public final SimEvent<SimTimeDoubleUnit> scheduleEvent(final Time executionTime, final short priority, final Object source,
81              final Object target, final String method, final Object[] args) throws SimRuntimeException
82      {
83          SimEvent<SimTimeDoubleUnit> result = new SimEvent<SimTimeDoubleUnit>(
84                  new SimTimeDoubleUnit(new Time(executionTime.getSI(), TimeUnit.BASE)), priority, source, target, method, args);
85          scheduleEvent(result);
86          return result;
87      }
88  
89  }