View Javadoc
1   package org.opentrafficsim.graphs;
2   
3   import java.awt.BorderLayout;
4   import java.awt.event.ActionEvent;
5   import java.awt.event.ActionListener;
6   import java.io.Serializable;
7   import java.util.ArrayList;
8   
9   import javax.swing.ButtonGroup;
10  import javax.swing.JFrame;
11  import javax.swing.JLabel;
12  import javax.swing.JMenu;
13  import javax.swing.JPopupMenu;
14  import javax.swing.JRadioButtonMenuItem;
15  import javax.swing.SwingConstants;
16  import javax.swing.event.EventListenerList;
17  
18  import org.djunits.unit.FrequencyUnit;
19  import org.djunits.unit.LinearDensityUnit;
20  import org.djunits.unit.SpeedUnit;
21  import org.djunits.value.vdouble.scalar.Duration;
22  import org.djunits.value.vdouble.scalar.Frequency;
23  import org.djunits.value.vdouble.scalar.LinearDensity;
24  import org.djunits.value.vdouble.scalar.Speed;
25  import org.jfree.chart.ChartFactory;
26  import org.jfree.chart.ChartPanel;
27  import org.jfree.chart.JFreeChart;
28  import org.jfree.chart.StandardChartTheme;
29  import org.jfree.chart.axis.NumberAxis;
30  import org.jfree.chart.axis.ValueAxis;
31  import org.jfree.chart.event.AxisChangeEvent;
32  import org.jfree.chart.plot.PlotOrientation;
33  import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
34  import org.jfree.data.DomainOrder;
35  import org.jfree.data.general.DatasetChangeEvent;
36  import org.jfree.data.general.DatasetChangeListener;
37  import org.jfree.data.general.DatasetGroup;
38  import org.jfree.data.xy.XYDataset;
39  import org.opentrafficsim.core.dsol.OTSDEVSSimulatorInterface;
40  import org.opentrafficsim.core.dsol.OTSSimulatorInterface;
41  import org.opentrafficsim.core.gtu.RelativePosition;
42  import org.opentrafficsim.core.network.NetworkException;
43  import org.opentrafficsim.road.gtu.lane.LaneBasedGTU;
44  import org.opentrafficsim.road.network.lane.CrossSectionElement;
45  import org.opentrafficsim.road.network.lane.Lane;
46  import org.opentrafficsim.road.network.lane.object.sensor.AbstractSensor;
47  
48  import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
49  import nl.tudelft.simulation.dsol.SimRuntimeException;
50  import nl.tudelft.simulation.language.Throw;
51  
52  /**
53   * The Fundamental Diagram Graph; see <a href="http://en.wikipedia.org/wiki/Fundamental_diagram_of_traffic_flow"> Wikipedia:
54   * http://en.wikipedia.org/wiki/Fundamental_diagram_of_traffic_flow</a>.
55   * <p>
56   * Copyright (c) 2013-2016 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
57   * BSD-style license. See <a href="http://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
58   * <p>
59   * $LastChangedDate: 2015-09-14 01:33:02 +0200 (Mon, 14 Sep 2015) $, @version $Revision: 1401 $, by $Author: averbraeck $,
60   * initial version Jul 31, 2014 <br>
61   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
62   */
63  public class FundamentalDiagramLane extends JFrame implements XYDataset, ActionListener
64  {
65      /** */
66      private static final long serialVersionUID = 20140701L;
67  
68      /** The ChartPanel for this Fundamental Diagram. */
69      private JFreeChart chartPanel;
70  
71      /** Caption for this Fundamental Diagram. */
72      private final String caption;
73  
74      /** Area to show status information. */
75      private final JLabel statusLabel;
76  
77      /** Sample duration of the detector that generates this Fundamental Diagram. */
78      private final Duration aggregationTime;
79  
80      /** Storage for the Samples. */
81      private ArrayList<Sample> samples = new ArrayList<Sample>();
82  
83      /** Definition of the density axis. */
84      private Axis densityAxis = new Axis(new LinearDensity(0, LinearDensityUnit.PER_KILOMETER), new LinearDensity(200,
85              LinearDensityUnit.PER_KILOMETER), null, 0d, "Density [veh/km]", "Density", "density %.1f veh/km");
86  
87      /**
88       * @return densityAxis
89       */
90      public final Axis getDensityAxis()
91      {
92          return this.densityAxis;
93      }
94  
95      /** Definition of the speed axis. */
96      private Axis speedAxis = new Axis(new Speed(0, SpeedUnit.KM_PER_HOUR), new Speed(120, SpeedUnit.KM_PER_HOUR), null, 0d,
97              "Speed [km/h]", "Speed", "speed %.0f km/h");
98  
99      /**
100      * @return speedAxis
101      */
102     public final Axis getSpeedAxis()
103     {
104         return this.speedAxis;
105     }
106 
107     /**
108      * @return flowAxis
109      */
110     public final Axis getFlowAxis()
111     {
112         return this.flowAxis;
113     }
114 
115     /** Definition of the flow axis. */
116     private Axis flowAxis = new Axis(new Frequency(0, FrequencyUnit.PER_HOUR), new Frequency(3000d, FrequencyUnit.HERTZ), null,
117             0d, "Flow [veh/h]", "Flow", "flow %.0f veh/h");
118 
119     /** The currently shown X-axis. */
120     private Axis xAxis;
121 
122     /** The currently shown Y-axis. */
123     private Axis yAxis;
124 
125     /** List of parties interested in changes of this ContourPlot. */
126     private transient EventListenerList listenerList = new EventListenerList();
127 
128     /** Not used internally. */
129     private DatasetGroup datasetGroup = null;
130 
131     /** The lane for which data is gathered. */
132     private final Lane lane;
133 
134     /** The simulator to schedule sampling. */
135     private final OTSDEVSSimulatorInterface simulator;
136 
137     /** Flow counter. */
138 
139     int flow = 0;
140 
141     /**
142      * Retrieve the format string for the Y axis.
143      * @return format string
144      */
145     public final String getYAxisFormat()
146     {
147         return this.yAxis.getFormat();
148     }
149 
150     /**
151      * Retrieve the format string for the X axis.
152      * @return format string
153      */
154     public final String getXAxisFormat()
155     {
156         return this.xAxis.getFormat();
157     }
158 
159     /**
160      * Graph a Fundamental Diagram.
161      * @param caption String; the caption shown above the graphing area.
162      * @param aggregationTime DoubleScalarRel&lt;TimeUnit&gt;; the aggregation of the detector that generates the data for this
163      *            Fundamental diagram
164      * @param lane Lane; the Lane on which the traffic will be sampled
165      * @param simulator the simulator to schedule the sampling on
166      * @throws NetworkException on network inconsistency
167      * @throws SimRuntimeException in case scheduling of the sampler fails
168      */
169     public FundamentalDiagramLane(final String caption, final Duration aggregationTime, final Lane lane,
170             final OTSDEVSSimulatorInterface simulator) throws NetworkException, SimRuntimeException
171     {
172         if (aggregationTime.getSI() <= 0)
173         {
174             throw new Error("Aggregation time must be > 0 (got " + aggregationTime + ")");
175         }
176         this.aggregationTime = aggregationTime;
177         this.caption = caption;
178         this.lane = lane;
179         this.simulator = simulator;
180         ChartFactory.setChartTheme(new StandardChartTheme("JFree/Shadow", false));
181         this.chartPanel =
182                 ChartFactory.createXYLineChart(this.caption, "", "", this, PlotOrientation.VERTICAL, false, false, false);
183         FixCaption.fixCaption(this.chartPanel);
184         final XYLineAndShapeRenderer renderer = (XYLineAndShapeRenderer) this.chartPanel.getXYPlot().getRenderer();
185         renderer.setBaseShapesVisible(true);
186 
187         final ChartPanel cp = new ChartPanel(this.chartPanel);
188         PointerHandler ph = new PointerHandler()
189         {
190             /** */
191             private static final long serialVersionUID = 120140000;
192 
193             /** {@inheritDoc} */
194             @Override
195             void updateHint(final double domainValue, final double rangeValue)
196             {
197                 if (Double.isNaN(domainValue))
198                 {
199                     setStatusText(" ");
200                     return;
201                 }
202                 String s1 = String.format(getXAxisFormat(), domainValue);
203                 String s2 = String.format(getYAxisFormat(), rangeValue);
204                 setStatusText(s1 + ", " + s2);
205             }
206 
207         };
208         cp.addMouseMotionListener(ph);
209         cp.addMouseListener(ph);
210         cp.setMouseWheelEnabled(true);
211         final JMenu subMenu = new JMenu("Set layout");
212         final ButtonGroup group = new ButtonGroup();
213         final JRadioButtonMenuItem defaultItem = addMenuItem(subMenu, group, getDensityAxis(), this.flowAxis, true);
214         addMenuItem(subMenu, group, this.flowAxis, this.speedAxis, false);
215         addMenuItem(subMenu, group, this.densityAxis, this.speedAxis, false);
216         actionPerformed(new ActionEvent(this, 0, defaultItem.getActionCommand()));
217         final JPopupMenu popupMenu = cp.getPopupMenu();
218         popupMenu.insert(subMenu, 0);
219         this.add(cp, BorderLayout.CENTER);
220         this.statusLabel = new JLabel(" ", SwingConstants.CENTER);
221         this.add(this.statusLabel, BorderLayout.SOUTH);
222         simulator.scheduleEventRel(this.aggregationTime, this, this, "addData", null);
223         new FlowSensor(lane);
224     }
225 
226     /**
227      * Update the status text.
228      * @param newText String; the new text to show
229      */
230     public final void setStatusText(final String newText)
231     {
232         this.statusLabel.setText(newText);
233     }
234 
235     /**
236      * @return aggregationTime
237      */
238     public final Duration getAggregationTime()
239     {
240         return this.aggregationTime;
241     }
242 
243     /**
244      * Build one JRadioButtonMenuItem for the sub menu of the context menu.
245      * @param subMenu JMenu; the menu to which the new JRadioButtonMenuItem is added
246      * @param group ButtonGroup; the buttonGroup for the new JRadioButtonMenuItem
247      * @param xAxisToSelect Axis; the Axis that will become X-axis when this item is clicked
248      * @param yAxisToSelect Axis; the Axis that will become Y-axis when this item is clicked
249      * @param selected Boolean; if true, the new JRadioButtonMenuItem will be selected; if false, the new JRadioButtonMenuItem
250      *            will <b>not</b> be selected
251      * @return JRatioButtonMenuItem; the newly added item
252      */
253     private JRadioButtonMenuItem addMenuItem(final JMenu subMenu, final ButtonGroup group, final Axis xAxisToSelect,
254             final Axis yAxisToSelect, final boolean selected)
255     {
256         final JRadioButtonMenuItem item =
257                 new JRadioButtonMenuItem(yAxisToSelect.getShortName() + " / " + xAxisToSelect.getShortName());
258         item.setSelected(selected);
259         item.setActionCommand(yAxisToSelect.getShortName() + "/" + xAxisToSelect.getShortName());
260         item.addActionListener(this);
261         subMenu.add(item);
262         group.add(item);
263         return item;
264     }
265 
266     /**
267      * Add the density and average speed on the lane to this Fundamental Diagram.
268      * @throws SimRuntimeException when scheduling of next sampling time fails
269      */
270     public final void addData() throws SimRuntimeException
271     {
272         // collect (harmonic) mean speed and number of vehicles per meter on the lane
273         double n = this.lane.getGtuList().size();
274         double density = n / this.lane.getLength().si;
275         if (density > 0.0)
276         {
277             double meanSpeed = 0.0;
278             for (LaneBasedGTU gtu : this.lane.getGtuList())
279             {
280                 meanSpeed += 1 / gtu.getSpeed().si;
281             }
282             meanSpeed = n / meanSpeed;
283             this.samples.add(new Sample(meanSpeed, density, this.flow / this.aggregationTime.si));
284             this.flow = 0;
285         }
286         this.simulator.scheduleEventRel(this.aggregationTime, this, this, "addData", null);
287     }
288 
289     /**
290      * Set up a JFreeChart axis.
291      * @param valueAxis ValueAxis; the axis to set up
292      * @param axis Axis; the Axis that provides the data to setup the ValueAxis
293      */
294     private static void configureAxis(final ValueAxis valueAxis, final Axis axis)
295     {
296         valueAxis.setLabel("\u2192 " + axis.getName());
297         valueAxis.setRange(axis.getMinimumValue().getInUnit(), axis.getMaximumValue().getInUnit());
298     }
299 
300     /**
301      * Redraw this TrajectoryGraph (after the underlying data has been changed, or to change axes).
302      */
303     public final void reGraph()
304     {
305         NumberAxis numberAxis = new NumberAxis();
306         configureAxis(numberAxis, this.xAxis);
307         this.chartPanel.getXYPlot().setDomainAxis(numberAxis);
308         this.chartPanel.getPlot().axisChanged(new AxisChangeEvent(numberAxis));
309         numberAxis = new NumberAxis();
310         configureAxis(numberAxis, this.yAxis);
311         this.chartPanel.getXYPlot().setRangeAxis(numberAxis);
312         this.chartPanel.getPlot().axisChanged(new AxisChangeEvent(numberAxis));
313         notifyListeners(new DatasetChangeEvent(this, null)); // This guess work actually works!
314     }
315 
316     /**
317      * Notify interested parties of an event affecting this TrajectoryPlot.
318      * @param event DatasetChangedEvent
319      */
320     private void notifyListeners(final DatasetChangeEvent event)
321     {
322         for (DatasetChangeListener dcl : this.listenerList.getListeners(DatasetChangeListener.class))
323         {
324             dcl.datasetChanged(event);
325         }
326     }
327 
328     /** {@inheritDoc} */
329     @Override
330     public final int getSeriesCount()
331     {
332         return 1;
333     }
334 
335     /** {@inheritDoc} */
336     @Override
337     public final Comparable<Integer> getSeriesKey(final int series)
338     {
339         return series;
340     }
341 
342     /** {@inheritDoc} */
343     @SuppressWarnings("rawtypes")
344     @Override
345     public final int indexOf(final Comparable seriesKey)
346     {
347         if (seriesKey instanceof Integer)
348         {
349             return (Integer) seriesKey;
350         }
351         return -1;
352     }
353 
354     /** {@inheritDoc} */
355     @Override
356     public final void addChangeListener(final DatasetChangeListener listener)
357     {
358         this.listenerList.add(DatasetChangeListener.class, listener);
359     }
360 
361     /** {@inheritDoc} */
362     @Override
363     public final void removeChangeListener(final DatasetChangeListener listener)
364     {
365         this.listenerList.remove(DatasetChangeListener.class, listener);
366     }
367 
368     /** {@inheritDoc} */
369     @Override
370     public final DatasetGroup getGroup()
371     {
372         return this.datasetGroup;
373     }
374 
375     /** {@inheritDoc} */
376     @Override
377     public final void setGroup(final DatasetGroup group)
378     {
379         this.datasetGroup = group;
380     }
381 
382     /** {@inheritDoc} */
383     @Override
384     public final DomainOrder getDomainOrder()
385     {
386         return DomainOrder.ASCENDING;
387     }
388 
389     /** {@inheritDoc} */
390     @Override
391     public final int getItemCount(final int series)
392     {
393         return this.samples.size();
394     }
395 
396     /**
397      * Retrieve a value from the recorded samples.
398      * @param item Integer; the rank number of the sample
399      * @param axis Axis; the axis that determines which quantity to retrieve
400      * @return Double; the requested value, or Double.NaN if the sample does not (yet) exist
401      */
402     private Double getSample(final int item, final Axis axis)
403     {
404         if (item >= this.samples.size())
405         {
406             return Double.NaN;
407         }
408         double result = this.samples.get(item).getValue(axis);
409         return result;
410     }
411 
412     /** {@inheritDoc} */
413     @Override
414     public final Number getX(final int series, final int item)
415     {
416         return getXValue(series, item);
417     }
418 
419     /** {@inheritDoc} */
420     @Override
421     public final double getXValue(final int series, final int item)
422     {
423         return getSample(item, this.xAxis);
424     }
425 
426     /** {@inheritDoc} */
427     @Override
428     public final Number getY(final int series, final int item)
429     {
430         return getYValue(series, item);
431     }
432 
433     /** {@inheritDoc} */
434     @Override
435     public final double getYValue(final int series, final int item)
436     {
437         return getSample(item, this.yAxis);
438     }
439 
440     /** {@inheritDoc} */
441     @SuppressFBWarnings("ES_COMPARING_STRINGS_WITH_EQ")
442     @Override
443     public final void actionPerformed(final ActionEvent actionEvent)
444     {
445         final String command = actionEvent.getActionCommand();
446         // System.out.println("command is \"" + command + "\"");
447         final String[] fields = command.split("[/]");
448         if (fields.length == 2)
449         {
450             for (String field : fields)
451             {
452                 if (field.equalsIgnoreCase(this.densityAxis.getShortName()))
453                 {
454                     if (field == fields[0])
455                     {
456                         this.yAxis = this.densityAxis;
457                     }
458                     else
459                     {
460                         this.xAxis = this.densityAxis;
461                     }
462                 }
463                 else if (field.equalsIgnoreCase(this.flowAxis.getShortName()))
464                 {
465                     if (field == fields[0])
466                     {
467                         this.yAxis = this.flowAxis;
468                     }
469                     else
470                     {
471                         this.xAxis = this.flowAxis;
472                     }
473                 }
474                 else if (field.equalsIgnoreCase(this.speedAxis.getShortName()))
475                 {
476                     if (field == fields[0])
477                     {
478                         this.yAxis = this.speedAxis;
479                     }
480                     else
481                     {
482                         this.xAxis = this.speedAxis;
483                     }
484                 }
485                 else
486                 {
487                     throw new Error("Cannot find axis name: " + field);
488                 }
489             }
490             reGraph();
491         }
492         else
493         {
494             throw new Error("Unknown ActionEvent");
495         }
496     }
497 
498     /** {@inheritDoc} */
499     @Override
500     public final String toString()
501     {
502         return "FundamentalDiagramLane [caption=" + this.caption + ", aggregationTime=" + this.aggregationTime
503                 + ", samples.size=" + this.samples.size() + ", lane=" + this.lane + ", flow=" + this.flow + "]";
504     }
505 
506     /**
507      * Storage for one sample of data collected with mean speed [m/s] and number of vehicles per km. Flow per second can be
508      * calculated from these two numbers; currently the flow is provided (but never used).
509      * <p>
510      * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
511      */
512     class Sample implements Serializable
513     {
514         /** */
515         private static final long serialVersionUID = 20140000L;
516 
517         /** Mean speed observed during this sample [m/s]. */
518         private final double meanSpeed;
519 
520         /** Density [veh/m]. */
521         private final double density;
522 
523         /** Flow [veh/s]. */
524         private final double flow;
525 
526         /**
527          * @param meanSpeed mean speed observed during this sample [m/s]
528          * @param density density [veh/m]
529          * @param flow [veh/s]
530          */
531         public Sample(final double meanSpeed, final double density, final double flow)
532         {
533             super();
534             this.meanSpeed = meanSpeed;
535             this.density = density;
536             this.flow = flow;
537         }
538 
539         /**
540          * Retrieve a value stored in this Sample.
541          * @param axis Axis; the axis along which the data is requested
542          * @return double; the retrieved value
543          */
544         public double getValue(final Axis axis)
545         {
546             if (axis == getDensityAxis())
547             {
548                 return 1000.0 * this.density; // [veh/km]
549             }
550             else if (axis == getFlowAxis())
551             {
552                 return 3600.0 * this.meanSpeed * this.density; // [veh/h]
553             }
554             else if (axis == getSpeedAxis())
555             {
556                 return 3.6 * this.meanSpeed; // [km / h]
557             }
558             else
559             {
560                 throw new Error("Sample.getValue: Can not identify axis");
561             }
562         }
563 
564         /** {@inheritDoc} */
565         @Override
566         public final String toString()
567         {
568             return "Sample [meanSpeed=" + this.meanSpeed + ", density=" + this.density + ", flow=" + this.flow + "]";
569         }
570     }
571 
572     /** */
573     private class FlowSensor extends AbstractSensor
574     {
575         /** */
576         private static final long serialVersionUID = 1L;
577 
578         /**
579          * @param lane the lane for which to build the flowSensor
580          * @throws NetworkException 
581          */
582         public FlowSensor(final Lane lane) throws NetworkException
583         {
584             super("FLOW", lane, lane.getLength().divideBy(2.0), RelativePosition.FRONT, null);
585         }
586 
587         /** {@inheritDoc} */
588         @Override
589         public void triggerResponse(final LaneBasedGTU gtu)
590         {
591             FundamentalDiagramLane.this.flow += 1;
592         }
593 
594         /** {@inheritDoc} */
595         @Override
596         public final String toString()
597         {
598             return "FlowSensor []";
599         }
600 
601         /** {@inheritDoc} */
602         @Override
603         public FlowSensor clone(final CrossSectionElement newCSE, final OTSSimulatorInterface newSimulator,
604                 final boolean animation) throws NetworkException
605         {
606             Throw.when(!(newCSE instanceof Lane), NetworkException.class, "sensors can only be cloned for Lanes");
607             return new FlowSensor((Lane) newCSE);
608         }
609 
610     }
611 
612 }