View Javadoc
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.djunits.value.vdouble.scalar.Time;
12  import org.opentrafficsim.animation.GraphLaneUtil;
13  import org.opentrafficsim.base.OtsRuntimeException;
14  import org.opentrafficsim.core.dsol.OtsAnimator;
15  import org.opentrafficsim.core.dsol.OtsSimulatorInterface;
16  import org.opentrafficsim.core.network.LinkPosition;
17  import org.opentrafficsim.core.network.NetworkException;
18  import org.opentrafficsim.core.perception.HistoryManagerDevs;
19  import org.opentrafficsim.draw.graphs.ContourDataSource;
20  import org.opentrafficsim.draw.graphs.ContourPlotAcceleration;
21  import org.opentrafficsim.draw.graphs.ContourPlotDensity;
22  import org.opentrafficsim.draw.graphs.ContourPlotFlow;
23  import org.opentrafficsim.draw.graphs.ContourPlotSpeed;
24  import org.opentrafficsim.draw.graphs.FundamentalDiagram;
25  import org.opentrafficsim.draw.graphs.FundamentalDiagram.Quantity;
26  import org.opentrafficsim.draw.graphs.GraphCrossSection;
27  import org.opentrafficsim.draw.graphs.GraphPath;
28  import org.opentrafficsim.draw.graphs.PlotScheduler;
29  import org.opentrafficsim.draw.graphs.TrajectoryPlot;
30  import org.opentrafficsim.kpi.interfaces.LaneData;
31  import org.opentrafficsim.road.network.RoadNetwork;
32  import org.opentrafficsim.road.network.lane.Lane;
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.OtsAnimationPanel;
41  import org.opentrafficsim.swing.gui.OtsSimulationApplication;
42  
43  import nl.tudelft.simulation.dsol.SimRuntimeException;
44  import nl.tudelft.simulation.dsol.swing.gui.TablePanel;
45  import nl.tudelft.simulation.dsol.swing.gui.inputparameters.TabbedParameterDialog;
46  import nl.tudelft.simulation.language.DsolException;
47  
48  /**
49   * Circular road simulation demo.
50   * <p>
51   * Copyright (c) 2013-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
52   * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
53   * </p>
54   * @author <a href="https://github.com/peter-knoppers">Peter Knoppers</a>
55   */
56  public class CircularRoadSwing extends OtsSimulationApplication<CircularRoadModel>
57  {
58      /** */
59      private static final long serialVersionUID = 1L;
60  
61      /**
62       * Create a CircularRoad Swing application.
63       * @param title the title of the Frame
64       * @param panel the tabbed panel to display
65       * @param model the model
66       */
67      public CircularRoadSwing(final String title, final OtsAnimationPanel panel, final CircularRoadModel model)
68      {
69          super(model, panel, DefaultsFactory.GTU_TYPE_MARKERS.toMap());
70  
71          // NetworkAnimation networkAnimation = new NetworkAnimation(model.getNetwork());
72          // networkAnimation.addDrawingInfoClass(Lane.class, new DrawingInfoShape<>(Color.GRAY));
73          RoadNetwork network = model.getNetwork();
74      }
75  
76      @Override
77      protected void addTabs()
78      {
79          addStatisticsTabs(getModel().getSimulator());
80      }
81  
82      /**
83       * Main program.
84       * @param args the command line arguments (not used)
85       */
86      public static void main(final String[] args)
87      {
88          // simulatorDemo();
89          demo(true);
90      }
91  
92      /**
93       * Start the demo.
94       * @param exitOnClose when running stand-alone: true; when running as part of a demo: false
95       */
96      public static void demo(final boolean exitOnClose)
97      {
98          try
99          {
100             OtsAnimator simulator = new OtsAnimator("CircularRoadSwing");
101             final CircularRoadModel otsModel = new CircularRoadModel(simulator);
102             if (TabbedParameterDialog.process(otsModel.getInputParameterMap()))
103             {
104                 simulator.initialize(Time.ZERO, Duration.ZERO, Duration.ofSI(3600.0), otsModel,
105                         HistoryManagerDevs.noHistory(simulator));
106                 OtsAnimationPanel animationPanel = new OtsAnimationPanel(otsModel.getNetwork().getExtent(), simulator, otsModel,
107                         DEFAULT_GTU_COLORERS, otsModel.getNetwork());
108                 CircularRoadSwing app = new CircularRoadSwing("Circular Road", animationPanel, otsModel);
109                 app.setExitOnClose(exitOnClose);
110                 animationPanel.enableSimulationControlButtons();
111             }
112             else
113             {
114                 if (exitOnClose)
115                 {
116                     System.exit(0);
117                 }
118             }
119         }
120         catch (SimRuntimeException | NamingException | RemoteException | DsolException exception)
121         {
122             exception.printStackTrace();
123         }
124     }
125 
126     /**
127      * Add the statistics tabs.
128      * @param simulator the simulator on which sampling can be scheduled
129      */
130     protected final void addStatisticsTabs(final OtsSimulatorInterface simulator)
131     {
132         GraphPath<LaneDataRoad> path01;
133         GraphPath<LaneDataRoad> path0;
134         GraphPath<LaneDataRoad> path1;
135         try
136         {
137             List<String> names = new ArrayList<>();
138             names.add("Left lane");
139             names.add("Right lane");
140             List<Lane> start = new ArrayList<>();
141             start.add(getModel().getPath(0).get(0));
142             start.add(getModel().getPath(1).get(0));
143             path01 = GraphLaneUtil.createPath(names, start).setCircular(true);
144             path0 = GraphLaneUtil.createPath(names.get(0), start.get(0)).setCircular(true);
145             path1 = GraphLaneUtil.createPath(names.get(1), start.get(1)).setCircular(true);
146         }
147         catch (NetworkException exception)
148         {
149             throw new OtsRuntimeException("Could not create a path as a lane has no set speed limit.", exception);
150         }
151         RoadSampler sampler = new RoadSampler(getModel().getNetwork());
152         GraphPath.initRecording(sampler, path01);
153         GraphPath.initRecording(sampler, path0);
154         GraphPath.initRecording(sampler, path1);
155         ContourDataSource dataPool0 = new ContourDataSource(sampler.getSamplerData(), path0);
156         ContourDataSource dataPool1 = new ContourDataSource(sampler.getSamplerData(), path1);
157         Duration updateInterval = Duration.ofSI(10.0);
158 
159         SwingPlot plot = null;
160         GraphPath<LaneDataRoad> path = null;
161         ContourDataSource dataPool = null;
162 
163         TablePanel trajectoryChart = new TablePanel(2, 2);
164         PlotScheduler scheduler = new OtsPlotScheduler(simulator);
165         plot = new SwingTrajectoryPlot(
166                 new TrajectoryPlot("Trajectory all lanes", updateInterval, scheduler, sampler.getSamplerData(), path01));
167         trajectoryChart.setCell(plot.getContentPane(), 0, 0);
168 
169         List<LaneData> lanes = new ArrayList<>();
170         List<Length> positions = new ArrayList<>();
171         lanes.add(path01.get(0).getSource(0));
172         lanes.add(path1.get(0).getSource(0));
173         positions.add(Length.ZERO);
174         positions.add(Length.ZERO);
175         List<String> names = new ArrayList<>();
176         names.add("Left lane");
177         names.add("Right lane");
178         LinkPosition linkPosition = new LinkPosition(getModel().getPath(0).get(0).getLink(), 0.0);
179         GraphCrossSection<LaneDataRoad> crossSection;
180         try
181         {
182             crossSection = GraphLaneUtil.createCrossSection(names, linkPosition);
183         }
184         catch (NetworkException exception)
185         {
186             throw new OtsRuntimeException(exception);
187         }
188 
189         plot = new SwingFundamentalDiagram(
190                 new FundamentalDiagram("Fundamental diagram Density-Flow", Quantity.DENSITY, Quantity.FLOW, scheduler,
191                         FundamentalDiagram.sourceFromSampler(sampler, crossSection, true, Duration.ofSI(60.0), false), null));
192         trajectoryChart.setCell(plot.getContentPane(), 1, 0);
193 
194         plot = new SwingFundamentalDiagram(
195                 new FundamentalDiagram("Fundamental diagram Flow-Speed", Quantity.FLOW, Quantity.SPEED, scheduler,
196                         FundamentalDiagram.sourceFromSampler(sampler, crossSection, false, Duration.ofSI(60.0), false), null));
197         trajectoryChart.setCell(plot.getContentPane(), 1, 1);
198 
199         getAnimationPanel().getTabbedPane().addTab(getAnimationPanel().getTabbedPane().getTabCount(), "Trajectories",
200                 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             dataPool = lane == 0 ? dataPool0 : dataPool1;
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, scheduler, dataPool));
213             charts.setCell(plot.getContentPane(), 1, 0);
214 
215             plot = new SwingContourPlot(new ContourPlotSpeed("Speed lane " + lane, scheduler, dataPool));
216             charts.setCell(plot.getContentPane(), 1, 1);
217 
218             plot = new SwingContourPlot(new ContourPlotFlow("Flow lane " + lane, scheduler, dataPool));
219             charts.setCell(plot.getContentPane(), 2, 0);
220 
221             plot = new SwingContourPlot(new ContourPlotAcceleration("Accceleration lane " + lane, scheduler, dataPool));
222             charts.setCell(plot.getContentPane(), 2, 1);
223 
224             getAnimationPanel().getTabbedPane().addTab(getAnimationPanel().getTabbedPane().getTabCount(), "stats lane " + lane,
225                     charts);
226         }
227     }
228 
229 }