1 package org.opentrafficsim.simulationengine;
2
3 import java.awt.Dimension;
4 import java.awt.geom.Rectangle2D;
5 import java.rmi.RemoteException;
6
7 import nl.tudelft.simulation.dsol.SimRuntimeException;
8 import nl.tudelft.simulation.dsol.animation.D2.AnimationPanel;
9 import nl.tudelft.simulation.dsol.experiment.ReplicationMode;
10 import nl.tudelft.simulation.dsol.formalisms.eventscheduling.SimEvent;
11 import nl.tudelft.simulation.dsol.formalisms.eventscheduling.SimEventInterface;
12 import nl.tudelft.simulation.dsol.gui.swing.DSOLPanel;
13 import nl.tudelft.simulation.dsol.simulators.DEVSSimulator;
14 import nl.tudelft.simulation.dsol.simulators.SimulatorInterface;
15 import nl.tudelft.simulation.event.Event;
16
17 import org.opentrafficsim.core.dsol.OTSDEVSAnimator;
18 import org.opentrafficsim.core.dsol.OTSDEVSSimulator;
19 import org.opentrafficsim.core.dsol.OTSModelInterface;
20 import org.opentrafficsim.core.dsol.OTSReplication;
21 import org.opentrafficsim.core.dsol.OTSSimTimeDouble;
22 import org.opentrafficsim.core.unit.TimeUnit;
23 import org.opentrafficsim.core.value.vdouble.scalar.DoubleScalar;
24
25
26
27
28
29
30
31
32
33
34 public class SimpleSimulator
35 {
36
37 private int lastReplication = 0;
38
39
40 private final DSOLPanel<DoubleScalar.Abs<TimeUnit>, DoubleScalar.Rel<TimeUnit>, OTSSimTimeDouble> panel;
41
42
43 private final DEVSSimulator<DoubleScalar.Abs<TimeUnit>, DoubleScalar.Rel<TimeUnit>, OTSSimTimeDouble> simulator;
44
45
46
47
48
49
50
51
52
53
54
55 public SimpleSimulator(final DoubleScalar.Abs<TimeUnit> startTime, final DoubleScalar.Rel<TimeUnit> warmupPeriod,
56 final DoubleScalar.Rel<TimeUnit> runLength, final OTSModelInterface model) throws RemoteException,
57 SimRuntimeException
58 {
59 this.simulator = new OTSDEVSSimulator();
60 this.simulator.setPauseOnError(true);
61 this.simulator.initialize(new OTSReplication("rep" + ++this.lastReplication, new OTSSimTimeDouble(startTime),
62 warmupPeriod, runLength, model), ReplicationMode.TERMINATING);
63 this.panel =
64 new DSOLPanel<DoubleScalar.Abs<TimeUnit>, DoubleScalar.Rel<TimeUnit>, OTSSimTimeDouble>(model,
65 this.simulator);
66 }
67
68
69
70
71
72
73
74
75
76
77
78
79 public SimpleSimulator(final DoubleScalar.Abs<TimeUnit> startTime, final DoubleScalar.Rel<TimeUnit> warmupPeriod,
80 final DoubleScalar.Rel<TimeUnit> runLength, final OTSModelInterface model, final Rectangle2D extent)
81 throws RemoteException, SimRuntimeException
82 {
83 this.simulator = new OTSDEVSAnimator();
84 this.simulator.setPauseOnError(true);
85 this.simulator.initialize(new OTSReplication("rep" + ++this.lastReplication, new OTSSimTimeDouble(startTime),
86 warmupPeriod, runLength, model), ReplicationMode.TERMINATING);
87 this.panel =
88 new DSOLPanel<DoubleScalar.Abs<TimeUnit>, DoubleScalar.Rel<TimeUnit>, OTSSimTimeDouble>(model,
89 this.simulator);
90 Dimension size = new Dimension(1024, 768);
91 AnimationPanel animationPanel = new AnimationPanel(extent, size, this.simulator);
92 this.panel.getTabbedPane().addTab(0, "animation", animationPanel);
93 animationPanel.notify(new Event(SimulatorInterface.START_REPLICATION_EVENT, this.simulator, null));
94 this.panel.getTabbedPane().setSelectedIndex(0);
95 }
96
97
98
99
100
101 public final DSOLPanel<DoubleScalar.Abs<TimeUnit>, DoubleScalar.Rel<TimeUnit>, OTSSimTimeDouble> getPanel()
102 {
103 return this.panel;
104 }
105
106
107
108
109
110 public final DEVSSimulator<DoubleScalar.Abs<TimeUnit>, DoubleScalar.Rel<TimeUnit>, OTSSimTimeDouble> getSimulator()
111 {
112 return this.simulator;
113 }
114
115
116 private SimEvent<OTSSimTimeDouble> stopAtEvent = null;
117
118
119
120
121
122
123 public final void runUpTo(final DoubleScalar.Abs<TimeUnit> when) throws SimRuntimeException
124 {
125 this.stopAtEvent =
126 new SimEvent<OTSSimTimeDouble>(new OTSSimTimeDouble(new DoubleScalar.Abs<TimeUnit>(when.getSI(),
127 TimeUnit.SECOND)), SimEventInterface.MAX_PRIORITY, this, this, "autoPauseSimulator", null);
128 this.simulator.scheduleEvent(this.stopAtEvent);
129 while (this.simulator.getSimulatorTime().get().getSI() < when.getSI())
130 {
131 this.simulator.step();
132 }
133 }
134
135
136
137
138 @SuppressWarnings("unused")
139 private void autoPauseSimulator()
140 {
141 if (this.simulator.isRunning())
142 {
143 this.simulator.stop();
144 }
145 }
146
147 }