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.core.dsol.OtsAnimator;
12  import org.opentrafficsim.core.dsol.OtsSimulatorInterface;
13  import org.opentrafficsim.core.network.NetworkException;
14  import org.opentrafficsim.draw.core.OtsDrawingException;
15  import org.opentrafficsim.draw.graphs.ContourDataSource;
16  import org.opentrafficsim.draw.graphs.ContourPlotAcceleration;
17  import org.opentrafficsim.draw.graphs.ContourPlotDensity;
18  import org.opentrafficsim.draw.graphs.ContourPlotFlow;
19  import org.opentrafficsim.draw.graphs.ContourPlotSpeed;
20  import org.opentrafficsim.draw.graphs.GraphPath;
21  import org.opentrafficsim.draw.graphs.TrajectoryPlot;
22  import org.opentrafficsim.draw.graphs.road.GraphLaneUtil;
23  import org.opentrafficsim.road.network.sampling.LaneDataRoad;
24  import org.opentrafficsim.road.network.sampling.RoadSampler;
25  import org.opentrafficsim.swing.graphs.SwingContourPlot;
26  import org.opentrafficsim.swing.graphs.SwingPlot;
27  import org.opentrafficsim.swing.graphs.SwingTrajectoryPlot;
28  import org.opentrafficsim.swing.gui.OtsAnimationPanel;
29  import org.opentrafficsim.swing.gui.OtsSimulationApplication;
30  
31  import nl.tudelft.simulation.dsol.SimRuntimeException;
32  import nl.tudelft.simulation.dsol.swing.gui.TablePanel;
33  import nl.tudelft.simulation.dsol.swing.gui.inputparameters.TabbedParameterDialog;
34  import nl.tudelft.simulation.language.DSOLException;
35  
36  /**
37   * Simplest contour plots demonstration.
38   * <p>
39   * Copyright (c) 2013-2023 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
40   * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
41   * </p>
42   * @author <a href="https://tudelft.nl/staff/p.knoppers-1">Peter Knoppers</a>
43   */
44  public class StraightSwing extends OtsSimulationApplication<StraightModel> implements UNITS
45  {
46      /** */
47      private static final long serialVersionUID = 1L;
48  
49      /**
50       * Create a Straight Swing application.
51       * @param title String; the title of the Frame
52       * @param panel OtsAnimationPanel; the tabbed panel to display
53       * @param model StraightModel; the model
54       * @throws OtsDrawingException on animation error
55       */
56      public StraightSwing(final String title, final OtsAnimationPanel panel, final StraightModel model)
57              throws OtsDrawingException
58      {
59          super(model, panel);
60      }
61  
62      /** {@inheritDoc} */
63      @Override
64      protected void addTabs()
65      {
66          addStatisticsTabs(getModel().getSimulator());
67      }
68  
69      /**
70       * Main program.
71       * @param args String[]; the command line arguments (not used)
72       */
73      public static void main(final String[] args)
74      {
75          demo(true);
76      }
77  
78      /**
79       * Start the demo.
80       * @param exitOnClose boolean; when running stand-alone: true; when running as part of a demo: false
81       */
82      public static void demo(final boolean exitOnClose)
83      {
84          try
85          {
86              OtsAnimator simulator = new OtsAnimator("StraightSwing");
87              final StraightModel otsModel = new StraightModel(simulator);
88              if (TabbedParameterDialog.process(otsModel.getInputParameterMap()))
89              {
90                  simulator.initialize(Time.ZERO, Duration.ZERO, Duration.instantiateSI(1500.0), otsModel);
91                  OtsAnimationPanel animationPanel = new OtsAnimationPanel(otsModel.getNetwork().getExtent(),
92                          new Dimension(800, 600), simulator, otsModel, DEFAULT_COLORER, 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 | OtsDrawingException | DSOLException exception)
106         {
107             exception.printStackTrace();
108         }
109     }
110 
111     /**
112      * Add the statistics tabs.
113      * @param simulator OtsSimulatorInterface; 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 
133         plot = new SwingTrajectoryPlot(
134                 new TrajectoryPlot("TrajectoryPlot", Duration.instantiateSI(10.0), simulator, sampler.getSamplerData(), path));
135         charts.setCell(plot.getContentPane(), 0, 0);
136 
137         plot = new SwingContourPlot(new ContourPlotDensity("DensityPlot", simulator, dataPool));
138         charts.setCell(plot.getContentPane(), 1, 0);
139 
140         plot = new SwingContourPlot(new ContourPlotSpeed("SpeedPlot", simulator, dataPool));
141         charts.setCell(plot.getContentPane(), 2, 0);
142 
143         plot = new SwingContourPlot(new ContourPlotFlow("FlowPlot", simulator, dataPool));
144         charts.setCell(plot.getContentPane(), 1, 1);
145 
146         plot = new SwingContourPlot(new ContourPlotAcceleration("AccelerationPlot", simulator, dataPool));
147         charts.setCell(plot.getContentPane(), 2, 1);
148 
149         getAnimationPanel().getTabbedPane().addTab(getAnimationPanel().getTabbedPane().getTabCount(), "statistics ", charts);
150     }
151 }