View Javadoc
1   package org.opentrafficsim.demo;
2   
3   import java.awt.Dimension;
4   import java.awt.event.ActionEvent;
5   import java.rmi.RemoteException;
6   import java.util.ArrayList;
7   import java.util.List;
8   
9   import javax.naming.NamingException;
10  import javax.swing.Box;
11  import javax.swing.ButtonGroup;
12  import javax.swing.JLabel;
13  import javax.swing.JOptionPane;
14  import javax.swing.JPanel;
15  import javax.swing.JRadioButton;
16  
17  import org.djunits.unit.util.UNITS;
18  import org.djunits.value.vdouble.scalar.Duration;
19  import org.djunits.value.vdouble.scalar.Time;
20  import org.opentrafficsim.core.dsol.OTSAnimator;
21  import org.opentrafficsim.core.dsol.OTSSimulatorInterface;
22  import org.opentrafficsim.core.gtu.GTUDirectionality;
23  import org.opentrafficsim.core.network.NetworkException;
24  import org.opentrafficsim.draw.core.OTSDrawingException;
25  import org.opentrafficsim.draw.graphs.GraphPath;
26  import org.opentrafficsim.draw.graphs.TrajectoryPlot;
27  import org.opentrafficsim.draw.graphs.road.GraphLaneUtil;
28  import org.opentrafficsim.kpi.sampling.KpiLaneDirection;
29  import org.opentrafficsim.road.network.OTSRoadNetwork;
30  import org.opentrafficsim.road.network.lane.LaneDirection;
31  import org.opentrafficsim.road.network.sampling.RoadSampler;
32  import org.opentrafficsim.swing.graphs.SwingPlot;
33  import org.opentrafficsim.swing.gui.OTSAnimationPanel;
34  import org.opentrafficsim.swing.gui.OTSSimulationApplication;
35  
36  import nl.tudelft.simulation.dsol.SimRuntimeException;
37  import nl.tudelft.simulation.dsol.model.inputparameters.InputParameter;
38  import nl.tudelft.simulation.dsol.model.inputparameters.InputParameterMap;
39  import nl.tudelft.simulation.dsol.model.inputparameters.InputParameterSelectionMap;
40  import nl.tudelft.simulation.dsol.swing.gui.TablePanel;
41  import nl.tudelft.simulation.dsol.swing.gui.inputparameters.AbstractInputField;
42  import nl.tudelft.simulation.dsol.swing.gui.inputparameters.InputField;
43  import nl.tudelft.simulation.dsol.swing.gui.inputparameters.TabbedParameterDialog;
44  import nl.tudelft.simulation.language.DSOLException;
45  
46  /**
47   * Simplest contour plots demonstration.
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: 2019-01-06 01:35:05 +0100 (Sun, 06 Jan 2019) $, @version $Revision: 4831 $, by $Author: averbraeck $,
53   * initial version 12 nov. 2014 <br>
54   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
55   */
56  public class NetworksSwing extends OTSSimulationApplication<NetworksModel> implements UNITS
57  {
58      /** */
59      private static final long serialVersionUID = 1L;
60  
61      /**
62       * Create a Networks Swing application.
63       * @param title String; the title of the Frame
64       * @param panel OTSAnimationPanel; the tabbed panel to display
65       * @param model NetworksModel; the model
66       * @throws OTSDrawingException on animation error
67       */
68      public NetworksSwing(final String title, final OTSAnimationPanel panel, final NetworksModel model)
69              throws OTSDrawingException
70      {
71          super(model, panel);
72          OTSRoadNetwork network = model.getNetwork();
73          System.out.println(network.getLinkMap());
74      }
75  
76      /** {@inheritDoc} */
77      @Override
78      protected void addTabs()
79      {
80          addStatisticsTabs(getModel().getSimulator());
81      }
82  
83      /**
84       * Main program.
85       * @param args String[]; the command line arguments (not used)
86       */
87      public static void main(final String[] args)
88      {
89          demo(true);
90      }
91  
92      /**
93       * Start the demo.
94       * @param exitOnClose boolean; when running stand-alone: true; when running as part of a demo: false
95       */
96      public static void demo(final boolean exitOnClose)
97      {
98          try
99          {
100             OTSAnimator simulator = new OTSAnimator("NetworksSwing");
101             final NetworksModelml#NetworksModel">NetworksModel otsModel = new NetworksModel(simulator);
102             if (NetworksParameterDialog.process(otsModel.getInputParameterMap()))
103             {
104                 simulator.initialize(Time.ZERO, Duration.ZERO, Duration.instantiateSI(3600.0), otsModel);
105                 OTSAnimationPanel animationPanel = new OTSAnimationPanel(otsModel.getNetwork().getExtent(),
106                         new Dimension(800, 600), simulator, otsModel, DEFAULT_COLORER, otsModel.getNetwork());
107                 NetworksSwingng.html#NetworksSwing">NetworksSwing app = new NetworksSwing("Networks", animationPanel, otsModel);
108                 app.setExitOnClose(exitOnClose);
109             }
110             else
111             {
112                 if (exitOnClose)
113                 {
114                     System.exit(0);
115                 }
116             }
117         }
118         catch (SimRuntimeException | NamingException | RemoteException | OTSDrawingException | DSOLException exception)
119         {
120             exception.printStackTrace();
121         }
122     }
123 
124     /**
125      * Add the statistics tabs.
126      * @param simulator OTSSimulatorInterface; the simulator on which sampling can be scheduled
127      */
128     protected final void addStatisticsTabs(final OTSSimulatorInterface simulator)
129     {
130         int graphCount = getModel().pathCount();
131         int columns = 1;
132         int rows = 0 == columns ? 0 : (int) Math.ceil(graphCount * 1.0 / columns);
133         TablePanel charts = new TablePanel(columns, rows);
134         RoadSampler sampler = new RoadSampler(simulator);
135         Duration updateInterval = Duration.instantiateSI(10.0);
136         for (int graphIndex = 0; graphIndex < graphCount; graphIndex++)
137         {
138             List<LaneDirection> start = new ArrayList<>();
139             start.add(new LaneDirection(getModel().getPath(graphIndex).get(0), GTUDirectionality.DIR_PLUS));
140             GraphPath<KpiLaneDirection> path;
141             try
142             {
143                 path = GraphLaneUtil.createPath("name", start.get(0));
144             }
145             catch (NetworkException exception)
146             {
147                 throw new RuntimeException(exception);
148             }
149             SwingPlot plot = new SwingPlot(
150                     new TrajectoryPlot("Trajectories on lane " + (graphIndex + 1), updateInterval, simulator, sampler, path));
151             charts.setCell(plot.getContentPane(), graphIndex % columns, graphIndex / columns);
152         }
153 
154         getAnimationPanel().getTabbedPane().addTab(getAnimationPanel().getTabbedPane().getTabCount(), "statistics ", charts);
155     }
156 
157     /** A parameter dialog with a radio button for the network choice tab. */
158     private static class NetworksParameterDialog extends TabbedParameterDialog
159     {
160         /** */
161         private static final long serialVersionUID = 1L;
162 
163         /**
164          * @param inputParameterMap InputParameterMap; the parameter map to display
165          */
166         NetworksParameterDialog(final InputParameterMap inputParameterMap)
167         {
168             super(inputParameterMap);
169         }
170 
171         /** {@inheritDoc} */
172         @SuppressWarnings({"rawtypes", "unchecked"})
173         @Override
174         public void addParameterField(final JPanel panel, final InputParameter<?, ?> parameter)
175         {
176             if (parameter instanceof InputParameterSelectionMap<?, ?>)
177             {
178                 this.fields.add(new InputFieldSelectionMapRadio(panel, (InputParameterSelectionMap<?, ?>) parameter));
179             }
180             else
181             {
182                 super.addParameterField(panel, parameter);
183             }
184         }
185 
186         /** {@inheritDoc} */
187         @Override
188         public void actionPerformed(final ActionEvent e)
189         {
190             boolean ok = true;
191             try
192             {
193                 for (InputField field : this.fields)
194                 {
195                     if (field instanceof InputFieldSelectionMapRadio<?, ?>)
196                     {
197                         InputFieldSelectionMapRadio<?, ?> f = (InputFieldSelectionMapRadio<?, ?>) field;
198                         f.getParameter().setObjectValue(f.getValue());
199                     }
200                 }
201             }
202             catch (Exception exception)
203             {
204                 JOptionPane.showMessageDialog(null, exception.getMessage(), "Data Entry Error", JOptionPane.ERROR_MESSAGE);
205                 ok = false;
206             }
207             if (ok)
208             {
209                 super.actionPerformed(e);
210             }
211         }
212 
213         /**
214          * Construct a tabbed parameter dialog that is not a part of a higher dialog.
215          * @param inputParameterMap InputParameterMap; the parameter map to use
216          * @return whether the data was entered correctly or not
217          */
218         public static boolean process(final InputParameterMap inputParameterMap)
219         {
220             NetworksParameterDialog dialog = new NetworksParameterDialog(inputParameterMap);
221             return !dialog.stopped;
222         }
223     }
224 
225     /**
226      * Radio button selection map.
227      * @param <K> key of the selection map
228      * @param <T> return type of the selection map
229      */
230     public static class InputFieldSelectionMapRadio<K, T> extends AbstractInputField
231     {
232         /** combo box for the user interface. */
233         private List<JRadioButton> buttons = new ArrayList<>();
234 
235         /** combo box for the user interface. */
236         private List<T> values = new ArrayList<>();
237 
238         /**
239          * Create a string field on the screen.
240          * @param panel JPanel; panel to add the field to
241          * @param parameter InputParameterSelectionMap&lt;K,T&gt;; the parameter
242          */
243         public InputFieldSelectionMapRadio(final JPanel panel, final InputParameterSelectionMap<K, T> parameter)
244         {
245             super(parameter);
246             Box box = Box.createVerticalBox();
247             box.add(new JLabel("  "));
248             box.add(new JLabel(parameter.getShortName()));
249             ButtonGroup group = new ButtonGroup();
250             for (K option : parameter.getOptions().keySet())
251             {
252                 String item = option.toString();
253                 T value = parameter.getOptions().get(option);
254                 JRadioButton button = new JRadioButton(item);
255                 button.setActionCommand(item);
256                 if (value.equals(parameter.getDefaultValue()))
257                 {
258                     button.setSelected(true);
259                 }
260                 group.add(button);
261                 box.add(button);
262                 this.buttons.add(button);
263                 this.values.add(value);
264             }
265             panel.add(box);
266         }
267 
268         /** {@inheritDoc} */
269         @SuppressWarnings("unchecked")
270         @Override
271         public InputParameterSelectionMap<K, T> getParameter()
272         {
273             return (InputParameterSelectionMap<K, T>) super.getParameter();
274         }
275 
276         /** @return the mapped value of the field in the gui, selected by the key's toString() value. */
277         public T getValue()
278         {
279             for (JRadioButton button : this.buttons)
280             {
281                 if (button.isSelected())
282                 {
283                     System.out.println("SELECTED: " + this.values.get(this.buttons.indexOf(button)));
284                     return this.values.get(this.buttons.indexOf(button));
285                 }
286             }
287             return this.values.get(0);
288         }
289     }
290 }