-
Notifications
You must be signed in to change notification settings - Fork 1k
/
TimetableSnapshotSource.java
942 lines (821 loc) · 38.7 KB
/
TimetableSnapshotSource.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
/* This program is free software: you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public License
as published by the Free Software Foundation, either version 3 of
the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. */
package org.opentripplanner.updater.stoptime;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.BitSet;
import java.util.Calendar;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TimeZone;
import java.util.concurrent.locks.ReentrantLock;
import com.google.common.base.Preconditions;
import com.google.common.collect.Maps;
import org.onebusaway.gtfs.model.Agency;
import org.onebusaway.gtfs.model.AgencyAndId;
import org.onebusaway.gtfs.model.Route;
import org.onebusaway.gtfs.model.Stop;
import org.onebusaway.gtfs.model.StopTime;
import org.onebusaway.gtfs.model.Trip;
import org.onebusaway.gtfs.model.calendar.ServiceDate;
import org.opentripplanner.model.StopPattern;
import org.opentripplanner.routing.edgetype.Timetable;
import org.opentripplanner.routing.edgetype.TripPattern;
import org.opentripplanner.routing.edgetype.TimetableSnapshot;
import org.opentripplanner.routing.graph.Graph;
import org.opentripplanner.routing.graph.GraphIndex;
import org.opentripplanner.routing.trippattern.TripTimes;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.google.transit.realtime.GtfsRealtime.TripDescriptor;
import com.google.transit.realtime.GtfsRealtime.TripUpdate;
import com.google.transit.realtime.GtfsRealtime.TripUpdate.StopTimeUpdate;
/**
* This class should be used to create snapshots of lookup tables of realtime data. This is
* necessary to provide planning threads a consistent constant view of a graph with realtime data at
* a specific point in time.
*/
public class TimetableSnapshotSource {
private static final Logger LOG = LoggerFactory.getLogger(TimetableSnapshotSource.class);
/**
* Number of milliseconds per second
*/
private static final int MILLIS_PER_SECOND = 1000;
/**
* Maximum time in seconds since midnight for arrivals and departures
*/
private static final long MAX_ARRIVAL_DEPARTURE_TIME = 48 * 60 * 60;
public int logFrequency = 2000;
private int appliedBlockCount = 0;
/**
* If a timetable snapshot is requested less than this number of milliseconds after the previous
* snapshot, just return the same one. Throttles the potentially resource-consuming task of
* duplicating a TripPattern -> Timetable map and indexing the new Timetables.
*/
public int maxSnapshotFrequency = 1000; // msec
/**
* The last committed snapshot that was handed off to a routing thread. This snapshot may be
* given to more than one routing thread if the maximum snapshot frequency is exceeded.
*/
private volatile TimetableSnapshot snapshot = null;
/**
* The working copy of the timetable snapshot. Should not be visible to routing threads.
*/
private TimetableSnapshot buffer = new TimetableSnapshot();
/**
* Lock to indicate that buffer is in use
*/
private final ReentrantLock bufferLock = new ReentrantLock(true);
/**
* A synchronized cache of trip patterns that are added to the graph due to GTFS-realtime messages.
*/
private TripPatternCache tripPatternCache = new TripPatternCache();
/** Should expired realtime data be purged from the graph. */
public boolean purgeExpiredData = true;
protected ServiceDate lastPurgeDate = null;
protected long lastSnapshotTime = -1;
private final TimeZone timeZone;
private GraphIndex graphIndex;
private Agency dummyAgency;
public TimetableSnapshotSource(Graph graph) {
timeZone = graph.getTimeZone();
graphIndex = graph.index;
// Create dummy agency for added trips
dummyAgency = new Agency();
dummyAgency.setId("");
dummyAgency.setName("");
}
/**
* @return an up-to-date snapshot mapping TripPatterns to Timetables. This snapshot and the
* timetable objects it references are guaranteed to never change, so the requesting
* thread is provided a consistent view of all TripTimes. The routing thread need only
* release its reference to the snapshot to release resources.
*/
public TimetableSnapshot getTimetableSnapshot() {
TimetableSnapshot snapshotToReturn;
// Try to get a lock on the buffer
if (bufferLock.tryLock()) {
// Make a new snapshot if necessary
try {
snapshotToReturn = getTimetableSnapshot(false);
} finally {
bufferLock.unlock();
}
} else {
// No lock could be obtained because there is either a snapshot commit busy or updates
// are applied at this moment, just return the current snapshot
snapshotToReturn = snapshot;
}
return snapshotToReturn;
}
private TimetableSnapshot getTimetableSnapshot(boolean force) {
long now = System.currentTimeMillis();
if (force || now - lastSnapshotTime > maxSnapshotFrequency) {
if (force || buffer.isDirty()) {
LOG.debug("Committing {}", buffer.toString());
snapshot = buffer.commit(force);
} else {
LOG.debug("Buffer was unchanged, keeping old snapshot.");
}
lastSnapshotTime = System.currentTimeMillis();
} else {
LOG.debug("Snapshot frequency exceeded. Reusing snapshot {}", snapshot);
}
return snapshot;
}
/**
* Method to apply a trip update list to the most recent version of the timetable snapshot. A
* GTFS-RT feed is always applied against a single static feed (indicated by feedId).
*
* However, multi-feed support is not completed and we currently assume there is only one static
* feed when matching IDs.
*
* @param graph graph to update (needed for adding/changing stop patterns)
* @param fullDataset true iff the list with updates represent all updates that are active right
* now, i.e. all previous updates should be disregarded
* @param updates GTFS-RT TripUpdate's that should be applied atomically
* @param feedId
*/
public void applyTripUpdates(Graph graph, boolean fullDataset, final List<TripUpdate> updates, final String feedId) {
if (updates == null) {
LOG.warn("updates is null");
return;
}
// Acquire lock on buffer
bufferLock.lock();
try {
if (fullDataset) {
// Remove all updates from the buffer
buffer.clear();
}
LOG.debug("message contains {} trip updates", updates.size());
int uIndex = 0;
for (TripUpdate tripUpdate : updates) {
if (!tripUpdate.hasTrip()) {
LOG.warn("Missing TripDescriptor in gtfs-rt trip update: \n{}", tripUpdate);
continue;
}
ServiceDate serviceDate = new ServiceDate();
TripDescriptor tripDescriptor = tripUpdate.getTrip();
if (tripDescriptor.hasStartDate()) {
try {
serviceDate = ServiceDate.parseString(tripDescriptor.getStartDate());
} catch (ParseException e) {
LOG.warn("Failed to parse start date in gtfs-rt trip update: \n{}", tripUpdate);
continue;
}
}
uIndex += 1;
LOG.debug("trip update #{} ({} updates) :",
uIndex, tripUpdate.getStopTimeUpdateCount());
LOG.trace("{}", tripUpdate);
// Determine what kind of trip update this is
boolean applied = false;
final TripDescriptor.ScheduleRelationship tripScheduleRelationship = determineTripScheduleRelationship(
tripUpdate);
switch (tripScheduleRelationship) {
case SCHEDULED:
applied = handleScheduledTrip(tripUpdate, feedId, serviceDate);
break;
case ADDED:
applied = validateAndHandleAddedTrip(graph, tripUpdate, feedId, serviceDate);
break;
case UNSCHEDULED:
applied = handleUnscheduledTrip(tripUpdate, feedId, serviceDate);
break;
case CANCELED:
applied = handleCanceledTrip(tripUpdate, feedId, serviceDate);
break;
case MODIFIED:
applied = validateAndHandleModifiedTrip(graph, tripUpdate, feedId, serviceDate);
break;
}
if (applied) {
appliedBlockCount++;
} else {
LOG.warn("Failed to apply TripUpdate.");
LOG.trace(" Contents: {}", tripUpdate);
}
if (appliedBlockCount % logFrequency == 0) {
LOG.info("Applied {} trip updates.", appliedBlockCount);
}
}
LOG.debug("end of update message");
// Make a snapshot after each message in anticipation of incoming requests
// Purge data if necessary (and force new snapshot if anything was purged)
// Make sure that the public (locking) getTimetableSnapshot function is not called.
if (purgeExpiredData) {
boolean modified = purgeExpiredData();
getTimetableSnapshot(modified);
} else {
getTimetableSnapshot(false);
}
} finally {
// Always release lock
bufferLock.unlock();
}
}
/**
* Determine how the trip update should be handled.
*
* @param tripUpdate trip update
* @return TripDescriptor.ScheduleRelationship indicating how the trip update should be handled
*/
private TripDescriptor.ScheduleRelationship determineTripScheduleRelationship(final TripUpdate tripUpdate) {
// Assume default value
TripDescriptor.ScheduleRelationship tripScheduleRelationship = TripDescriptor.ScheduleRelationship.SCHEDULED;
// If trip update contains schedule relationship, use it
if (tripUpdate.hasTrip() && tripUpdate.getTrip().hasScheduleRelationship()) {
tripScheduleRelationship = tripUpdate.getTrip().getScheduleRelationship();
}
if (tripScheduleRelationship.equals(TripDescriptor.ScheduleRelationship.SCHEDULED)) {
// Loop over stops to check whether there are ADDED or SKIPPED stops
boolean hasModifiedStops = false;
for (StopTimeUpdate stopTimeUpdate : tripUpdate.getStopTimeUpdateList()) {
// Check schedule relationship
if (stopTimeUpdate.hasScheduleRelationship()) {
StopTimeUpdate.ScheduleRelationship stopScheduleRelationship = stopTimeUpdate
.getScheduleRelationship();
if (stopScheduleRelationship.equals(StopTimeUpdate.ScheduleRelationship.ADDED) ||
stopScheduleRelationship.equals(StopTimeUpdate.ScheduleRelationship.SKIPPED)) {
hasModifiedStops = true;
}
}
}
// If stops are modified, handle trip update like a modified trip
if (hasModifiedStops) {
tripScheduleRelationship = TripDescriptor.ScheduleRelationship.MODIFIED;
}
}
return tripScheduleRelationship;
}
private boolean handleScheduledTrip(TripUpdate tripUpdate, String feedId, ServiceDate serviceDate) {
TripDescriptor tripDescriptor = tripUpdate.getTrip();
// This does not include Agency ID or feed ID, trips are feed-unique and we currently assume a single static feed.
String tripId = tripDescriptor.getTripId();
TripPattern pattern = getPatternForTripId(tripId);
if (pattern == null) {
LOG.warn("No pattern found for tripId {}, skipping TripUpdate.", tripId);
return false;
}
if (tripUpdate.getStopTimeUpdateCount() < 1) {
LOG.warn("TripUpdate contains no updates, skipping.");
return false;
}
// Apply update on the *scheduled* time table and set the updated trip times in the buffer
TripTimes updatedTripTimes = pattern.scheduledTimetable.createUpdatedTripTimes(tripUpdate,
timeZone, serviceDate);
boolean success = buffer.update(pattern, updatedTripTimes, serviceDate);
return success;
}
/**
* Validate and handle GTFS-RT TripUpdate message containing an ADDED trip.
*
* @param graph graph to update
* @param tripUpdate GTFS-RT TripUpdate message
* @param feedId
* @param serviceDate
* @return true iff successful
*/
private boolean validateAndHandleAddedTrip(final Graph graph, final TripUpdate tripUpdate,
final String feedId, final ServiceDate serviceDate) {
// Preconditions
Preconditions.checkNotNull(graph);
Preconditions.checkNotNull(tripUpdate);
Preconditions.checkNotNull(serviceDate);
//
// Validate added trip
//
// Check whether trip id of ADDED trip is available
TripDescriptor tripDescriptor = tripUpdate.getTrip();
if (!tripDescriptor.hasTripId()) {
LOG.warn("No trip id found for ADDED trip, skipping.");
return false;
}
// Check whether trip id already exists in graph
String tripId = tripDescriptor.getTripId();
Trip trip = getTripForTripId(tripId);
if (trip != null) {
// TODO: should we support this and add a new instantiation of this trip (making it
// frequency based)?
LOG.warn("Graph already contains trip id of ADDED trip, skipping.");
return false;
}
// Check whether a start date exists
if (!tripDescriptor.hasStartDate()) {
// TODO: should we support this and apply update to all days?
LOG.warn("ADDED trip doesn't have a start date in TripDescriptor, skipping.");
return false;
}
// Check whether at least two stop updates exist
if (tripUpdate.getStopTimeUpdateCount() < 2) {
LOG.warn("ADDED trip has less then two stops, skipping.");
return false;
}
// Check whether all stop times are available and all stops exist
List<Stop> stops = checkNewStopTimeUpdatesAndFindStops(tripUpdate);
if (stops == null) {
return false;
}
//
// Handle added trip
//
boolean success = handleAddedTrip(graph, tripUpdate, stops, feedId, serviceDate);
return success;
}
/**
* Check stop time updates of trip update that results in a new trip (ADDED or MODIFIED) and
* find all stops of that trip.
*
* @param tripUpdate trip update
* @return stops when stop time updates are correct; null if there are errors
*/
private List<Stop> checkNewStopTimeUpdatesAndFindStops(final TripUpdate tripUpdate) {
Integer previousStopSequence = null;
Long previousTime = null;
List<StopTimeUpdate> stopTimeUpdates = tripUpdate.getStopTimeUpdateList();
List<Stop> stops = new ArrayList<>(stopTimeUpdates.size());
for (int index = 0; index < stopTimeUpdates.size(); ++index) {
StopTimeUpdate stopTimeUpdate = stopTimeUpdates.get(index);
// Check stop sequence
if (stopTimeUpdate.hasStopSequence()) {
Integer stopSequence = stopTimeUpdate.getStopSequence();
// Check non-negative
if (stopSequence < 0) {
LOG.warn("Trip update contains negative stop sequence, skipping.");
return null;
}
// Check whether sequence is increasing
if (previousStopSequence != null && previousStopSequence > stopSequence) {
LOG.warn("Trip update contains decreasing stop sequence, skipping.");
return null;
}
previousStopSequence = stopSequence;
} else {
LOG.warn("Trip update misses some stop sequences, skipping.");
return null;
}
// Find stops
if (stopTimeUpdate.hasStopId()) {
// Find stop
Stop stop = getStopForStopId(stopTimeUpdate.getStopId());
if (stop != null) {
// Remember stop
stops.add(stop);
} else {
LOG.warn("Graph doesn't contain stop id \"{}\" of trip update, skipping.",
stopTimeUpdate.getStopId());
return null;
}
} else {
LOG.warn("Trip update misses some stop ids, skipping.");
return null;
}
// Check arrival time
if (stopTimeUpdate.hasArrival() && stopTimeUpdate.getArrival().hasTime()) {
// Check for increasing time
Long time = stopTimeUpdate.getArrival().getTime();
if (previousTime != null && previousTime > time) {
LOG.warn("Trip update contains decreasing times, skipping.");
return null;
}
previousTime = time;
} else {
// Only first stop is allowed to miss arrival time
// TODO: should we support only requiring an arrival time on the last stop and interpolate?
if (index > 0) {
LOG.warn("Trip update misses arrival time, skipping.");
return null;
}
}
// Check departure time
if (stopTimeUpdate.hasDeparture() && stopTimeUpdate.getDeparture().hasTime()) {
// Check for increasing time
Long time = stopTimeUpdate.getDeparture().getTime();
if (previousTime != null && previousTime > time) {
LOG.warn("Trip update contains decreasing times, skipping.");
return null;
}
previousTime = time;
} else {
// Only last stop is allowed to miss departure time
// TODO: should we support only requiring a departure time on the first stop and interpolate?
if (index < stopTimeUpdates.size() - 1) {
LOG.warn("Trip update misses departure time, skipping.");
return null;
}
}
}
return stops;
}
/**
* Handle GTFS-RT TripUpdate message containing an ADDED trip.
*
* @param graph graph to update
* @param tripUpdate GTFS-RT TripUpdate message
* @param stops the stops of each StopTimeUpdate in the TripUpdate message
* @param feedId
* @param serviceDate service date for added trip
* @return true iff successful
*/
private boolean handleAddedTrip(final Graph graph, final TripUpdate tripUpdate, final List<Stop> stops,
final String feedId, final ServiceDate serviceDate) {
// Preconditions
Preconditions.checkNotNull(stops);
Preconditions.checkArgument(tripUpdate.getStopTimeUpdateCount() == stops.size(),
"number of stop should match the number of stop time updates");
// Check whether trip id has been used for previously ADDED trip message and cancel
// previously created trip
String tripId = tripUpdate.getTrip().getTripId();
cancelPreviouslyAddedTrip(tripId, serviceDate);
//
// Create added trip
//
Route route = null;
if (tripUpdate.getTrip().hasRouteId()) {
// Try to find route
route = getRouteForRouteId(tripUpdate.getTrip().getRouteId());
}
if (route == null) {
// Create new Route
route = new Route();
// Use route id of trip descriptor if available
if (tripUpdate.getTrip().hasRouteId()) {
route.setId(new AgencyAndId(feedId, tripUpdate.getTrip().getRouteId()));
} else {
route.setId(new AgencyAndId(feedId, tripId));
}
route.setAgency(dummyAgency);
// Guess the route type as it doesn't exist yet in the specifications
route.setType(3); // Bus. Used for short- and long-distance bus routes.
// Create route name
route.setLongName(tripId);
}
// Create new Trip
Trip trip = new Trip();
// TODO: which Agency ID to use? Currently use feed id.
trip.setId(new AgencyAndId(feedId, tripUpdate.getTrip().getTripId()));
trip.setRoute(route);
// Find service ID running on this service date
Set<AgencyAndId> serviceIds = graph.getCalendarService().getServiceIdsOnDate(serviceDate);
if (serviceIds.isEmpty()) {
// No service id exists: return error for now
LOG.warn("ADDED trip has service date for which no service id is available, skipping.");
return false;
} else {
// Just use first service id of set
trip.setServiceId(serviceIds.iterator().next());
}
boolean success = addTripToGraphAndBuffer(graph, trip, tripUpdate, stops, serviceDate);
return success;
}
/**
* Add a (new) trip to the graph and the buffer
* @param graph graph
* @param trip trip
* @param tripUpdate trip update containing stop time updates
* @param stops list of stops corresponding to stop time updates
* @param serviceDate service date of trip
*
* @return true iff successful
*/
private boolean addTripToGraphAndBuffer(final Graph graph, Trip trip,
final TripUpdate tripUpdate, final List<Stop> stops, final ServiceDate serviceDate) {
// Preconditions
Preconditions.checkNotNull(stops);
Preconditions.checkArgument(tripUpdate.getStopTimeUpdateCount() == stops.size(),
"number of stop should match the number of stop time updates");
// Calculate seconds since epoch on GTFS midnight (noon minus 12h) of service date
Calendar serviceCalendar = serviceDate.getAsCalendar(timeZone);
final long midnightSecondsSinceEpoch = serviceCalendar.getTimeInMillis() / MILLIS_PER_SECOND;
// Create StopTimes
List<StopTime> stopTimes = new ArrayList<>(tripUpdate.getStopTimeUpdateCount());
for (int index = 0; index < tripUpdate.getStopTimeUpdateCount(); ++index) {
StopTimeUpdate stopTimeUpdate = tripUpdate.getStopTimeUpdate(index);
Stop stop = stops.get(index);
// Only create stop time for non-skipped stops
if (!(stopTimeUpdate.hasScheduleRelationship() && stopTimeUpdate.getScheduleRelationship().equals(
StopTimeUpdate.ScheduleRelationship.SKIPPED))) {
// Create stop time
StopTime stopTime = new StopTime();
stopTime.setTrip(trip);
stopTime.setStop(stop);
// Set arrival time
if (stopTimeUpdate.hasArrival() && stopTimeUpdate.getArrival().hasTime()) {
long arrivalTime = stopTimeUpdate.getArrival().getTime() - midnightSecondsSinceEpoch;
if (arrivalTime < 0 || arrivalTime > MAX_ARRIVAL_DEPARTURE_TIME) {
LOG.warn("ADDED trip has invalid arrival time (compared to start date in "
+ "TripDescriptor), skipping.");
return false;
}
stopTime.setArrivalTime((int) arrivalTime);
}
// Set departure time
if (stopTimeUpdate.hasDeparture() && stopTimeUpdate.getDeparture().hasTime()) {
long departureTime = stopTimeUpdate.getDeparture().getTime() - midnightSecondsSinceEpoch;
if (departureTime < 0 || departureTime > MAX_ARRIVAL_DEPARTURE_TIME) {
LOG.warn("ADDED trip has invalid departure time (compared to start date in "
+ "TripDescriptor), skipping.");
return false;
}
stopTime.setDepartureTime((int) departureTime);
}
stopTime.setTimepoint(1); // Exact time
stopTime.setStopSequence(stopTimeUpdate.getStopSequence());
// Set pickup type
// Set different pickup type for last stop
if (index == tripUpdate.getStopTimeUpdateCount() - 1) {
stopTime.setPickupType(1); // No pickup available
} else {
stopTime.setPickupType(0); // Regularly scheduled pickup
}
// Set drop off type
// Set different drop off type for first stop
if (index == 0) {
stopTime.setDropOffType(1); // No drop off available
} else {
stopTime.setDropOffType(0); // Regularly scheduled drop off
}
// Add stop time to list
stopTimes.add(stopTime);
}
}
// TODO: filter/interpolate stop times like in GTFSPatternHopFactory?
// Create StopPattern
StopPattern stopPattern = new StopPattern(stopTimes);
// Get cached trip pattern or create one if it doesn't exist yet
TripPattern pattern = tripPatternCache.getOrCreateTripPattern(stopPattern, trip.getRoute(), graph);
// Add service code to bitset of pattern (using copy on write)
int serviceCode = graph.serviceCodes.get(trip.getServiceId());
BitSet services = (BitSet) pattern.getServices().clone();
services.set(serviceCode);
pattern.setServices(services);
// Create new trip times
TripTimes newTripTimes = new TripTimes(trip, stopTimes, graph.deduplicator);
// Update all times to mark trip times as realtime
for (int stopIndex = 0; stopIndex < newTripTimes.getNumStops(); stopIndex++) {
newTripTimes.updateArrivalTime(stopIndex, newTripTimes.getScheduledArrivalTime(stopIndex));
newTripTimes.updateDepartureTime(stopIndex, newTripTimes.getScheduledDepartureTime(stopIndex));
}
// Set service code of new trip times
newTripTimes.serviceCode = serviceCode;
// Add new trip times to the buffer
boolean success = buffer.update(pattern, newTripTimes, serviceDate);
return success;
}
/**
* Cancel scheduled trip in buffer given trip id (without agency id) on service date
*
* @param tripId trip id without agency id
* @param serviceDate service date
* @return true if scheduled trip was cancelled
*/
private boolean cancelScheduledTrip(String tripId, final ServiceDate serviceDate) {
boolean success = false;
TripPattern pattern = getPatternForTripId(tripId);
if (pattern != null) {
// Cancel scheduled trip times for this trip in this pattern
Timetable timetable = pattern.scheduledTimetable;
int tripIndex = timetable.getTripIndex(tripId);
if (tripIndex == -1) {
LOG.warn("Could not cancel scheduled trip {}", tripId);
} else {
TripTimes newTripTimes = new TripTimes(timetable.getTripTimes(tripIndex));
newTripTimes.cancel();
buffer.update(pattern, newTripTimes, serviceDate);
success = true;
}
}
return success;
}
/**
* Cancel previously added trip from buffer if there is a previously added trip with given trip
* id (without agency id) on service date
*
* @param tripId trip id without agency id
* @param serviceDate service date
* @return true if a previously added trip was cancelled
*/
private boolean cancelPreviouslyAddedTrip(String tripId, final ServiceDate serviceDate) {
boolean success = false;
TripPattern pattern = buffer.getLastAddedTripPattern(tripId, serviceDate);
if (pattern != null) {
// Cancel trip times for this trip in this pattern
Timetable timetable = buffer.resolve(pattern, serviceDate);
int tripIndex = timetable.getTripIndex(tripId);
if (tripIndex == -1) {
LOG.warn("Could not cancel previously added trip {}", tripId);
} else {
TripTimes newTripTimes = new TripTimes(timetable.getTripTimes(tripIndex));
newTripTimes.cancel();
buffer.update(pattern, newTripTimes, serviceDate);
success = true;
}
}
return success;
}
private boolean handleUnscheduledTrip(TripUpdate tripUpdate, String feedId, ServiceDate serviceDate) {
// TODO: Handle unscheduled trip
LOG.warn("Unscheduled trips are currently unsupported. Skipping TripUpdate.");
return false;
}
/**
* Validate and handle GTFS-RT TripUpdate message containing a MODIFIED trip.
*
* @param graph graph to update
* @param tripUpdate GTFS-RT TripUpdate message
* @param feedId
* @param serviceDate
* @return true iff successful
*/
private boolean validateAndHandleModifiedTrip(Graph graph, TripUpdate tripUpdate, String feedId, ServiceDate serviceDate) {
// Preconditions
Preconditions.checkNotNull(graph);
Preconditions.checkNotNull(tripUpdate);
Preconditions.checkNotNull(serviceDate);
//
// Validate modified trip
//
// Check whether trip id of MODIFIED trip is available
TripDescriptor tripDescriptor = tripUpdate.getTrip();
if (!tripDescriptor.hasTripId()) {
LOG.warn("No trip id found for MODIFIED trip, skipping.");
return false;
}
// Check whether trip id already exists in graph
String tripId = tripDescriptor.getTripId();
Trip trip = getTripForTripId(tripId);
if (trip == null) {
// TODO: should we support this and consider it an ADDED trip?
LOG.warn("Graph does not contain trip id of MODIFIED trip, skipping.");
return false;
}
// Check whether a start date exists
if (!tripDescriptor.hasStartDate()) {
// TODO: should we support this and apply update to all days?
LOG.warn("MODIFIED trip doesn't have a start date in TripDescriptor, skipping.");
return false;
} else {
// Check whether service date is served by trip
Set<AgencyAndId> serviceIds = graph.getCalendarService().getServiceIdsOnDate(serviceDate);
if (!serviceIds.contains(trip.getServiceId())) {
// TODO: should we support this and change service id of trip?
LOG.warn("MODIFIED trip has a service date that is not served by trip, skipping.");
return false;
}
}
// Check whether at least two stop updates exist
if (tripUpdate.getStopTimeUpdateCount() < 2) {
LOG.warn("MODIFIED trip has less then two stops, skipping.");
return false;
}
// Check whether all stop times are available and all stops exist
List<Stop> stops = checkNewStopTimeUpdatesAndFindStops(tripUpdate);
if (stops == null) {
return false;
}
//
// Handle modified trip
//
boolean success = handleModifiedTrip(graph, trip, tripUpdate, stops, feedId, serviceDate);
return success;
}
/**
* Handle GTFS-RT TripUpdate message containing a MODIFIED trip.
*
* @param graph graph to update
* @param trip trip that is modified
* @param tripUpdate GTFS-RT TripUpdate message
* @param stops the stops of each StopTimeUpdate in the TripUpdate message
* @param feedId
* @param serviceDate service date for modified trip
* @return true iff successful
*/
private boolean handleModifiedTrip(Graph graph, Trip trip, TripUpdate tripUpdate, List<Stop> stops,
String feedId, ServiceDate serviceDate) {
// Preconditions
Preconditions.checkNotNull(stops);
Preconditions.checkArgument(tripUpdate.getStopTimeUpdateCount() == stops.size(),
"number of stop should match the number of stop time updates");
// Cancel scheduled trip
String tripId = tripUpdate.getTrip().getTripId();
cancelScheduledTrip(tripId, serviceDate);
// Check whether trip id has been used for previously ADDED/MODIFIED trip message and cancel
// previously created trip
cancelPreviouslyAddedTrip(tripId, serviceDate);
// Add new trip
boolean success = addTripToGraphAndBuffer(graph, trip, tripUpdate, stops, serviceDate);
return success;
}
private boolean handleCanceledTrip(TripUpdate tripUpdate, String feedId, ServiceDate serviceDate) {
boolean success = false;
if (tripUpdate.getTrip().hasTripId()) {
// Try to cancel scheduled trip
String tripId = tripUpdate.getTrip().getTripId();
success = cancelScheduledTrip(tripId, serviceDate);
// If not successful, try to cancel previously added trip
if (!success) {
// Try to cancel previously added trip
success = cancelPreviouslyAddedTrip(tripId, serviceDate);
}
if (!success) {
LOG.warn("No pattern found for tripId {}, skipping TripUpdate.", tripId);
}
} else {
LOG.warn("No trip id in CANCELED trip update, skipping TripUpdate.");
}
return success;
}
private boolean purgeExpiredData() {
ServiceDate today = new ServiceDate();
ServiceDate previously = today.previous().previous(); // Just to be safe...
if(lastPurgeDate != null && lastPurgeDate.compareTo(previously) > 0) {
return false;
}
LOG.debug("purging expired realtime data");
lastPurgeDate = previously;
return buffer.purgeExpiredData(previously);
}
private TripPattern getPatternForTripId(String tripIdWithoutAgency) {
Trip trip = getTripForTripId(tripIdWithoutAgency);
TripPattern pattern = graphIndex.patternForTrip.get(trip);
return pattern;
}
/**
* Retrieve route given a route id without an agency
*
* @param routeId route id without the agency
* @return route or null if route can't be found in graph index
*/
private Route getRouteForRouteId(String routeId) {
/* Lazy-initialize a separate index that ignores agency IDs.
* Stopgap measure assuming no cross-feed ID conflicts, until we get GTFS loader replaced. */
if (graphIndex.routeForIdWithoutAgency == null) {
Map<String, Route> map = Maps.newHashMap();
for (Route route : graphIndex.routeForId.values()) {
Route previousValue = map.put(route.getId().getId(), route);
if (previousValue != null) {
LOG.warn("Duplicate route id detected when agency is ignored for "
+ "realtime updates: {}", route.getId().getId());
}
}
graphIndex.routeForIdWithoutAgency = map;
}
Route route = graphIndex.routeForIdWithoutAgency.get(routeId);
return route;
}
/**
* Retrieve trip given a trip id without an agency
*
* @param tripId trip id without the agency
* @return trip or null if trip can't be found in graph index
*/
private Trip getTripForTripId(String tripId) {
/* Lazy-initialize a separate index that ignores agency IDs.
* Stopgap measure assuming no cross-feed ID conflicts, until we get GTFS loader replaced. */
if (graphIndex.tripForIdWithoutAgency == null) {
Map<String, Trip> map = Maps.newHashMap();
for (Trip trip : graphIndex.tripForId.values()) {
Trip previousValue = map.put(trip.getId().getId(), trip);
if (previousValue != null) {
LOG.warn("Duplicate trip id detected when agency is ignored for realtime"
+ "updates: {}", trip.getId().getId());
}
}
graphIndex.tripForIdWithoutAgency = map;
}
Trip trip = graphIndex.tripForIdWithoutAgency.get(tripId);
return trip;
}
/**
* Retrieve stop given a stop id without an agency
*
* @param stopId trip id without the agency
* @return stop or null if stop doesn't exist
*/
private Stop getStopForStopId(String stopId) {
/* Lazy-initialize a separate index that ignores agency IDs.
* Stopgap measure assuming no cross-feed ID conflicts, until we get GTFS loader replaced. */
if (graphIndex.stopForIdWithoutAgency == null) {
Map<String, Stop> map = Maps.newHashMap();
for (Stop stop : graphIndex.stopForId.values()) {
Stop previousValue = map.put(stop.getId().getId(), stop);
if (previousValue != null) {
LOG.warn("Duplicate stop id detected when agency is ignored for realtime updates: {}", stopId);
}
}
graphIndex.stopForIdWithoutAgency = map;
}
Stop stop = graphIndex.stopForIdWithoutAgency.get(stopId);
return stop;
}
}