Search in sources :

Example 16 with BlockLocation

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);
}
Also used : AgencyAndId(org.onebusaway.gtfs.model.AgencyAndId) StopTimeInstance(org.onebusaway.transit_data_federation.model.StopTimeInstance) VehicleLocationRecordCacheImpl(org.onebusaway.transit_data_federation.impl.realtime.VehicleLocationRecordCacheImpl) TargetTime(org.onebusaway.transit_data_federation.model.TargetTime) ScheduledBlockLocation(org.onebusaway.transit_data_federation.services.blocks.ScheduledBlockLocation) BlockLocation(org.onebusaway.transit_data_federation.services.realtime.BlockLocation) Date(java.util.Date) ScheduledBlockLocation(org.onebusaway.transit_data_federation.services.blocks.ScheduledBlockLocation) BlockInstance(org.onebusaway.transit_data_federation.services.blocks.BlockInstance) VehicleLocationRecord(org.onebusaway.realtime.api.VehicleLocationRecord) ArrivalAndDepartureInstance(org.onebusaway.transit_data_federation.services.realtime.ArrivalAndDepartureInstance) ScheduledBlockLocationServiceImpl(org.onebusaway.transit_data_federation.impl.blocks.ScheduledBlockLocationServiceImpl) BlockEntryImpl(org.onebusaway.transit_data_federation.impl.transit_graph.BlockEntryImpl) BlockConfigurationEntry(org.onebusaway.transit_data_federation.services.transit_graph.BlockConfigurationEntry) BlockStopTimeEntry(org.onebusaway.transit_data_federation.services.transit_graph.BlockStopTimeEntry)

Example 17 with BlockLocation

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());
}
Also used : ScheduledBlockLocation(org.onebusaway.transit_data_federation.services.blocks.ScheduledBlockLocation) CoordinatePoint(org.onebusaway.geospatial.model.CoordinatePoint) BlockInstance(org.onebusaway.transit_data_federation.services.blocks.BlockInstance) BlockEntryImpl(org.onebusaway.transit_data_federation.impl.transit_graph.BlockEntryImpl) TargetTime(org.onebusaway.transit_data_federation.model.TargetTime) ScheduledBlockLocation(org.onebusaway.transit_data_federation.services.blocks.ScheduledBlockLocation) BlockLocation(org.onebusaway.transit_data_federation.services.realtime.BlockLocation) BlockConfigurationEntry(org.onebusaway.transit_data_federation.services.transit_graph.BlockConfigurationEntry) TripEntryImpl(org.onebusaway.transit_data_federation.impl.transit_graph.TripEntryImpl) StopEntryImpl(org.onebusaway.transit_data_federation.impl.transit_graph.StopEntryImpl) Test(org.junit.Test)

Example 18 with BlockLocation

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;
}
Also used : AgencyAndId(org.onebusaway.gtfs.model.AgencyAndId) TripEntry(org.onebusaway.transit_data_federation.services.transit_graph.TripEntry) BlockTripEntry(org.onebusaway.transit_data_federation.services.transit_graph.BlockTripEntry) BlockLocation(org.onebusaway.transit_data_federation.services.realtime.BlockLocation) BlockInstance(org.onebusaway.transit_data_federation.services.blocks.BlockInstance) StopEntry(org.onebusaway.transit_data_federation.services.transit_graph.StopEntry) ArrayList(java.util.ArrayList) List(java.util.List) ArrivalAndDepartureInstance(org.onebusaway.transit_data_federation.services.realtime.ArrivalAndDepartureInstance) HashMap(java.util.HashMap) Map(java.util.Map) FactoryMap(org.onebusaway.collections.FactoryMap)

Example 19 with BlockLocation

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);
            }
        }
    }
}
Also used : ArrivalAndDepartureInstance(org.onebusaway.transit_data_federation.services.realtime.ArrivalAndDepartureInstance) BlockLocation(org.onebusaway.transit_data_federation.services.realtime.BlockLocation)

Example 20 with BlockLocation

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;
}
Also used : StopTimeInstance(org.onebusaway.transit_data_federation.model.StopTimeInstance) ArrayList(java.util.ArrayList) FrequencyEntry(org.onebusaway.transit_data_federation.services.transit_graph.FrequencyEntry) BlockLocation(org.onebusaway.transit_data_federation.services.realtime.BlockLocation) Date(java.util.Date) BlockInstance(org.onebusaway.transit_data_federation.services.blocks.BlockInstance) ArrayList(java.util.ArrayList) List(java.util.List) ArrivalAndDepartureInstance(org.onebusaway.transit_data_federation.services.realtime.ArrivalAndDepartureInstance) HashMap(java.util.HashMap) Map(java.util.Map) FactoryMap(org.onebusaway.collections.FactoryMap)

Aggregations

BlockLocation (org.onebusaway.transit_data_federation.services.realtime.BlockLocation)33 BlockInstance (org.onebusaway.transit_data_federation.services.blocks.BlockInstance)19 AgencyAndId (org.onebusaway.gtfs.model.AgencyAndId)15 ScheduledBlockLocation (org.onebusaway.transit_data_federation.services.blocks.ScheduledBlockLocation)15 ArrayList (java.util.ArrayList)14 TargetTime (org.onebusaway.transit_data_federation.model.TargetTime)12 Date (java.util.Date)11 ArrivalAndDepartureInstance (org.onebusaway.transit_data_federation.services.realtime.ArrivalAndDepartureInstance)10 List (java.util.List)8 StopTimeInstance (org.onebusaway.transit_data_federation.model.StopTimeInstance)8 BlockConfigurationEntry (org.onebusaway.transit_data_federation.services.transit_graph.BlockConfigurationEntry)8 BlockStopTimeEntry (org.onebusaway.transit_data_federation.services.transit_graph.BlockStopTimeEntry)8 CoordinatePoint (org.onebusaway.geospatial.model.CoordinatePoint)7 BlockEntryImpl (org.onebusaway.transit_data_federation.impl.transit_graph.BlockEntryImpl)7 VehicleLocationRecord (org.onebusaway.realtime.api.VehicleLocationRecord)6 HashMap (java.util.HashMap)5 Map (java.util.Map)5 Record (org.onebusaway.transit_data.model.realtime.CurrentVehicleEstimateQueryBean.Record)5 ScheduledBlockLocationServiceImpl (org.onebusaway.transit_data_federation.impl.blocks.ScheduledBlockLocationServiceImpl)5 VehicleLocationRecordCacheImpl (org.onebusaway.transit_data_federation.impl.realtime.VehicleLocationRecordCacheImpl)5