use of org.opentripplanner.model.TripPattern in project OpenTripPlanner by opentripplanner.
the class TimetableSnapshotSourceTest method testHandleModifiedTrip.
@Test
public void testHandleModifiedTrip() throws ParseException {
// TODO
// GIVEN
// Get service date of today because old dates will be purged after applying updates
ServiceDate serviceDate = new ServiceDate(Calendar.getInstance());
String modifiedTripId = "10.1";
TripUpdate tripUpdate;
{
final TripDescriptor.Builder tripDescriptorBuilder = TripDescriptor.newBuilder();
tripDescriptorBuilder.setTripId(modifiedTripId);
tripDescriptorBuilder.setScheduleRelationship(TripDescriptor.ScheduleRelationship.MODIFIED);
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 O
final StopTimeUpdate.Builder stopTimeUpdateBuilder = tripUpdateBuilder.addStopTimeUpdateBuilder();
stopTimeUpdateBuilder.setScheduleRelationship(StopTimeUpdate.ScheduleRelationship.SCHEDULED);
stopTimeUpdateBuilder.setStopId("O");
stopTimeUpdateBuilder.setStopSequence(10);
{
// Arrival
final StopTimeEvent.Builder arrivalBuilder = stopTimeUpdateBuilder.getArrivalBuilder();
arrivalBuilder.setTime(midnightSecondsSinceEpoch + (12 * 3600) + (30 * 60));
arrivalBuilder.setDelay(0);
}
{
// Departure
final StopTimeEvent.Builder departureBuilder = stopTimeUpdateBuilder.getDepartureBuilder();
departureBuilder.setTime(midnightSecondsSinceEpoch + (12 * 3600) + (30 * 60));
departureBuilder.setDelay(0);
}
}
{
// Stop C
final StopTimeUpdate.Builder stopTimeUpdateBuilder = tripUpdateBuilder.addStopTimeUpdateBuilder();
stopTimeUpdateBuilder.setScheduleRelationship(StopTimeUpdate.ScheduleRelationship.SCHEDULED);
stopTimeUpdateBuilder.setStopId("C");
stopTimeUpdateBuilder.setStopSequence(30);
{
// Arrival
final StopTimeEvent.Builder arrivalBuilder = stopTimeUpdateBuilder.getArrivalBuilder();
arrivalBuilder.setTime(midnightSecondsSinceEpoch + (12 * 3600) + (40 * 60));
arrivalBuilder.setDelay(0);
}
{
// Departure
final StopTimeEvent.Builder departureBuilder = stopTimeUpdateBuilder.getDepartureBuilder();
departureBuilder.setTime(midnightSecondsSinceEpoch + (12 * 3600) + (45 * 60));
departureBuilder.setDelay(0);
}
}
{
// Stop D
final StopTimeUpdate.Builder stopTimeUpdateBuilder = tripUpdateBuilder.addStopTimeUpdateBuilder();
stopTimeUpdateBuilder.setScheduleRelationship(StopTimeUpdate.ScheduleRelationship.SKIPPED);
stopTimeUpdateBuilder.setStopId("D");
stopTimeUpdateBuilder.setStopSequence(40);
{
// Arrival
final StopTimeEvent.Builder arrivalBuilder = stopTimeUpdateBuilder.getArrivalBuilder();
arrivalBuilder.setTime(midnightSecondsSinceEpoch + (12 * 3600) + (50 * 60));
arrivalBuilder.setDelay(0);
}
{
// Departure
final StopTimeEvent.Builder departureBuilder = stopTimeUpdateBuilder.getDepartureBuilder();
departureBuilder.setTime(midnightSecondsSinceEpoch + (12 * 3600) + (51 * 60));
departureBuilder.setDelay(0);
}
}
{
// Stop P
final StopTimeUpdate.Builder stopTimeUpdateBuilder = tripUpdateBuilder.addStopTimeUpdateBuilder();
stopTimeUpdateBuilder.setScheduleRelationship(StopTimeUpdate.ScheduleRelationship.SCHEDULED);
stopTimeUpdateBuilder.setStopId("P");
stopTimeUpdateBuilder.setStopSequence(50);
{
// Arrival
final StopTimeEvent.Builder arrivalBuilder = stopTimeUpdateBuilder.getArrivalBuilder();
arrivalBuilder.setTime(midnightSecondsSinceEpoch + (12 * 3600) + (55 * 60));
arrivalBuilder.setDelay(0);
}
{
// Departure
final StopTimeEvent.Builder departureBuilder = stopTimeUpdateBuilder.getDepartureBuilder();
departureBuilder.setTime(midnightSecondsSinceEpoch + (12 * 3600) + (55 * 60));
departureBuilder.setDelay(0);
}
}
tripUpdate = tripUpdateBuilder.build();
}
// WHEN
updater.applyTripUpdates(graph, fullDataset, Arrays.asList(tripUpdate), feedId);
// THEN
final TimetableSnapshot snapshot = updater.getTimetableSnapshot();
// Original trip pattern
{
final FeedScopedId tripId = new FeedScopedId(feedId, modifiedTripId);
final Trip trip = graph.index.getTripForId().get(tripId);
final TripPattern originalTripPattern = graph.index.getPatternForTrip().get(trip);
final Timetable originalTimetableForToday = snapshot.resolve(originalTripPattern, serviceDate);
final Timetable originalTimetableScheduled = snapshot.resolve(originalTripPattern, null);
assertNotSame(originalTimetableForToday, originalTimetableScheduled);
final int originalTripIndexScheduled = originalTimetableScheduled.getTripIndex(modifiedTripId);
assertTrue("Original trip should be found in scheduled time table", originalTripIndexScheduled > -1);
final TripTimes originalTripTimesScheduled = originalTimetableScheduled.getTripTimes(originalTripIndexScheduled);
assertFalse("Original trip times should not be canceled in scheduled time table", originalTripTimesScheduled.isCanceled());
assertEquals(RealTimeState.SCHEDULED, originalTripTimesScheduled.getRealTimeState());
final int originalTripIndexForToday = originalTimetableForToday.getTripIndex(modifiedTripId);
assertTrue("Original trip should be found in time table for service date", originalTripIndexForToday > -1);
final TripTimes originalTripTimesForToday = originalTimetableForToday.getTripTimes(originalTripIndexForToday);
assertTrue("Original trip times should be canceled in time table for service date", originalTripTimesForToday.isCanceled());
assertEquals(RealTimeState.CANCELED, originalTripTimesForToday.getRealTimeState());
}
// New trip pattern
{
final TripPattern newTripPattern = snapshot.getLastAddedTripPattern(new FeedScopedId(feedId, modifiedTripId), serviceDate);
assertNotNull("New trip pattern should be found", newTripPattern);
final Timetable newTimetableForToday = snapshot.resolve(newTripPattern, serviceDate);
final Timetable newTimetableScheduled = snapshot.resolve(newTripPattern, null);
assertNotSame(newTimetableForToday, newTimetableScheduled);
final int newTimetableForTodayModifiedTripIndex = newTimetableForToday.getTripIndex(modifiedTripId);
assertTrue("New trip should be found in time table for service date", newTimetableForTodayModifiedTripIndex > -1);
assertEquals(RealTimeState.MODIFIED, newTimetableForToday.getTripTimes(newTimetableForTodayModifiedTripIndex).getRealTimeState());
assertEquals("New trip should not be found in scheduled time table", -1, newTimetableScheduled.getTripIndex(modifiedTripId));
}
}
use of org.opentripplanner.model.TripPattern in project OpenTripPlanner by opentripplanner.
the class TimetableSnapshotSourceTest method testHandleDelayedTrip.
@Test
public void testHandleDelayedTrip() {
final FeedScopedId tripId = new FeedScopedId(feedId, "1.1");
final FeedScopedId tripId2 = new FeedScopedId(feedId, "1.2");
final Trip trip = graph.index.getTripForId().get(tripId);
final TripPattern pattern = graph.index.getPatternForTrip().get(trip);
final int tripIndex = pattern.scheduledTimetable.getTripIndex(tripId);
final int tripIndex2 = pattern.scheduledTimetable.getTripIndex(tripId2);
final TripDescriptor.Builder tripDescriptorBuilder = TripDescriptor.newBuilder();
tripDescriptorBuilder.setTripId("1.1");
tripDescriptorBuilder.setScheduleRelationship(TripDescriptor.ScheduleRelationship.SCHEDULED);
final TripUpdate.Builder tripUpdateBuilder = TripUpdate.newBuilder();
tripUpdateBuilder.setTrip(tripDescriptorBuilder);
final StopTimeUpdate.Builder stopTimeUpdateBuilder = tripUpdateBuilder.addStopTimeUpdateBuilder();
stopTimeUpdateBuilder.setScheduleRelationship(StopTimeUpdate.ScheduleRelationship.SCHEDULED);
stopTimeUpdateBuilder.setStopSequence(2);
final StopTimeEvent.Builder arrivalBuilder = stopTimeUpdateBuilder.getArrivalBuilder();
final StopTimeEvent.Builder departureBuilder = stopTimeUpdateBuilder.getDepartureBuilder();
arrivalBuilder.setDelay(1);
departureBuilder.setDelay(1);
final TripUpdate tripUpdate = tripUpdateBuilder.build();
updater.applyTripUpdates(graph, fullDataset, Arrays.asList(tripUpdate), feedId);
final TimetableSnapshot snapshot = updater.getTimetableSnapshot();
final Timetable forToday = snapshot.resolve(pattern, serviceDate);
final Timetable schedule = snapshot.resolve(pattern, null);
assertNotSame(forToday, schedule);
assertNotSame(forToday.getTripTimes(tripIndex), schedule.getTripTimes(tripIndex));
assertSame(forToday.getTripTimes(tripIndex2), schedule.getTripTimes(tripIndex2));
assertEquals(1, forToday.getTripTimes(tripIndex).getArrivalDelay(1));
assertEquals(1, forToday.getTripTimes(tripIndex).getDepartureDelay(1));
assertEquals(RealTimeState.SCHEDULED, schedule.getTripTimes(tripIndex).getRealTimeState());
assertEquals(RealTimeState.UPDATED, forToday.getTripTimes(tripIndex).getRealTimeState());
assertEquals(RealTimeState.SCHEDULED, schedule.getTripTimes(tripIndex2).getRealTimeState());
assertEquals(RealTimeState.SCHEDULED, forToday.getTripTimes(tripIndex2).getRealTimeState());
}
use of org.opentripplanner.model.TripPattern in project OpenTripPlanner by opentripplanner.
the class ItineraryMapper method createItinerary.
private Itinerary createItinerary(Path<TripSchedule> path, StopAtDistance accessPath, StopAtDistance egressPath) {
if (path == null) {
return null;
}
Itinerary itinerary = new Itinerary();
itinerary.walkDistance = 0.0;
itinerary.transitTime = 0;
itinerary.waitingTime = 0;
itinerary.weight = path.cost();
int numberOfTransits = 0;
// Access leg
Leg leg = new Leg();
AccessPathLeg<TripSchedule> accessLeg = path.accessLeg();
leg.startTime = accessLeg.fromTime();
leg.endTime = accessLeg.toTime();
leg.from = request.tc().fromPlace;
leg.to = request.tc().toPlace;
leg.mode = WALK;
leg.distance = accessPath.distance;
itinerary.addLeg(leg);
PathLeg<TripSchedule> pathLeg = accessLeg.nextLeg();
int previousArrivalTime = -1;
while (pathLeg.isTransitLeg() || pathLeg.isTransferLeg()) {
leg = new Leg();
// Transfer leg if present
if (pathLeg.isTransferLeg()) {
TransferPathLeg<?> it = pathLeg.asTransferLeg();
previousArrivalTime = it.toTime();
leg.startTime = it.fromTime();
leg.endTime = previousArrivalTime;
leg.mode = WALK;
leg.from = mapToPlace(it.fromStop());
leg.to = mapToPlace(it.toStop());
// distanceMMToMeters (transferPath.getDistance());
leg.distance = -1.0;
} else {
// Transit leg
TransitPathLeg<TripSchedule> it = pathLeg.asTransitLeg();
itinerary.transitTime += it.toTime() - it.fromTime();
itinerary.waitingTime += it.fromTime() - previousArrivalTime;
previousArrivalTime = it.toTime();
++numberOfTransits;
leg.distance = 0.0;
TripSchedule tripSchedule = it.trip();
TripPattern tripPattern = tripSchedule.getOriginalTripPattern();
Route routeInfo = tripPattern.route;
leg.from = mapToPlace(it.fromStop());
leg.to = mapToPlace(it.toStop());
leg.route = routeInfo.getShortName();
leg.agencyName = routeInfo.getAgency().getName();
leg.tripShortName = tripSchedule.getOriginalTripPattern().name;
leg.agencyId = routeInfo.getAgency().getId();
leg.routeShortName = routeInfo.getShortName();
leg.routeLongName = routeInfo.getLongName();
leg.mode = TraverseMode.fromTransitMode(tripSchedule.getOriginalTripPattern().getMode());
leg.startTime = it.fromTime();
leg.endTime = it.toTime();
}
itinerary.addLeg(leg);
pathLeg = pathLeg.nextLeg();
}
// Egress leg
leg = new Leg();
EgressPathLeg<TripSchedule> egressLeg = pathLeg.asEgressLeg();
leg.startTime = egressLeg.fromTime();
leg.endTime = egressLeg.toTime();
leg.from = mapToPlace(egressLeg.fromStop());
leg.to = request.tc().toPlace;
leg.mode = WALK;
leg.distance = egressPath.distance;
itinerary.addLeg(leg);
itinerary.startTime = itinerary.legs.get(0).startTime;
itinerary.endTime = leg.endTime;
itinerary.duration = itinerary.endTime - itinerary.startTime;
// The number of transfers is the number of transits minus one, we can NOT count the number of Transfers
// in the path or itinerary, because transfers at the same stop does not produce a transfer object, just two
// transits following each other.
itinerary.transfers = numberOfTransits - 1;
return itinerary;
}
use of org.opentripplanner.model.TripPattern in project OpenTripPlanner by opentripplanner.
the class TripPatternMapper method mapOldTripPatternToRaptorTripPattern.
/**
* Convert all old TripPatterns into new ones, keeping a Map between the two. Do this conversion
* up front (rather than lazily on demand) to ensure pattern IDs match the sequence of patterns
* in source data.
*/
static Map<TripPattern, TripPatternWithRaptorStopIndexes> mapOldTripPatternToRaptorTripPattern(StopIndexForRaptor stopIndex, Collection<TripPattern> oldTripPatterns) {
Map<TripPattern, TripPatternWithRaptorStopIndexes> newTripPatternForOld;
newTripPatternForOld = new HashMap<>();
for (TripPattern oldTripPattern : oldTripPatterns) {
TripPatternWithRaptorStopIndexes newTripPattern = new TripPatternWithRaptorStopIndexes(stopIndex.listStopIndexesForStops(oldTripPattern.stopPattern.stops), oldTripPattern);
newTripPatternForOld.put(oldTripPattern, newTripPattern);
}
return newTripPatternForOld;
}
use of org.opentripplanner.model.TripPattern in project OpenTripPlanner by opentripplanner.
the class StopTimesHelper method stopTimesForStop.
/**
* Fetch upcoming vehicle departures from a stop.
* It goes though all patterns passing the stop for the previous, current and next service date.
* It uses a priority queue to keep track of the next departures. The queue is shared between all
* dates, as services from the previous service date can visit the stop later than the current
* service date's services. This happens eg. with sleeper trains.
*
* TODO: Add frequency based trips
* @param stop Stop object to perform the search for
* @param startTime Start time for the search. Seconds from UNIX epoch
* @param timeRange Searches forward for timeRange seconds from startTime
* @param numberOfDepartures Number of departures to fetch per pattern
* @param omitNonPickups If true, do not include vehicles that will not pick up passengers.
*/
public static List<StopTimesInPattern> stopTimesForStop(RoutingService routingService, TimetableSnapshot timetableSnapshot, Stop stop, long startTime, int timeRange, int numberOfDepartures, boolean omitNonPickups) {
if (startTime == 0) {
startTime = System.currentTimeMillis() / 1000;
}
List<StopTimesInPattern> ret = new ArrayList<>();
Date date = new Date(startTime * 1000);
ServiceDate[] serviceDates = { new ServiceDate(date).previous(), new ServiceDate(date), new ServiceDate(date).next() };
Collection<TripPattern> patternsForStop = routingService.getPatternsForStop(stop, true);
for (TripPattern pattern : patternsForStop) {
Queue<TripTimeShort> pq = listTripTimeShortsForPatternAtStop(routingService, timetableSnapshot, stop, pattern, startTime, timeRange, numberOfDepartures, omitNonPickups, serviceDates);
if (pq.size() != 0) {
StopTimesInPattern stopTimes = new StopTimesInPattern(pattern);
while (pq.size() != 0) {
stopTimes.times.add(0, pq.poll());
}
ret.add(stopTimes);
}
}
return ret;
}
Aggregations