View Javadoc
1   package org.opentrafficsim.demo;
2   
3   import java.rmi.RemoteException;
4   import java.util.List;
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.opentrafficsim.animation.data.util.GraphLaneUtil;
11  import org.opentrafficsim.animation.graphs.ContourDataSource;
12  import org.opentrafficsim.animation.graphs.ContourPlotAcceleration;
13  import org.opentrafficsim.animation.graphs.ContourPlotDensity;
14  import org.opentrafficsim.animation.graphs.ContourPlotFlow;
15  import org.opentrafficsim.animation.graphs.ContourPlotSpeed;
16  import org.opentrafficsim.animation.graphs.GraphPath;
17  import org.opentrafficsim.animation.graphs.PlotScheduler;
18  import org.opentrafficsim.animation.graphs.TrajectoryPlot;
19  import org.opentrafficsim.base.OtsRuntimeException;
20  import org.opentrafficsim.core.dsol.OtsAnimator;
21  import org.opentrafficsim.core.network.Network;
22  import org.opentrafficsim.core.network.NetworkException;
23  import org.opentrafficsim.core.perception.HistoryManagerDevs;
24  import org.opentrafficsim.road.network.Lane;
25  import org.opentrafficsim.road.network.RoadNetwork;
26  import org.opentrafficsim.road.network.sampling.LaneDataRoad;
27  import org.opentrafficsim.road.network.sampling.RoadSampler;
28  import org.opentrafficsim.swing.graphs.OtsPlotScheduler;
29  import org.opentrafficsim.swing.graphs.SwingContourPlot;
30  import org.opentrafficsim.swing.graphs.SwingPlot;
31  import org.opentrafficsim.swing.graphs.SwingTrajectoryPlot;
32  import org.opentrafficsim.swing.gui.OtsSimulationApplication;
33  import org.opentrafficsim.swing.gui.OtsSimulationPanel;
34  import org.opentrafficsim.swing.gui.OtsSimulationPanelDecorator;
35  
36  import nl.tudelft.simulation.dsol.SimRuntimeException;
37  import nl.tudelft.simulation.dsol.swing.gui.TablePanel;
38  import nl.tudelft.simulation.dsol.swing.gui.inputparameters.TabbedParameterDialog;
39  import nl.tudelft.simulation.language.DsolException;
40  
41  /**
42   * Simplest contour plots demonstration.
43   * <p>
44   * Copyright (c) 2013-2026 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
45   * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
46   * </p>
47   * @author Peter Knoppers
48   */
49  public class StraightSwing extends OtsSimulationApplication<StraightModel> implements UNITS
50  {
51      /** Serialization version UID. */
52      private static final long serialVersionUID = 1L;
53  
54      /**
55       * Create a Straight Swing application.
56       * @param title the title of the Frame
57       * @param panel the tabbed panel to display
58       * @param model the model
59       */
60      public StraightSwing(final String title, final OtsSimulationPanel panel, final StraightModel model)
61      {
62          super(model, panel);
63      }
64  
65      /**
66       * Main program.
67       * @param args the command line arguments (not used)
68       */
69      public static void main(final String[] args)
70      {
71          demo(true);
72      }
73  
74      /**
75       * Start the demo.
76       * @param exitOnClose when running stand-alone: true; when running as part of a demo: false
77       */
78      public static void demo(final boolean exitOnClose)
79      {
80          try
81          {
82              OtsAnimator simulator = new OtsAnimator("StraightSwing");
83              final StraightModel otsModel = new StraightModel(simulator);
84              if (TabbedParameterDialog.process(otsModel.getInputParameterMap()))
85              {
86                  simulator.initialize(Duration.ZERO, Duration.ZERO, Duration.ofSI(1500.0), otsModel,
87                          HistoryManagerDevs.noHistory(simulator));
88                  OtsSimulationPanel simulationPanel =
89                          new OtsSimulationPanel(otsModel.getNetwork(), new OtsSimulationPanelDecorator()
90                          {
91                              @Override
92                              public void addTabs(final OtsSimulationPanel simulationPanel, final Network network)
93                              {
94                                  addStatisticsTabs(simulationPanel, (RoadNetwork) network, otsModel.getPath());
95                              }
96                          });
97                  StraightSwing app = new StraightSwing("Straight", simulationPanel, otsModel);
98                  app.setExitOnClose(exitOnClose);
99                  simulationPanel.enableSimulationControlButtons();
100             }
101             else
102             {
103                 if (exitOnClose)
104                 {
105                     System.exit(0);
106                 }
107             }
108         }
109         catch (SimRuntimeException | NamingException | RemoteException | DsolException exception)
110         {
111             exception.printStackTrace();
112         }
113     }
114 
115     /**
116      * Add the statistics tabs.
117      * @param simulation simulation panel
118      * @param network network
119      * @param lanePath path
120      */
121     private static void addStatisticsTabs(final OtsSimulationPanel simulation, final RoadNetwork network,
122             final List<Lane> lanePath)
123     {
124         GraphPath<LaneDataRoad> path;
125         try
126         {
127             path = GraphLaneUtil.createPath("Lane", lanePath.get(0));
128         }
129         catch (NetworkException exception)
130         {
131             throw new OtsRuntimeException("Could not create a path as a lane has no set speed limit.", exception);
132         }
133 
134         RoadSampler sampler = new RoadSampler(network);
135         GraphPath.initRecording(sampler, path);
136         PlotScheduler scheduler = new OtsPlotScheduler(network.getSimulator());
137         ContourDataSource source = new ContourDataSource(sampler.getSamplerData(), path, scheduler);
138         TablePanel charts = new TablePanel(3, 2);
139         SwingPlot plot = null;
140 
141         plot = new SwingTrajectoryPlot(
142                 new TrajectoryPlot("TrajectoryPlot", Duration.ofSI(10.0), scheduler, sampler.getSamplerData(), path));
143         charts.setCell(plot.getContentPane(), 0, 0);
144 
145         plot = new SwingContourPlot(new ContourPlotDensity("DensityPlot", source));
146         charts.setCell(plot.getContentPane(), 1, 0);
147 
148         plot = new SwingContourPlot(new ContourPlotSpeed("SpeedPlot", source));
149         charts.setCell(plot.getContentPane(), 2, 0);
150 
151         plot = new SwingContourPlot(new ContourPlotFlow("FlowPlot", source));
152         charts.setCell(plot.getContentPane(), 1, 1);
153 
154         plot = new SwingContourPlot(new ContourPlotAcceleration("AccelerationPlot", source));
155         charts.setCell(plot.getContentPane(), 2, 1);
156 
157         simulation.getTabbedPane().addTab(simulation.getTabbedPane().getTabCount(), "statistics ", charts);
158     }
159 }