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-2017 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: 2017-04-29 12:51:08 +0200 (Sat, 29 Apr 2017) $, @version $Revision: 3570 $, 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, 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 OTSSimTimeDouble(startTime), warmupPeriod, runLength, model),
76 ReplicationMode.TERMINATING);
77 }
78
79 /** {@inheritDoc} */
80 @Override
81 public final SimEvent<OTSSimTimeDouble> 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<OTSSimTimeDouble> result = new SimEvent<OTSSimTimeDouble>(
85 new OTSSimTimeDouble(new Time(executionTime.getSI(), TimeUnit.BASE)), priority, source, target, method, args);
86 scheduleEvent(result);
87 return result;
88 }
89
90 }