use of org.opentripplanner.model.calendar.ServiceDate in project OpenTripPlanner by opentripplanner.
the class TimetableSnapshotTest method testUpdate.
@Test(expected = ConcurrentModificationException.class)
public void testUpdate() {
ServiceDate today = new ServiceDate();
ServiceDate yesterday = today.previous();
TripPattern pattern = patternIndex.get(new FeedScopedId("agency", "1.1"));
TimetableSnapshot resolver = new TimetableSnapshot();
Timetable origNow = resolver.resolve(pattern, today);
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();
// new timetable for today
updateResolver(resolver, pattern, tripUpdate, "agency", today);
Timetable updatedNow = resolver.resolve(pattern, today);
assertNotSame(origNow, updatedNow);
// reuse timetable for today
updateResolver(resolver, pattern, tripUpdate, "agency", today);
assertEquals(updatedNow, resolver.resolve(pattern, today));
// create new timetable for tomorrow
updateResolver(resolver, pattern, tripUpdate, "agency", yesterday);
assertNotSame(origNow, resolver.resolve(pattern, yesterday));
assertNotSame(updatedNow, resolver.resolve(pattern, yesterday));
// exception if we try to modify a snapshot
TimetableSnapshot snapshot = resolver.commit();
updateResolver(snapshot, pattern, tripUpdate, "agency", yesterday);
}
use of org.opentripplanner.model.calendar.ServiceDate in project OpenTripPlanner by opentripplanner.
the class LegTest method isPartiallySameTransitLeg.
@Test
public void isPartiallySameTransitLeg() {
final int fromStopIndex = 2;
final int toStopIndex = 12;
final int tripId0 = 33;
final int tripIdOther = 44;
final ServiceDate day1 = new ServiceDate(2020, 9, 17);
final ServiceDate day2 = new ServiceDate(2020, 9, 18);
Leg t0 = createLegIgnoreTime(tripId0, fromStopIndex, toStopIndex, day1);
Leg t1;
assertTrue("t0 is same trip as it self", t0.isPartiallySameTransitLeg(t0));
// Create a new trip with the same trip Id witch ride only between the two first stops of trip 0.
t1 = createLegIgnoreTime(tripId0, fromStopIndex, fromStopIndex + 1, day1);
assertTrue("t1 overlap t0", t1.isPartiallySameTransitLeg(t0));
assertTrue("t0 overlap t1", t0.isPartiallySameTransitLeg(t1));
// Create a new trip with the same trip Id but on a diffrent service day
t1 = createLegIgnoreTime(tripId0, fromStopIndex, fromStopIndex + 1, day2);
assertFalse("t1 diffrent serviceDate from t0", t1.isPartiallySameTransitLeg(t0));
assertFalse("t0 diffrent serviceDate from t1", t0.isPartiallySameTransitLeg(t1));
// Create a new trip with the same trip Id witch ride only between the two last stops of trip 0.
t1 = createLegIgnoreTime(tripId0, toStopIndex - 1, toStopIndex, day1);
assertTrue("t1 overlap t0", t1.isPartiallySameTransitLeg(t0));
assertTrue("t0 overlap t1", t0.isPartiallySameTransitLeg(t1));
// Create a new trip witch alight at the board stop of t0 - should not overlap
t1 = createLegIgnoreTime(tripId0, fromStopIndex - 1, fromStopIndex, day1);
assertFalse("t1 do not overlap t0", t1.isPartiallySameTransitLeg(t0));
assertFalse("t0 do not overlap t1", t0.isPartiallySameTransitLeg(t1));
// Two legs do NOT overlap is on diffrent trips
t1 = createLegIgnoreTime(tripIdOther, fromStopIndex, toStopIndex, day1);
assertFalse("t1 do not overlap t0", t1.isPartiallySameTransitLeg(t0));
assertFalse("t0 do not overlap t1", t0.isPartiallySameTransitLeg(t1));
}
use of org.opentripplanner.model.calendar.ServiceDate in project OpenTripPlanner by opentripplanner.
the class TimetableSnapshotSourceTest method testHandleAddedTrip.
@Test
public void testHandleAddedTrip() throws ParseException {
// GIVEN
// Get service date of today because old dates will be purged after applying updates
final ServiceDate serviceDate = new ServiceDate(Calendar.getInstance());
final String addedTripId = "added_trip";
TripUpdate tripUpdate;
{
final TripDescriptor.Builder tripDescriptorBuilder = TripDescriptor.newBuilder();
tripDescriptorBuilder.setTripId(addedTripId);
tripDescriptorBuilder.setScheduleRelationship(TripDescriptor.ScheduleRelationship.ADDED);
tripDescriptorBuilder.setStartDate(serviceDate.asCompactString());
final Calendar calendar = serviceDate.getAsCalendar(graph.getTimeZone());
final long midnightSecondsSinceEpoch = calendar.getTimeInMillis() / 1000;
final TripUpdate.Builder tripUpdateBuilder = TripUpdate.newBuilder();
tripUpdateBuilder.setTrip(tripDescriptorBuilder);
{
// Stop A
final StopTimeUpdate.Builder stopTimeUpdateBuilder = tripUpdateBuilder.addStopTimeUpdateBuilder();
stopTimeUpdateBuilder.setScheduleRelationship(StopTimeUpdate.ScheduleRelationship.SCHEDULED);
stopTimeUpdateBuilder.setStopId("A");
{
// Arrival
final StopTimeEvent.Builder arrivalBuilder = stopTimeUpdateBuilder.getArrivalBuilder();
arrivalBuilder.setTime(midnightSecondsSinceEpoch + (8 * 3600) + (30 * 60));
arrivalBuilder.setDelay(0);
}
{
// Departure
final StopTimeEvent.Builder departureBuilder = stopTimeUpdateBuilder.getDepartureBuilder();
departureBuilder.setTime(midnightSecondsSinceEpoch + (8 * 3600) + (30 * 60));
departureBuilder.setDelay(0);
}
}
{
// Stop C
final StopTimeUpdate.Builder stopTimeUpdateBuilder = tripUpdateBuilder.addStopTimeUpdateBuilder();
stopTimeUpdateBuilder.setScheduleRelationship(StopTimeUpdate.ScheduleRelationship.SCHEDULED);
stopTimeUpdateBuilder.setStopId("C");
{
// Arrival
final StopTimeEvent.Builder arrivalBuilder = stopTimeUpdateBuilder.getArrivalBuilder();
arrivalBuilder.setTime(midnightSecondsSinceEpoch + (8 * 3600) + (40 * 60));
arrivalBuilder.setDelay(0);
}
{
// Departure
final StopTimeEvent.Builder departureBuilder = stopTimeUpdateBuilder.getDepartureBuilder();
departureBuilder.setTime(midnightSecondsSinceEpoch + (8 * 3600) + (45 * 60));
departureBuilder.setDelay(0);
}
}
{
// Stop E
final StopTimeUpdate.Builder stopTimeUpdateBuilder = tripUpdateBuilder.addStopTimeUpdateBuilder();
stopTimeUpdateBuilder.setScheduleRelationship(StopTimeUpdate.ScheduleRelationship.SCHEDULED);
stopTimeUpdateBuilder.setStopId("E");
{
// Arrival
final StopTimeEvent.Builder arrivalBuilder = stopTimeUpdateBuilder.getArrivalBuilder();
arrivalBuilder.setTime(midnightSecondsSinceEpoch + (8 * 3600) + (55 * 60));
arrivalBuilder.setDelay(0);
}
{
// Departure
final StopTimeEvent.Builder departureBuilder = stopTimeUpdateBuilder.getDepartureBuilder();
departureBuilder.setTime(midnightSecondsSinceEpoch + (8 * 3600) + (55 * 60));
departureBuilder.setDelay(0);
}
}
tripUpdate = tripUpdateBuilder.build();
}
// WHEN
updater.applyTripUpdates(graph, fullDataset, Arrays.asList(tripUpdate), feedId);
// THEN
// Find new pattern in graph starting from stop A
Stop stopA = graph.index.getStopForId(new FeedScopedId(feedId, "A"));
// Get trip pattern of last (most recently added) outgoing edge
// FIXME create a new test to see that add-trip realtime updates work
TripPattern tripPattern = null;
assertNotNull("Added trip pattern should be found", tripPattern);
final TimetableSnapshot snapshot = updater.getTimetableSnapshot();
final Timetable forToday = snapshot.resolve(tripPattern, serviceDate);
final Timetable schedule = snapshot.resolve(tripPattern, null);
assertNotSame(forToday, schedule);
final int forTodayAddedTripIndex = forToday.getTripIndex(addedTripId);
assertTrue("Added trip should be found in time table for service date", forTodayAddedTripIndex > -1);
assertEquals(RealTimeState.ADDED, forToday.getTripTimes(forTodayAddedTripIndex).getRealTimeState());
final int scheduleTripIndex = schedule.getTripIndex(addedTripId);
assertEquals("Added trip should not be found in scheduled time table", -1, scheduleTripIndex);
}
use of org.opentripplanner.model.calendar.ServiceDate in project OpenTripPlanner by opentripplanner.
the class SiriTimetableSnapshotSource method getPatternsForTrip.
private Set<TripPattern> getPatternsForTrip(Set<Trip> matches, VehicleActivityStructure.MonitoredVehicleJourney monitoredVehicleJourney) {
if (monitoredVehicleJourney.getOriginRef() == null) {
return null;
}
ZonedDateTime date = monitoredVehicleJourney.getOriginAimedDepartureTime();
if (date == null) {
// If no date is set - assume Realtime-data is reported for 'today'.
date = ZonedDateTime.now();
}
ServiceDate realTimeReportedServiceDate = new ServiceDate(date.getYear(), date.getMonthValue(), date.getDayOfMonth());
Set<TripPattern> patterns = new HashSet<>();
for (Iterator<Trip> iterator = matches.iterator(); iterator.hasNext(); ) {
Trip currentTrip = iterator.next();
TripPattern tripPattern = routingService.getPatternForTrip().get(currentTrip);
Set<ServiceDate> serviceDates = routingService.getCalendarService().getServiceDatesForServiceId(currentTrip.getServiceId());
if (!serviceDates.contains(realTimeReportedServiceDate)) {
// Current trip has no service on the date of the 'MonitoredVehicleJourney'
continue;
}
Stop firstStop = tripPattern.getStop(0);
Stop lastStop = tripPattern.getStop(tripPattern.getStops().size() - 1);
String siriOriginRef = monitoredVehicleJourney.getOriginRef().getValue();
if (monitoredVehicleJourney.getDestinationRef() != null) {
String siriDestinationRef = monitoredVehicleJourney.getDestinationRef().getValue();
boolean firstStopIsMatch = firstStop.getId().getId().equals(siriOriginRef);
boolean lastStopIsMatch = lastStop.getId().getId().equals(siriDestinationRef);
if (!firstStopIsMatch && firstStop.isPartOfStation()) {
Stop otherFirstStop = routingService.getStopForId(new FeedScopedId(firstStop.getId().getFeedId(), siriOriginRef));
firstStopIsMatch = firstStop.isPartOfSameStationAs(otherFirstStop);
}
if (!lastStopIsMatch && lastStop.isPartOfStation()) {
Stop otherLastStop = routingService.getStopForId(new FeedScopedId(lastStop.getId().getFeedId(), siriDestinationRef));
lastStopIsMatch = lastStop.isPartOfSameStationAs(otherLastStop);
}
if (firstStopIsMatch & lastStopIsMatch) {
// Origin and destination matches
TripPattern lastAddedTripPattern = buffer.getLastAddedTripPattern(currentTrip.getId(), realTimeReportedServiceDate);
if (lastAddedTripPattern != null) {
patterns.add(lastAddedTripPattern);
} else {
patterns.add(tripPattern);
}
}
} else {
// Match origin only - since destination is not defined
if (firstStop.getId().getId().equals(siriOriginRef)) {
// TODO does this line do anything?
tripPattern.scheduledTimetable.tripTimes.get(0).getDepartureTime(0);
patterns.add(tripPattern);
}
}
}
return patterns;
}
use of org.opentripplanner.model.calendar.ServiceDate in project OpenTripPlanner by opentripplanner.
the class SiriTimetableSnapshotSource method getServiceDateForEstimatedVehicleJourney.
private ServiceDate getServiceDateForEstimatedVehicleJourney(EstimatedVehicleJourney estimatedVehicleJourney) {
ZonedDateTime date;
if (estimatedVehicleJourney.getRecordedCalls() != null && !estimatedVehicleJourney.getRecordedCalls().getRecordedCalls().isEmpty()) {
date = estimatedVehicleJourney.getRecordedCalls().getRecordedCalls().get(0).getAimedDepartureTime();
} else {
EstimatedCall firstCall = estimatedVehicleJourney.getEstimatedCalls().getEstimatedCalls().get(0);
date = firstCall.getAimedDepartureTime();
}
if (date == null) {
return null;
}
return new ServiceDate(date.getYear(), date.getMonthValue(), date.getDayOfMonth());
}
Aggregations