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