Search in sources :

Example 21 with TripUpdate

use of com.google.transit.realtime.GtfsRealtime.TripUpdate in project OpenTripPlanner by opentripplanner.

the class TestTransfers method applyUpdateToTripPattern.

/**
 * Apply an update to a table trip pattern and check whether the update was applied correctly
 */
private void applyUpdateToTripPattern(TripPattern pattern, String tripId, String stopId, int stopSeq, int arrive, int depart, ScheduleRelationship scheduleRelationship, int timestamp, ServiceDate serviceDate) throws ParseException {
    TimetableSnapshot snapshot = graph.timetableSnapshotSource.getTimetableSnapshot();
    Timetable timetable = snapshot.resolve(pattern, serviceDate);
    TimeZone timeZone = new SimpleTimeZone(-7, "PST");
    long today = serviceDate.getAsDate(timeZone).getTime() / 1000;
    TripDescriptor.Builder tripDescriptorBuilder = TripDescriptor.newBuilder();
    tripDescriptorBuilder.setTripId(tripId);
    StopTimeEvent.Builder departStopTimeEventBuilder = StopTimeEvent.newBuilder();
    StopTimeEvent.Builder arriveStopTimeEventBuilder = StopTimeEvent.newBuilder();
    departStopTimeEventBuilder.setTime(today + depart);
    arriveStopTimeEventBuilder.setTime(today + arrive);
    StopTimeUpdate.Builder stopTimeUpdateBuilder = StopTimeUpdate.newBuilder();
    stopTimeUpdateBuilder.setStopSequence(stopSeq);
    stopTimeUpdateBuilder.setDeparture(departStopTimeEventBuilder);
    stopTimeUpdateBuilder.setArrival(arriveStopTimeEventBuilder);
    stopTimeUpdateBuilder.setScheduleRelationship(scheduleRelationship);
    TripUpdate.Builder tripUpdateBuilder = TripUpdate.newBuilder();
    tripUpdateBuilder.setTrip(tripDescriptorBuilder);
    tripUpdateBuilder.addStopTimeUpdate(0, stopTimeUpdateBuilder);
    TripUpdate tripUpdate = tripUpdateBuilder.build();
    TripTimes updatedTripTimes = timetable.createUpdatedTripTimes(tripUpdate, timeZone, serviceDate);
    assertNotNull(updatedTripTimes);
    int tripIndex = timetable.getTripIndex(tripId);
    assertTrue(tripIndex != -1);
    timetable.setTripTimes(tripIndex, updatedTripTimes);
}
Also used : Timetable(org.opentripplanner.routing.edgetype.Timetable) TripUpdate(com.google.transit.realtime.GtfsRealtime.TripUpdate) TimetableSnapshot(org.opentripplanner.routing.edgetype.TimetableSnapshot) TimeZone(java.util.TimeZone) SimpleTimeZone(java.util.SimpleTimeZone) StopTimeEvent(com.google.transit.realtime.GtfsRealtime.TripUpdate.StopTimeEvent) TripDescriptor(com.google.transit.realtime.GtfsRealtime.TripDescriptor) SimpleTimeZone(java.util.SimpleTimeZone) StopTimeUpdate(com.google.transit.realtime.GtfsRealtime.TripUpdate.StopTimeUpdate) TripTimes(org.opentripplanner.routing.trippattern.TripTimes)

Example 22 with TripUpdate

use of com.google.transit.realtime.GtfsRealtime.TripUpdate 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)

Example 23 with TripUpdate

use of com.google.transit.realtime.GtfsRealtime.TripUpdate in project OpenTripPlanner by opentripplanner.

the class TripUpdateGraphWriterRunnableTest method testTripUpdateGraphWriterRunnable.

@Test
public void testTripUpdateGraphWriterRunnable() {
    final boolean fullDataset = false;
    final String agencyId = "Agency ID";
    final List<TripUpdate> updates = Collections.singletonList(TripUpdate.newBuilder().buildPartial());
    final TripUpdateGraphWriterRunnable tripUpdateGraphWriterRunnable = new TripUpdateGraphWriterRunnable(fullDataset, updates, agencyId);
    Graph graph = mock(Graph.class);
    TimetableSnapshotSource timetableSnapshotSource = mock(TimetableSnapshotSource.class);
    graph.timetableSnapshotSource = timetableSnapshotSource;
    tripUpdateGraphWriterRunnable.run(graph);
    verify(timetableSnapshotSource).applyTripUpdates(graph, fullDataset, updates, agencyId);
}
Also used : TripUpdate(com.google.transit.realtime.GtfsRealtime.TripUpdate) Graph(org.opentripplanner.routing.graph.Graph) Test(org.junit.Test)

Example 24 with TripUpdate

use of com.google.transit.realtime.GtfsRealtime.TripUpdate 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 AgencyAndId("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);
}
Also used : ServiceDate(org.onebusaway.gtfs.model.calendar.ServiceDate) TripUpdate(com.google.transit.realtime.GtfsRealtime.TripUpdate) AgencyAndId(org.onebusaway.gtfs.model.AgencyAndId) TripDescriptor(com.google.transit.realtime.GtfsRealtime.TripDescriptor) Test(org.junit.Test)

Example 25 with TripUpdate

use of com.google.transit.realtime.GtfsRealtime.TripUpdate in project OpenTripPlanner by opentripplanner.

the class GtfsRealtimeHttpTripUpdateSource method getUpdates.

@Override
public List<TripUpdate> getUpdates() {
    FeedMessage feedMessage = null;
    List<FeedEntity> feedEntityList = null;
    List<TripUpdate> updates = null;
    fullDataset = true;
    try {
        InputStream is = HttpUtils.getData(url);
        if (is != null) {
            // Decode message
            feedMessage = FeedMessage.PARSER.parseFrom(is);
            feedEntityList = feedMessage.getEntityList();
            // Change fullDataset value if this is an incremental update
            if (feedMessage.hasHeader() && feedMessage.getHeader().hasIncrementality() && feedMessage.getHeader().getIncrementality().equals(GtfsRealtime.FeedHeader.Incrementality.DIFFERENTIAL)) {
                fullDataset = false;
            }
            // Create List of TripUpdates
            updates = new ArrayList<>(feedEntityList.size());
            for (FeedEntity feedEntity : feedEntityList) {
                if (feedEntity.hasTripUpdate())
                    updates.add(feedEntity.getTripUpdate());
            }
        }
    } catch (Exception e) {
        LOG.warn("Failed to parse gtfs-rt feed from " + url + ":", e);
    }
    return updates;
}
Also used : FeedMessage(com.google.transit.realtime.GtfsRealtime.FeedMessage) TripUpdate(com.google.transit.realtime.GtfsRealtime.TripUpdate) InputStream(java.io.InputStream) FeedEntity(com.google.transit.realtime.GtfsRealtime.FeedEntity)

Aggregations

TripUpdate (com.google.transit.realtime.GtfsRealtime.TripUpdate)30 Test (org.junit.Test)20 StopTimeUpdate (com.google.transit.realtime.GtfsRealtime.TripUpdate.StopTimeUpdate)16 TripDescriptor (com.google.transit.realtime.GtfsRealtime.TripDescriptor)13 BlockInstance (org.onebusaway.transit_data_federation.services.blocks.BlockInstance)10 BlockConfigurationEntry (org.onebusaway.transit_data_federation.services.transit_graph.BlockConfigurationEntry)10 ServiceDate (org.onebusaway.gtfs.model.calendar.ServiceDate)9 VehicleLocationRecord (org.onebusaway.realtime.api.VehicleLocationRecord)9 BlockEntryImpl (org.onebusaway.transit_data_federation.impl.transit_graph.BlockEntryImpl)9 TripEntryImpl (org.onebusaway.transit_data_federation.impl.transit_graph.TripEntryImpl)9 AgencyAndId (org.onebusaway.gtfs.model.AgencyAndId)8 FeedEntity (com.google.transit.realtime.GtfsRealtime.FeedEntity)7 FeedMessage (com.google.transit.realtime.GtfsRealtime.FeedMessage)6 StopTimeEvent (com.google.transit.realtime.GtfsRealtime.TripUpdate.StopTimeEvent)6 TimetableSnapshot (org.opentripplanner.routing.edgetype.TimetableSnapshot)6 TripPattern (org.opentripplanner.routing.edgetype.TripPattern)5 ArrayList (java.util.ArrayList)4 TimepointPredictionRecord (org.onebusaway.realtime.api.TimepointPredictionRecord)4 Timetable (org.opentripplanner.routing.edgetype.Timetable)4 TripTimes (org.opentripplanner.routing.trippattern.TripTimes)4