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.draw.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.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://tudelft.nl/staff/p.knoppers-1">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 String; the title of the Frame
54       * @param panel OtsAnimationPanel; the tabbed panel to display
55       * @param model StraightModel; the model
56       * @throws OtsDrawingException on animation error
57       */
58      public StraightSwing(final String title, final OtsAnimationPanel panel, final StraightModel model)
59              throws OtsDrawingException
60      {
61          super(model, panel);
62      }
63  
64      /** {@inheritDoc} */
65      @Override
66      protected void addTabs()
67      {
68          addStatisticsTabs(getModel().getSimulator());
69      }
70  
71      /**
72       * Main program.
73       * @param args String[]; the command line arguments (not used)
74       */
75      public static void main(final String[] args)
76      {
77          demo(true);
78      }
79  
80      /**
81       * Start the demo.
82       * @param exitOnClose boolean; when running stand-alone: true; when running as part of a demo: false
83       */
84      public static void demo(final boolean exitOnClose)
85      {
86          try
87          {
88              OtsAnimator simulator = new OtsAnimator("StraightSwing");
89              final StraightModel otsModel = new StraightModel(simulator);
90              if (TabbedParameterDialog.process(otsModel.getInputParameterMap()))
91              {
92                  simulator.initialize(Time.ZERO, Duration.ZERO, Duration.instantiateSI(1500.0), otsModel);
93                  OtsAnimationPanel animationPanel = new OtsAnimationPanel(otsModel.getNetwork().getExtent(),
94                          new Dimension(800, 600), simulator, otsModel, DEFAULT_COLORER, otsModel.getNetwork());
95                  StraightSwing app = new StraightSwing("Straight", animationPanel, otsModel);
96                  app.setExitOnClose(exitOnClose);
97                  animationPanel.enableSimulationControlButtons();
98              }
99              else
100             {
101                 if (exitOnClose)
102                 {
103                     System.exit(0);
104                 }
105             }
106         }
107         catch (SimRuntimeException | NamingException | RemoteException | OtsDrawingException | DsolException exception)
108         {
109             exception.printStackTrace();
110         }
111     }
112 
113     /**
114      * Add the statistics tabs.
115      * @param simulator OtsSimulatorInterface; the simulator on which sampling can be scheduled
116      */
117     protected final void addStatisticsTabs(final OtsSimulatorInterface simulator)
118     {
119         GraphPath<LaneDataRoad> path;
120         try
121         {
122             path = GraphLaneUtil.createPath("Lane", getModel().getPath().get(0));
123         }
124         catch (NetworkException exception)
125         {
126             throw new RuntimeException("Could not create a path as a lane has no set speed limit.", exception);
127         }
128 
129         RoadSampler sampler = new RoadSampler(getModel().getNetwork());
130         GraphPath.initRecording(sampler, path);
131         ContourDataSource dataPool = new ContourDataSource(sampler.getSamplerData(), path);
132         TablePanel charts = new TablePanel(3, 2);
133         SwingPlot plot = null;
134         PlotScheduler scheduler = new OtsPlotScheduler(simulator);
135         
136         plot = new SwingTrajectoryPlot(
137                 new TrajectoryPlot("TrajectoryPlot", Duration.instantiateSI(10.0), scheduler, sampler.getSamplerData(), path));
138         charts.setCell(plot.getContentPane(), 0, 0);
139 
140         plot = new SwingContourPlot(new ContourPlotDensity("DensityPlot", scheduler, dataPool));
141         charts.setCell(plot.getContentPane(), 1, 0);
142 
143         plot = new SwingContourPlot(new ContourPlotSpeed("SpeedPlot", scheduler, dataPool));
144         charts.setCell(plot.getContentPane(), 2, 0);
145 
146         plot = new SwingContourPlot(new ContourPlotFlow("FlowPlot", scheduler, dataPool));
147         charts.setCell(plot.getContentPane(), 1, 1);
148 
149         plot = new SwingContourPlot(new ContourPlotAcceleration("AccelerationPlot", scheduler, dataPool));
150         charts.setCell(plot.getContentPane(), 2, 1);
151 
152         getAnimationPanel().getTabbedPane().addTab(getAnimationPanel().getTabbedPane().getTabCount(), "statistics ", charts);
153     }
154 }