View Javadoc
1   package org.opentrafficsim.sim0mq.kpi;
2   
3   import java.io.Serializable;
4   
5   import org.djunits.unit.SpeedUnit;
6   import org.djunits.value.vdouble.scalar.Speed;
7   import org.djunits.value.vdouble.scalar.Time;
8   import org.opentrafficsim.kpi.sampling.indicator.MeanSpeed;
9   import org.opentrafficsim.kpi.sampling.indicator.MeanTravelTimePerDistance;
10  import org.opentrafficsim.kpi.sampling.indicator.MeanTripLength;
11  import org.opentrafficsim.kpi.sampling.indicator.TotalDelay;
12  import org.opentrafficsim.kpi.sampling.indicator.TotalNumberOfStops;
13  import org.opentrafficsim.kpi.sampling.indicator.TotalTravelDistance;
14  import org.opentrafficsim.kpi.sampling.indicator.TotalTravelTime;
15  
16  /**
17   * OTS can publish messages about statistics or Key Performance Indicators (KPIs) of the relation between GTUs and a part of the
18   * network. Examples are average speeds, travel times, trip durations, and number of stops. The statistics can be transmitted
19   * via IMB at certain intervals. The statistics are based on the classes in the ots.road.sampling package. Most statistics
20   * consider a time interval (can be unbound) and a region in space (a collection of lanes; can be the entire network), filtered
21   * on metadata such as GTU type, origin, destination, or route.<br>
22   * When a statistic is published for the first time, a NEW message is sent to IMB to identify the type of statistic, the time
23   * interval and the lane(s) for which the statistic is calculated, and the metadata used to filter the GTUs. The CHANGE message
24   * is posted whenever an updated statistic is available, or when the sample frequency time is reached. When a statistic is no
25   * longer published, a DELETE event is posted. The Graph NEW messages are posted after the Network NEW, Node NEW, Link NEW, and
26   * Lane NEW messages are posted, as it has to be able to identify Lanes.
27   * <p>
28   * <style>table,th,td {border:1px solid grey; border-style:solid; text-align:left; border-collapse: collapse;}</style>
29   * <h2>NEW</h2>
30   * <table summary="" style="width:800px;">
31   * <thead>
32   * <tr>
33   * <th style="width:25%;">Variable</th>
34   * <th style="width:15%;">Type</th>
35   * <th style="width:60%;">Comments</th>
36   * </tr>
37   * </thead><tbody>
38   * <tr>
39   * <td>timestamp</td>
40   * <td>double</td>
41   * <td>time of the event, in simulation time seconds</td>
42   * </tr>
43   * <tr>
44   * <td>statisticId</td>
45   * <td>String</td>
46   * <td>a unique id for the statistic, e.g. a UUID string</td>
47   * </tr>
48   * <tr>
49   * <td>description</td>
50   * <td>String</td>
51   * <td>textual description of the statistic</td>
52   * </tr>
53   * <tr>
54   * <td>networkId</td>
55   * <td>String</td>
56   * <td>id of the Network for which the statistic is made</td>
57   * </tr>
58   * <tr>
59   * <td>numberMetadataEntries</td>
60   * <td>int</td>
61   * <td>number of metadata entries</td>
62   * </tr>
63   * <tr>
64   * <td>metadataId_1</td>
65   * <td>String</td>
66   * <td>id of the first metadata entry</td>
67   * </tr>
68   * <tr>
69   * <td>metadataType_1</td>
70   * <td>String</td>
71   * <td>type of metadata, one of GTUTYPE, ORIGIN, DESTINATION, ROUTE</td>
72   * </tr>
73   * <tr>
74   * <td>metadataValue_1</td>
75   * <td>String</td>
76   * <td>value of the first metadata entry</td>
77   * </tr>
78   * <tr>
79   * <td>...</td>
80   * <td>&nbsp;</td>
81   * <td>&nbsp;</td>
82   * </tr>
83   * <tr>
84   * <td>metadataId_n</td>
85   * <td>String</td> <tdid of the last metadata entry</td>
86   * </tr>
87   * <tr>
88   * <td>metadataType_n</td>
89   * <td>String</td>
90   * <td>type of metadata, one of GTUTYPE, ORIGIN, DESTINATION, ROUTE</td>
91   * </tr>
92   * <tr>
93   * <td>metadataValue_n</td>
94   * <td>String</td>
95   * <td>value of the last metadata entry</td>
96   * </tr>
97   * <td>numberSpaceTimeRegions</td>
98   * <td>int</td>
99   * <td>number of space-time regions for this statistic</td>
100  * </tr>
101  * <tr>
102  * <td>startTime_1</td>
103  * <td>double</td>
104  * <td>start time for the first space time region, in seconds</td>
105  * </tr>
106  * <tr>
107  * <td>endTime_1</td>
108  * <td>double</td>
109  * <td>end time for the first space time region, in seconds</td>
110  * </tr>
111  * <tr>
112  * <td>linkId_1</td>
113  * <td>String</td>
114  * <td>id of the first Link for the space-time region</td>
115  * </tr>
116  * <tr>
117  * <td>laneId_1</td>
118  * <td>String</td>
119  * <td>id of the first Lane in the link for the space-time region</td>
120  * </tr>
121  * <tr>
122  * <td>...</td>
123  * <td>&nbsp;</td>
124  * <td>&nbsp;</td>
125  * </tr>
126  * <tr>
127  * <td>startTime_n</td>
128  * <td>double</td>
129  * <td>start time for the last space time region, in seconds</td>
130  * </tr>
131  * <tr>
132  * <td>endTime_n</td>
133  * <td>double</td>
134  * <td>end time for the last space time region, in seconds</td>
135  * </tr>
136  * <tr>
137  * <td>linkId_n</td>
138  * <td>String</td>
139  * <td>id of the last Link for the space-time region</td>
140  * </tr>
141  * <tr>
142  * <td>laneId_n</td>
143  * <td>String</td>
144  * <td>id of the last Lane in the link for the space-time region</td>
145  * </tr>
146  * <tr>
147  * <td>connected</td>
148  * <td>boolean</td>
149  * <td>whether the lanes in the space-time regions are longitudinally connected or not</td>
150  * </tr>
151  * <tr>
152  * <td>transmissionInterval</td>
153  * <td>double</td>
154  * <td>transmission interval of the statistic in seconds</td>
155  * </tr>
156  * </tbody>
157  * </table>
158  * </p>
159  * <p>
160  * <h2>CHANGE</h2>
161  * <table summary="" style="width:800px;">
162  * <thead>
163  * <tr>
164  * <th style="width:25%;">Variable</th>
165  * <th style="width:15%;">Type</th>
166  * <th style="width:60%;">Comments</th>
167  * </tr>
168  * </thead><tbody>
169  * <tr>
170  * <td>timestamp</td>
171  * <td>double</td>
172  * <td>time of the event, in simulation time seconds</td>
173  * </tr>
174  * <tr>
175  * <td>statisticId</td>
176  * <td>String</td>
177  * <td>the unique id for the statistic, e.g. a UUID string</td>
178  * </tr>
179  * <tr>
180  * <td>totalGtuDistance</td>
181  * <td>double</td>
182  * <td>total distance traveled by filtered GTUs in the given time and space, in meters</td>
183  * </tr>
184  * <tr>
185  * <td>totalGtuTravelTime</td>
186  * <td>double</td>
187  * <td>total travel time by filtered GTUs in the given time and space, in seconds</td>
188  * </tr>
189  * <tr>
190  * <td>averageGtuSpeed</td>
191  * <td>double</td>
192  * <td>average filtered GTU speed in the given time and space, in meter/second</td>
193  * </tr>
194  * <tr>
195  * <td>averageGtuTravelTimePerKm</td>
196  * <td>double</td>
197  * <td>average filtered GTU travel time in the given time and space, in seconds per km</td>
198  * </tr>
199  * <tr>
200  * <td>totalGtuTimeDelay</td>
201  * <td>double</td>
202  * <td>total time delay incurred by the filtered GTUs in the given time and space, in seconds</td>
203  * </tr>
204  * <tr>
205  * <td>averageTripLength</td>
206  * <td>double</td>
207  * <td>average length of the trip of the filtered GTUs in the given time and space, in seconds</td>
208  * </tr>
209  * <tr>
210  * <td>totalNumberStops</td>
211  * <td>double</td>
212  * <td>total number of stops that GTUs made in the given time and space, dimensionless</td>
213  * </tr>
214  * </tbody>
215  * </table>
216  * </p>
217  * <p>
218  * <h2>DELETE</h2>
219  * <table summary="" style="width:800px;">
220  * <thead>
221  * <tr>
222  * <th style="width:25%;">Variable</th>
223  * <th style="width:15%;">Type</th>
224  * <th style="width:60%;">Comments</th>
225  * </tr>
226  * </thead><tbody>
227  * <tr>
228  * <td>timestamp</td>
229  * <td>double</td>
230  * <td>time of the event, in simulation time seconds</td>
231  * </tr>
232  * <tr>
233  * <td>statisticId</td>
234  * <td>String</td>
235  * <td>the unique id for the statistic that is removed</td>
236  * </tr>
237  * </tbody>
238  * </table>
239  * </p>
240  * <p>
241  * Copyright (c) 2013-2017 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
242  * BSD-style license. See <a href="http://opentrafficsim.org/docs/current/license.html">OpenTrafficSim License</a>.
243  * <p>
244  * @version $Revision$, $LastChangedDate$, by $Author$, initial version Sep 16, 2016 <br>
245  * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
246  * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
247  */
248 public class Sim0MQKpiTransceiver implements Serializable
249 {
250 
251     /** */
252     private static final long serialVersionUID = 20160923L;
253 
254     /** IMB connector. */
255     // private final Connector connector;
256 
257     /** The query for the statistic. */
258     // private final Query query;
259 
260     /** The Network id for which the graph is made. */
261     // private final String networkId;
262 
263     /** The interval between generation of graphs. */
264     // private final Duration transmissionInterval;
265 
266     private TotalTravelDistance totalTravelDistance = new TotalTravelDistance();
267 
268     private TotalTravelTime totalTravelTime = new TotalTravelTime();
269 
270     private MeanSpeed meanSpeed = new MeanSpeed(this.totalTravelDistance, this.totalTravelTime);
271 
272     private MeanTravelTimePerDistance meanTravelTimePerKm = new MeanTravelTimePerDistance(this.meanSpeed);
273 
274     private MeanTripLength meanTripLength = new MeanTripLength();
275 
276     private TotalDelay totalDelay = new TotalDelay(new Speed(130.0, SpeedUnit.KM_PER_HOUR));
277 
278     private TotalNumberOfStops totalNumberOfStops = new TotalNumberOfStops();
279 
280     private Time updateTime = Time.ZERO;
281 
282     // TODO implement DELETE message
283 
284 //    /**
285 //     * Construct a new ImbKpiTransceiver.
286 //     * @param connector Connector; the IMB connector
287 //     * @param time Time; time of creation
288 //     * @param networkId String; the network id
289 //     * @param query Query; the statistics query
290 //     * @param transmissionInterval Duration; the interval between generation of graphs
291 //     * @throws IMBException when the post of the IMB message fails
292 //     */
293 //    public Sim0MQKpiTransceiver(final Connector connector, Time time, String networkId, final Query query,
294 //            final Duration transmissionInterval) throws IMBException
295 //    {
296 //        this.connector = connector;
297 //        this.query = query;
298 //        this.networkId = networkId;
299 //        this.transmissionInterval = transmissionInterval;
300 //
301 //        List<Object> newMessage = new ArrayList<>();
302 //        newMessage.add(time.si);
303 //        newMessage.add(query.getId());
304 //        newMessage.add(query.toString());
305 //        newMessage.add(this.networkId);
306 //        newMessage.add((int) 0); // TODO numberMetadataEntries
307 //        newMessage.add((int) 0); // TODO numberSpaceTimeRegions
308 //        newMessage.add(false); // TODO "connected" not part of query anymore
309 //        newMessage.add(true); // TODO totalTrajectory
310 //        newMessage.add(transmissionInterval.si);
311 //
312 //        this.connector.postIMBMessage("StatisticsGTULane", IMBEventType.NEW, newMessage.toArray());
313 //        sendStatisticsUpdate();
314 //    }
315 //
316 //    /**
317 //     * Notifies about time, such that statistics over some period (very recently ended) can be gathered and published.
318 //     * @param time the time to be used in the notification (usually the current time)
319 //     */
320 //    public void notifyTime(Time time)
321 //    {
322 //        if (time.gt(this.updateTime))
323 //        {
324 //            try
325 //            {
326 //                sendStatisticsUpdate();
327 //            }
328 //            catch (IMBException exception)
329 //            {
330 //                throw new RuntimeException("Cannot send statistics update.", exception);
331 //            }
332 //        }
333 //    }
334 //
335 //    /**
336 //     * @throws IMBException when the transmission of the IMB message fails
337 //     */
338 //    public void sendStatisticsUpdate() throws IMBException
339 //    {
340 //        Length tdist = this.totalTravelDistance.getValue(this.query, this.updateTime);
341 //        Duration ttt = this.totalTravelTime.getValue(this.query, this.updateTime);
342 //        Speed ms = this.meanSpeed.getValue(this.query, this.updateTime);
343 //        Duration mttpkm = this.meanTravelTimePerKm.getValue(this.query, this.updateTime);
344 //        Length mtl = this.meanTripLength.getValue(this.query, this.updateTime);
345 //        Duration tdel = this.totalDelay.getValue(this.query, this.updateTime);
346 //        Dimensionless nos = this.totalNumberOfStops.getValue(this.query, this.updateTime);
347 //        double time = this.updateTime.si;
348 //        System.out.println("===== @time " + time + " s =====");
349 //        System.out.println("Total distance " + tdist);
350 //        System.out.println("Total travel time " + ttt);
351 //        System.out.println("Mean speed " + ms);
352 //        System.out.println("Mean travel time " + mttpkm + " (per km)");
353 //        System.out.println("Mean trip length " + mtl);
354 //        System.out.println("Total delay " + tdel);
355 //        System.out.println("Number of stops " + nos);
356 //        this.connector.postIMBMessage("StatisticsGTULane", IMBEventType.CHANGE,
357 //                new Object[] { time, this.query.getId(), tdist.si, ttt.si, ms.si, mttpkm.si, tdel.si, mtl.si, nos.si });
358 //        this.updateTime = this.updateTime.plus(this.transmissionInterval);
359 //    }
360 
361 }