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.value.vdouble.scalar.Duration;
9   import org.djunits.value.vdouble.scalar.Time;
10  import org.opentrafficsim.core.dsol.OTSAnimator;
11  import org.opentrafficsim.core.dsol.OTSSimulatorInterface;
12  import org.opentrafficsim.core.gtu.GTUDirectionality;
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.kpi.sampling.KpiLaneDirection;
24  import org.opentrafficsim.road.network.OTSRoadNetwork;
25  import org.opentrafficsim.road.network.lane.LaneDirection;
26  import org.opentrafficsim.road.network.sampling.RoadSampler;
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   * Circular lane simulation demo.
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: 2018-11-18 20:49:04 +0100 (Sun, 18 Nov 2018) $, @version $Revision: 4743 $, by $Author: averbraeck $,
45   * initial version 21 nov. 2014 <br>
46   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
47   */
48  public class CircularLaneSwing extends OTSSimulationApplication<CircularLaneModel>
49  {
50      /** */
51      private static final long serialVersionUID = 1L;
52  
53      /**
54       * Create a CircularLane Swing application.
55       * @param title String; the title of the Frame
56       * @param panel OTSAnimationPanel; the tabbed panel to display
57       * @param model CircularLaneModel; the model
58       * @throws OTSDrawingException on animation error
59       */
60      public CircularLaneSwing(final String title, final OTSAnimationPanel panel, final CircularLaneModel model)
61              throws OTSDrawingException
62      {
63          super(model, panel);
64          OTSRoadNetwork network = model.getNetwork();
65          System.out.println(network.getLinkMap());
66  
67      }
68  
69      /** {@inheritDoc} */
70      @Override
71      protected void addTabs()
72      {
73          addStatisticsTabs(getModel().getSimulator());
74      }
75  
76      /**
77       * Main program.
78       * @param args String[]; the command line arguments (not used)
79       */
80      public static void main(final String[] args)
81      {
82          demo(true);
83      }
84  
85      /**
86       * Start the demo.
87       * @param exitOnClose boolean; when running stand-alone: true; when running as part of a demo: false
88       */
89      public static void demo(final boolean exitOnClose)
90      {
91          try
92          {
93              OTSAnimator simulator = new OTSAnimator("CircularLaneSwing");
94              final CircularLaneModelml#CircularLaneModel">CircularLaneModel otsModel = new CircularLaneModel(simulator);
95              if (TabbedParameterDialog.process(otsModel.getInputParameterMap()))
96              {
97                  simulator.initialize(Time.ZERO, Duration.ZERO, Duration.instantiateSI(3600.0), otsModel);
98                  OTSAnimationPanel animationPanel = new OTSAnimationPanel(otsModel.getNetwork().getExtent(),
99                          new Dimension(800, 600), simulator, otsModel, DEFAULT_COLORER, otsModel.getNetwork());
100                 CircularLaneSwingng.html#CircularLaneSwing">CircularLaneSwing app = new CircularLaneSwing("Circular Lane", animationPanel, otsModel);
101                 app.setExitOnClose(exitOnClose);
102                 animationPanel.enableSimulationControlButtons();
103             }
104             else
105             {
106                 if (exitOnClose)
107                 {
108                     System.exit(0);
109                 }
110             }
111         }
112         catch (SimRuntimeException | NamingException | RemoteException | OTSDrawingException | DSOLException exception)
113         {
114             exception.printStackTrace();
115         }
116     }
117 
118     /**
119      * Add the statistics tabs.
120      * @param simulator OTSSimulatorInterface; the simulator on which sampling can be scheduled
121      */
122     protected final void addStatisticsTabs(final OTSSimulatorInterface simulator)
123     {
124         GraphPath<KpiLaneDirection> path;
125         try
126         {
127             path = GraphLaneUtil.createPath("Lane", new LaneDirection(getModel().getPath().get(0), GTUDirectionality.DIR_PLUS));
128         }
129         catch (NetworkException exception)
130         {
131             throw new RuntimeException("Could not create a path as a lane has no set speed limit.", exception);
132         }
133 
134         RoadSampler sampler = new RoadSampler(getModel().getNetwork());
135         GraphPath.initRecording(sampler, path);
136         ContourDataSource<?> dataPool = new ContourDataSource<>(sampler.getSamplerData(), path);
137         TablePanel charts = new TablePanel(3, 2);
138         SwingPlot plot = null;
139 
140         plot = new SwingTrajectoryPlot(
141                 new TrajectoryPlot("TrajectoryPlot", Duration.instantiateSI(10.0), simulator, sampler.getSamplerData(), path));
142         charts.setCell(plot.getContentPane(), 0, 0);
143 
144         plot = new SwingContourPlot(new ContourPlotDensity("DensityPlot", simulator, dataPool));
145         charts.setCell(plot.getContentPane(), 1, 0);
146 
147         plot = new SwingContourPlot(new ContourPlotSpeed("SpeedPlot", simulator, dataPool));
148         charts.setCell(plot.getContentPane(), 2, 0);
149 
150         plot = new SwingContourPlot(new ContourPlotFlow("FlowPlot", simulator, dataPool));
151         charts.setCell(plot.getContentPane(), 1, 1);
152 
153         plot = new SwingContourPlot(new ContourPlotAcceleration("AccelerationPlot", simulator, dataPool));
154         charts.setCell(plot.getContentPane(), 2, 1);
155 
156         getAnimationPanel().getTabbedPane().addTab(getAnimationPanel().getTabbedPane().getTabCount(), "statistics ", charts);
157     }
158 
159 }