Search in sources :

Example 56 with BlockInstance

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;
}
Also used : AgencyAndId(org.onebusaway.gtfs.model.AgencyAndId) BlockEntry(org.onebusaway.transit_data_federation.services.transit_graph.BlockEntry) BlockInstance(org.onebusaway.transit_data_federation.services.blocks.BlockInstance) TripEntry(org.onebusaway.transit_data_federation.services.transit_graph.TripEntry) BlockTripEntry(org.onebusaway.transit_data_federation.services.transit_graph.BlockTripEntry)

Example 57 with 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);
}
Also used : BlockSequenceIndex(org.onebusaway.transit_data_federation.services.blocks.BlockSequenceIndex) DoubleArrayList(cern.colt.list.DoubleArrayList) ArrayList(java.util.ArrayList) ListBean(org.onebusaway.transit_data.model.ListBean) ScheduledBlockLocation(org.onebusaway.transit_data_federation.services.blocks.ScheduledBlockLocation) BlockLocation(org.onebusaway.transit_data_federation.services.realtime.BlockLocation) Date(java.util.Date) CurrentVehicleEstimateBean(org.onebusaway.transit_data.model.realtime.CurrentVehicleEstimateBean) BlockInstance(org.onebusaway.transit_data_federation.services.blocks.BlockInstance) Record(org.onebusaway.transit_data.model.realtime.CurrentVehicleEstimateQueryBean.Record) DoubleArrayList(cern.colt.list.DoubleArrayList) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map)

Example 58 with BlockInstance

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();
}
Also used : AgencyAndId(org.onebusaway.gtfs.model.AgencyAndId) BlockInstance(org.onebusaway.transit_data_federation.services.blocks.BlockInstance) DoubleArrayList(cern.colt.list.DoubleArrayList) ArrayList(java.util.ArrayList) List(java.util.List)

Example 59 with BlockInstance

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();
}
Also used : CoordinatePoint(org.onebusaway.geospatial.model.CoordinatePoint) ScheduledBlockLocation(org.onebusaway.transit_data_federation.services.blocks.ScheduledBlockLocation) BlockInstance(org.onebusaway.transit_data_federation.services.blocks.BlockInstance) ServiceIdActivation(org.onebusaway.transit_data_federation.services.transit_graph.ServiceIdActivation) BlockConfigurationEntry(org.onebusaway.transit_data_federation.services.transit_graph.BlockConfigurationEntry)

Example 60 with BlockInstance

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();
    }
}
Also used : AgencyAndId(org.onebusaway.gtfs.model.AgencyAndId) VehicleLocationCacheEntry(org.onebusaway.transit_data_federation.services.realtime.VehicleLocationCacheEntry) BlockInstance(org.onebusaway.transit_data_federation.services.blocks.BlockInstance)

Aggregations

BlockInstance (org.onebusaway.transit_data_federation.services.blocks.BlockInstance)63 BlockConfigurationEntry (org.onebusaway.transit_data_federation.services.transit_graph.BlockConfigurationEntry)36 BlockEntryImpl (org.onebusaway.transit_data_federation.impl.transit_graph.BlockEntryImpl)25 AgencyAndId (org.onebusaway.gtfs.model.AgencyAndId)24 Test (org.junit.Test)20 TripEntryImpl (org.onebusaway.transit_data_federation.impl.transit_graph.TripEntryImpl)20 ArrayList (java.util.ArrayList)19 BlockLocation (org.onebusaway.transit_data_federation.services.realtime.BlockLocation)19 VehicleLocationRecord (org.onebusaway.realtime.api.VehicleLocationRecord)16 Date (java.util.Date)15 ScheduledBlockLocation (org.onebusaway.transit_data_federation.services.blocks.ScheduledBlockLocation)12 BlockTripEntry (org.onebusaway.transit_data_federation.services.transit_graph.BlockTripEntry)12 List (java.util.List)11 CoordinatePoint (org.onebusaway.geospatial.model.CoordinatePoint)11 TripUpdate (com.google.transit.realtime.GtfsRealtime.TripUpdate)10 ArrivalAndDepartureInstance (org.onebusaway.transit_data_federation.services.realtime.ArrivalAndDepartureInstance)10 BlockStopTimeEntry (org.onebusaway.transit_data_federation.services.transit_graph.BlockStopTimeEntry)10 StopTimeUpdate (com.google.transit.realtime.GtfsRealtime.TripUpdate.StopTimeUpdate)9 StopTimeInstance (org.onebusaway.transit_data_federation.model.StopTimeInstance)9 TripEntry (org.onebusaway.transit_data_federation.services.transit_graph.TripEntry)9