View Javadoc
1   package org.opentrafficsim.demo;
2   
3   import java.awt.Dimension;
4   import java.rmi.RemoteException;
5   import java.util.ArrayList;
6   import java.util.List;
7   
8   import javax.naming.NamingException;
9   
10  import org.djunits.value.vdouble.scalar.Duration;
11  import org.djunits.value.vdouble.scalar.Length;
12  import org.djunits.value.vdouble.scalar.Time;
13  import org.opentrafficsim.core.dsol.OTSAnimator;
14  import org.opentrafficsim.core.dsol.OTSSimulatorInterface;
15  import org.opentrafficsim.core.gtu.GTUDirectionality;
16  import org.opentrafficsim.core.network.DirectedLinkPosition;
17  import org.opentrafficsim.core.network.NetworkException;
18  import org.opentrafficsim.draw.core.OTSDrawingException;
19  import org.opentrafficsim.draw.graphs.ContourDataSource;
20  import org.opentrafficsim.draw.graphs.ContourPlotAcceleration;
21  import org.opentrafficsim.draw.graphs.ContourPlotDensity;
22  import org.opentrafficsim.draw.graphs.ContourPlotFlow;
23  import org.opentrafficsim.draw.graphs.ContourPlotSpeed;
24  import org.opentrafficsim.draw.graphs.FundamentalDiagram;
25  import org.opentrafficsim.draw.graphs.FundamentalDiagram.Quantity;
26  import org.opentrafficsim.draw.graphs.GraphCrossSection;
27  import org.opentrafficsim.draw.graphs.GraphPath;
28  import org.opentrafficsim.draw.graphs.TrajectoryPlot;
29  import org.opentrafficsim.draw.graphs.road.GraphLaneUtil;
30  import org.opentrafficsim.kpi.sampling.KpiLaneDirection;
31  import org.opentrafficsim.road.network.OTSRoadNetwork;
32  import org.opentrafficsim.road.network.lane.LaneDirection;
33  import org.opentrafficsim.road.network.sampling.RoadSampler;
34  import org.opentrafficsim.swing.graphs.SwingContourPlot;
35  import org.opentrafficsim.swing.graphs.SwingFundamentalDiagram;
36  import org.opentrafficsim.swing.graphs.SwingPlot;
37  import org.opentrafficsim.swing.graphs.SwingTrajectoryPlot;
38  import org.opentrafficsim.swing.gui.OTSAnimationPanel;
39  import org.opentrafficsim.swing.gui.OTSSimulationApplication;
40  
41  import nl.tudelft.simulation.dsol.SimRuntimeException;
42  import nl.tudelft.simulation.dsol.swing.gui.TablePanel;
43  import nl.tudelft.simulation.dsol.swing.gui.inputparameters.TabbedParameterDialog;
44  
45  /**
46   * Circular road simulation demo.
47   * <p>
48   * Copyright (c) 2013-2020 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
49   * BSD-style license. See <a href="http://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
50   * <p>
51   * $LastChangedDate: 2018-11-18 20:49:04 +0100 (Sun, 18 Nov 2018) $, @version $Revision: 4743 $, by $Author: averbraeck $,
52   * initial version 21 nov. 2014 <br>
53   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
54   */
55  public class CircularRoadSwing extends OTSSimulationApplication<CircularRoadModel>
56  {
57      /** */
58      private static final long serialVersionUID = 1L;
59  
60      /**
61       * Create a CircularRoad Swing application.
62       * @param title String; the title of the Frame
63       * @param panel OTSAnimationPanel; the tabbed panel to display
64       * @param model CircularRoadModel; the model
65       * @throws OTSDrawingException on animation error
66       */
67      public CircularRoadSwing(final String title, final OTSAnimationPanel panel, final CircularRoadModel model)
68              throws OTSDrawingException
69      {
70          super(model, panel);
71  
72          // NetworkAnimation networkAnimation = new NetworkAnimation(model.getNetwork());
73          // networkAnimation.addDrawingInfoClass(Lane.class, new DrawingInfoShape<>(Color.GRAY));
74          OTSRoadNetwork network = model.getNetwork();
75          System.out.println(network.getLinkMap());
76      }
77  
78      /** {@inheritDoc} */
79      @Override
80      protected void addTabs()
81      {
82          addStatisticsTabs(getModel().getSimulator());
83      }
84  
85      /**
86       * Main program.
87       * @param args String[]; the command line arguments (not used)
88       */
89      public static void main(final String[] args)
90      {
91          demo(true);
92      }
93  
94      /**
95       * Start the demo.
96       * @param exitOnClose boolean; when running stand-alone: true; when running as part of a demo: false
97       */
98      public static void demo(final boolean exitOnClose)
99      {
100         try
101         {
102             OTSAnimator simulator = new OTSAnimator();
103             final CircularRoadModelml#CircularRoadModel">CircularRoadModel otsModel = new CircularRoadModel(simulator);
104             if (TabbedParameterDialog.process(otsModel.getInputParameterMap()))
105             {
106                 simulator.initialize(Time.ZERO, Duration.ZERO, Duration.instantiateSI(3600.0), otsModel);
107                 OTSAnimationPanel animationPanel = new OTSAnimationPanel(otsModel.getNetwork().getExtent(),
108                         new Dimension(800, 600), simulator, otsModel, DEFAULT_COLORER, otsModel.getNetwork());
109                 CircularRoadSwingng.html#CircularRoadSwing">CircularRoadSwing app = new CircularRoadSwing("Circular Road", animationPanel, otsModel);
110                 app.setExitOnClose(exitOnClose);
111             }
112             else
113             {
114                 if (exitOnClose)
115                 {
116                     System.exit(0);
117                 }
118             }
119         }
120         catch (SimRuntimeException | NamingException | RemoteException | OTSDrawingException exception)
121         {
122             exception.printStackTrace();
123         }
124     }
125 
126     /**
127      * Add the statistics tabs.
128      * @param simulator OTSSimulatorInterface; the simulator on which sampling can be scheduled
129      */
130     protected final void addStatisticsTabs(final OTSSimulatorInterface simulator)
131     {
132         GraphPath<KpiLaneDirection> path01;
133         GraphPath<KpiLaneDirection> path0;
134         GraphPath<KpiLaneDirection> path1;
135         try
136         {
137             List<String> names = new ArrayList<>();
138             names.add("Left lane");
139             names.add("Right lane");
140             List<LaneDirection> start = new ArrayList<>();
141             start.add(new LaneDirection(getModel().getPath(0).get(0), GTUDirectionality.DIR_PLUS));
142             start.add(new LaneDirection(getModel().getPath(1).get(0), GTUDirectionality.DIR_PLUS));
143             path01 = GraphLaneUtil.createPath(names, start);
144             path0 = GraphLaneUtil.createPath(names.get(0), start.get(0));
145             path1 = GraphLaneUtil.createPath(names.get(1), start.get(1));
146         }
147         catch (NetworkException exception)
148         {
149             throw new RuntimeException("Could not create a path as a lane has no set speed limit.", exception);
150         }
151         RoadSampler sampler = new RoadSampler(simulator);
152         ContourDataSource<?> dataPool0 = new ContourDataSource<>(sampler, path0);
153         ContourDataSource<?> dataPool1 = new ContourDataSource<>(sampler, path1);
154         Duration updateInterval = Duration.instantiateSI(10.0);
155 
156         SwingPlot plot = null;
157         GraphPath<KpiLaneDirection> path = null;
158         ContourDataSource<?> dataPool = null;
159 
160         TablePanel trajectoryChart = new TablePanel(2, 2);
161         plot = new SwingTrajectoryPlot(new TrajectoryPlot("Trajectory all lanes", updateInterval, simulator, sampler, path01));
162         trajectoryChart.setCell(plot.getContentPane(), 0, 0);
163 
164         List<KpiLaneDirection> lanes = new ArrayList<>();
165         List<Length> positions = new ArrayList<>();
166         lanes.add(path01.get(0).getSource(0));
167         lanes.add(path1.get(0).getSource(0));
168         positions.add(Length.ZERO);
169         positions.add(Length.ZERO);
170         List<String> names = new ArrayList<>();
171         names.add("Left lane");
172         names.add("Right lane");
173         DirectedLinkPosition linkPosition =
174                 new DirectedLinkPosition(getModel().getPath(0).get(0).getParentLink(), 0.0, GTUDirectionality.DIR_PLUS);
175         GraphCrossSection<KpiLaneDirection> crossSection;
176         try
177         {
178             crossSection = GraphLaneUtil.createCrossSection(names, linkPosition);
179         }
180         catch (NetworkException exception)
181         {
182             throw new RuntimeException(exception);
183         }
184 
185         plot = new SwingFundamentalDiagram(new FundamentalDiagram("Fundamental diagram Density-Flow", Quantity.DENSITY,
186                 Quantity.FLOW, simulator, sampler, crossSection, true, Duration.instantiateSI(60.0), false));
187         trajectoryChart.setCell(plot.getContentPane(), 1, 0);
188 
189         plot = new SwingFundamentalDiagram(new FundamentalDiagram("Fundamental diagram Flow-Speed", Quantity.FLOW,
190                 Quantity.SPEED, simulator, sampler, crossSection, false, Duration.instantiateSI(60.0), false));
191         trajectoryChart.setCell(plot.getContentPane(), 1, 1);
192 
193         getAnimationPanel().getTabbedPane().addTab(getAnimationPanel().getTabbedPane().getTabCount(), "Trajectories",
194                 trajectoryChart);
195 
196         for (int lane : new int[] {0, 1})
197         {
198             TablePanel charts = new TablePanel(3, 2);
199             path = lane == 0 ? path0 : path1;
200             dataPool = lane == 0 ? dataPool0 : dataPool1;
201 
202             plot = new SwingTrajectoryPlot(
203                     new TrajectoryPlot("Trajectory lane " + lane, updateInterval, simulator, sampler, path));
204             charts.setCell(plot.getContentPane(), 0, 0);
205 
206             plot = new SwingContourPlot(new ContourPlotDensity("Density lane " + lane, simulator, dataPool));
207             charts.setCell(plot.getContentPane(), 1, 0);
208 
209             plot = new SwingContourPlot(new ContourPlotSpeed("Speed lane " + lane, simulator, dataPool));
210             charts.setCell(plot.getContentPane(), 1, 1);
211 
212             plot = new SwingContourPlot(new ContourPlotFlow("Flow lane " + lane, simulator, dataPool));
213             charts.setCell(plot.getContentPane(), 2, 0);
214 
215             plot = new SwingContourPlot(new ContourPlotAcceleration("Accceleration lane " + lane, simulator, dataPool));
216             charts.setCell(plot.getContentPane(), 2, 1);
217 
218             getAnimationPanel().getTabbedPane().addTab(getAnimationPanel().getTabbedPane().getTabCount(), "stats lane " + lane,
219                     charts);
220         }
221     }
222 
223 }