use of org.opentripplanner.model.calendar.ServiceDate in project OpenTripPlanner by opentripplanner.
the class TimetableSnapshotSource method applyTripUpdates.
/**
* 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(final Graph graph, final 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(feedId);
}
LOG.debug("message contains {} trip updates", updates.size());
int uIndex = 0;
for (TripUpdate tripUpdate : updates) {
if (fuzzyTripMatcher != null && tripUpdate.hasTrip()) {
final TripDescriptor trip = fuzzyTripMatcher.match(feedId, tripUpdate.getTrip());
tripUpdate = tripUpdate.toBuilder().setTrip(trip).build();
}
if (!tripUpdate.hasTrip()) {
LOG.warn("Missing TripDescriptor in gtfs-rt trip update: \n{}", tripUpdate);
continue;
}
ServiceDate serviceDate = new ServiceDate();
final TripDescriptor tripDescriptor = tripUpdate.getTrip();
if (tripDescriptor.hasStartDate()) {
try {
serviceDate = ServiceDate.parseString(tripDescriptor.getStartDate());
} catch (final ParseException e) {
LOG.warn("Failed to parse start date in gtfs-rt trip update: \n{}", tripUpdate);
continue;
}
} else {
// TODO: figure out the correct service date. For the special case that a trip
// starts for example at 40:00, yesterday would probably be a better guess.
}
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 sure that the public (locking) getTimetableSnapshot function is not called.
if (purgeExpiredData) {
final boolean modified = purgeExpiredData();
getTimetableSnapshot(modified);
} else {
getTimetableSnapshot(false);
}
} finally {
// Always release lock
bufferLock.unlock();
}
}
use of org.opentripplanner.model.calendar.ServiceDate in project OpenTripPlanner by opentripplanner.
the class TimetableSnapshotTest method testResolve.
@Test
public void testResolve() {
ServiceDate today = new ServiceDate();
ServiceDate yesterday = today.previous();
ServiceDate tomorrow = today.next();
TripPattern pattern = patternIndex.get(new FeedScopedId("agency", "1.1"));
TimetableSnapshot resolver = new TimetableSnapshot();
Timetable scheduled = resolver.resolve(pattern, today);
assertEquals(scheduled, resolver.resolve(pattern, null));
TripDescriptor.Builder tripDescriptorBuilder = TripDescriptor.newBuilder();
tripDescriptorBuilder.setTripId("1.1");
tripDescriptorBuilder.setScheduleRelationship(ScheduleRelationship.CANCELED);
TripUpdate.Builder tripUpdateBuilder = TripUpdate.newBuilder();
tripUpdateBuilder.setTrip(tripDescriptorBuilder);
TripUpdate tripUpdate = tripUpdateBuilder.build();
// add a new timetable for today
updateResolver(resolver, pattern, tripUpdate, "agency", today);
Timetable forNow = resolver.resolve(pattern, today);
assertEquals(scheduled, resolver.resolve(pattern, yesterday));
assertNotSame(scheduled, forNow);
assertEquals(scheduled, resolver.resolve(pattern, tomorrow));
assertEquals(scheduled, resolver.resolve(pattern, null));
// add a new timetable for yesterday
updateResolver(resolver, pattern, tripUpdate, "agency", yesterday);
Timetable forYesterday = resolver.resolve(pattern, yesterday);
assertNotSame(scheduled, forYesterday);
assertNotSame(scheduled, forNow);
assertEquals(scheduled, resolver.resolve(pattern, tomorrow));
assertEquals(scheduled, resolver.resolve(pattern, null));
}
use of org.opentripplanner.model.calendar.ServiceDate in project OpenTripPlanner by opentripplanner.
the class TimetableSnapshotTest method testCommit.
@Test(expected = ConcurrentModificationException.class)
public void testCommit() {
ServiceDate today = new ServiceDate();
ServiceDate yesterday = today.previous();
TripPattern pattern = patternIndex.get(new FeedScopedId("agency", "1.1"));
TimetableSnapshot resolver = new TimetableSnapshot();
// only return a new snapshot if there are changes
TimetableSnapshot snapshot = resolver.commit();
assertNull(snapshot);
TripDescriptor.Builder tripDescriptorBuilder = TripDescriptor.newBuilder();
tripDescriptorBuilder.setTripId("1.1");
tripDescriptorBuilder.setScheduleRelationship(ScheduleRelationship.CANCELED);
TripUpdate.Builder tripUpdateBuilder = TripUpdate.newBuilder();
tripUpdateBuilder.setTrip(tripDescriptorBuilder);
TripUpdate tripUpdate = tripUpdateBuilder.build();
// add a new timetable for today, commit, and everything should match
assertTrue(updateResolver(resolver, pattern, tripUpdate, "agency", today));
snapshot = resolver.commit();
assertEquals(snapshot.resolve(pattern, today), resolver.resolve(pattern, today));
assertEquals(snapshot.resolve(pattern, yesterday), resolver.resolve(pattern, yesterday));
// add a new timetable for today, don't commit, and everything should not match
assertTrue(updateResolver(resolver, pattern, tripUpdate, "agency", today));
assertNotSame(snapshot.resolve(pattern, today), resolver.resolve(pattern, today));
assertEquals(snapshot.resolve(pattern, yesterday), resolver.resolve(pattern, yesterday));
// add a new timetable for today, on another day, and things should still not match
assertTrue(updateResolver(resolver, pattern, tripUpdate, "agency", yesterday));
assertNotSame(snapshot.resolve(pattern, yesterday), resolver.resolve(pattern, yesterday));
// commit, and things should match
snapshot = resolver.commit();
assertEquals(snapshot.resolve(pattern, today), resolver.resolve(pattern, today));
assertEquals(snapshot.resolve(pattern, yesterday), resolver.resolve(pattern, yesterday));
// exception if we try to commit to a snapshot
snapshot.commit();
}
use of org.opentripplanner.model.calendar.ServiceDate in project OpenTripPlanner by opentripplanner.
the class TimetableSnapshotTest method testCompare.
@Test
public void testCompare() {
Timetable orig = new Timetable(null);
Timetable a = new Timetable(orig, new ServiceDate().previous());
Timetable b = new Timetable(orig, new ServiceDate());
assertTrue(new TimetableSnapshot.SortedTimetableComparator().compare(a, b) < 0);
}
use of org.opentripplanner.model.calendar.ServiceDate in project OpenTripPlanner by opentripplanner.
the class NetexLoaderSmokeTest method assetServiceCalendar.
private void assetServiceCalendar(CalendarServiceData cal) {
assertEquals("[RB:RUT:Authority:RUT]", cal.getAgencyIds().toString());
assertEquals("Europe/Oslo", cal.getTimeZoneForAgencyId(new FeedScopedId("RB", "RUT:Authority:RUT")).toZoneId().toString());
assertEquals("[RUT:DayType:0-105025+RUT:DayType:0-105026+RUT:DayType:6-101468, RUT:DayType:6-101468]", cal.getServiceIds().stream().map(FeedScopedId::getId).sorted().collect(Collectors.toList()).toString());
assertEquals("[2017-12-21, 2017-12-22, 2017-12-25, 2017-12-26, 2017-12-27, 2017-12-28, 2017-12-29, 2018-01-02, 2018-01-03, 2018-01-04]", cal.getServiceDatesForServiceId(fId("RUT:DayType:6-101468")).toString());
ServiceDate DEC_29 = new ServiceDate(2017, 12, 29);
assertEquals("RUT:DayType:0-105025+RUT:DayType:0-105026+RUT:DayType:6-101468, RUT:DayType:6-101468", cal.getServiceIdsForDate(DEC_29).stream().map(FeedScopedId::getId).sorted().collect(Collectors.joining(", ")));
assertEquals(2, cal.getServiceIds().size());
assertEquals(1, cal.getAgencyIds().size());
}
Aggregations