use of org.onebusaway.transit_data_federation.services.blocks.BlockInstance in project onebusaway-application-modules by camsys.
the class GtfsRealtimeTripLibraryTest method testTprOnFutureTrip.
/**
* This method tests that we propagate a time point prediction record
* when it comes from a trip that hasn't started yet.
*
* Current time = 7:31. Trip update delay = 2 minutes
* Schedule time Real-time from feed
* Stop A (trip A) 7:30 7:33
* Stop A (trip B) 7:40 7:44
*/
@Test
public void testTprOnFutureTrip() {
_library.setCurrentTime(time(7, 31) * 1000);
TripEntryImpl tripA = trip("tripA");
TripEntryImpl tripB = trip("tripB");
StopEntryImpl stopA = stop("stopA", 0, 0);
stopTime(0, stopA, tripA, time(7, 30), 0.0);
stopTime(0, stopA, tripB, time(7, 40), 0.0);
BlockEntryImpl blockA = block("blockA");
BlockConfigurationEntry blockConfigA = blockConfiguration(blockA, serviceIds("s1"), tripA, tripB);
BlockInstance blockInstanceA = new BlockInstance(blockConfigA, 0L);
StopTimeUpdate.Builder stuA = stopTimeUpdateWithDepartureDelay("stopA", 180);
TripUpdate.Builder tuA = tripUpdate("tripA", _library.getCurrentTime() / 1000, 120, stuA);
StopTimeUpdate.Builder stuB = stopTimeUpdateWithDepartureDelay("stopA", 240);
TripUpdate.Builder tuB = tripUpdate("tripB", _library.getCurrentTime() / 1000, 0, stuB);
tuA.setVehicle(vehicle("bus1"));
tuB.setVehicle(vehicle("bus1"));
Mockito.when(_entitySource.getTrip("tripA")).thenReturn(tripA);
Mockito.when(_entitySource.getTrip("tripB")).thenReturn(tripB);
Mockito.when(_blockCalendarService.getActiveBlocks(Mockito.eq(blockA.getId()), Mockito.anyLong(), Mockito.anyLong())).thenReturn(Arrays.asList(blockInstanceA));
VehicleLocationRecord record = vehicleLocationRecord(tuA, tuB);
long tripADept = getPredictedDepartureTimeByStopIdAndTripId(record, "stopA", "tripA");
assertEquals(tripADept, time(7, 33) * 1000);
long tripBDept = getPredictedDepartureTimeByStopIdAndTripId(record, "stopA", "tripB");
assertEquals(tripBDept, time(7, 44) * 1000);
}
use of org.onebusaway.transit_data_federation.services.blocks.BlockInstance in project onebusaway-application-modules by camsys.
the class ServiceAlertsServiceImplTest method testGetServiceAlertsForStopCall.
@Test
public void testGetServiceAlertsForStopCall() {
/**
* These alerts should match
*/
ServiceAlertRecord alert1 = new ServiceAlertRecord();
alert1.setAgencyId("1");
alert1.setServiceAlertId("A");
ServiceAlertsSituationAffectsClause affectsClause = new ServiceAlertsSituationAffectsClause();
affectsClause.setAgencyId("1");
affectsClause.setStopId("10020");
affectsClause.setTripId("TripA");
alert1.getAllAffects().add(affectsClause);
alert1 = _service.createOrUpdateServiceAlert(alert1);
ServiceAlertRecord alert2 = new ServiceAlertRecord();
alert2.setAgencyId("1");
alert2.setServiceAlertId("B");
ServiceAlertsSituationAffectsClause affectsClause2 = new ServiceAlertsSituationAffectsClause();
affectsClause2.setAgencyId("1");
affectsClause2.setTripId("TripA");
alert2.getAllAffects().add(affectsClause2);
alert2 = _service.createOrUpdateServiceAlert(alert2);
ServiceAlertRecord alert3 = new ServiceAlertRecord();
alert3.setAgencyId("1");
alert3.setServiceAlertId("C");
ServiceAlertsSituationAffectsClause affectsClause3 = new ServiceAlertsSituationAffectsClause();
affectsClause3.setAgencyId("1");
affectsClause3.setRouteId("RouteX");
alert3.getAllAffects().add(affectsClause3);
alert3 = _service.createOrUpdateServiceAlert(alert3);
ServiceAlertRecord alert4 = new ServiceAlertRecord();
alert4.setAgencyId("1");
alert4.setServiceAlertId("D");
ServiceAlertsSituationAffectsClause affectsClause4 = new ServiceAlertsSituationAffectsClause();
affectsClause4.setAgencyId("1");
affectsClause4.setRouteId("RouteX");
affectsClause4.setDirectionId("1");
alert4.getAllAffects().add(affectsClause4);
alert4 = _service.createOrUpdateServiceAlert(alert4);
/**
* These alerts shouldn't match
*/
ServiceAlertRecord alert5 = new ServiceAlertRecord();
alert5.setAgencyId("1");
alert5.setServiceAlertId("E");
ServiceAlertsSituationAffectsClause affectsClause5 = new ServiceAlertsSituationAffectsClause();
affectsClause5.setAgencyId("1");
affectsClause5.setStopId("10021");
affectsClause5.setTripId("TripA");
alert5.getAllAffects().add(affectsClause5);
alert5 = _service.createOrUpdateServiceAlert(alert5);
ServiceAlertRecord alert6 = new ServiceAlertRecord();
alert6.setAgencyId("1");
alert6.setServiceAlertId("F");
ServiceAlertsSituationAffectsClause affectsClause6 = new ServiceAlertsSituationAffectsClause();
affectsClause6.setAgencyId("1");
affectsClause6.setStopId("10020");
affectsClause6.setTripId("TripB");
alert6.getAllAffects().add(affectsClause6);
alert6 = _service.createOrUpdateServiceAlert(alert6);
ServiceAlertRecord alert7 = new ServiceAlertRecord();
alert7.setAgencyId("1");
alert7.setServiceAlertId("G");
ServiceAlertsSituationAffectsClause affectsClause7 = new ServiceAlertsSituationAffectsClause();
affectsClause7.setAgencyId("1");
affectsClause7.setTripId("TripB");
alert7.getAllAffects().add(affectsClause7);
alert7 = _service.createOrUpdateServiceAlert(alert7);
ServiceAlertRecord alert8 = new ServiceAlertRecord();
alert8.setAgencyId("1");
alert8.setServiceAlertId("H");
ServiceAlertsSituationAffectsClause affectsClause8 = new ServiceAlertsSituationAffectsClause();
affectsClause8.setAgencyId("1");
affectsClause8.setRouteId("RouteY");
alert8.getAllAffects().add(affectsClause8);
alert8 = _service.createOrUpdateServiceAlert(alert8);
ServiceAlertRecord alert9 = new ServiceAlertRecord();
alert9.setAgencyId("1");
alert9.setServiceAlertId("I");
ServiceAlertsSituationAffectsClause affectsClause9 = new ServiceAlertsSituationAffectsClause();
affectsClause9.setAgencyId("1");
affectsClause9.setRouteId("RouteX");
affectsClause9.setDirectionId("0");
alert9.getAllAffects().add(affectsClause9);
alert9 = _service.createOrUpdateServiceAlert(alert9);
RouteEntryImpl route = route("RouteX");
routeCollection("RouteX", route);
StopEntryImpl stop = stop("10020", 47.0, -122.0);
TripEntryImpl trip = trip("TripA");
trip.setRoute(route);
trip.setDirectionId("1");
stopTime(0, stop, trip, time(8, 53), 0);
BlockEntryImpl block = block("block");
BlockConfigurationEntry blockConfig = blockConfiguration(block, serviceIds(lsids("a"), lsids()), trip);
BlockInstance blockInstance = new BlockInstance(blockConfig, System.currentTimeMillis());
List<ServiceAlertRecord> alerts = _service.getServiceAlertsForStopCall(System.currentTimeMillis(), blockInstance, blockConfig.getStopTimes().get(0), new AgencyAndId("1", "1111"));
assertEquals(4, alerts.size());
assertTrue(alerts.contains(alert1));
assertTrue(alerts.contains(alert2));
assertTrue(alerts.contains(alert3));
assertTrue(alerts.contains(alert4));
}
use of org.onebusaway.transit_data_federation.services.blocks.BlockInstance in project onebusaway-application-modules by camsys.
the class AbstractOrbcadRecordSource method getBlockInstanceForRecord.
private BlockInstance getBlockInstanceForRecord(OrbcadRecord record) {
long recordTime = record.getTime() * 1000;
long timeFrom = recordTime - 30 * 60 * 1000;
long timeTo = recordTime + 30 * 60 * 1000;
List<AgencyAndId> blockIds = getBlockIdsForRecord(record);
List<BlockInstance> allInstances = new ArrayList<BlockInstance>();
for (AgencyAndId blockId : blockIds) {
List<BlockInstance> instances = _blockCalendarService.getActiveBlocks(blockId, timeFrom, timeTo);
allInstances.addAll(instances);
}
// TODO : We currently assume we don't have overlapping blocks.
if (allInstances.size() != 1)
return null;
return allInstances.get(0);
}
use of org.onebusaway.transit_data_federation.services.blocks.BlockInstance 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.blocks.BlockInstance 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