1 package org.opentrafficsim.demo;
2
3 import java.awt.Dimension;
4 import java.rmi.RemoteException;
5
6 import javax.naming.NamingException;
7
8 import org.djunits.unit.util.UNITS;
9 import org.djunits.value.vdouble.scalar.Duration;
10 import org.djunits.value.vdouble.scalar.Time;
11 import org.opentrafficsim.core.dsol.OTSAnimator;
12 import org.opentrafficsim.core.dsol.OTSSimulatorInterface;
13 import org.opentrafficsim.core.gtu.GTUDirectionality;
14 import org.opentrafficsim.core.network.NetworkException;
15 import org.opentrafficsim.draw.core.OTSDrawingException;
16 import org.opentrafficsim.draw.graphs.ContourDataSource;
17 import org.opentrafficsim.draw.graphs.ContourPlotAcceleration;
18 import org.opentrafficsim.draw.graphs.ContourPlotDensity;
19 import org.opentrafficsim.draw.graphs.ContourPlotFlow;
20 import org.opentrafficsim.draw.graphs.ContourPlotSpeed;
21 import org.opentrafficsim.draw.graphs.GraphPath;
22 import org.opentrafficsim.draw.graphs.TrajectoryPlot;
23 import org.opentrafficsim.draw.graphs.road.GraphLaneUtil;
24 import org.opentrafficsim.kpi.sampling.KpiLaneDirection;
25 import org.opentrafficsim.road.network.lane.LaneDirection;
26 import org.opentrafficsim.road.network.sampling.RoadSampler;
27 import org.opentrafficsim.swing.graphs.SwingContourPlot;
28 import org.opentrafficsim.swing.graphs.SwingPlot;
29 import org.opentrafficsim.swing.graphs.SwingTrajectoryPlot;
30 import org.opentrafficsim.swing.gui.OTSAnimationPanel;
31 import org.opentrafficsim.swing.gui.OTSSimulationApplication;
32
33 import nl.tudelft.simulation.dsol.SimRuntimeException;
34 import nl.tudelft.simulation.dsol.swing.gui.TablePanel;
35 import nl.tudelft.simulation.dsol.swing.gui.inputparameters.TabbedParameterDialog;
36 import nl.tudelft.simulation.language.DSOLException;
37
38
39
40
41
42
43
44
45
46
47
48 public class StraightSwing extends OTSSimulationApplication<StraightModel> implements UNITS
49 {
50
51 private static final long serialVersionUID = 1L;
52
53
54
55
56
57
58
59
60 public StraightSwing(final String title, final OTSAnimationPanel panel, final StraightModel model)
61 throws OTSDrawingException
62 {
63 super(model, panel);
64 }
65
66
67 @Override
68 protected void addTabs()
69 {
70 addStatisticsTabs(getModel().getSimulator());
71 }
72
73
74
75
76
77 public static void main(final String[] args)
78 {
79 demo(true);
80 }
81
82
83
84
85
86 public static void demo(final boolean exitOnClose)
87 {
88 try
89 {
90 OTSAnimator simulator = new OTSAnimator("StraightSwing");
91 final StraightModelml#StraightModel">StraightModel otsModel = new StraightModel(simulator);
92 if (TabbedParameterDialog.process(otsModel.getInputParameterMap()))
93 {
94 simulator.initialize(Time.ZERO, Duration.ZERO, Duration.instantiateSI(1500.0), otsModel);
95 OTSAnimationPanel animationPanel = new OTSAnimationPanel(otsModel.getNetwork().getExtent(), new Dimension(800,
96 600), simulator, otsModel, DEFAULT_COLORER, otsModel.getNetwork());
97 StraightSwingng.html#StraightSwing">StraightSwing app = new StraightSwing("Straight", animationPanel, otsModel);
98 app.setExitOnClose(exitOnClose);
99 animationPanel.enableSimulationControlButtons();
100 }
101 else
102 {
103 if (exitOnClose)
104 {
105 System.exit(0);
106 }
107 }
108 }
109 catch (SimRuntimeException | NamingException | RemoteException | OTSDrawingException | DSOLException exception)
110 {
111 exception.printStackTrace();
112 }
113 }
114
115
116
117
118
119 protected final void addStatisticsTabs(final OTSSimulatorInterface simulator)
120 {
121 GraphPath<KpiLaneDirection> path;
122 try
123 {
124 path = GraphLaneUtil.createPath("Lane", new LaneDirection(getModel().getPath().get(0), GTUDirectionality.DIR_PLUS));
125 }
126 catch (NetworkException exception)
127 {
128 throw new RuntimeException("Could not create a path as a lane has no set speed limit.", exception);
129 }
130
131 RoadSampler sampler = new RoadSampler(getModel().getNetwork());
132 GraphPath.initRecording(sampler, path);
133 ContourDataSource<?> dataPool = new ContourDataSource<>(sampler.getSamplerData(), path);
134 TablePanel charts = new TablePanel(3, 2);
135 SwingPlot plot = null;
136
137 plot = new SwingTrajectoryPlot(new TrajectoryPlot("TrajectoryPlot", Duration.instantiateSI(10.0), simulator, sampler
138 .getSamplerData(), path));
139 charts.setCell(plot.getContentPane(), 0, 0);
140
141 plot = new SwingContourPlot(new ContourPlotDensity("DensityPlot", simulator, dataPool));
142 charts.setCell(plot.getContentPane(), 1, 0);
143
144 plot = new SwingContourPlot(new ContourPlotSpeed("SpeedPlot", simulator, dataPool));
145 charts.setCell(plot.getContentPane(), 2, 0);
146
147 plot = new SwingContourPlot(new ContourPlotFlow("FlowPlot", simulator, dataPool));
148 charts.setCell(plot.getContentPane(), 1, 1);
149
150 plot = new SwingContourPlot(new ContourPlotAcceleration("AccelerationPlot", simulator, dataPool));
151 charts.setCell(plot.getContentPane(), 2, 1);
152
153 getAnimationPanel().getTabbedPane().addTab(getAnimationPanel().getTabbedPane().getTabCount(), "statistics ", charts);
154 }
155 }