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