Search in sources :

Example 6 with TripEntry

use of org.onebusaway.transit_data_federation.services.transit_graph.TripEntry in project onebusaway-application-modules by camsys.

the class GtfsRealtimeEntitySource method getTrip.

public TripEntry getTrip(String tripId) {
    for (String agencyId : _agencyIds) {
        AgencyAndId id = new AgencyAndId(agencyId, tripId);
        TripEntry trip = _transitGraphDao.getTripEntryForId(id);
        if (trip != null)
            return trip;
    }
    try {
        AgencyAndId id = AgencyAndId.convertFromString(tripId);
        TripEntry trip = _transitGraphDao.getTripEntryForId(id);
        if (trip != null)
            return trip;
    } catch (IllegalArgumentException ex) {
    }
    return null;
}
Also used : AgencyAndId(org.onebusaway.gtfs.model.AgencyAndId) TripEntry(org.onebusaway.transit_data_federation.services.transit_graph.TripEntry)

Example 7 with TripEntry

use of org.onebusaway.transit_data_federation.services.transit_graph.TripEntry in project onebusaway-application-modules by camsys.

the class GtfsRealtimeTripLibrary method getTripStartTime.

private long getTripStartTime(String tripId) {
    TripEntry tripEntry = _entitySource.getTrip(tripId);
    long min = Long.MAX_VALUE;
    if (tripEntry == null)
        return min;
    for (StopTimeEntry stopTime : tripEntry.getStopTimes()) {
        if (stopTime.getArrivalTime() < min)
            min = stopTime.getArrivalTime();
    }
    return min;
}
Also used : StopTimeEntry(org.onebusaway.transit_data_federation.services.transit_graph.StopTimeEntry) BlockStopTimeEntry(org.onebusaway.transit_data_federation.services.transit_graph.BlockStopTimeEntry) TripEntry(org.onebusaway.transit_data_federation.services.transit_graph.TripEntry) BlockTripEntry(org.onebusaway.transit_data_federation.services.transit_graph.BlockTripEntry)

Example 8 with TripEntry

use of org.onebusaway.transit_data_federation.services.transit_graph.TripEntry in project onebusaway-application-modules by camsys.

the class GtfsRealtimeTripLibrary method getTripDescriptorAsBlockDescriptor.

private BlockDescriptor getTripDescriptorAsBlockDescriptor(MonitoredResult result, TripDescriptor trip, long currentTime) {
    if (!trip.hasTripId()) {
        return null;
    }
    TripEntry tripEntry = _entitySource.getTrip(trip.getTripId());
    if (tripEntry == null) {
        if (result != null) {
            _log.debug("reporting unmatched trip with id=" + trip.getTripId());
            result.addUnmatchedTripId(trip.getTripId());
        } else {
            _log.warn("no trip found with id=" + trip.getTripId());
        }
        return null;
    }
    ServiceDate serviceDate = null;
    BlockInstance instance;
    BlockEntry block = tripEntry.getBlock();
    if (trip.hasStartDate() && !"0".equals(trip.getStartDate())) {
        try {
            serviceDate = ServiceDate.parseString(trip.getStartDate());
        } catch (ParseException ex) {
            _log.warn("Could not parse service date " + trip.getStartDate(), ex);
        }
    }
    if (serviceDate != null) {
        instance = _blockCalendarService.getBlockInstance(block.getId(), serviceDate.getAsDate().getTime());
        if (instance == null) {
            _log.warn("block " + block.getId() + " does not exist on service date " + serviceDate);
            return null;
        }
    } else {
        long timeFrom = currentTime - 30 * 60 * 1000;
        long timeTo = currentTime + 30 * 60 * 1000;
        List<BlockInstance> instances = _blockCalendarService.getActiveBlocks(block.getId(), timeFrom, timeTo);
        if (instances.isEmpty()) {
            instances = _blockCalendarService.getClosestActiveBlocks(block.getId(), currentTime);
        }
        if (instances.isEmpty()) {
            _log.warn("could not find any active instances for the specified block=" + block.getId() + " trip=" + trip);
            return null;
        }
        instance = instances.get(0);
    }
    if (serviceDate == null) {
        serviceDate = new ServiceDate(new Date(instance.getServiceDate()));
    }
    BlockDescriptor blockDescriptor = new BlockDescriptor();
    blockDescriptor.setBlockInstance(instance);
    blockDescriptor.setStartDate(serviceDate);
    if (trip.hasScheduleRelationship()) {
        blockDescriptor.setScheduleRelationshipValue(trip.getScheduleRelationship().toString());
    }
    int tripStartTime = 0;
    int blockStartTime = 0;
    if (trip.hasStartTime() && !"0".equals(trip.getStartTime())) {
        try {
            tripStartTime = StopTimeFieldMappingFactory.getStringAsSeconds(trip.getStartTime());
        } catch (InvalidStopTimeException iste) {
            _log.error("invalid stopTime of " + trip.getStartTime() + " for trip " + trip);
        }
        blockStartTime = getBlockStartTimeForTripStartTime(instance, tripEntry.getId(), tripStartTime);
        blockDescriptor.setStartTime(blockStartTime);
    }
    return blockDescriptor;
}
Also used : ServiceDate(org.onebusaway.gtfs.model.calendar.ServiceDate) BlockEntry(org.onebusaway.transit_data_federation.services.transit_graph.BlockEntry) InvalidStopTimeException(org.onebusaway.gtfs.serialization.mappings.InvalidStopTimeException) BlockInstance(org.onebusaway.transit_data_federation.services.blocks.BlockInstance) TripEntry(org.onebusaway.transit_data_federation.services.transit_graph.TripEntry) BlockTripEntry(org.onebusaway.transit_data_federation.services.transit_graph.BlockTripEntry) ParseException(java.text.ParseException) Date(java.util.Date) ServiceDate(org.onebusaway.gtfs.model.calendar.ServiceDate) CoordinatePoint(org.onebusaway.geospatial.model.CoordinatePoint)

Example 9 with TripEntry

use of org.onebusaway.transit_data_federation.services.transit_graph.TripEntry in project onebusaway-application-modules by camsys.

the class GtfsRealtimeTripLibrary method applyTripUpdatesToRecord.

private void applyTripUpdatesToRecord(MonitoredResult result, BlockDescriptor blockDescriptor, List<TripUpdate> tripUpdates, VehicleLocationRecord record, String vehicleId, String bestTripId) {
    BlockInstance instance = blockDescriptor.getBlockInstance();
    BlockConfigurationEntry blockConfiguration = instance.getBlock();
    List<BlockTripEntry> blockTrips = blockConfiguration.getTrips();
    Map<String, List<TripUpdate>> tripUpdatesByTripId = MappingLibrary.mapToValueList(tripUpdates, "trip.tripId");
    long t = currentTime();
    int currentTime = (int) ((t - instance.getServiceDate()) / 1000);
    BestScheduleDeviation best = new BestScheduleDeviation();
    long lastStopScheduleTime = Long.MIN_VALUE;
    boolean singleTimepointRecord = false;
    List<TimepointPredictionRecord> timepointPredictions = new ArrayList<TimepointPredictionRecord>();
    for (BlockTripEntry blockTrip : blockTrips) {
        TripEntry trip = blockTrip.getTrip();
        AgencyAndId tripId = trip.getId();
        List<TripUpdate> updatesForTrip = tripUpdatesByTripId.get(tripId.getId());
        boolean tripUpdateHasDelay = false;
        // onBestTrip is only relevant if bestTripId is set, which indicates that the TripUpdates
        // came from the vehicleId map (as opposed to block index).
        boolean onBestTrip = bestTripId == null || tripId.getId().equals(bestTripId);
        if (updatesForTrip != null) {
            for (TripUpdate tripUpdate : updatesForTrip) {
                /**
                 * TODO: delete this code once all upstream systems have been
                 * migrated the new "delay" and "timestamp" fields.
                 */
                if (tripUpdate.hasExtension(GtfsRealtimeOneBusAway.obaTripUpdate) && onBestTrip) {
                    OneBusAwayTripUpdate obaTripUpdate = tripUpdate.getExtension(GtfsRealtimeOneBusAway.obaTripUpdate);
                    if (obaTripUpdate.hasDelay()) {
                        /**
                         * TODO: Improved logic around picking the "best" schedule deviation
                         */
                        int delay = obaTripUpdate.getDelay();
                        best.delta = 0;
                        best.isInPast = false;
                        best.scheduleDeviation = delay;
                        best.tripId = tripId;
                        tripUpdateHasDelay = true;
                    }
                    if (obaTripUpdate.hasTimestamp() && onBestTrip) {
                        best.timestamp = obaTripUpdate.getTimestamp() * 1000;
                    }
                }
                if (tripUpdate.hasDelay() && onBestTrip) {
                    /**
                     * TODO: Improved logic around picking the "best" schedule deviation
                     */
                    best.delta = 0;
                    best.isInPast = false;
                    best.scheduleDeviation = tripUpdate.getDelay();
                    best.tripId = tripId;
                    tripUpdateHasDelay = true;
                }
                if (tripUpdate.hasTimestamp() && onBestTrip) {
                    best.timestamp = tripUpdate.getTimestamp() * 1000;
                }
                for (StopTimeUpdate stopTimeUpdate : tripUpdate.getStopTimeUpdateList()) {
                    BlockStopTimeEntry blockStopTime = getBlockStopTimeForStopTimeUpdate(result, tripUpdate, stopTimeUpdate, blockTrip.getStopTimes(), instance.getServiceDate());
                    // loop through and store last stop time on trip
                    List<BlockStopTimeEntry> stopTimes = blockTrip.getStopTimes();
                    for (BlockStopTimeEntry bste : stopTimes) {
                        long scheduleTime = instance.getServiceDate() + bste.getStopTime().getArrivalTime() * 1000;
                        if (scheduleTime > lastStopScheduleTime) {
                            lastStopScheduleTime = scheduleTime;
                        }
                    }
                    if (blockStopTime == null)
                        continue;
                    StopTimeEntry stopTime = blockStopTime.getStopTime();
                    TimepointPredictionRecord tpr = new TimepointPredictionRecord();
                    tpr.setTimepointId(stopTime.getStop().getId());
                    tpr.setTripId(stopTime.getTrip().getId());
                    tpr.setTimepointScheduledTime(instance.getServiceDate() + stopTime.getArrivalTime() * 1000);
                    if (stopTimeUpdate.hasStopSequence()) {
                        tpr.setStopSequence(stopTimeUpdate.getStopSequence());
                    }
                    int currentArrivalTime = computeArrivalTime(stopTime, stopTimeUpdate, instance.getServiceDate());
                    int currentDepartureTime = computeDepartureTime(stopTime, stopTimeUpdate, instance.getServiceDate());
                    if (currentArrivalTime >= 0) {
                        if (onBestTrip) {
                            updateBestScheduleDeviation(currentTime, stopTime.getArrivalTime(), currentArrivalTime, best, tripId, vehicleId);
                        }
                        long timepointPredictedTime = instance.getServiceDate() + (currentArrivalTime * 1000L);
                        tpr.setTimepointPredictedArrivalTime(timepointPredictedTime);
                    }
                    if (currentDepartureTime >= 0) {
                        if (onBestTrip) {
                            updateBestScheduleDeviation(currentTime, stopTime.getDepartureTime(), currentDepartureTime, best, tripId, vehicleId);
                        }
                        long timepointPredictedTime = instance.getServiceDate() + (currentDepartureTime * 1000L);
                        tpr.setTimepointPredictedDepartureTime(timepointPredictedTime);
                    }
                    if (tpr.getTimepointPredictedArrivalTime() != -1 || tpr.getTimepointPredictedDepartureTime() != -1) {
                        timepointPredictions.add(tpr);
                    }
                }
            }
        }
        if (timepointPredictions.size() == 1 && tripUpdates.get(0).getStopTimeUpdateList().size() == 1) {
            singleTimepointRecord = true;
        }
        // tripUpdateHasDelay = true => best.scheduleDeviation is TripUpdate delay
        if ((timepointPredictions.size() > 0 && tripUpdateHasDelay) || singleTimepointRecord) {
            Set<AgencyAndId> records = new HashSet<AgencyAndId>();
            for (TimepointPredictionRecord tpr : timepointPredictions) {
                records.add(tpr.getTimepointId());
            }
            long tprStartTime = getEarliestTimeInRecords(timepointPredictions);
            for (StopTimeEntry stopTime : trip.getStopTimes()) {
                if (records.contains(stopTime.getStop().getId())) {
                    continue;
                }
                long predictionOffset = instance.getServiceDate() + (best.scheduleDeviation * 1000L);
                long predictedDepartureTime = (stopTime.getDepartureTime() * 1000L) + predictionOffset;
                long predictedArrivalTime = (stopTime.getArrivalTime() * 1000L) + predictionOffset;
                long scheduledArrivalTime = instance.getServiceDate() + stopTime.getArrivalTime() * 1000;
                long time = best.timestamp != 0 ? best.timestamp : currentTime();
                /*
             * if the timpepointrecord needs interpolated (one before, one after),
             * OR
             * we have a single Timepoint record and the arrival is
              * in the future and before the last stop
             */
                if ((predictedDepartureTime > time && predictedDepartureTime < tprStartTime) || (singleTimepointRecord && (predictedDepartureTime > time && scheduledArrivalTime <= lastStopScheduleTime))) {
                    TimepointPredictionRecord tpr = new TimepointPredictionRecord();
                    tpr.setTimepointId(stopTime.getStop().getId());
                    tpr.setTripId(stopTime.getTrip().getId());
                    tpr.setStopSequence(stopTime.getGtfsSequence());
                    tpr.setTimepointPredictedArrivalTime(predictedArrivalTime);
                    tpr.setTimepointPredictedDepartureTime(predictedDepartureTime);
                    tpr.setTimepointScheduledTime(scheduledArrivalTime);
                    timepointPredictions.add(tpr);
                }
            }
        }
    }
    record.setServiceDate(instance.getServiceDate());
    if (blockDescriptor.getStartTime() != null) {
        record.setBlockStartTime(blockDescriptor.getStartTime());
    }
    record.setScheduleDeviation(best.scheduleDeviation);
    if (best.timestamp != 0) {
        record.setTimeOfRecord(best.timestamp);
    }
    record.setTimepointPredictions(timepointPredictions);
}
Also used : AgencyAndId(org.onebusaway.gtfs.model.AgencyAndId) BlockTripEntry(org.onebusaway.transit_data_federation.services.transit_graph.BlockTripEntry) ArrayList(java.util.ArrayList) StopTimeEntry(org.onebusaway.transit_data_federation.services.transit_graph.StopTimeEntry) BlockStopTimeEntry(org.onebusaway.transit_data_federation.services.transit_graph.BlockStopTimeEntry) StopTimeUpdate(com.google.transit.realtime.GtfsRealtime.TripUpdate.StopTimeUpdate) List(java.util.List) ArrayList(java.util.ArrayList) BlockConfigurationEntry(org.onebusaway.transit_data_federation.services.transit_graph.BlockConfigurationEntry) BlockStopTimeEntry(org.onebusaway.transit_data_federation.services.transit_graph.BlockStopTimeEntry) HashSet(java.util.HashSet) TripUpdate(com.google.transit.realtime.GtfsRealtime.TripUpdate) OneBusAwayTripUpdate(com.google.transit.realtime.GtfsRealtimeOneBusAway.OneBusAwayTripUpdate) TripEntry(org.onebusaway.transit_data_federation.services.transit_graph.TripEntry) BlockTripEntry(org.onebusaway.transit_data_federation.services.transit_graph.BlockTripEntry) CoordinatePoint(org.onebusaway.geospatial.model.CoordinatePoint) BlockInstance(org.onebusaway.transit_data_federation.services.blocks.BlockInstance) TimepointPredictionRecord(org.onebusaway.realtime.api.TimepointPredictionRecord) OneBusAwayTripUpdate(com.google.transit.realtime.GtfsRealtimeOneBusAway.OneBusAwayTripUpdate)

Example 10 with TripEntry

use of org.onebusaway.transit_data_federation.services.transit_graph.TripEntry in project onebusaway-application-modules by camsys.

the class BlockBeanServiceImpl method getBlockTripAsBean.

@Override
public BlockTripBean getBlockTripAsBean(BlockTripEntry blockTrip) {
    TripEntry trip = blockTrip.getTrip();
    TripBean tripBean = _tripBeanService.getTripForId(trip.getId());
    if (tripBean == null)
        throw new IllegalStateException("unknown trip: " + trip.getId());
    BlockTripBean bean = new BlockTripBean();
    bean.setTrip(tripBean);
    bean.setAccumulatedSlackTime(blockTrip.getAccumulatedSlackTime());
    bean.setDistanceAlongBlock(blockTrip.getDistanceAlongBlock());
    List<BlockStopTimeBean> blockStopTimes = new ArrayList<BlockStopTimeBean>();
    for (BlockStopTimeEntry blockStopTime : blockTrip.getStopTimes()) {
        BlockStopTimeBean blockStopTimeAsBean = getBlockStopTimeAsBean(blockStopTime);
        blockStopTimes.add(blockStopTimeAsBean);
    }
    bean.setBlockStopTimes(blockStopTimes);
    return bean;
}
Also used : BlockTripBean(org.onebusaway.transit_data.model.blocks.BlockTripBean) ArrayList(java.util.ArrayList) TripEntry(org.onebusaway.transit_data_federation.services.transit_graph.TripEntry) BlockTripEntry(org.onebusaway.transit_data_federation.services.transit_graph.BlockTripEntry) BlockTripBean(org.onebusaway.transit_data.model.blocks.BlockTripBean) TripBean(org.onebusaway.transit_data.model.trips.TripBean) BlockStopTimeBean(org.onebusaway.transit_data.model.blocks.BlockStopTimeBean) BlockStopTimeEntry(org.onebusaway.transit_data_federation.services.transit_graph.BlockStopTimeEntry)

Aggregations

TripEntry (org.onebusaway.transit_data_federation.services.transit_graph.TripEntry)73 AgencyAndId (org.onebusaway.gtfs.model.AgencyAndId)46 BlockTripEntry (org.onebusaway.transit_data_federation.services.transit_graph.BlockTripEntry)39 ArrayList (java.util.ArrayList)20 BlockStopTimeEntry (org.onebusaway.transit_data_federation.services.transit_graph.BlockStopTimeEntry)18 StopTimeEntry (org.onebusaway.transit_data_federation.services.transit_graph.StopTimeEntry)15 StopEntry (org.onebusaway.transit_data_federation.services.transit_graph.StopEntry)14 HashSet (java.util.HashSet)13 TripEntryImpl (org.onebusaway.transit_data_federation.impl.transit_graph.TripEntryImpl)10 BlockEntry (org.onebusaway.transit_data_federation.services.transit_graph.BlockEntry)10 List (java.util.List)9 BlockInstance (org.onebusaway.transit_data_federation.services.blocks.BlockInstance)9 Date (java.util.Date)7 Test (org.junit.Test)7 CoordinatePoint (org.onebusaway.geospatial.model.CoordinatePoint)7 BlockConfigurationEntry (org.onebusaway.transit_data_federation.services.transit_graph.BlockConfigurationEntry)7 FrequencyEntry (org.onebusaway.transit_data_federation.services.transit_graph.FrequencyEntry)7 Cacheable (org.onebusaway.container.cache.Cacheable)6 RouteEntry (org.onebusaway.transit_data_federation.services.transit_graph.RouteEntry)6 ServiceIdActivation (org.onebusaway.transit_data_federation.services.transit_graph.ServiceIdActivation)6