use of org.onebusaway.transit_data_federation.services.realtime.ArrivalAndDepartureInstance in project onebusaway-application-modules by camsys.
the class ArrivalsAndDeparturesBeanServiceImpl method getArrivalAndDepartureForStop.
@Override
public ArrivalAndDepartureBean getArrivalAndDepartureForStop(ArrivalAndDepartureQuery query) {
long time = query.getTime();
ArrivalAndDepartureInstance instance = _arrivalAndDepartureService.getArrivalAndDepartureForStop(query);
if (instance == null) {
return null;
}
ArrivalAndDepartureBean bean = getStopTimeInstanceAsBean(time, instance, new HashMap<AgencyAndId, StopBean>());
applyBlockLocationToBean(instance, bean, time);
applySituationsToBean(time, instance, bean);
if (!this.useScheduleDeviationHistory) {
return bean;
}
int step = 120;
ScheduleDeviationHistogram histo = _realTimeHistoryService.getScheduleDeviationHistogramForArrivalAndDepartureInstance(instance, step);
if (histo != null) {
int[] sds = histo.getScheduleDeviations();
double[] values = new double[sds.length];
String[] labels = new String[sds.length];
for (int i = 0; i < sds.length; i++) {
int sd = sds[i];
values[i] = sd;
labels[i] = Integer.toString(sd / 60);
}
HistogramBean hb = new HistogramBean();
hb.setValues(values);
hb.setCounts(histo.getCounts());
hb.setLabels(labels);
bean.setScheduleDeviationHistogram(hb);
}
return bean;
}
use of org.onebusaway.transit_data_federation.services.realtime.ArrivalAndDepartureInstance in project onebusaway-application-modules by camsys.
the class UserReportingServiceImpl method getRecordAsBean.
private TripProblemReportBean getRecordAsBean(TripProblemReportRecord record) {
AgencyAndId stopId = record.getStopId();
AgencyAndId tripId = record.getTripId();
TripProblemReportBean bean = new TripProblemReportBean();
bean.setCode(record.getCode());
bean.setId(record.getId());
bean.setServiceDate(record.getServiceDate());
bean.setStatus(record.getStatus());
bean.setLabel(record.getLabel());
bean.setStopId(AgencyAndIdLibrary.convertToString(stopId));
bean.setTime(record.getTime());
bean.setTripId(AgencyAndIdLibrary.convertToString(tripId));
bean.setUserComment(record.getUserComment());
bean.setUserLat(record.getUserLat());
bean.setUserLon(record.getUserLon());
bean.setUserLocationAccuracy(record.getUserLocationAccuracy());
bean.setUserOnVehicle(record.isUserOnVehicle());
bean.setUserVehicleNumber(record.getUserVehicleNumber());
bean.setPredicted(record.isPredicted());
bean.setVehicleId(AgencyAndIdLibrary.convertToString(record.getVehicleId()));
bean.setDistanceAlongBlock(record.getDistanceAlongBlock());
bean.setScheduleDeviation(record.getScheduleDeviation());
bean.setVehicleLat(record.getVehicleLat());
bean.setVehicleLon(record.getVehicleLon());
if (stopId != null) {
try {
bean.setStop(_stopBeanService.getStopForId(stopId));
} catch (NoSuchStopServiceException ex) {
}
}
if (tripId != null) {
bean.setTrip(_tripBeanService.getTripForId(tripId));
}
if (tripId != null && stopId != null) {
TripEntry trip = _graph.getTripEntryForId(tripId);
StopEntry stop = _graph.getStopEntryForId(stopId);
if (trip != null && stop != null) {
AgencyAndId vehicleId = record.getMatchedVehicleId();
if (vehicleId == null)
vehicleId = record.getVehicleId();
ArrivalAndDepartureQuery query = new ArrivalAndDepartureQuery();
query.setStop(stop);
query.setStopSequence(-1);
query.setTrip(trip);
query.setServiceDate(record.getServiceDate());
query.setVehicleId(vehicleId);
query.setTime(record.getTime());
ArrivalAndDepartureInstance instance = _arrivalAndDepartureService.getArrivalAndDepartureForStop(query);
if (instance != null) {
StopTimeInstance sti = instance.getStopTimeInstance();
StopTimeInstanceBean stopTimeBean = _stopTimeBeanService.getStopTimeInstanceAsBean(sti);
bean.setStopTime(stopTimeBean);
}
}
}
return bean;
}
use of org.onebusaway.transit_data_federation.services.realtime.ArrivalAndDepartureInstance in project onebusaway-application-modules by camsys.
the class ArrivalAndDepartureServiceImplTest method testGetArrivalsAndDeparturesForStopInTimeRange01.
/**
* This method tests time point predictions upstream of a stop for *arrival*
* times.
*
* Test configuration: Time point predictions are upstream of the stop and
* include the given stop_ids, which means that the bus hasn't passed these
* bus stops yet. There are 2 bus stops which have the real time arrival times
* (time point predictions). In this case
* getArrivalsAndDeparturesForStopInTimeRange() should return the absolute
* time point prediction for particular stop that was provided by the feed,
* which replaces the scheduled time from GTFS for these stops.
*
* Current time = 13:00
* Schedule time Real-time from feed
* Stop A 13:30 13:30
* Stop B 13:40 13:50
*
* When requesting arrival estimate for Stop B, result should be 13:50 (same
* as exact prediction from real-time feed).
*/
@Test
public void testGetArrivalsAndDeparturesForStopInTimeRange01() {
// Set time point predictions for stop A
TimepointPredictionRecord tprA = new TimepointPredictionRecord();
tprA.setTimepointId(mStopA.getId());
long tprATime = createPredictedTime(time(13, 30));
tprA.setTimepointPredictedArrivalTime(tprATime);
tprA.setTripId(mTrip1.getId());
// Set time point predictions for stop B
TimepointPredictionRecord tprB = new TimepointPredictionRecord();
tprB.setTimepointId(mStopB.getId());
long tprBTime = createPredictedTime(time(13, 50));
tprB.setTimepointPredictedArrivalTime(tprBTime);
tprB.setTripId(mTrip1.getId());
// Call ArrivalsAndDeparturesForStopInTimeRange method in
// ArrivalAndDepartureServiceImpl
List<ArrivalAndDepartureInstance> arrivalsAndDepartures = getArrivalsAndDeparturesForStopInTimeRangeByTimepointPredictionRecord(Arrays.asList(tprA, tprB));
long predictedArrivalTime = getPredictedArrivalTimeByStopId(arrivalsAndDepartures, mStopB.getId());
/**
* Check if the predictedArrivalTime is exactly the same as the
* TimepointPrediction.
*/
assertEquals(tprB.getTimepointPredictedArrivalTime(), predictedArrivalTime);
}
use of org.onebusaway.transit_data_federation.services.realtime.ArrivalAndDepartureInstance in project onebusaway-application-modules by camsys.
the class ArrivalAndDepartureServiceImplTest method getArrivalsAndDeparturesForStopInTimeRangeForCancelledTrip.
/**
* Set up the BlockLocationServiceImpl for the test, using the given
* timepointPredictions
*
* This method creates a normal route with a single trip and two stops in a block
*
* stop_id trip_id stop_sequence
* A 1 0
* B 1 1
*
* @param timepointPredictions real-time predictions to apply to the
* BlockLocationServiceImpl
* @return a list of ArrivalAndDepartureInstances which is used to access
* predicted arrival/departure times for a stop, for comparison
* against the expected values
*/
private List<ArrivalAndDepartureInstance> getArrivalsAndDeparturesForStopInTimeRangeForCancelledTrip() {
TargetTime target = new TargetTime(mCurrentTime, mCurrentTime);
// Setup block
BlockEntryImpl block = block("blockA");
stopTime(0, mStopA, mTrip1, time(13, 30), time(13, 35), 1000);
stopTime(1, mStopB, mTrip1, time(13, 45), time(13, 50), 2000);
BlockConfigurationEntry blockConfig = blockConfiguration(block, serviceIds(lsids("sA"), lsids()), mTrip1);
BlockStopTimeEntry bstAA = blockConfig.getStopTimes().get(0);
BlockStopTimeEntry bstAB = blockConfig.getStopTimes().get(1);
BlockStopTimeEntry bstBA = blockConfig.getStopTimes().get(0);
// Setup block location instance for trip B
BlockInstance blockInstance = new BlockInstance(blockConfig, mServiceDate);
BlockLocation blockLocationB = new BlockLocation();
blockLocationB.setActiveTrip(bstBA.getTrip());
blockLocationB.setBlockInstance(blockInstance);
blockLocationB.setClosestStop(bstBA);
blockLocationB.setDistanceAlongBlock(400);
blockLocationB.setInService(true);
blockLocationB.setNextStop(bstAA);
blockLocationB.setPredicted(false);
blockLocationB.setScheduledDistanceAlongBlock(400);
// Mock StopTimeInstance with time frame
long stopTimeFrom = dateAsLong("2015-07-23 00:00");
long stopTimeTo = dateAsLong("2015-07-24 00:00");
StopTimeInstance sti1 = new StopTimeInstance(bstAB, blockInstance.getState());
ArrivalAndDepartureInstance in1 = new ArrivalAndDepartureInstance(sti1);
in1.setBlockLocation(blockLocationB);
in1.setPredictedArrivalTime((long) (in1.getScheduledArrivalTime()));
in1.setPredictedDepartureTime((long) (in1.getScheduledDepartureTime()));
StopTimeInstance sti2 = new StopTimeInstance(bstBA, blockInstance.getState());
ArrivalAndDepartureInstance in2 = new ArrivalAndDepartureInstance(sti2);
in2.setBlockLocation(blockLocationB);
Date fromTimeBuffered = new Date(stopTimeFrom - _blockStatusService.getRunningLateWindow() * 1000);
Date toTimeBuffered = new Date(stopTimeTo + _blockStatusService.getRunningEarlyWindow() * 1000);
Mockito.when(_stopTimeService.getStopTimeInstancesInTimeRange(mStopB, fromTimeBuffered, toTimeBuffered, EFrequencyStopTimeBehavior.INCLUDE_UNSPECIFIED)).thenReturn(Arrays.asList(sti1, sti2));
// Create and add vehicle location record cache
VehicleLocationRecordCacheImpl _cache = new VehicleLocationRecordCacheImpl();
VehicleLocationRecord vlr = new VehicleLocationRecord();
vlr.setBlockId(blockLocationB.getBlockInstance().getBlock().getBlock().getId());
vlr.setTripId(mTrip1.getId());
vlr.setTimepointPredictions(blockLocationB.getTimepointPredictions());
vlr.setTimeOfRecord(mCurrentTime);
vlr.setVehicleId(new AgencyAndId("1", "123"));
vlr.setStatus(TransitDataConstants.STATUS_CANCELED);
// Create ScheduledBlockLocation for cache
ScheduledBlockLocation sbl = new ScheduledBlockLocation();
sbl.setActiveTrip(blockLocationB.getActiveTrip());
// Add data to cache
_cache.addRecord(blockInstance, vlr, sbl, null);
_blockLocationService.setVehicleLocationRecordCache(_cache);
ScheduledBlockLocationServiceImpl scheduledBlockLocationServiceImpl = new ScheduledBlockLocationServiceImpl();
_blockLocationService.setScheduledBlockLocationService(scheduledBlockLocationServiceImpl);
// Call ArrivalAndDepartureService
return _service.getArrivalsAndDeparturesForStopInTimeRange(mStopB, target, stopTimeFrom, stopTimeTo);
}
use of org.onebusaway.transit_data_federation.services.realtime.ArrivalAndDepartureInstance in project onebusaway-application-modules by camsys.
the class ArrivalAndDepartureServiceImplTest method getArrivalsAndDeparturesForLoopInTheMiddleOfRouteInTimeRangeByTimepointPredictionRecord.
/**
* Set up the BlockLocationServiceImpl for the test, using the given
* timepointPredictions
*
* This method creates a loop route with a single trip and four stops in a block
* Stop B is visited twice in the middle of the route
*
* stop_id trip_id stop_sequence
* A 1 0
* B 1 1
* C 1 2
* B 1 3
* D 1 4
*
* @param timepointPredictions real-time predictions to apply to the
* BlockLocationServiceImpl
* @return a list of ArrivalAndDepartureInstances which is used to access
* predicted arrival/departure times for a stop, for comparison
* against the expected values
*/
private List<ArrivalAndDepartureInstance> getArrivalsAndDeparturesForLoopInTheMiddleOfRouteInTimeRangeByTimepointPredictionRecord(List<TimepointPredictionRecord> timepointPredictions) {
TargetTime target = new TargetTime(mCurrentTime, mCurrentTime);
// Setup block
BlockEntryImpl block = block("blockA");
stopTime(0, mStopA, mTrip1, time(13, 30), time(13, 35), 1000);
stopTime(1, mStopB, mTrip1, time(13, 45), time(13, 50), 2000);
stopTime(2, mStopC, mTrip1, time(13, 55), time(14, 00), 2000);
stopTime(3, mStopB, mTrip1, time(14, 05), time(14, 10), 2000);
stopTime(4, mStopD, mTrip1, time(14, 15), time(14, 20), 2000);
BlockConfigurationEntry blockConfig = blockConfiguration(block, serviceIds(lsids("sA"), lsids()), mTrip1);
BlockStopTimeEntry bstAA = blockConfig.getStopTimes().get(0);
BlockStopTimeEntry bstAB = blockConfig.getStopTimes().get(1);
BlockStopTimeEntry bstAC = blockConfig.getStopTimes().get(2);
BlockStopTimeEntry bstABB = blockConfig.getStopTimes().get(3);
BlockStopTimeEntry bstAD = blockConfig.getStopTimes().get(4);
// Setup block location instance for trip B
BlockInstance blockInstance = new BlockInstance(blockConfig, mServiceDate);
BlockLocation blockLocationB = new BlockLocation();
blockLocationB.setActiveTrip(bstAA.getTrip());
blockLocationB.setBlockInstance(blockInstance);
blockLocationB.setClosestStop(bstAB);
blockLocationB.setDistanceAlongBlock(400);
blockLocationB.setInService(true);
blockLocationB.setNextStop(bstAA);
blockLocationB.setPredicted(false);
blockLocationB.setScheduledDistanceAlongBlock(400);
blockLocationB.setTimepointPredictions(timepointPredictions);
// Mock StopTimeInstance with time frame
long stopTimeFrom = dateAsLong("2015-07-23 00:00");
long stopTimeTo = dateAsLong("2015-07-24 00:00");
StopTimeInstance sti1 = new StopTimeInstance(bstAB, blockInstance.getState());
ArrivalAndDepartureInstance in1 = new ArrivalAndDepartureInstance(sti1);
in1.setBlockLocation(blockLocationB);
in1.setPredictedArrivalTime((long) (in1.getScheduledArrivalTime()));
in1.setPredictedDepartureTime((long) (in1.getScheduledDepartureTime()));
StopTimeInstance sti2 = new StopTimeInstance(bstAA, blockInstance.getState());
ArrivalAndDepartureInstance in2 = new ArrivalAndDepartureInstance(sti2);
in2.setBlockLocation(blockLocationB);
StopTimeInstance sti3 = new StopTimeInstance(bstAC, blockInstance.getState());
ArrivalAndDepartureInstance in3 = new ArrivalAndDepartureInstance(sti3);
in3.setBlockLocation(blockLocationB);
StopTimeInstance sti4 = new StopTimeInstance(bstABB, blockInstance.getState());
ArrivalAndDepartureInstance in4 = new ArrivalAndDepartureInstance(sti4);
in4.setBlockLocation(blockLocationB);
StopTimeInstance sti5 = new StopTimeInstance(bstAD, blockInstance.getState());
ArrivalAndDepartureInstance in5 = new ArrivalAndDepartureInstance(sti5);
in5.setBlockLocation(blockLocationB);
Date fromTimeBuffered = new Date(stopTimeFrom - _blockStatusService.getRunningLateWindow() * 1000);
Date toTimeBuffered = new Date(stopTimeTo + _blockStatusService.getRunningEarlyWindow() * 1000);
Mockito.when(_stopTimeService.getStopTimeInstancesInTimeRange(mStopB, fromTimeBuffered, toTimeBuffered, EFrequencyStopTimeBehavior.INCLUDE_UNSPECIFIED)).thenReturn(Arrays.asList(sti1, sti2, sti3, sti4, sti5));
// Create and add vehicle location record cache
VehicleLocationRecordCacheImpl _cache = new VehicleLocationRecordCacheImpl();
VehicleLocationRecord vlr = new VehicleLocationRecord();
vlr.setBlockId(blockLocationB.getBlockInstance().getBlock().getBlock().getId());
vlr.setTripId(mTrip1.getId());
vlr.setTimepointPredictions(blockLocationB.getTimepointPredictions());
vlr.setTimeOfRecord(mCurrentTime);
vlr.setVehicleId(new AgencyAndId("1", "123"));
// Create ScheduledBlockLocation for cache
ScheduledBlockLocation sbl = new ScheduledBlockLocation();
sbl.setActiveTrip(blockLocationB.getActiveTrip());
// Add data to cache
_cache.addRecord(blockInstance, vlr, sbl, null);
_blockLocationService.setVehicleLocationRecordCache(_cache);
ScheduledBlockLocationServiceImpl scheduledBlockLocationServiceImpl = new ScheduledBlockLocationServiceImpl();
_blockLocationService.setScheduledBlockLocationService(scheduledBlockLocationServiceImpl);
// Call ArrivalAndDepartureService
return _service.getArrivalsAndDeparturesForStopInTimeRange(mStopB, target, stopTimeFrom, stopTimeTo);
}
Aggregations