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.animation.GraphLaneUtil;
21  import org.opentrafficsim.core.dsol.OtsAnimator;
22  import org.opentrafficsim.core.dsol.OtsSimulatorInterface;
23  import org.opentrafficsim.core.network.NetworkException;
24  import org.opentrafficsim.core.perception.HistoryManagerDevs;
25  import org.opentrafficsim.draw.graphs.GraphPath;
26  import org.opentrafficsim.draw.graphs.TrajectoryPlot;
27  import org.opentrafficsim.road.network.RoadNetwork;
28  import org.opentrafficsim.road.network.lane.Lane;
29  import org.opentrafficsim.road.network.sampling.LaneDataRoad;
30  import org.opentrafficsim.road.network.sampling.RoadSampler;
31  import org.opentrafficsim.swing.graphs.OtsPlotScheduler;
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-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
50   * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
51   * </p>
52   * @author <a href="https://github.com/peter-knoppers">Peter Knoppers</a>
53   */
54  public class NetworksSwing extends OtsSimulationApplication<NetworksModel> implements UNITS
55  {
56      /** */
57      private static final long serialVersionUID = 1L;
58  
59      /**
60       * Create a Networks Swing application.
61       * @param title the title of the Frame
62       * @param panel the tabbed panel to display
63       * @param model the model
64       */
65      public NetworksSwing(final String title, final OtsAnimationPanel panel, final NetworksModel model)
66      {
67          super(model, panel, DefaultsFactory.GTU_TYPE_MARKERS.toMap());
68          RoadNetwork network = model.getNetwork();
69          System.out.println(network.getLinkMap());
70      }
71  
72      @Override
73      protected void addTabs()
74      {
75          addStatisticsTabs(getModel().getSimulator());
76      }
77  
78      /**
79       * Main program.
80       * @param args the command line arguments (not used)
81       */
82      public static void main(final String[] args)
83      {
84          demo(true);
85      }
86  
87      /**
88       * Start the demo.
89       * @param exitOnClose when running stand-alone: true; when running as part of a demo: false
90       */
91      public static void demo(final boolean exitOnClose)
92      {
93          try
94          {
95              OtsAnimator simulator = new OtsAnimator("NetworksSwing");
96              final NetworksModel otsModel = new NetworksModel(simulator);
97              if (NetworksParameterDialog.process(otsModel.getInputParameterMap()))
98              {
99                  simulator.initialize(Time.ZERO, Duration.ZERO, Duration.instantiateSI(3600.0), otsModel,
100                         HistoryManagerDevs.noHistory(simulator));
101                 OtsAnimationPanel animationPanel = new OtsAnimationPanel(otsModel.getNetwork().getExtent(),
102                         new Dimension(800, 600), simulator, otsModel, DEFAULT_GTU_COLORERS, otsModel.getNetwork());
103                 NetworksSwing app = new NetworksSwing("Networks", animationPanel, otsModel);
104                 app.setExitOnClose(exitOnClose);
105                 animationPanel.enableSimulationControlButtons();
106             }
107             else
108             {
109                 if (exitOnClose)
110                 {
111                     System.exit(0);
112                 }
113             }
114         }
115         catch (SimRuntimeException | NamingException | RemoteException | DsolException exception)
116         {
117             exception.printStackTrace();
118         }
119     }
120 
121     /**
122      * Add the statistics tabs.
123      * @param simulator the simulator on which sampling can be scheduled
124      */
125     protected final void addStatisticsTabs(final OtsSimulatorInterface simulator)
126     {
127         int graphCount = getModel().pathCount();
128         int columns = 1;
129         int rows = 0 == columns ? 0 : (int) Math.ceil(graphCount * 1.0 / columns);
130         TablePanel charts = new TablePanel(columns, rows);
131         RoadSampler sampler = new RoadSampler(getModel().getNetwork());
132         Duration updateInterval = Duration.instantiateSI(10.0);
133         for (int graphIndex = 0; graphIndex < graphCount; graphIndex++)
134         {
135             List<Lane> start = new ArrayList<>();
136             start.add(getModel().getPath(graphIndex).get(0));
137             GraphPath<LaneDataRoad> path;
138             try
139             {
140                 path = GraphLaneUtil.createPath("name", start.get(0));
141             }
142             catch (NetworkException exception)
143             {
144                 throw new RuntimeException(exception);
145             }
146             GraphPath.initRecording(sampler, path);
147             SwingPlot plot = new SwingPlot(new TrajectoryPlot("Trajectories on lane " + (graphIndex + 1), updateInterval,
148                     new OtsPlotScheduler(simulator), sampler.getSamplerData(), path));
149             charts.setCell(plot.getContentPane(), graphIndex % columns, graphIndex / columns);
150         }
151 
152         getAnimationPanel().getTabbedPane().addTab(getAnimationPanel().getTabbedPane().getTabCount(), "statistics ", charts);
153     }
154 
155     /** A parameter dialog with a radio button for the network choice tab. */
156     private static class NetworksParameterDialog extends TabbedParameterDialog
157     {
158         /** */
159         private static final long serialVersionUID = 1L;
160 
161         /**
162          * @param inputParameterMap the parameter map to display
163          */
164         NetworksParameterDialog(final InputParameterMap inputParameterMap)
165         {
166             super(inputParameterMap);
167         }
168 
169         @SuppressWarnings({"rawtypes", "unchecked"})
170         @Override
171         public void addParameterField(final JPanel panel, final InputParameter<?, ?> parameter)
172         {
173             if (parameter instanceof InputParameterSelectionMap<?, ?>)
174             {
175                 this.fields.add(new InputFieldSelectionMapRadio(panel, (InputParameterSelectionMap<?, ?>) parameter));
176             }
177             else
178             {
179                 super.addParameterField(panel, parameter);
180             }
181         }
182 
183         @Override
184         public void actionPerformed(final ActionEvent e)
185         {
186             boolean ok = true;
187             try
188             {
189                 for (InputField field : this.fields)
190                 {
191                     if (field instanceof InputFieldSelectionMapRadio<?, ?>)
192                     {
193                         InputFieldSelectionMapRadio<?, ?> f = (InputFieldSelectionMapRadio<?, ?>) field;
194                         f.getParameter().setObjectValue(f.getValue());
195                     }
196                 }
197             }
198             catch (Exception exception)
199             {
200                 JOptionPane.showMessageDialog(null, exception.getMessage(), "Data Entry Error", JOptionPane.ERROR_MESSAGE);
201                 ok = false;
202             }
203             if (ok)
204             {
205                 super.actionPerformed(e);
206             }
207         }
208 
209         /**
210          * Construct a tabbed parameter dialog that is not a part of a higher dialog.
211          * @param inputParameterMap the parameter map to use
212          * @return whether the data was entered correctly or not
213          */
214         public static boolean process(final InputParameterMap inputParameterMap)
215         {
216             NetworksParameterDialog dialog = new NetworksParameterDialog(inputParameterMap);
217             return !dialog.stopped;
218         }
219     }
220 
221     /**
222      * Radio button selection map.
223      * @param <K> key of the selection map
224      * @param <T> return type of the selection map
225      */
226     public static class InputFieldSelectionMapRadio<K, T> extends AbstractInputField
227     {
228         /** combo box for the user interface. */
229         private List<JRadioButton> buttons = new ArrayList<>();
230 
231         /** combo box for the user interface. */
232         private List<T> values = new ArrayList<>();
233 
234         /**
235          * Create a string field on the screen.
236          * @param panel panel to add the field to
237          * @param parameter the parameter
238          */
239         public InputFieldSelectionMapRadio(final JPanel panel, final InputParameterSelectionMap<K, T> parameter)
240         {
241             super(parameter);
242             Box box = Box.createVerticalBox();
243             box.add(new JLabel("  "));
244             box.add(new JLabel(parameter.getShortName()));
245             ButtonGroup group = new ButtonGroup();
246             for (K option : parameter.getOptions().keySet())
247             {
248                 String item = option.toString();
249                 T value = parameter.getOptions().get(option);
250                 JRadioButton button = new JRadioButton(item);
251                 button.setActionCommand(item);
252                 if (value.equals(parameter.getDefaultValue()))
253                 {
254                     button.setSelected(true);
255                 }
256                 group.add(button);
257                 box.add(button);
258                 this.buttons.add(button);
259                 this.values.add(value);
260             }
261             panel.add(box);
262         }
263 
264         @SuppressWarnings("unchecked")
265         @Override
266         public InputParameterSelectionMap<K, T> getParameter()
267         {
268             return (InputParameterSelectionMap<K, T>) super.getParameter();
269         }
270 
271         /** @return the mapped value of the field in the gui, selected by the key's toString() value. */
272         public T getValue()
273         {
274             for (JRadioButton button : this.buttons)
275             {
276                 if (button.isSelected())
277                 {
278                     System.out.println("SELECTED: " + this.values.get(this.buttons.indexOf(button)));
279                     return this.values.get(this.buttons.indexOf(button));
280                 }
281             }
282             return this.values.get(0);
283         }
284     }
285 }