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.OTSDEVSRealTimeClock;
12  import org.opentrafficsim.core.dsol.OTSModelInterface;
13  import org.opentrafficsim.core.dsol.OTSReplication;
14  import org.opentrafficsim.core.dsol.OTSSimTimeDouble;
15  
16  /**
17   * <p>
18   * Copyright (c) 2013-2015 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
19   * BSD-style license. See <a href="http://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
20   * <p>
21   * $LastChangedDate: 2015-12-29 03:56:14 +0100 (Tue, 29 Dec 2015) $, @version $Revision: 1668 $, by $Author: averbraeck $,
22   * initial version 11 mei 2015 <br>
23   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
24   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
25   */
26  public class SimpleAnimator extends OTSDEVSRealTimeClock implements SimpleSimulatorInterface
27  {
28      /** */
29      private static final long serialVersionUID = 20150511L;
30  
31      /** Counter for replication. */
32      private int lastReplication = 0;
33  
34      /**
35       * Create a simulation engine with animation; the easy way. PauseOnError is set to true;
36       * @param startTime Time.Abs; 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 context for the animation cannot be created
43       */
44      public SimpleAnimator(final Time.Abs startTime, final Time.Rel warmupPeriod, final Time.Rel runLength,
45          final OTSModelInterface model) throws SimRuntimeException, NamingException
46      {
47          setPauseOnError(true);
48          setAnimationDelay(20); // 50 Hz animation update
49          initialize(new OTSReplication("rep" + ++this.lastReplication, new OTSSimTimeDouble(startTime), warmupPeriod,
50              runLength, model), ReplicationMode.TERMINATING);
51      }
52  
53      /**
54       * {@inheritDoc}
55       */
56      public final SimEvent<OTSSimTimeDouble> scheduleEvent(final Time.Abs executionTime, final short priority,
57          final Object source, final Object target, final String method, final Object[] args) throws SimRuntimeException
58      {
59          SimEvent<OTSSimTimeDouble> result =
60              new SimEvent<OTSSimTimeDouble>(new OTSSimTimeDouble(new Time.Abs(executionTime.getSI(), TimeUnit.SECOND)),
61                  priority, source, target, method, args);
62          scheduleEvent(result);
63          return result;
64      }
65  }