Search in sources :

Example 26 with TripUpdate

use of com.google.transit.realtime.GtfsRealtime.TripUpdate in project onebusaway-application-modules by camsys.

the class GtfsRealtimeTripLibraryTest method testCreateVehicleLocationRecordForUpdate_FutureDay.

// Ensure that if we get an update for a future day we propagate a prediction for that day.
// (This is equivalent to timestamp on feed being early incorrectly, since currentTime in
// GtfsRealtimeTripLibrary is set via the timestamp.)
@Test
public void testCreateVehicleLocationRecordForUpdate_FutureDay() {
    final long day = TimeUnit.DAYS.toMillis(1);
    StopTimeUpdate.Builder stopTimeUpdate = stopTimeUpdateWithDepartureDelay("stopA", 180);
    TripUpdate.Builder tripUpdate = tripUpdate("tripA", (_library.getCurrentTime() + day) / 1000, 120, stopTimeUpdate);
    TripEntryImpl tripA = trip("tripA");
    stopTime(0, stop("stopA", 0, 0), tripA, time(7, 30), 0.0);
    BlockEntryImpl blockA = block("blockA");
    BlockConfigurationEntry blockConfigA = blockConfiguration(blockA, serviceIds("s1"), tripA);
    BlockInstance blockInstanceA = new BlockInstance(blockConfigA, 0L);
    BlockInstance blockInstanceB = new BlockInstance(blockConfigA, day);
    Mockito.when(_entitySource.getTrip("tripA")).thenReturn(tripA);
    Mockito.when(_blockCalendarService.getActiveBlocks(Mockito.eq(blockA.getId()), Mockito.anyLong(), Mockito.longThat(new ArgumentMatcher<Long>() {

        @Override
        public boolean matches(Object argument) {
            return ((Long) argument) < day;
        }
    }))).thenReturn(Arrays.asList(blockInstanceA));
    Mockito.when(_blockCalendarService.getActiveBlocks(Mockito.eq(blockA.getId()), Mockito.anyLong(), Mockito.longThat(new ArgumentMatcher<Long>() {

        @Override
        public boolean matches(Object argument) {
            return ((Long) argument) >= day;
        }
    }))).thenReturn(Arrays.asList(blockInstanceB));
    FeedMessage.Builder TU = createFeed();
    TU.addEntity(feed(tripUpdate));
    FeedMessage.Builder VP = createFeed();
    List<CombinedTripUpdatesAndVehiclePosition> updates = _library.groupTripUpdatesAndVehiclePositions(TU.build(), VP.build());
    CombinedTripUpdatesAndVehiclePosition update = updates.get(0);
    VehicleLocationRecord record = _library.createVehicleLocationRecordForUpdate(update);
    TimepointPredictionRecord tpr = record.getTimepointPredictions().get(0);
    long departure = tpr.getTimepointPredictedDepartureTime();
    // 7:30 + 3 min delay + on next day
    assertEquals(departure, time(7, 33) * 1000 + day);
}
Also used : TripUpdate(com.google.transit.realtime.GtfsRealtime.TripUpdate) TripEntryImpl(org.onebusaway.transit_data_federation.impl.transit_graph.TripEntryImpl) FeedMessage(com.google.transit.realtime.GtfsRealtime.FeedMessage) BlockInstance(org.onebusaway.transit_data_federation.services.blocks.BlockInstance) ArgumentMatcher(org.mockito.ArgumentMatcher) StopTimeUpdate(com.google.transit.realtime.GtfsRealtime.TripUpdate.StopTimeUpdate) VehicleLocationRecord(org.onebusaway.realtime.api.VehicleLocationRecord) TimepointPredictionRecord(org.onebusaway.realtime.api.TimepointPredictionRecord) BlockEntryImpl(org.onebusaway.transit_data_federation.impl.transit_graph.BlockEntryImpl) BlockConfigurationEntry(org.onebusaway.transit_data_federation.services.transit_graph.BlockConfigurationEntry) Test(org.junit.Test)

Example 27 with TripUpdate

use of com.google.transit.realtime.GtfsRealtime.TripUpdate in project onebusaway-application-modules by camsys.

the class GtfsRealtimeTripLibraryTest method testCreateVehicleLocationRecordForUpdate_NoStopTimeUpdates.

@Test
public void testCreateVehicleLocationRecordForUpdate_NoStopTimeUpdates() {
    TripUpdate tripUpdate = TripUpdate.newBuilder().setTrip(TripDescriptor.newBuilder().setTripId("tripA")).setDelay(120).setTimestamp(123456789).build();
    TripEntryImpl tripA = trip("tripA");
    stopTime(0, stop("stopA", 0, 0), tripA, time(7, 30), 0.0);
    BlockEntryImpl blockA = block("blockA");
    BlockConfigurationEntry blockConfigA = blockConfiguration(blockA, serviceIds("s1"), tripA);
    BlockInstance blockInstanceA = new BlockInstance(blockConfigA, 0L);
    Mockito.when(_blockCalendarService.getActiveBlocks(Mockito.eq(blockA.getId()), Mockito.anyLong(), Mockito.anyLong())).thenReturn(Arrays.asList(blockInstanceA));
    CombinedTripUpdatesAndVehiclePosition update = new CombinedTripUpdatesAndVehiclePosition();
    update.block = new BlockDescriptor();
    update.block.setBlockInstance(blockInstanceA);
    update.tripUpdates = Arrays.asList(tripUpdate);
    VehicleLocationRecord record = _library.createVehicleLocationRecordForUpdate(update);
    assertEquals(123456789000L, record.getTimeOfRecord());
    assertEquals(120, record.getScheduleDeviation(), 0.0);
}
Also used : TripUpdate(com.google.transit.realtime.GtfsRealtime.TripUpdate) BlockInstance(org.onebusaway.transit_data_federation.services.blocks.BlockInstance) VehicleLocationRecord(org.onebusaway.realtime.api.VehicleLocationRecord) BlockEntryImpl(org.onebusaway.transit_data_federation.impl.transit_graph.BlockEntryImpl) BlockConfigurationEntry(org.onebusaway.transit_data_federation.services.transit_graph.BlockConfigurationEntry) TripEntryImpl(org.onebusaway.transit_data_federation.impl.transit_graph.TripEntryImpl) Test(org.junit.Test)

Example 28 with TripUpdate

use of com.google.transit.realtime.GtfsRealtime.TripUpdate in project onebusaway-application-modules by camsys.

the class GtfsRealtimeTripLibraryTest method testCreateVehicleLocationRecordForUpdate_WithStopTimeUpdates.

@Test
public void testCreateVehicleLocationRecordForUpdate_WithStopTimeUpdates() {
    StopTimeUpdate.Builder stopTimeUpdate = StopTimeUpdate.newBuilder();
    stopTimeUpdate.setStopId("stopA");
    StopTimeEvent.Builder stopTimeEvent = StopTimeEvent.newBuilder();
    stopTimeEvent.setDelay(180);
    stopTimeUpdate.setDeparture(stopTimeEvent);
    TripUpdate tripUpdate = TripUpdate.newBuilder().setTrip(TripDescriptor.newBuilder().setTripId("tripA")).setDelay(120).setTimestamp(123456789).addStopTimeUpdate(stopTimeUpdate).build();
    TripEntryImpl tripA = trip("tripA");
    stopTime(0, stop("stopA", 0, 0), tripA, time(7, 30), 0.0);
    BlockEntryImpl blockA = block("blockA");
    BlockConfigurationEntry blockConfigA = blockConfiguration(blockA, serviceIds("s1"), tripA);
    BlockInstance blockInstanceA = new BlockInstance(blockConfigA, 0L);
    Mockito.when(_blockCalendarService.getActiveBlocks(Mockito.eq(blockA.getId()), Mockito.anyLong(), Mockito.anyLong())).thenReturn(Arrays.asList(blockInstanceA));
    CombinedTripUpdatesAndVehiclePosition update = new CombinedTripUpdatesAndVehiclePosition();
    update.block = new BlockDescriptor();
    update.block.setBlockInstance(blockInstanceA);
    update.tripUpdates = Arrays.asList(tripUpdate);
    VehicleLocationRecord record = _library.createVehicleLocationRecordForUpdate(update);
    TimepointPredictionRecord tpr = record.getTimepointPredictions().get(0);
    long departure = tpr.getTimepointPredictedDepartureTime();
    // 7:30 plus 3 min delay, + now we are in ms.
    assertEquals(departure, time(7, 33) * 1000);
}
Also used : TripUpdate(com.google.transit.realtime.GtfsRealtime.TripUpdate) TripEntryImpl(org.onebusaway.transit_data_federation.impl.transit_graph.TripEntryImpl) StopTimeEvent(com.google.transit.realtime.GtfsRealtime.TripUpdate.StopTimeEvent) BlockInstance(org.onebusaway.transit_data_federation.services.blocks.BlockInstance) StopTimeUpdate(com.google.transit.realtime.GtfsRealtime.TripUpdate.StopTimeUpdate) VehicleLocationRecord(org.onebusaway.realtime.api.VehicleLocationRecord) TimepointPredictionRecord(org.onebusaway.realtime.api.TimepointPredictionRecord) BlockEntryImpl(org.onebusaway.transit_data_federation.impl.transit_graph.BlockEntryImpl) BlockConfigurationEntry(org.onebusaway.transit_data_federation.services.transit_graph.BlockConfigurationEntry) Test(org.junit.Test)

Example 29 with TripUpdate

use of com.google.transit.realtime.GtfsRealtime.TripUpdate in project onebusaway-application-modules by camsys.

the class GtfsRealtimeTripLibraryTest method testTprInterpolation_2.

/**
 * Test that we do NOT create new timepoint prediction record when it
 * already exists.
 *
 * Current time = 7:25. Trip update delay = 2 minutes
 *          Schedule time    Real-time from feed  Timepoint predicted departure time
 * Stop A   7:30             7:33                 7:33 (and only one)
 */
@Test
public void testTprInterpolation_2() {
    _library.setCurrentTime(time(7, 25) * 1000);
    TripEntryImpl tripA = trip("tripA");
    stopTime(0, stop("stopA", 0, 0), tripA, time(7, 30), 0.0);
    BlockEntryImpl blockA = block("blockA");
    BlockConfigurationEntry blockConfigA = blockConfiguration(blockA, serviceIds("s1"), tripA);
    BlockInstance blockInstanceA = new BlockInstance(blockConfigA, 0L);
    StopTimeUpdate.Builder stopTimeUpdate = stopTimeUpdateWithDepartureDelay("stopA", 180);
    TripUpdate.Builder tripUpdate = tripUpdate("tripA", _library.getCurrentTime() / 1000, 120, stopTimeUpdate);
    Mockito.when(_entitySource.getTrip("tripA")).thenReturn(tripA);
    Mockito.when(_blockCalendarService.getActiveBlocks(Mockito.eq(blockA.getId()), Mockito.anyLong(), Mockito.anyLong())).thenReturn(Arrays.asList(blockInstanceA));
    VehicleLocationRecord record = vehicleLocationRecord(tripUpdate);
    long stopADept = getPredictedDepartureTimeByStopId(record, "stopA");
    assertEquals(stopADept, time(7, 33) * 1000);
    assertEquals(record.getTimepointPredictions().size(), 1);
}
Also used : TripUpdate(com.google.transit.realtime.GtfsRealtime.TripUpdate) BlockInstance(org.onebusaway.transit_data_federation.services.blocks.BlockInstance) StopTimeUpdate(com.google.transit.realtime.GtfsRealtime.TripUpdate.StopTimeUpdate) VehicleLocationRecord(org.onebusaway.realtime.api.VehicleLocationRecord) BlockEntryImpl(org.onebusaway.transit_data_federation.impl.transit_graph.BlockEntryImpl) BlockConfigurationEntry(org.onebusaway.transit_data_federation.services.transit_graph.BlockConfigurationEntry) TripEntryImpl(org.onebusaway.transit_data_federation.impl.transit_graph.TripEntryImpl) Test(org.junit.Test)

Example 30 with TripUpdate

use of com.google.transit.realtime.GtfsRealtime.TripUpdate in project onebusaway-application-modules by camsys.

the class GtfsRealtimeTripLibrary method groupTripUpdatesAndVehiclePositions.

public List<CombinedTripUpdatesAndVehiclePosition> groupTripUpdatesAndVehiclePositions(MonitoredResult result, FeedMessage tripUpdateMessage, FeedMessage vehiclePositionsMessage) {
    List<CombinedTripUpdatesAndVehiclePosition> updates = new ArrayList<CombinedTripUpdatesAndVehiclePosition>();
    Map<String, TripUpdate> bestTripByVehicleId = new HashMap<String, TripUpdate>();
    ListMultimap<String, TripUpdate> tripUpdatesByVehicleId = ArrayListMultimap.create();
    Map<String, VehiclePosition> vehiclePositionsByVehicleId = new HashMap<String, VehiclePosition>();
    ListMultimap<BlockDescriptor, TripUpdate> anonymousTripUpdatesByBlock = ArrayListMultimap.<BlockDescriptor, TripUpdate>create();
    Map<BlockDescriptor, VehiclePosition> anonymousVehiclePositionsByBlock = new HashMap<BlockDescriptor, VehiclePosition>();
    Set<BlockDescriptor> badAnonymousVehiclePositions = new HashSet<BlockDescriptor>();
    for (FeedEntity fe : tripUpdateMessage.getEntityList()) {
        if (!fe.hasTripUpdate()) {
            continue;
        }
        TripUpdate tu = fe.getTripUpdate();
        if (tu.hasVehicle() && tu.getVehicle().hasId() && StringUtils.isNotBlank(tu.getVehicle().getId())) {
            // Trip update has a vehicle ID - index by vehicle ID
            String vehicleId = tu.getVehicle().getId();
            tripUpdatesByVehicleId.put(vehicleId, tu);
            if (!bestTripByVehicleId.containsKey(vehicleId)) {
                bestTripByVehicleId.put(vehicleId, tu);
            } else {
                // upcoming merge will fix this
                _log.debug("Multiple TripUpdates for vehicle {}; taking best.", vehicleId);
                if (tripMoreAppropriate(tu, bestTripByVehicleId.get(vehicleId), vehicleId)) {
                    bestTripByVehicleId.put(vehicleId, tu);
                }
            }
        } else {
            /*
         * Trip update does not have a vehicle ID - index by TripDescriptor
         * (includes start date and time).
         */
            TripDescriptor td = tu.getTrip();
            long time = tu.hasTimestamp() ? tu.getTimestamp() * 1000 : currentTime();
            BlockDescriptor bd = getTripDescriptorAsBlockDescriptor(result, td, time);
            if (bd == null) {
                continue;
            }
            if (!anonymousTripUpdatesByBlock.containsKey(bd)) {
                anonymousTripUpdatesByBlock.put(bd, tu);
            } else {
                _log.warn("Multiple anonymous TripUpdates for trip {}; will not map to VehiclePosition.", td.getTripId());
                anonymousTripUpdatesByBlock.put(bd, tu);
            }
        }
    }
    for (FeedEntity fe : vehiclePositionsMessage.getEntityList()) {
        if (!fe.hasVehicle()) {
            continue;
        }
        VehiclePosition vp = fe.getVehicle();
        if (vp.hasVehicle() && vp.getVehicle().hasId()) {
            // Vehicle position has a vehicle ID - index by vehicle ID
            String vehicleId = vp.getVehicle().getId();
            if (!vehiclePositionsByVehicleId.containsKey(vehicleId)) {
                vehiclePositionsByVehicleId.put(vehicleId, vp);
            } else {
                _log.warn("Multiple updates for vehicle {}; taking newest.", vehicleId);
                VehiclePosition otherUpdate = vehiclePositionsByVehicleId.get(vehicleId);
                long otherTimestamp = otherUpdate.getTimestamp();
                if (vp.getTimestamp() > otherTimestamp) {
                    vehiclePositionsByVehicleId.put(vehicleId, vp);
                }
            }
        } else if (vp.hasTrip()) {
            /*
         * Vehicle position does not have vehicle ID but has TripDescriptor, so
         * use that, but only if there is only one.
         */
            TripDescriptor td = vp.getTrip();
            long time = vp.hasTimestamp() ? vp.getTimestamp() * 1000 : currentTime();
            BlockDescriptor bd = getTripDescriptorAsBlockDescriptor(result, td, time);
            if (bd == null) {
                continue;
            }
            if (!anonymousVehiclePositionsByBlock.containsKey(bd)) {
                anonymousVehiclePositionsByBlock.put(bd, vp);
            } else {
                /*
           * When we have multiple VehiclePositions for a block but no way to
           * uniquely distinguish them there is nothing useful or reasonable we
           * can do with the data.
           */
                _log.warn("Multiple anonymous VehiclePositions for trip {}; giving up.", td.getTripId());
                badAnonymousVehiclePositions.add(bd);
            }
        } else {
            /*
         * Pathological VehiclePosition contains no identifying information;
         * skip.
         */
            continue;
        }
    }
    // a block
    for (BlockDescriptor bd : badAnonymousVehiclePositions) {
        anonymousVehiclePositionsByBlock.remove(bd);
    }
    // Map updates by vehicle ID
    for (Map.Entry<String, Collection<TripUpdate>> e : tripUpdatesByVehicleId.asMap().entrySet()) {
        CombinedTripUpdatesAndVehiclePosition update = new CombinedTripUpdatesAndVehiclePosition();
        String vehicleId = e.getKey();
        Collection<TripUpdate> tripUpdates = e.getValue();
        TripUpdate tu = bestTripByVehicleId.get(vehicleId);
        long time = tu.hasTimestamp() ? tu.getTimestamp() * 1000 : currentTime();
        update.block = getTripDescriptorAsBlockDescriptor(result, tu.getTrip(), time);
        update.tripUpdates = new ArrayList<TripUpdate>(tripUpdates);
        update.bestTrip = tu.getTrip().getTripId();
        if (vehiclePositionsByVehicleId.containsKey(vehicleId)) {
            update.vehiclePosition = vehiclePositionsByVehicleId.get(vehicleId);
        }
        updates.add(update);
    }
    // Map anonymous updates by block descriptor
    for (Entry<BlockDescriptor, Collection<TripUpdate>> e : anonymousTripUpdatesByBlock.asMap().entrySet()) {
        CombinedTripUpdatesAndVehiclePosition update = new CombinedTripUpdatesAndVehiclePosition();
        BlockDescriptor bd = e.getKey();
        update.block = bd;
        update.tripUpdates = new ArrayList<TripUpdate>(e.getValue());
        if (update.tripUpdates.size() == 1 && anonymousVehiclePositionsByBlock.containsKey(bd)) {
            update.vehiclePosition = anonymousVehiclePositionsByBlock.get(bd);
        }
        updates.add(update);
    }
    // Set vehicle ID in block if possible
    for (CombinedTripUpdatesAndVehiclePosition update : updates) {
        String vehicleId = null;
        for (TripUpdate tu : update.tripUpdates) {
            if (tu.hasVehicle() && tu.getVehicle().hasId()) {
                vehicleId = tu.getVehicle().getId();
                break;
            }
        }
        if (vehicleId == null && update.vehiclePosition != null && update.vehiclePosition.hasVehicle() && update.vehiclePosition.getVehicle().hasId()) {
            vehicleId = update.vehiclePosition.getVehicle().getId();
        }
        if (vehicleId != null && update.block != null) {
            update.block.setVehicleId(vehicleId);
        }
    }
    return updates;
}
Also used : TripUpdate(com.google.transit.realtime.GtfsRealtime.TripUpdate) OneBusAwayTripUpdate(com.google.transit.realtime.GtfsRealtimeOneBusAway.OneBusAwayTripUpdate) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) VehiclePosition(com.google.transit.realtime.GtfsRealtime.VehiclePosition) TripDescriptor(com.google.transit.realtime.GtfsRealtime.TripDescriptor) Collection(java.util.Collection) FeedEntity(com.google.transit.realtime.GtfsRealtime.FeedEntity) Map(java.util.Map) HashMap(java.util.HashMap) HashSet(java.util.HashSet)

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