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