use of org.onebusaway.transit_data_federation.services.blocks.BlockInstance in project onebusaway-application-modules by camsys.
the class BlockLocationServiceImpl method getVehicleLocationRecordAsBlockInstance.
/**
**
* Private Methods
***
*/
private BlockInstance getVehicleLocationRecordAsBlockInstance(VehicleLocationRecord record) {
AgencyAndId blockId = record.getBlockId();
if (blockId == null) {
AgencyAndId tripId = record.getTripId();
if (tripId == null)
throw new IllegalArgumentException("at least one of blockId or tripId must be specified for VehicleLocationRecord");
TripEntry tripEntry = _transitGraphDao.getTripEntryForId(tripId);
if (tripEntry == null)
throw new IllegalArgumentException("trip not found with id=" + tripId);
BlockEntry block = tripEntry.getBlock();
blockId = block.getId();
}
if (record.getServiceDate() == 0)
throw new IllegalArgumentException("you must specify a serviceDate");
if (record.getTimeOfRecord() == 0)
throw new IllegalArgumentException("you must specify a record time");
BlockInstance blockInstance = getBestBlockForRecord(blockId, record.getServiceDate(), record.getTimeOfRecord());
return blockInstance;
}
use of org.onebusaway.transit_data_federation.services.blocks.BlockInstance in project onebusaway-application-modules by camsys.
the class CurrentVehicleEstimationServiceImpl method getCurrentVehicleEstimates.
@Override
public ListBean<CurrentVehicleEstimateBean> getCurrentVehicleEstimates(CurrentVehicleEstimateQueryBean query) {
long minT = SystemTime.currentTimeMillis() - _maxWindow * 60 * 1000;
minT = 0;
List<Record> records = getRecords(query.getRecords(), minT);
if (records.isEmpty())
return new ListBean<CurrentVehicleEstimateBean>();
List<CurrentVehicleEstimateBean> beans = new ArrayList<CurrentVehicleEstimateBean>();
if (tryDirectMatchAgainstVehicleId(query, records, beans))
return new ListBean<CurrentVehicleEstimateBean>(beans, true);
Map<Date, Record> recordsByTime = getRecordsByTimestamp(records);
List<Date> timestamps = new ArrayList<Date>(recordsByTime.keySet());
Collections.sort(timestamps);
if (tryDirectMatchAgainstBlockId(query, records, recordsByTime, timestamps, query.getMinProbability(), beans))
return new ListBean<CurrentVehicleEstimateBean>(beans, true);
Set<BlockSequenceIndex> allIndices = getBlockSequenceIndicesForRecords(recordsByTime);
for (BlockSequenceIndex index : allIndices) {
Map<BlockInstance, List<List<BlockLocation>>> allLocations = _blockStatusService.getBlocksForIndex(index, timestamps);
for (Map.Entry<BlockInstance, List<List<BlockLocation>>> entry : allLocations.entrySet()) {
BlockInstance blockInstance = entry.getKey();
List<List<BlockLocation>> realTimeLocations = entry.getValue();
computeEstimatesForBlockInstance(records, recordsByTime, blockInstance, realTimeLocations, query.getMinProbability(), beans);
}
}
Collections.sort(beans);
return new ListBean<CurrentVehicleEstimateBean>(beans, false);
}
use of org.onebusaway.transit_data_federation.services.blocks.BlockInstance in project onebusaway-application-modules by camsys.
the class CurrentVehicleEstimationServiceImpl method tryDirectMatchAgainstBlockId.
private boolean tryDirectMatchAgainstBlockId(CurrentVehicleEstimateQueryBean query, List<Record> records, Map<Date, Record> recordsByTime, List<Date> timestamps, double minProbabilityForConsideration, List<CurrentVehicleEstimateBean> beans) {
String blockIdAsString = query.getBlockId();
long serviceDate = query.getServiceDate();
if (blockIdAsString == null || serviceDate == 0)
return false;
AgencyAndId blockId = AgencyAndIdLibrary.convertFromString(blockIdAsString);
BlockInstance blockInstance = _blockCalendarService.getBlockInstance(blockId, serviceDate);
if (blockInstance == null)
return false;
Map<AgencyAndId, List<BlockLocation>> locationsForBlockInstance = _blockLocationService.getLocationsForBlockInstance(blockInstance, timestamps, query.getTime());
Collection<List<BlockLocation>> realTimeLocations = locationsForBlockInstance.values();
computeEstimatesForBlockInstance(records, recordsByTime, blockInstance, realTimeLocations, minProbabilityForConsideration, beans);
return !beans.isEmpty();
}
use of org.onebusaway.transit_data_federation.services.blocks.BlockInstance in project onebusaway-application-modules by camsys.
the class SiriLikeRealtimeSource method getEffectiveScheduleTime.
private long getEffectiveScheduleTime(TripEntry trip, double lat, double lon, long timestamp, long serviceDate) {
ServiceIdActivation serviceIds = new ServiceIdActivation(trip.getServiceId());
// todo!
BlockConfigurationEntry blockConfig = blockConfiguration(trip.getBlock(), serviceIds, trip);
BlockInstance block = new BlockInstance(blockConfig, serviceDate);
CoordinatePoint location = new CoordinatePoint(lat, lon);
ScheduledBlockLocation loc = _blockGeospatialService.getBestScheduledBlockLocationForLocation(block, location, timestamp, 0, trip.getTotalTripDistance());
return loc.getScheduledTime();
}
use of org.onebusaway.transit_data_federation.services.blocks.BlockInstance in project onebusaway-application-modules by camsys.
the class VehicleLocationRecordCacheImpl method addRecord.
@Override
public VehicleLocationCacheElements addRecord(BlockInstance blockInstance, VehicleLocationRecord record, ScheduledBlockLocation scheduledBlockLocation, ScheduleDeviationSamples samples) {
AgencyAndId vehicleId = record.getVehicleId();
while (true) {
VehicleLocationCacheEntry newCacheEntry = new VehicleLocationCacheEntry(blockInstance);
VehicleLocationCacheEntry cacheEntry = _entriesByVehicleId.putIfAbsent(vehicleId, newCacheEntry);
if (cacheEntry == null) {
cacheEntry = newCacheEntry;
/**
* Since we're adding a new entry, we indicate the connection between
* this block instance and vehicleId
*/
ConcurrentCollectionsLibrary.addToMapValueSet(_vehicleIdsByBlockInstance, blockInstance, vehicleId);
}
/**
* If the block instance of a vehicle has changed mid-stream, we close off
* the cache entry and remove the block=>vid mapping
*/
if (cacheEntry.isClosedBecauseBlockInstanceChanged(blockInstance)) {
_entriesByVehicleId.remove(vehicleId);
ConcurrentCollectionsLibrary.removeFromMapValueSet(_vehicleIdsByBlockInstance, cacheEntry.getBlockInstance(), vehicleId);
continue;
}
/**
* If the element failed to add because the entry is closed, we loop.
* Someone closed the entry while we were in the process of requesting it
* from the map. On the next loop, it should no longer be in the map.
*/
if (!cacheEntry.addElement(record, scheduledBlockLocation, samples))
continue;
BlockInstance existingBlockInstance = cacheEntry.getBlockInstance();
if (!blockInstance.equals(existingBlockInstance))
ConcurrentCollectionsLibrary.removeFromMapValueSet(_vehicleIdsByBlockInstance, existingBlockInstance, vehicleId);
return cacheEntry.getElements();
}
}
Aggregations