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.gtu.GTUDirectionality;
14  import org.opentrafficsim.core.network.NetworkException;
15  import org.opentrafficsim.draw.core.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.TrajectoryPlot;
23  import org.opentrafficsim.draw.graphs.road.GraphLaneUtil;
24  import org.opentrafficsim.kpi.sampling.KpiLaneDirection;
25  import org.opentrafficsim.road.network.OTSRoadNetwork;
26  import org.opentrafficsim.road.network.lane.LaneDirection;
27  import org.opentrafficsim.road.network.sampling.RoadSampler;
28  import org.opentrafficsim.swing.graphs.SwingContourPlot;
29  import org.opentrafficsim.swing.graphs.SwingPlot;
30  import org.opentrafficsim.swing.graphs.SwingTrajectoryPlot;
31  import org.opentrafficsim.swing.gui.OTSAnimationPanel;
32  import org.opentrafficsim.swing.gui.OTSSimulationApplication;
33  
34  import nl.tudelft.simulation.dsol.SimRuntimeException;
35  import nl.tudelft.simulation.dsol.swing.gui.TablePanel;
36  import nl.tudelft.simulation.dsol.swing.gui.inputparameters.TabbedParameterDialog;
37  
38  /**
39   * Simplest contour plots demonstration.
40   * <p>
41   * Copyright (c) 2013-2020 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
42   * BSD-style license. See <a href="http://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
43   * <p>
44   * $LastChangedDate: 2019-01-06 01:35:05 +0100 (Sun, 06 Jan 2019) $, @version $Revision: 4831 $, by $Author: averbraeck $,
45   * initial version 12 nov. 2014 <br>
46   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
47   */
48  public class StraightSwing extends OTSSimulationApplication<StraightModel> implements UNITS
49  {
50      /** */
51      private static final long serialVersionUID = 1L;
52  
53      /**
54       * Create a Straight Swing application.
55       * @param title String; the title of the Frame
56       * @param panel OTSAnimationPanel; the tabbed panel to display
57       * @param model StraightModel; the model
58       * @throws OTSDrawingException on animation error
59       */
60      public StraightSwing(final String title, final OTSAnimationPanel panel, final StraightModel model)
61              throws OTSDrawingException
62      {
63          super(model, panel);
64          OTSRoadNetwork network = model.getNetwork();
65          System.out.println(network.getLinkMap());
66      }
67  
68      /** {@inheritDoc} */
69      @Override
70      protected void addTabs()
71      {
72          addStatisticsTabs(getModel().getSimulator());
73      }
74  
75      /**
76       * Main program.
77       * @param args String[]; the command line arguments (not used)
78       */
79      public static void main(final String[] args)
80      {
81          demo(true);
82      }
83  
84      /**
85       * Start the demo.
86       * @param exitOnClose boolean; when running stand-alone: true; when running as part of a demo: false
87       */
88      public static void demo(final boolean exitOnClose)
89      {
90          try
91          {
92              OTSAnimator simulator = new OTSAnimator();
93              final StraightModelml#StraightModel">StraightModel otsModel = new StraightModel(simulator);
94              if (TabbedParameterDialog.process(otsModel.getInputParameterMap()))
95              {
96                  simulator.initialize(Time.ZERO, Duration.ZERO, Duration.instantiateSI(1500.0), otsModel);
97                  OTSAnimationPanel animationPanel = new OTSAnimationPanel(otsModel.getNetwork().getExtent(),
98                          new Dimension(800, 600), simulator, otsModel, DEFAULT_COLORER, otsModel.getNetwork());
99                  StraightSwingng.html#StraightSwing">StraightSwing app = new StraightSwing("Straight", animationPanel, otsModel);
100                 app.setExitOnClose(exitOnClose);
101             }
102             else
103             {
104                 if (exitOnClose)
105                 {
106                     System.exit(0);
107                 }
108             }
109         }
110         catch (SimRuntimeException | NamingException | RemoteException | OTSDrawingException exception)
111         {
112             exception.printStackTrace();
113         }
114     }
115 
116     /**
117      * Add the statistics tabs.
118      * @param simulator OTSSimulatorInterface; the simulator on which sampling can be scheduled
119      */
120     protected final void addStatisticsTabs(final OTSSimulatorInterface simulator)
121     {
122         GraphPath<KpiLaneDirection> path;
123         try
124         {
125             path = GraphLaneUtil.createPath("Lane", new LaneDirection(getModel().getPath().get(0), GTUDirectionality.DIR_PLUS));
126         }
127         catch (NetworkException exception)
128         {
129             throw new RuntimeException("Could not create a path as a lane has no set speed limit.", exception);
130         }
131 
132         RoadSampler sampler = new RoadSampler(simulator);
133         ContourDataSource<?> dataPool = new ContourDataSource<>(sampler, path);
134         TablePanel charts = new TablePanel(3, 2);
135         SwingPlot plot = null;
136 
137         plot = new SwingTrajectoryPlot(new TrajectoryPlot("TrajectoryPlot", Duration.instantiateSI(10.0), simulator, sampler, path));
138         charts.setCell(plot.getContentPane(), 0, 0);
139 
140         plot = new SwingContourPlot(new ContourPlotDensity("DensityPlot", simulator, dataPool));
141         charts.setCell(plot.getContentPane(), 1, 0);
142 
143         plot = new SwingContourPlot(new ContourPlotSpeed("SpeedPlot", simulator, dataPool));
144         charts.setCell(plot.getContentPane(), 2, 0);
145 
146         plot = new SwingContourPlot(new ContourPlotFlow("FlowPlot", simulator, dataPool));
147         charts.setCell(plot.getContentPane(), 1, 1);
148 
149         plot = new SwingContourPlot(new ContourPlotAcceleration("AccelerationPlot", simulator, dataPool));
150         charts.setCell(plot.getContentPane(), 2, 1);
151 
152         getAnimationPanel().getTabbedPane().addTab(getAnimationPanel().getTabbedPane().getTabCount(), "statistics ", charts);
153     }
154 }