use of org.onebusaway.transit_data_federation.services.transit_graph.BlockStopTimeEntry in project onebusaway-application-modules by camsys.
the class StopScheduleBeanServiceImpl method groupStopTimeInstancesByRouteCollectionId.
private void groupStopTimeInstancesByRouteCollectionId(StopEntry stopEntry, ServiceDate date, Map<AgencyAndId, List<StopTimeInstance>> stopTimesByRouteCollectionId, Map<AgencyAndId, List<StopTimeInstance>> frequenciesByRouteCollectionId) {
Map<AgencyAndId, Set<FrequencyEntry>> frequencyLabelsByRouteCollectionId = new FactoryMap<AgencyAndId, Set<FrequencyEntry>>(new HashSet<FrequencyEntry>());
for (BlockStopTimeIndex index : _blockIndexService.getStopTimeIndicesForStop(stopEntry)) {
ServiceIdActivation serviceIds = index.getServiceIds();
Set<ServiceDate> serviceDates = _calendarService.getServiceDatesForServiceIds(serviceIds);
if (!serviceDates.contains(date))
continue;
Date serviceDate = date.getAsDate(serviceIds.getTimeZone());
for (BlockStopTimeEntry stopTime : index.getStopTimes()) {
BlockTripEntry blockTrip = stopTime.getTrip();
TripEntry trip = blockTrip.getTrip();
AgencyAndId routeCollectionId = trip.getRouteCollection().getId();
FrequencyEntry frequencyLabel = trip.getFrequencyLabel();
InstanceState state = new InstanceState(serviceDate.getTime(), frequencyLabel);
StopTimeInstance sti = new StopTimeInstance(stopTime, state);
if (frequencyLabel == null) {
stopTimesByRouteCollectionId.get(routeCollectionId).add(sti);
} else if (frequencyLabelsByRouteCollectionId.get(routeCollectionId).add(frequencyLabel)) {
frequenciesByRouteCollectionId.get(routeCollectionId).add(sti);
}
}
}
}
use of org.onebusaway.transit_data_federation.services.transit_graph.BlockStopTimeEntry in project onebusaway-application-modules by camsys.
the class BlockGeospatialServiceImpl method groupBlockSequenceIndicesByShapeIds.
/**
**
* Private Methods
***
*/
private void groupBlockSequenceIndicesByShapeIds() {
List<BlockSequenceIndex> indices = _blockIndexService.getAllBlockSequenceIndices();
for (BlockSequenceIndex index : indices) {
Set<AgencyAndId> shapeIdsForIndex = new HashSet<AgencyAndId>();
for (BlockSequence sequence : index.getSequences()) {
for (BlockStopTimeEntry bst : sequence.getStopTimes()) {
BlockTripEntry blockTrip = bst.getTrip();
TripEntry trip = blockTrip.getTrip();
AgencyAndId shapeId = trip.getShapeId();
if (shapeId != null)
shapeIdsForIndex.add(shapeId);
}
}
for (AgencyAndId shapeId : shapeIdsForIndex) {
List<BlockSequenceIndex> list = _blockSequenceIndicesByShapeId.get(shapeId);
if (list == null) {
list = new ArrayList<BlockSequenceIndex>();
_blockSequenceIndicesByShapeId.put(shapeId, list);
}
list.add(index);
}
}
}
use of org.onebusaway.transit_data_federation.services.transit_graph.BlockStopTimeEntry 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);
}
use of org.onebusaway.transit_data_federation.services.transit_graph.BlockStopTimeEntry 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;
}
use of org.onebusaway.transit_data_federation.services.transit_graph.BlockStopTimeEntry in project onebusaway-application-modules by camsys.
the class StopTimeServiceImpl method getStopTimesForStopAndServiceDateAndTimeRange.
private int getStopTimesForStopAndServiceDateAndTimeRange(HasIndexedBlockStopTimes index, Date serviceDate, Date from, Date to, List<StopTimeInstance> instances) {
List<BlockStopTimeEntry> blockStopTimes = index.getStopTimes();
int relativeFrom = effectiveTime(serviceDate, from);
int relativeTo = effectiveTime(serviceDate, to);
int fromIndex = GenericBinarySearch.search(index, blockStopTimes.size(), relativeFrom, IndexAdapters.BLOCK_STOP_TIME_DEPARTURE_INSTANCE);
int toIndex = GenericBinarySearch.search(index, blockStopTimes.size(), relativeTo, IndexAdapters.BLOCK_STOP_TIME_ARRIVAL_INSTANCE);
InstanceState state = new InstanceState(serviceDate.getTime());
for (int in = fromIndex; in < toIndex; in++) {
BlockStopTimeEntry blockStopTime = blockStopTimes.get(in);
instances.add(new StopTimeInstance(blockStopTime, state));
}
return fromIndex;
}
Aggregations