Search in sources :

Example 51 with BlockInstance

use of org.onebusaway.transit_data_federation.services.blocks.BlockInstance in project onebusaway-application-modules by camsys.

the class BlockCalendarServiceImpl method getClosestActiveBlocks.

@Override
public List<BlockInstance> getClosestActiveBlocks(AgencyAndId blockId, long time) {
    Date timeAsDate = new Date(time);
    Min<BlockInstance> m = new Min<BlockInstance>();
    BlockEntry blockEntry = _transitGraphDao.getBlockEntryForId(blockId);
    for (BlockConfigurationEntry blockConfig : blockEntry.getConfigurations()) {
        List<Date> serviceDates = _calendarService.getDatesForServiceIdsAsOrderedList(blockConfig.getServiceIds());
        int index = index(Collections.binarySearch(serviceDates, timeAsDate));
        if (index > 0) {
            BlockInstance instance = new BlockInstance(blockConfig, serviceDates.get(index - 1).getTime());
            long delta = getTimeToBlockInstance(instance, time);
            m.add(delta, instance);
        }
        if (index < serviceDates.size()) {
            BlockInstance instance = new BlockInstance(blockConfig, serviceDates.get(index).getTime());
            long delta = getTimeToBlockInstance(instance, time);
            m.add(delta, instance);
        }
    }
    return m.getMinElements();
}
Also used : Min(org.onebusaway.collections.Min) BlockEntry(org.onebusaway.transit_data_federation.services.transit_graph.BlockEntry) BlockInstance(org.onebusaway.transit_data_federation.services.blocks.BlockInstance) BlockConfigurationEntry(org.onebusaway.transit_data_federation.services.transit_graph.BlockConfigurationEntry) Date(java.util.Date)

Example 52 with BlockInstance

use of org.onebusaway.transit_data_federation.services.blocks.BlockInstance in project onebusaway-application-modules by camsys.

the class BlockCalendarServiceImpl method findBlockLayoversInRange.

private void findBlockLayoversInRange(LayoverIntervalBlock intervals, Date serviceDate, Date timeFrom, Date timeTo, List<BlockTripEntry> trips, Collection<BlockInstance> instances) {
    int scheduledTimeFrom = (int) ((timeFrom.getTime() - serviceDate.getTime()) / 1000);
    int scheduledTimeTo = (int) ((timeTo.getTime() - serviceDate.getTime()) / 1000);
    int indexFrom = index(Arrays.binarySearch(intervals.getEndTimes(), scheduledTimeFrom));
    int indexTo = index(Arrays.binarySearch(intervals.getStartTimes(), scheduledTimeTo));
    InstanceState state = new InstanceState(serviceDate.getTime());
    for (int in = indexFrom; in < indexTo; in++) {
        BlockTripEntry trip = trips.get(in);
        BlockConfigurationEntry block = trip.getBlockConfiguration();
        BlockInstance instance = new BlockInstance(block, state);
        instances.add(instance);
    }
}
Also used : InstanceState(org.onebusaway.transit_data_federation.services.blocks.InstanceState) BlockTripEntry(org.onebusaway.transit_data_federation.services.transit_graph.BlockTripEntry) BlockInstance(org.onebusaway.transit_data_federation.services.blocks.BlockInstance) BlockConfigurationEntry(org.onebusaway.transit_data_federation.services.transit_graph.BlockConfigurationEntry)

Example 53 with BlockInstance

use of org.onebusaway.transit_data_federation.services.blocks.BlockInstance in project onebusaway-application-modules by camsys.

the class BlockLocationServiceImpl method groupRecord.

private Map<BlockLocationRecordKey, List<BlockLocationRecord>> groupRecord(List<BlockLocationRecord> predictions) {
    Map<BlockLocationRecordKey, List<BlockLocationRecord>> recordsByKey = new FactoryMap<BlockLocationRecordKey, List<BlockLocationRecord>>(new ArrayList<BlockLocationRecord>());
    for (BlockLocationRecord record : predictions) {
        AgencyAndId blockId = record.getBlockId();
        long serviceDate = record.getServiceDate();
        BlockInstance blockInstance = _blockCalendarService.getBlockInstance(blockId, serviceDate);
        if (blockInstance != null) {
            BlockLocationRecordKey key = new BlockLocationRecordKey(blockInstance, record.getVehicleId());
            recordsByKey.get(key).add(record);
        }
    }
    return recordsByKey;
}
Also used : FactoryMap(org.onebusaway.collections.FactoryMap) AgencyAndId(org.onebusaway.gtfs.model.AgencyAndId) BlockInstance(org.onebusaway.transit_data_federation.services.blocks.BlockInstance) List(java.util.List) ArrayList(java.util.ArrayList)

Example 54 with BlockInstance

use of org.onebusaway.transit_data_federation.services.blocks.BlockInstance in project onebusaway-application-modules by camsys.

the class BlockLocationServiceImpl method handleVehicleLocationRecord.

/**
 **
 * {@link BlockVehicleLocationListener} Interface
 ***
 */
@Override
public void handleVehicleLocationRecord(VehicleLocationRecord record) {
    BlockInstance instance = getVehicleLocationRecordAsBlockInstance(record);
    if (instance != null) {
        ScheduledBlockLocation scheduledBlockLocation = getScheduledBlockLocationForVehicleLocationRecord(record, instance);
        if (!record.isScheduleDeviationSet()) {
            int deviation = (int) ((record.getTimeOfRecord() - record.getServiceDate()) / 1000 - scheduledBlockLocation.getScheduledTime());
            record.setScheduleDeviation(deviation);
        }
        ScheduleDeviationSamples samples = null;
        if (_sampleScheduleDeviationHistory == true) {
            samples = _realTimeHistoryService.sampleScheduleDeviationsForVehicle(instance, record, scheduledBlockLocation);
        }
        putBlockLocationRecord(instance, record, scheduledBlockLocation, samples);
    }
}
Also used : ScheduledBlockLocation(org.onebusaway.transit_data_federation.services.blocks.ScheduledBlockLocation) BlockInstance(org.onebusaway.transit_data_federation.services.blocks.BlockInstance) ScheduleDeviationSamples(org.onebusaway.transit_data_federation.services.realtime.ScheduleDeviationSamples) CoordinatePoint(org.onebusaway.geospatial.model.CoordinatePoint)

Example 55 with BlockInstance

use of org.onebusaway.transit_data_federation.services.blocks.BlockInstance in project onebusaway-application-modules by camsys.

the class BlockLocationServiceImpl method getBestBlockForRecord.

private BlockInstance getBestBlockForRecord(AgencyAndId blockId, long serviceDate, long timeOfRecord) {
    long timeFrom = timeOfRecord - _blockInstanceMatchingWindow;
    long timeTo = timeOfRecord + _blockInstanceMatchingWindow;
    List<BlockInstance> blocks = _blockCalendarService.getActiveBlocks(blockId, timeFrom, timeTo);
    if (blocks.isEmpty())
        return null;
    else if (blocks.size() == 1)
        return blocks.get(0);
    Min<BlockInstance> m = new Min<BlockInstance>();
    for (BlockInstance block : blocks) {
        long delta = Math.abs(block.getServiceDate() - serviceDate);
        m.add(delta, block);
    }
    return m.getMinElement();
}
Also used : Min(org.onebusaway.collections.Min) 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