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.DEVSRealTimeClock;
19  
20  /**
21   * <p>
22   * Copyright (c) 2013-2018 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: 2018-09-19 13:55:45 +0200 (Wed, 19 Sep 2018) $, @version $Revision: 4006 $, 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 DEVSRealTimeClock.TimeDoubleUnit 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 SimTimeDoubleUnit(startTime), warmupPeriod, runLength,
55                  model), ReplicationMode.TERMINATING);
56      }
57  
58      /**
59       * Create a simulation engine with animation and prescribed replication number; the easy way. PauseOnError is set to true;
60       * @param startTime Time; the start time of the simulation
61       * @param warmupPeriod Duration; the warm up period of the simulation (use new Duration(0, SECOND) if you don't know what
62       *            this is)
63       * @param runLength Duration; the duration of the simulation
64       * @param model OTSModelInterface; the simulation to execute
65       * @param replication int; the replication number
66       * @throws SimRuntimeException on ???
67       * @throws NamingException when context for the animation cannot be created
68       * @throws PropertyException when one of the user modified properties has the empty string as key
69       */
70      public SimpleAnimator(final Time startTime, final Duration warmupPeriod, final Duration runLength,
71              final OTSModelInterface model, final int replication) throws SimRuntimeException, NamingException, PropertyException
72      {
73          setPauseOnError(true);
74          setAnimationDelay(20); // 50 Hz animation update
75          initialize(new OTSReplication("rep" + replication, new SimTimeDoubleUnit(startTime), warmupPeriod, runLength, model),
76                  ReplicationMode.TERMINATING);
77      }
78  
79      /** {@inheritDoc} */
80      @Override
81      public final SimEvent<SimTimeDoubleUnit> scheduleEvent(final Time executionTime, final short priority, final Object source,
82              final Object target, final String method, final Object[] args) throws SimRuntimeException
83      {
84          SimEvent<SimTimeDoubleUnit> result = new SimEvent<SimTimeDoubleUnit>(
85                  new SimTimeDoubleUnit(new Time(executionTime.getSI(), TimeUnit.BASE)), priority, source, target, method, args);
86          scheduleEvent(result);
87          return result;
88      }
89  
90  }