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