use of org.onebusaway.transit_data_federation.services.realtime.BlockLocation in project onebusaway-application-modules by camsys.
the class ArrivalAndDepartureServiceImplTest method getArrivalsAndDeparturesForStopInTimeRangeByTimepointPredictionRecord.
/**
* 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> getArrivalsAndDeparturesForStopInTimeRangeByTimepointPredictionRecord(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);
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);
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(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"));
// 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.BlockLocation in project onebusaway-application-modules by camsys.
the class BlockLocationServiceImplTest method testWithShapeInfo.
@Test
public void testWithShapeInfo() {
StopEntryImpl stopA = stop("a", 47.5, -122.5);
StopEntryImpl stopB = stop("b", 47.6, -122.4);
StopEntryImpl stopC = stop("c", 47.5, -122.3);
BlockEntryImpl block = block("block");
TripEntryImpl tripA = trip("tripA", "serviceId");
TripEntryImpl tripB = trip("tripB", "serviceId");
stopTime(0, stopA, tripA, 30, 90, 0);
stopTime(1, stopB, tripA, 120, 120, 100);
stopTime(2, stopC, tripA, 180, 210, 200);
stopTime(3, stopC, tripB, 240, 240, 300);
stopTime(4, stopB, tripB, 270, 270, 400);
stopTime(5, stopA, tripB, 300, 300, 500);
BlockConfigurationEntry blockConfig = linkBlockTrips(block, tripA, tripB);
long serviceDate = 1000 * 1000;
double epsilon = 0.001;
TargetTime target = new TargetTime(t(serviceDate, 0, 0), System.currentTimeMillis());
BlockInstance blockInstance = new BlockInstance(blockConfig, serviceDate);
BlockLocation location = _service.getLocationForBlockInstance(blockInstance, target);
assertNull(location);
ScheduledBlockLocation p = new ScheduledBlockLocation();
p.setActiveTrip(blockConfig.getTrips().get(0));
p.setClosestStop(blockConfig.getStopTimes().get(0));
p.setClosestStopTimeOffset(0);
p.setDistanceAlongBlock(0);
p.setLocation(new CoordinatePoint(stopA.getStopLat(), stopA.getStopLon()));
p.setInService(true);
Mockito.when(_blockLocationService.getScheduledBlockLocationFromScheduledTime(blockConfig, 1800)).thenReturn(p);
target = new TargetTime(t(serviceDate, 0, 30), System.currentTimeMillis());
location = _service.getLocationForBlockInstance(blockInstance, target);
assertTrue(location.isInService());
assertEquals(blockConfig.getStopTimes().get(0), location.getClosestStop());
assertEquals(0, location.getClosestStopTimeOffset());
assertEquals(stopA.getStopLocation(), location.getLocation());
assertFalse(location.isScheduleDeviationSet());
assertTrue(Double.isNaN(location.getScheduleDeviation()));
assertFalse(location.isDistanceAlongBlockSet());
assertTrue(Double.isNaN(location.getDistanceAlongBlock()));
assertEquals(blockInstance, location.getBlockInstance());
assertEquals(0, location.getLastUpdateTime());
assertEquals(blockConfig.getTrips().get(0), location.getActiveTrip());
assertNull(location.getVehicleId());
assertEquals(47.5, location.getLocation().getLat(), epsilon);
assertEquals(-122.5, location.getLocation().getLon(), epsilon);
assertEquals(blockConfig.getStopTimes().get(0), location.getClosestStop());
assertEquals(0, location.getClosestStopTimeOffset());
}
use of org.onebusaway.transit_data_federation.services.realtime.BlockLocation in project onebusaway-application-modules by camsys.
the class ArrivalAndDepartureServiceImpl method getArrivalAndDepartureForStop.
@Override
public ArrivalAndDepartureInstance getArrivalAndDepartureForStop(ArrivalAndDepartureQuery query) {
StopEntry stop = query.getStop();
int stopSequence = query.getStopSequence();
TripEntry trip = query.getTrip();
long serviceDate = query.getServiceDate();
AgencyAndId vehicleId = query.getVehicleId();
long time = query.getTime();
Map<BlockInstance, List<BlockLocation>> locationsByInstance = _blockStatusService.getBlocks(trip.getBlock().getId(), serviceDate, vehicleId, time);
if (locationsByInstance.isEmpty())
return null;
Map.Entry<BlockInstance, List<BlockLocation>> entry = locationsByInstance.entrySet().iterator().next();
BlockInstance blockInstance = entry.getKey();
List<BlockLocation> locations = entry.getValue();
int timeOfServiceDate = (int) ((time - serviceDate) / 1000);
ArrivalAndDepartureInstance instance = createArrivalAndDeparture(blockInstance, trip.getId(), stop.getId(), stopSequence, serviceDate, timeOfServiceDate, time);
if (!locations.isEmpty()) {
/**
* What if there are multiple locations? Pick the first?
*/
BlockLocation location = locations.get(0);
applyBlockLocationToInstance(instance, location, time);
}
return instance;
}
use of org.onebusaway.transit_data_federation.services.realtime.BlockLocation in project onebusaway-application-modules by camsys.
the class ArrivalAndDepartureServiceImpl method applyRealTimeToStopTimeInstance.
/*
* here we map realtime on top of schedule and also filter
* out canceled trips.
*/
private void applyRealTimeToStopTimeInstance(StopTimeInstance sti, TargetTime targetTime, long fromTime, long toTime, long frequencyOffsetTime, BlockInstance blockInstance, List<BlockLocation> locations, List<ArrivalAndDepartureInstance> results) {
for (BlockLocation location : locations) {
if (sti.isFrequencyOffsetSpecified() && ((blockInstance.getBlock().getDepartureTimeForIndex(0) + sti.getFrequencyOffset()) != location.getBlockStartTime())) {
continue;
}
if (TransitDataConstants.STATUS_CANCELED.equals(location.getStatus())) {
continue;
}
ArrivalAndDepartureInstance instance = createArrivalAndDepartureForStopTimeInstance(sti, frequencyOffsetTime);
applyBlockLocationToInstance(instance, location, targetTime.getTargetTime());
if (isArrivalAndDepartureBeanInRange(instance, fromTime, toTime))
results.add(instance);
}
if (locations.isEmpty()) {
ArrivalAndDepartureInstance instance = createArrivalAndDepartureForStopTimeInstance(sti, frequencyOffsetTime);
if (sti.getFrequency() == null) {
/**
* We don't need to get the scheduled location of a vehicle unless its
* in our arrival window
*/
if (isArrivalAndDepartureBeanInRange(instance, fromTime, toTime)) {
BlockLocation scheduledLocation = _blockLocationService.getScheduledLocationForBlockInstance(blockInstance, targetTime.getTargetTime());
if (scheduledLocation != null)
applyBlockLocationToInstance(instance, scheduledLocation, targetTime.getTargetTime());
results.add(instance);
}
} else {
if (isFrequencyBasedArrivalInRange(blockInstance, sti.getFrequency(), fromTime, toTime)) {
results.add(instance);
}
}
}
}
use of org.onebusaway.transit_data_federation.services.realtime.BlockLocation in project onebusaway-application-modules by camsys.
the class ArrivalAndDepartureServiceImpl method getArrivalsAndDeparturesForStopInTimeRange.
@Override
public List<ArrivalAndDepartureInstance> getArrivalsAndDeparturesForStopInTimeRange(StopEntry stop, TargetTime targetTime, long fromTime, long toTime) {
// We add a buffer before and after to catch late and early buses
Date fromTimeBuffered = new Date(fromTime - _blockStatusService.getRunningLateWindow() * 1000);
Date toTimeBuffered = new Date(toTime + _blockStatusService.getRunningEarlyWindow() * 1000);
List<StopTimeInstance> stis = _stopTimeService.getStopTimeInstancesInTimeRange(stop, fromTimeBuffered, toTimeBuffered, EFrequencyStopTimeBehavior.INCLUDE_UNSPECIFIED);
long frequencyOffsetTime = Math.max(targetTime.getTargetTime(), fromTime);
Map<BlockInstance, List<StopTimeInstance>> stisByBlockId = getStopTimeInstancesByBlockInstance(stis);
List<ArrivalAndDepartureInstance> instances = new ArrayList<ArrivalAndDepartureInstance>();
for (Map.Entry<BlockInstance, List<StopTimeInstance>> entry : stisByBlockId.entrySet()) {
BlockInstance blockInstance = entry.getKey();
List<BlockLocation> locations = _blockLocationService.getLocationsForBlockInstance(blockInstance, targetTime);
List<StopTimeInstance> stisForBlock = entry.getValue();
for (StopTimeInstance sti : stisForBlock) {
applyRealTimeToStopTimeInstance(sti, targetTime, fromTime, toTime, frequencyOffsetTime, blockInstance, locations, instances);
if (sti.getFrequency() != null && sti.getFrequency().getExactTimes() == 0) {
/*
* adjust following schedule times relative to current realtime data
*/
applyPostInterpolateForFrequencyNoSchedule(sti, fromTime, toTime, frequencyOffsetTime, blockInstance, instances);
}
}
}
if (removeFuturePredictionsWithoutRealtime) {
List<ArrivalAndDepartureInstance> filteredInstances = new ArrayList<ArrivalAndDepartureInstance>();
for (ArrivalAndDepartureInstance instance : instances) {
FrequencyEntry entry = instance.getFrequency();
boolean toAdd = // not a frequency-based instance
(entry == null) || // frequency interval has started
(instance.getServiceDate() + (entry.getStartTime() * 1000) < targetTime.getTargetTime()) || // instance has realtime data
(instance.getBlockLocation() != null && instance.getBlockLocation().isPredicted());
if (toAdd)
filteredInstances.add(instance);
}
return filteredInstances;
}
return instances;
}
Aggregations