1 package org.opentrafficsim.demo;
2
3 import java.rmi.RemoteException;
4 import java.util.ArrayList;
5 import java.util.List;
6
7 import javax.naming.NamingException;
8
9 import org.djunits.value.vdouble.scalar.Duration;
10 import org.djunits.value.vdouble.scalar.Length;
11 import org.opentrafficsim.animation.data.util.GraphLaneUtil;
12 import org.opentrafficsim.animation.graphs.ContourDataSource;
13 import org.opentrafficsim.animation.graphs.ContourPlotAcceleration;
14 import org.opentrafficsim.animation.graphs.ContourPlotDensity;
15 import org.opentrafficsim.animation.graphs.ContourPlotFlow;
16 import org.opentrafficsim.animation.graphs.ContourPlotSpeed;
17 import org.opentrafficsim.animation.graphs.FdDataSource;
18 import org.opentrafficsim.animation.graphs.FundamentalDiagram;
19 import org.opentrafficsim.animation.graphs.GraphCrossSection;
20 import org.opentrafficsim.animation.graphs.GraphPath;
21 import org.opentrafficsim.animation.graphs.PlotScheduler;
22 import org.opentrafficsim.animation.graphs.TrajectoryPlot;
23 import org.opentrafficsim.animation.graphs.FundamentalDiagram.Quantity;
24 import org.opentrafficsim.base.OtsRuntimeException;
25 import org.opentrafficsim.core.dsol.OtsAnimator;
26 import org.opentrafficsim.core.network.LinkPosition;
27 import org.opentrafficsim.core.network.Network;
28 import org.opentrafficsim.core.network.NetworkException;
29 import org.opentrafficsim.core.perception.HistoryManagerDevs;
30 import org.opentrafficsim.kpi.interfaces.LaneData;
31 import org.opentrafficsim.road.network.Lane;
32 import org.opentrafficsim.road.network.RoadNetwork;
33 import org.opentrafficsim.road.network.sampling.LaneDataRoad;
34 import org.opentrafficsim.road.network.sampling.RoadSampler;
35 import org.opentrafficsim.swing.graphs.OtsPlotScheduler;
36 import org.opentrafficsim.swing.graphs.SwingContourPlot;
37 import org.opentrafficsim.swing.graphs.SwingFundamentalDiagram;
38 import org.opentrafficsim.swing.graphs.SwingPlot;
39 import org.opentrafficsim.swing.graphs.SwingTrajectoryPlot;
40 import org.opentrafficsim.swing.gui.OtsSimulationApplication;
41 import org.opentrafficsim.swing.gui.OtsSimulationPanel;
42 import org.opentrafficsim.swing.gui.OtsSimulationPanelDecorator;
43
44 import nl.tudelft.simulation.dsol.SimRuntimeException;
45 import nl.tudelft.simulation.dsol.swing.gui.TablePanel;
46 import nl.tudelft.simulation.dsol.swing.gui.inputparameters.TabbedParameterDialog;
47 import nl.tudelft.simulation.language.DsolException;
48
49
50
51
52
53
54
55
56
57 public class CircularRoadSwing extends OtsSimulationApplication<CircularRoadModel>
58 {
59
60 private static final long serialVersionUID = 1L;
61
62
63
64
65
66
67
68 public CircularRoadSwing(final String title, final OtsSimulationPanel panel, final CircularRoadModel model)
69 {
70 super(model, panel);
71 }
72
73
74
75
76
77 public static void main(final String[] args)
78 {
79
80 demo(true);
81 }
82
83
84
85
86
87 public static void demo(final boolean exitOnClose)
88 {
89 try
90 {
91 OtsAnimator simulator = new OtsAnimator("CircularRoadSwing");
92 final CircularRoadModel otsModel = new CircularRoadModel(simulator);
93 if (TabbedParameterDialog.process(otsModel.getInputParameterMap()))
94 {
95 simulator.initialize(Duration.ZERO, Duration.ZERO, Duration.ofSI(3600.0), otsModel,
96 HistoryManagerDevs.noHistory(simulator));
97 OtsSimulationPanel simulationPanel =
98 new OtsSimulationPanel(otsModel.getNetwork(), new OtsSimulationPanelDecorator()
99 {
100 @Override
101 public void addTabs(final OtsSimulationPanel simulationPanel, final Network network)
102 {
103 addStatisticsTabs(simulationPanel, network, otsModel.getPaths());
104 }
105 });
106 CircularRoadSwing app = new CircularRoadSwing("Circular Road", simulationPanel, otsModel);
107 app.setExitOnClose(exitOnClose);
108 simulationPanel.enableSimulationControlButtons();
109 }
110 else
111 {
112 if (exitOnClose)
113 {
114 System.exit(0);
115 }
116 }
117 }
118 catch (SimRuntimeException | NamingException | RemoteException | DsolException exception)
119 {
120 exception.printStackTrace();
121 }
122 }
123
124
125
126
127
128
129
130 private static void addStatisticsTabs(final OtsSimulationPanel simulationPanel, final Network network,
131 final List<List<Lane>> paths)
132 {
133 GraphPath<LaneDataRoad> path01;
134 GraphPath<LaneDataRoad> path0;
135 GraphPath<LaneDataRoad> path1;
136 try
137 {
138 List<String> names = new ArrayList<>();
139 names.add("Left lane");
140 names.add("Right lane");
141 List<Lane> start = new ArrayList<>();
142 start.add(paths.get(0).get(0));
143 start.add(paths.get(1).get(0));
144 path01 = GraphLaneUtil.createPath(names, start).setCircular(true);
145 path0 = GraphLaneUtil.createPath(names.get(0), start.get(0)).setCircular(true);
146 path1 = GraphLaneUtil.createPath(names.get(1), start.get(1)).setCircular(true);
147 }
148 catch (NetworkException exception)
149 {
150 throw new OtsRuntimeException("Could not create a path as a lane has no set speed limit.", exception);
151 }
152 RoadSampler sampler = new RoadSampler((RoadNetwork) network);
153 GraphPath.initRecording(sampler, path01);
154 GraphPath.initRecording(sampler, path0);
155 GraphPath.initRecording(sampler, path1);
156 PlotScheduler scheduler = new OtsPlotScheduler(network.getSimulator());
157 ContourDataSource source0 = new ContourDataSource(sampler.getSamplerData(), path0, scheduler);
158 ContourDataSource source1 = new ContourDataSource(sampler.getSamplerData(), path1, scheduler);
159 Duration updateInterval = Duration.ofSI(10.0);
160
161 SwingPlot plot = null;
162 GraphPath<LaneDataRoad> path = null;
163 ContourDataSource source = null;
164
165 TablePanel trajectoryChart = new TablePanel(2, 2);
166 plot = new SwingTrajectoryPlot(
167 new TrajectoryPlot("Trajectory all lanes", updateInterval, scheduler, sampler.getSamplerData(), path01));
168 trajectoryChart.setCell(plot.getContentPane(), 0, 0);
169
170 List<LaneData<?>> lanes = new ArrayList<>();
171 List<Length> positions = new ArrayList<>();
172 lanes.add(path01.get(0).getSource(0));
173 lanes.add(path1.get(0).getSource(0));
174 positions.add(Length.ZERO);
175 positions.add(Length.ZERO);
176 List<String> names = new ArrayList<>();
177 names.add("Left lane");
178 names.add("Right lane");
179 LinkPosition linkPosition = new LinkPosition(paths.get(0).get(0).getLink(), 0.0);
180 GraphCrossSection<LaneDataRoad> crossSection;
181 try
182 {
183 crossSection = GraphLaneUtil.createCrossSection(names, linkPosition);
184 }
185 catch (NetworkException exception)
186 {
187 throw new OtsRuntimeException(exception);
188 }
189
190 plot = new SwingFundamentalDiagram(new FundamentalDiagram("Fundamental diagram Density-Flow", Quantity.DENSITY,
191 Quantity.FLOW,
192 FdDataSource.sourceFromSampler(sampler, scheduler, crossSection, true, Duration.ofSI(60.0), false), null));
193 trajectoryChart.setCell(plot.getContentPane(), 1, 0);
194
195 plot = new SwingFundamentalDiagram(new FundamentalDiagram("Fundamental diagram Flow-Speed", Quantity.FLOW,
196 Quantity.SPEED,
197 FdDataSource.sourceFromSampler(sampler, scheduler, crossSection, false, Duration.ofSI(60.0), false), null));
198 trajectoryChart.setCell(plot.getContentPane(), 1, 1);
199
200 simulationPanel.getTabbedPane().addTab(simulationPanel.getTabbedPane().getTabCount(), "trajectories", trajectoryChart);
201
202 for (int lane : new int[] {0, 1})
203 {
204 TablePanel charts = new TablePanel(3, 2);
205 path = lane == 0 ? path0 : path1;
206 source = lane == 0 ? source0 : source1;
207
208 plot = new SwingTrajectoryPlot(
209 new TrajectoryPlot("Trajectory lane " + lane, updateInterval, scheduler, sampler.getSamplerData(), path));
210 charts.setCell(plot.getContentPane(), 0, 0);
211
212 plot = new SwingContourPlot(new ContourPlotDensity("Density lane " + lane, source));
213 charts.setCell(plot.getContentPane(), 1, 0);
214
215 plot = new SwingContourPlot(new ContourPlotSpeed("Speed lane " + lane, source));
216 charts.setCell(plot.getContentPane(), 1, 1);
217
218 plot = new SwingContourPlot(new ContourPlotFlow("Flow lane " + lane, source));
219 charts.setCell(plot.getContentPane(), 2, 0);
220
221 plot = new SwingContourPlot(new ContourPlotAcceleration("Accceleration lane " + lane, source));
222 charts.setCell(plot.getContentPane(), 2, 1);
223
224 simulationPanel.getTabbedPane().addTab(simulationPanel.getTabbedPane().getTabCount(), "stats lane " + lane, charts);
225 }
226 }
227
228 }