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.DEVSRealTimeClock;
16  
17  /**
18   * Construct a DSOL DEVSAnimator 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 11 mei 2015 <br>
25   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
26   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
27   */
28  public class OTSAnimator extends DEVSRealTimeClock.TimeDoubleUnit implements OTSAnimatorInterface, Serializable
29  {
30      /** */
31      private static final long serialVersionUID = 20150511L;
32  
33      /** Counter for replication. */
34      private int lastReplication = 0;
35  
36      /**
37       * Construct an OTSAnimator.
38       */
39      public OTSAnimator()
40      {
41          super();
42      }
43  
44      /** {@inheritDoc} */
45      @Override
46      public void initialize(final Time startTime, final Duration warmupPeriod, final Duration runLength,
47              final OTSModelInterface model) throws SimRuntimeException, NamingException
48      {
49          setPauseOnError(true);
50          setAnimationDelay(20); // 50 Hz animation update
51          OTSReplication newReplication =
52                  OTSReplication.create("rep" + ++this.lastReplication, startTime, warmupPeriod, runLength, model);
53          super.initialize(newReplication, ReplicationMode.TERMINATING);
54      }
55  
56      /** {@inheritDoc} */
57      @Override
58      public void initialize(final Time startTime, final Duration warmupPeriod, final Duration runLength,
59              final OTSModelInterface model, final int replicationnr) throws SimRuntimeException, NamingException
60      {
61          setPauseOnError(true);
62          setAnimationDelay(20); // 50 Hz animation update
63          OTSReplication newReplication = OTSReplication.create("rep" + replicationnr, startTime, warmupPeriod, runLength, model);
64          super.initialize(newReplication, ReplicationMode.TERMINATING);
65      }
66  
67      /** {@inheritDoc} */
68      @Override
69      public final SimEvent<SimTimeDoubleUnit> scheduleEvent(final Time executionTime, final short priority, final Object source,
70              final Object target, final String method, final Object[] args) throws SimRuntimeException
71      {
72          SimEvent<SimTimeDoubleUnit> result = new SimEvent<>(
73                  new SimTimeDoubleUnit(new Time(executionTime.getSI(), TimeUnit.BASE)), priority, source, target, method, args);
74          scheduleEvent(result);
75          return result;
76      }
77  
78      /** {@inheritDoc} */
79      @Override
80      public final OTSReplication getReplication()
81      {
82          return (OTSReplication) super.getReplication();
83      }
84  
85      /** {@inheritDoc} */
86      @Override
87      public String toString()
88      {
89          return "OTSAnimator [lastReplication=" + this.lastReplication + "]";
90      }
91  
92  }