Search in sources :

Example 11 with TimetableSnapshot

use of org.opentripplanner.routing.edgetype.TimetableSnapshot 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.getAsString());
        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.stopForId.get(new AgencyAndId(feedId, "A"));
    TransitStopDepart transitStopDepartA = graph.index.stopVertexForStop.get(stopA).departVertex;
    // Get trip pattern of last (most recently added) outgoing edge
    final List<Edge> outgoingEdges = (List<Edge>) transitStopDepartA.getOutgoing();
    final TripPattern tripPattern = ((TransitBoardAlight) outgoingEdges.get(outgoingEdges.size() - 1)).getPattern();
    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);
}
Also used : Timetable(org.opentripplanner.routing.edgetype.Timetable) TripUpdate(com.google.transit.realtime.GtfsRealtime.TripUpdate) TransitBoardAlight(org.opentripplanner.routing.edgetype.TransitBoardAlight) Calendar(java.util.Calendar) TimetableSnapshot(org.opentripplanner.routing.edgetype.TimetableSnapshot) TripPattern(org.opentripplanner.routing.edgetype.TripPattern) ServiceDate(org.onebusaway.gtfs.model.calendar.ServiceDate) StopTimeUpdate(com.google.transit.realtime.GtfsRealtime.TripUpdate.StopTimeUpdate) List(java.util.List) Edge(org.opentripplanner.routing.graph.Edge) TransitStopDepart(org.opentripplanner.routing.vertextype.TransitStopDepart) Test(org.junit.Test)

Aggregations

TimetableSnapshot (org.opentripplanner.routing.edgetype.TimetableSnapshot)11 TripPattern (org.opentripplanner.routing.edgetype.TripPattern)8 Timetable (org.opentripplanner.routing.edgetype.Timetable)7 TripUpdate (com.google.transit.realtime.GtfsRealtime.TripUpdate)6 Test (org.junit.Test)6 TripTimes (org.opentripplanner.routing.trippattern.TripTimes)6 StopTimeUpdate (com.google.transit.realtime.GtfsRealtime.TripUpdate.StopTimeUpdate)5 ServiceDate (org.onebusaway.gtfs.model.calendar.ServiceDate)5 TripDescriptor (com.google.transit.realtime.GtfsRealtime.TripDescriptor)4 Stop (org.onebusaway.gtfs.model.Stop)4 TransitStop (org.opentripplanner.routing.vertextype.TransitStop)4 StopTimeEvent (com.google.transit.realtime.GtfsRealtime.TripUpdate.StopTimeEvent)3 ServiceDay (org.opentripplanner.routing.core.ServiceDay)3 ArrayList (java.util.ArrayList)2 Calendar (java.util.Calendar)2 AgencyAndId (org.onebusaway.gtfs.model.AgencyAndId)2 StopTimesInPattern (org.opentripplanner.index.model.StopTimesInPattern)2 TripTimeShort (org.opentripplanner.index.model.TripTimeShort)2 TransitBoardAlight (org.opentripplanner.routing.edgetype.TransitBoardAlight)2 TransitStopDepart (org.opentripplanner.routing.vertextype.TransitStopDepart)2