View Javadoc
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.animation.GraphLaneUtil;
12  import org.opentrafficsim.core.dsol.OtsAnimator;
13  import org.opentrafficsim.core.dsol.OtsSimulatorInterface;
14  import org.opentrafficsim.core.network.NetworkException;
15  import org.opentrafficsim.core.perception.HistoryManagerDevs;
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.PlotScheduler;
23  import org.opentrafficsim.draw.graphs.TrajectoryPlot;
24  import org.opentrafficsim.road.network.sampling.LaneDataRoad;
25  import org.opentrafficsim.road.network.sampling.RoadSampler;
26  import org.opentrafficsim.swing.graphs.OtsPlotScheduler;
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   * Simplest contour plots demonstration.
40   * <p>
41   * Copyright (c) 2013-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
42   * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
43   * </p>
44   * @author <a href="https://github.com/peter-knoppers">Peter Knoppers</a>
45   */
46  public class StraightSwing extends OtsSimulationApplication<StraightModel> implements UNITS
47  {
48      /** */
49      private static final long serialVersionUID = 1L;
50  
51      /**
52       * Create a Straight Swing application.
53       * @param title the title of the Frame
54       * @param panel the tabbed panel to display
55       * @param model the model
56       */
57      public StraightSwing(final String title, final OtsAnimationPanel panel, final StraightModel model)
58      {
59          super(model, panel, DefaultsFactory.GTU_TYPE_MARKERS.toMap());
60      }
61  
62      @Override
63      protected void addTabs()
64      {
65          addStatisticsTabs(getModel().getSimulator());
66      }
67  
68      /**
69       * Main program.
70       * @param args the command line arguments (not used)
71       */
72      public static void main(final String[] args)
73      {
74          demo(true);
75      }
76  
77      /**
78       * Start the demo.
79       * @param exitOnClose when running stand-alone: true; when running as part of a demo: false
80       */
81      public static void demo(final boolean exitOnClose)
82      {
83          try
84          {
85              OtsAnimator simulator = new OtsAnimator("StraightSwing");
86              final StraightModel otsModel = new StraightModel(simulator);
87              if (TabbedParameterDialog.process(otsModel.getInputParameterMap()))
88              {
89                  simulator.initialize(Time.ZERO, Duration.ZERO, Duration.instantiateSI(1500.0), otsModel,
90                          HistoryManagerDevs.noHistory(simulator));
91                  OtsAnimationPanel animationPanel = new OtsAnimationPanel(otsModel.getNetwork().getExtent(),
92                          new Dimension(800, 600), simulator, otsModel, DEFAULT_GTU_COLORERS, otsModel.getNetwork());
93                  StraightSwing app = new StraightSwing("Straight", animationPanel, otsModel);
94                  app.setExitOnClose(exitOnClose);
95                  animationPanel.enableSimulationControlButtons();
96              }
97              else
98              {
99                  if (exitOnClose)
100                 {
101                     System.exit(0);
102                 }
103             }
104         }
105         catch (SimRuntimeException | NamingException | RemoteException | DsolException exception)
106         {
107             exception.printStackTrace();
108         }
109     }
110 
111     /**
112      * Add the statistics tabs.
113      * @param simulator the simulator on which sampling can be scheduled
114      */
115     protected final void addStatisticsTabs(final OtsSimulatorInterface simulator)
116     {
117         GraphPath<LaneDataRoad> path;
118         try
119         {
120             path = GraphLaneUtil.createPath("Lane", getModel().getPath().get(0));
121         }
122         catch (NetworkException exception)
123         {
124             throw new RuntimeException("Could not create a path as a lane has no set speed limit.", exception);
125         }
126 
127         RoadSampler sampler = new RoadSampler(getModel().getNetwork());
128         GraphPath.initRecording(sampler, path);
129         ContourDataSource dataPool = new ContourDataSource(sampler.getSamplerData(), path);
130         TablePanel charts = new TablePanel(3, 2);
131         SwingPlot plot = null;
132         PlotScheduler scheduler = new OtsPlotScheduler(simulator);
133 
134         plot = new SwingTrajectoryPlot(
135                 new TrajectoryPlot("TrajectoryPlot", Duration.instantiateSI(10.0), scheduler, sampler.getSamplerData(), path));
136         charts.setCell(plot.getContentPane(), 0, 0);
137 
138         plot = new SwingContourPlot(new ContourPlotDensity("DensityPlot", scheduler, dataPool));
139         charts.setCell(plot.getContentPane(), 1, 0);
140 
141         plot = new SwingContourPlot(new ContourPlotSpeed("SpeedPlot", scheduler, dataPool));
142         charts.setCell(plot.getContentPane(), 2, 0);
143 
144         plot = new SwingContourPlot(new ContourPlotFlow("FlowPlot", scheduler, dataPool));
145         charts.setCell(plot.getContentPane(), 1, 1);
146 
147         plot = new SwingContourPlot(new ContourPlotAcceleration("AccelerationPlot", scheduler, dataPool));
148         charts.setCell(plot.getContentPane(), 2, 1);
149 
150         getAnimationPanel().getTabbedPane().addTab(getAnimationPanel().getTabbedPane().getTabCount(), "statistics ", charts);
151     }
152 }