1 package org.opentrafficsim.demo;
2
3 import java.awt.Component;
4 import java.awt.Container;
5 import java.awt.Dimension;
6 import java.awt.Window;
7 import java.rmi.RemoteException;
8 import java.util.ArrayList;
9 import java.util.List;
10
11 import javax.naming.NamingException;
12 import javax.swing.JButton;
13
14 import org.djunits.value.vdouble.scalar.Duration;
15 import org.djunits.value.vdouble.scalar.Length;
16 import org.djunits.value.vdouble.scalar.Time;
17 import org.opentrafficsim.core.dsol.OTSAnimator;
18 import org.opentrafficsim.core.dsol.OTSSimulator;
19 import org.opentrafficsim.core.dsol.OTSSimulatorInterface;
20 import org.opentrafficsim.core.gtu.GTU;
21 import org.opentrafficsim.core.gtu.GTUDirectionality;
22 import org.opentrafficsim.core.network.DirectedLinkPosition;
23 import org.opentrafficsim.core.network.NetworkException;
24 import org.opentrafficsim.draw.core.OTSDrawingException;
25 import org.opentrafficsim.draw.graphs.ContourDataSource;
26 import org.opentrafficsim.draw.graphs.ContourPlotAcceleration;
27 import org.opentrafficsim.draw.graphs.ContourPlotDensity;
28 import org.opentrafficsim.draw.graphs.ContourPlotFlow;
29 import org.opentrafficsim.draw.graphs.ContourPlotSpeed;
30 import org.opentrafficsim.draw.graphs.FundamentalDiagram;
31 import org.opentrafficsim.draw.graphs.FundamentalDiagram.Quantity;
32 import org.opentrafficsim.draw.graphs.GraphCrossSection;
33 import org.opentrafficsim.draw.graphs.GraphPath;
34 import org.opentrafficsim.draw.graphs.TrajectoryPlot;
35 import org.opentrafficsim.draw.graphs.road.GraphLaneUtil;
36 import org.opentrafficsim.kpi.sampling.KpiLaneDirection;
37 import org.opentrafficsim.road.network.OTSRoadNetwork;
38 import org.opentrafficsim.road.network.lane.LaneDirection;
39 import org.opentrafficsim.road.network.sampling.RoadSampler;
40 import org.opentrafficsim.swing.graphs.SwingContourPlot;
41 import org.opentrafficsim.swing.graphs.SwingFundamentalDiagram;
42 import org.opentrafficsim.swing.graphs.SwingPlot;
43 import org.opentrafficsim.swing.graphs.SwingTrajectoryPlot;
44 import org.opentrafficsim.swing.gui.OTSAnimationPanel;
45 import org.opentrafficsim.swing.gui.OTSSimulationApplication;
46
47 import nl.tudelft.simulation.dsol.SimRuntimeException;
48 import nl.tudelft.simulation.dsol.swing.gui.TablePanel;
49 import nl.tudelft.simulation.dsol.swing.gui.inputparameters.TabbedParameterDialog;
50 import nl.tudelft.simulation.language.DSOLException;
51
52
53
54
55
56
57
58
59
60
61
62 public class CircularRoadSwing extends OTSSimulationApplication<CircularRoadModel>
63 {
64
65 private static final long serialVersionUID = 1L;
66
67
68
69
70
71
72
73
74 public CircularRoadSwing(final String title, final OTSAnimationPanel panel, final CircularRoadModel model)
75 throws OTSDrawingException
76 {
77 super(model, panel);
78
79
80
81 OTSRoadNetwork network = model.getNetwork();
82 System.out.println(network.getLinkMap());
83 }
84
85
86 @Override
87 protected void addTabs()
88 {
89 addStatisticsTabs(getModel().getSimulator());
90 }
91
92
93
94
95
96 public static void main(final String[] args)
97 {
98
99 demo(true);
100 }
101
102
103
104
105 public static void simulatorDemo()
106 {
107 try
108 {
109 OTSSimulator simulator = new OTSSimulator("CircularRoadSwing");
110 final CircularRoadModelml#CircularRoadModel">CircularRoadModel otsModel = new CircularRoadModel(simulator);
111 System.out.println(otsModel.getInputParameterMap());
112 TabbedParameterDialog.process(otsModel.getInputParameterMap());
113 simulator.initialize(Time.ZERO, Duration.ZERO, Duration.instantiateSI(3600.0), otsModel);
114 Thread getLocationThread = new Thread()
115 {
116 @Override
117 public void run()
118 {
119 System.out.println("getLocationThread starts up");
120 int iteration = 0;
121 int getLocationCalls = 0;
122 while (simulator.isStartingOrRunning())
123 {
124 iteration++;
125 for (GTU gtu : otsModel.getNetwork().getGTUs())
126 {
127 try
128 {
129 gtu.getLocation();
130 getLocationCalls++;
131 }
132 catch (RemoteException e)
133 {
134 e.printStackTrace();
135 }
136 }
137 try
138 {
139 Thread.sleep(1);
140 }
141 catch (InterruptedException e)
142 {
143 e.printStackTrace();
144 }
145 }
146 System.out.println("getLocationThread exits after " + iteration + " iterations and " + getLocationCalls
147 + " getLocation calls");
148 }
149
150 };
151 simulator.start();
152 getLocationThread.start();
153 while (simulator.isStartingOrRunning())
154 {
155 Thread.sleep(1000);
156
157 }
158 }
159 catch (Exception e)
160 {
161 e.printStackTrace();
162 }
163 System.exit(0);
164 }
165
166
167
168
169
170
171 public static boolean clickStart(final Component component)
172 {
173 if (component instanceof JButton)
174 {
175 JButton button = (JButton) component;
176 if (button.getText().contains("Start simulation model"))
177 {
178 button.doClick();
179 System.out.println("Auto clicked the start button");
180 return true;
181 }
182 }
183 else if (component instanceof Container)
184 {
185 for (Component comp : ((Container) component).getComponents())
186 {
187 if (clickStart(comp))
188 {
189 return true;
190 }
191 }
192 }
193 return false;
194 }
195
196
197
198
199
200
201 public static boolean clickRunPause(final Component component)
202 {
203 if (component instanceof JButton)
204 {
205 JButton button = (JButton) component;
206
207 if (button.getName().equals("runPauseButton"))
208 {
209 button.doClick();
210 System.out.println("Auto clicked the run button");
211 return true;
212 }
213 }
214 else if (component instanceof Container)
215 {
216 for (Component comp : ((Container) component).getComponents())
217 {
218 if (clickRunPause(comp))
219 {
220 return true;
221 }
222 }
223 }
224 return false;
225 }
226
227
228
229
230
231 public static void demo(final boolean exitOnClose)
232 {
233 try
234 {
235 OTSAnimator simulator = new OTSAnimator("CircularRoadSwing");
236 final CircularRoadModelml#CircularRoadModel">CircularRoadModel otsModel = new CircularRoadModel(simulator);
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269 if (TabbedParameterDialog.process(otsModel.getInputParameterMap()))
270 {
271 simulator.initialize(Time.ZERO, Duration.ZERO, Duration.instantiateSI(3600.0), otsModel);
272 OTSAnimationPanel animationPanel = new OTSAnimationPanel(otsModel.getNetwork().getExtent(),
273 new Dimension(800, 600), simulator, otsModel, DEFAULT_COLORER, otsModel.getNetwork());
274 CircularRoadSwingng.html#CircularRoadSwing">CircularRoadSwing app = new CircularRoadSwing("Circular Road", animationPanel, otsModel);
275 app.setExitOnClose(exitOnClose);
276
277
278
279
280
281
282
283
284
285 }
286 else
287 {
288 if (exitOnClose)
289 {
290 System.exit(0);
291 }
292 }
293 }
294 catch (SimRuntimeException | NamingException | RemoteException | OTSDrawingException | DSOLException exception)
295 {
296 exception.printStackTrace();
297 }
298 }
299
300
301
302
303
304 protected final void addStatisticsTabs(final OTSSimulatorInterface simulator)
305 {
306 GraphPath<KpiLaneDirection> path01;
307 GraphPath<KpiLaneDirection> path0;
308 GraphPath<KpiLaneDirection> path1;
309 try
310 {
311 List<String> names = new ArrayList<>();
312 names.add("Left lane");
313 names.add("Right lane");
314 List<LaneDirection> start = new ArrayList<>();
315 start.add(new LaneDirection(getModel().getPath(0).get(0), GTUDirectionality.DIR_PLUS));
316 start.add(new LaneDirection(getModel().getPath(1).get(0), GTUDirectionality.DIR_PLUS));
317 path01 = GraphLaneUtil.createPath(names, start);
318 path0 = GraphLaneUtil.createPath(names.get(0), start.get(0));
319 path1 = GraphLaneUtil.createPath(names.get(1), start.get(1));
320 }
321 catch (NetworkException exception)
322 {
323 throw new RuntimeException("Could not create a path as a lane has no set speed limit.", exception);
324 }
325 RoadSampler sampler = new RoadSampler(getModel().getNetwork());
326 GraphPath.initRecording(sampler, path01);
327 GraphPath.initRecording(sampler, path0);
328 GraphPath.initRecording(sampler, path1);
329 ContourDataSource<?> dataPool0 = new ContourDataSource<>(sampler.getSamplerData(), path0);
330 ContourDataSource<?> dataPool1 = new ContourDataSource<>(sampler.getSamplerData(), path1);
331 Duration updateInterval = Duration.instantiateSI(10.0);
332
333 SwingPlot plot = null;
334 GraphPath<KpiLaneDirection> path = null;
335 ContourDataSource<?> dataPool = null;
336
337 TablePanel trajectoryChart = new TablePanel(2, 2);
338 plot = new SwingTrajectoryPlot(
339 new TrajectoryPlot("Trajectory all lanes", updateInterval, simulator, sampler.getSamplerData(), path01));
340 trajectoryChart.setCell(plot.getContentPane(), 0, 0);
341
342 List<KpiLaneDirection> lanes = new ArrayList<>();
343 List<Length> positions = new ArrayList<>();
344 lanes.add(path01.get(0).getSource(0));
345 lanes.add(path1.get(0).getSource(0));
346 positions.add(Length.ZERO);
347 positions.add(Length.ZERO);
348 List<String> names = new ArrayList<>();
349 names.add("Left lane");
350 names.add("Right lane");
351 DirectedLinkPosition linkPosition =
352 new DirectedLinkPosition(getModel().getPath(0).get(0).getParentLink(), 0.0, GTUDirectionality.DIR_PLUS);
353 GraphCrossSection<KpiLaneDirection> crossSection;
354 try
355 {
356 crossSection = GraphLaneUtil.createCrossSection(names, linkPosition);
357 }
358 catch (NetworkException exception)
359 {
360 throw new RuntimeException(exception);
361 }
362
363 plot = new SwingFundamentalDiagram(new FundamentalDiagram("Fundamental diagram Density-Flow", Quantity.DENSITY,
364 Quantity.FLOW, simulator,
365 FundamentalDiagram.sourceFromSampler(sampler, crossSection, true, Duration.instantiateSI(60.0), false), null));
366 trajectoryChart.setCell(plot.getContentPane(), 1, 0);
367
368 plot = new SwingFundamentalDiagram(new FundamentalDiagram("Fundamental diagram Flow-Speed", Quantity.FLOW,
369 Quantity.SPEED, simulator,
370 FundamentalDiagram.sourceFromSampler(sampler, crossSection, false, Duration.instantiateSI(60.0), false), null));
371 trajectoryChart.setCell(plot.getContentPane(), 1, 1);
372
373 getAnimationPanel().getTabbedPane().addTab(getAnimationPanel().getTabbedPane().getTabCount(), "Trajectories",
374 trajectoryChart);
375
376 for (int lane : new int[] { 0, 1 })
377 {
378 TablePanel charts = new TablePanel(3, 2);
379 path = lane == 0 ? path0 : path1;
380 dataPool = lane == 0 ? dataPool0 : dataPool1;
381
382 plot = new SwingTrajectoryPlot(
383 new TrajectoryPlot("Trajectory lane " + lane, updateInterval, simulator, sampler.getSamplerData(), path));
384 charts.setCell(plot.getContentPane(), 0, 0);
385
386 plot = new SwingContourPlot(new ContourPlotDensity("Density lane " + lane, simulator, dataPool));
387 charts.setCell(plot.getContentPane(), 1, 0);
388
389 plot = new SwingContourPlot(new ContourPlotSpeed("Speed lane " + lane, simulator, dataPool));
390 charts.setCell(plot.getContentPane(), 1, 1);
391
392 plot = new SwingContourPlot(new ContourPlotFlow("Flow lane " + lane, simulator, dataPool));
393 charts.setCell(plot.getContentPane(), 2, 0);
394
395 plot = new SwingContourPlot(new ContourPlotAcceleration("Accceleration lane " + lane, simulator, dataPool));
396 charts.setCell(plot.getContentPane(), 2, 1);
397
398 getAnimationPanel().getTabbedPane().addTab(getAnimationPanel().getTabbedPane().getTabCount(), "stats lane " + lane,
399 charts);
400 }
401 }
402
403 }