Search in sources :

Example 21 with FrequencyEntry

use of org.onebusaway.transit_data_federation.services.transit_graph.FrequencyEntry in project onebusaway-application-modules by camsys.

the class ArrivalsAndDeparturesBeanServiceImpl method getArrivalsAndDeparturesByStopId.

/**
 **
 * {@link ArrivalsAndDeparturesBeanService} Interface
 ***
 */
@Override
public List<ArrivalAndDepartureBean> getArrivalsAndDeparturesByStopId(AgencyAndId stopId, ArrivalsAndDeparturesQueryBean query) {
    StopEntry stop = _transitGraphDao.getStopEntryForId(stopId, true);
    long time = query.getTime();
    int minutesBefore = Math.max(query.getMinutesBefore(), query.getFrequencyMinutesBefore());
    int minutesAfter = Math.max(query.getMinutesAfter(), query.getFrequencyMinutesAfter());
    long fromTime = time - minutesBefore * 60 * 1000;
    long toTime = time + minutesAfter * 60 * 1000;
    long nonFrequencyFromTime = time - query.getMinutesBefore() * 60 * 1000;
    long nonFrequencyToTime = time + query.getMinutesAfter() * 60 * 1000;
    long frequencyFromTime = time - query.getFrequencyMinutesBefore() * 60 * 1000;
    long frequencyToTime = time + query.getFrequencyMinutesAfter() * 60 * 1000;
    TargetTime target = new TargetTime(time, time);
    List<ArrivalAndDepartureInstance> instances = _arrivalAndDepartureService.getArrivalsAndDeparturesForStopInTimeRange(stop, target, fromTime, toTime);
    List<ArrivalAndDepartureBean> beans = new ArrayList<ArrivalAndDepartureBean>();
    Map<AgencyAndId, StopBean> stopBeanCache = new HashMap<AgencyAndId, StopBean>();
    for (ArrivalAndDepartureInstance instance : instances) {
        FrequencyEntry frequency = instance.getFrequency();
        long from = frequency != null ? frequencyFromTime : nonFrequencyFromTime;
        long to = frequency != null ? frequencyToTime : nonFrequencyToTime;
        if (!isArrivalAndDepartureInRange(instance, from, to))
            continue;
        ArrivalAndDepartureBean bean = getStopTimeInstanceAsBean(time, instance, stopBeanCache);
        applyBlockLocationToBean(instance, bean, time);
        Boolean isNegativeScheduledArrivalsEnabled = _gtfsRealtimeNegativeArrivals.getShowNegativeScheduledArrivalByAgencyId(instance.getBlockTrip().getTrip().getId().getAgencyId());
        if (isNegativeScheduledArrivalsEnabled != null && !isNegativeScheduledArrivalsEnabled && bean.getNumberOfStopsAway() < 0 && bean.getPredictedArrivalTime() <= 0)
            continue;
        applySituationsToBean(time, instance, bean);
        beans.add(bean);
    }
    Collections.sort(beans, new ArrivalAndDepartureComparator());
    return beans;
}
Also used : AgencyAndId(org.onebusaway.gtfs.model.AgencyAndId) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) FrequencyEntry(org.onebusaway.transit_data_federation.services.transit_graph.FrequencyEntry) TargetTime(org.onebusaway.transit_data_federation.model.TargetTime) ArrivalAndDepartureBean(org.onebusaway.transit_data.model.ArrivalAndDepartureBean) StopEntry(org.onebusaway.transit_data_federation.services.transit_graph.StopEntry) ArrivalAndDepartureInstance(org.onebusaway.transit_data_federation.services.realtime.ArrivalAndDepartureInstance) StopBean(org.onebusaway.transit_data.model.StopBean)

Example 22 with FrequencyEntry

use of org.onebusaway.transit_data_federation.services.transit_graph.FrequencyEntry in project onebusaway-application-modules by camsys.

the class FrequenciesEntriesFactoryTest method testSingleTripWithFrequencies.

@Test
public void testSingleTripWithFrequencies() {
    BlockEntryImpl block = block("block");
    TripEntryImpl tripEntryA = trip("trip").setRoute(_routeEntry).setServiceId(_lsid).setBlock(block);
    _graph.putTripEntry(tripEntryA);
    addStopTime(tripEntryA, stopTime().setStop(_stopA).setTime(time(7, 00)));
    BlockConfigurationEntry blockConfig = blockConfiguration(block, serviceIds("serviceId"), tripEntryA);
    Trip trip = new Trip();
    trip.setId(tripEntryA.getId());
    Frequency freqA = new Frequency();
    freqA.setTrip(trip);
    freqA.setStartTime(time(8, 00));
    freqA.setEndTime(time(10, 00));
    freqA.setHeadwaySecs(10 * 60);
    freqA.setExactTimes(0);
    Frequency freqB = new Frequency();
    freqB.setTrip(trip);
    freqB.setStartTime(time(10, 00));
    freqB.setEndTime(time(12, 00));
    freqB.setHeadwaySecs(10 * 60);
    freqB.setExactTimes(0);
    Mockito.when(_dao.getAllFrequencies()).thenReturn(Arrays.asList(freqA, freqB));
    _graph.initialize();
    _factory.processFrequencies(_graph);
    blockConfig = block.getConfigurations().get(0);
    List<FrequencyEntry> frequencies = blockConfig.getFrequencies();
    assertEquals(2, frequencies.size());
    FrequencyEntry frequency = frequencies.get(0);
    assertEquals(freqA.getStartTime(), frequency.getStartTime());
    assertEquals(freqA.getEndTime(), frequency.getEndTime());
    assertEquals(freqA.getHeadwaySecs(), frequency.getHeadwaySecs());
    assertEquals(freqA.getExactTimes(), frequency.getExactTimes());
}
Also used : Trip(org.onebusaway.gtfs.model.Trip) Frequency(org.onebusaway.gtfs.model.Frequency) FrequencyEntry(org.onebusaway.transit_data_federation.services.transit_graph.FrequencyEntry) BlockEntryImpl(org.onebusaway.transit_data_federation.impl.transit_graph.BlockEntryImpl) BlockConfigurationEntry(org.onebusaway.transit_data_federation.services.transit_graph.BlockConfigurationEntry) TripEntryImpl(org.onebusaway.transit_data_federation.impl.transit_graph.TripEntryImpl) Test(org.junit.Test)

Example 23 with FrequencyEntry

use of org.onebusaway.transit_data_federation.services.transit_graph.FrequencyEntry in project onebusaway-application-modules by camsys.

the class TripStatusBeanServiceImpl method getBlockLocationAsStatusBean.

@Override
public TripStatusBean getBlockLocationAsStatusBean(BlockLocation blockLocation, long time) {
    TripStatusBean bean = new TripStatusBean();
    bean.setStatus("default");
    BlockInstance blockInstance = blockLocation.getBlockInstance();
    long serviceDate = blockInstance.getServiceDate();
    bean.setServiceDate(serviceDate);
    bean.setLastUpdateTime(blockLocation.getLastUpdateTime());
    bean.setLastLocationUpdateTime(blockLocation.getLastLocationUpdateTime());
    bean.setLastKnownLocation(blockLocation.getLastKnownLocation());
    bean.setLastKnownOrientation(blockLocation.getLastKnownOrientation());
    bean.setLocation(blockLocation.getLocation());
    bean.setOrientation(blockLocation.getOrientation());
    bean.setLastKnownLocation(blockLocation.getLastKnownLocation());
    if (blockLocation.isLastKnownOrientationSet())
        bean.setLastKnownOrientation(blockLocation.getLastKnownOrientation());
    bean.setScheduleDeviation(blockLocation.getScheduleDeviation());
    BlockTripInstance activeTripInstance = blockLocation.getActiveTripInstance();
    if (activeTripInstance != null) {
        BlockTripEntry activeBlockTrip = activeTripInstance.getBlockTrip();
        bean.setScheduledDistanceAlongTrip(blockLocation.getScheduledDistanceAlongBlock() - activeBlockTrip.getDistanceAlongBlock());
        bean.setDistanceAlongTrip(blockLocation.getDistanceAlongBlock() - activeBlockTrip.getDistanceAlongBlock());
        TripEntry activeTrip = activeBlockTrip.getTrip();
        bean.setTotalDistanceAlongTrip(activeTrip.getTotalTripDistance());
        TripBean activeTripBean = _tripBeanService.getTripForId(activeTrip.getId());
        bean.setActiveTrip(activeTripBean);
        bean.setBlockTripSequence(activeBlockTrip.getSequence());
        if (blockLocation.isLastKnownDistanceAlongBlockSet()) {
            bean.setLastKnownDistanceAlongTrip(blockLocation.getLastKnownDistanceAlongBlock() - activeBlockTrip.getDistanceAlongBlock());
        }
        FrequencyEntry frequencyLabel = activeTripInstance.getFrequencyLabel();
        if (frequencyLabel != null) {
            FrequencyBean fb = FrequencyBeanLibrary.getBeanForFrequency(serviceDate, frequencyLabel);
            bean.setFrequency(fb);
        }
    } else {
        _log.warn("no active block trip for block location: blockInstance=" + blockLocation.getBlockInstance() + " time=" + time);
    }
    BlockStopTimeEntry closestStop = blockLocation.getClosestStop();
    if (closestStop != null) {
        StopTimeEntry stopTime = closestStop.getStopTime();
        StopBean stopBean = _stopBeanService.getStopForId(stopTime.getStop().getId());
        bean.setClosestStop(stopBean);
        bean.setClosestStopTimeOffset(blockLocation.getClosestStopTimeOffset());
    }
    BlockStopTimeEntry nextStop = blockLocation.getNextStop();
    if (nextStop != null) {
        StopTimeEntry stopTime = nextStop.getStopTime();
        StopBean stopBean = _stopBeanService.getStopForId(stopTime.getStop().getId());
        bean.setNextStop(stopBean);
        bean.setNextStopTimeOffset(blockLocation.getNextStopTimeOffset());
        bean.setNextStopDistanceFromVehicle(blockLocation.getNextStop().getDistanceAlongBlock() - blockLocation.getDistanceAlongBlock());
    }
    BlockStopTimeEntry previousStop = blockLocation.getPreviousStop();
    if (previousStop != null) {
        StopTimeEntry stopTime = previousStop.getStopTime();
        StopBean stopBean = _stopBeanService.getStopForId(stopTime.getStop().getId());
        bean.setPreviousStop(stopBean);
        bean.setPreviousStopTimeOffset(blockLocation.getPreviousStopTimeOffset());
        bean.setPreviousStopDistanceFromVehicle(blockLocation.getPreviousStop().getDistanceAlongBlock() - blockLocation.getDistanceAlongBlock());
    }
    EVehiclePhase phase = blockLocation.getPhase();
    if (phase != null)
        bean.setPhase(phase.toLabel());
    String status = blockLocation.getStatus();
    if (status != null)
        bean.setStatus(status);
    if (blockLocation.getVehicleType() != null)
        bean.setVehicleType(blockLocation.getVehicleType().toLabel());
    bean.setPredicted(blockLocation.isPredicted());
    AgencyAndId vid = blockLocation.getVehicleId();
    if (vid != null)
        bean.setVehicleId(ApplicationBeanLibrary.getId(vid));
    if (activeTripInstance != null) {
        List<ServiceAlertBean> situations = _serviceAlertBeanService.getServiceAlertsForVehicleJourney(time, activeTripInstance, blockLocation.getVehicleId());
        if (!situations.isEmpty())
            bean.setSituations(situations);
    }
    if (blockLocation.getTimepointPredictions() != null && blockLocation.getTimepointPredictions().size() > 0) {
        List<TimepointPredictionBean> timepointPredictions = new ArrayList<TimepointPredictionBean>();
        for (TimepointPredictionRecord tpr : blockLocation.getTimepointPredictions()) {
            TimepointPredictionBean tpb = new TimepointPredictionBean();
            tpb.setTimepointId(tpr.getTimepointId().toString());
            tpb.setTripId(tpr.getTripId().toString());
            tpb.setStopSequence(tpr.getStopSequence());
            tpb.setTimepointPredictedArrivalTime(tpr.getTimepointPredictedArrivalTime());
            tpb.setTimepointPredictedDepartureTime(tpr.getTimepointPredictedDepartureTime());
            timepointPredictions.add(tpb);
        }
        bean.setTimepointPredictions(timepointPredictions);
    }
    return bean;
}
Also used : TimepointPredictionBean(org.onebusaway.transit_data.model.trips.TimepointPredictionBean) BlockTripInstance(org.onebusaway.transit_data_federation.services.blocks.BlockTripInstance) AgencyAndId(org.onebusaway.gtfs.model.AgencyAndId) BlockTripEntry(org.onebusaway.transit_data_federation.services.transit_graph.BlockTripEntry) ArrayList(java.util.ArrayList) TripEntry(org.onebusaway.transit_data_federation.services.transit_graph.TripEntry) BlockTripEntry(org.onebusaway.transit_data_federation.services.transit_graph.BlockTripEntry) TripBean(org.onebusaway.transit_data.model.trips.TripBean) FrequencyEntry(org.onebusaway.transit_data_federation.services.transit_graph.FrequencyEntry) EVehiclePhase(org.onebusaway.realtime.api.EVehiclePhase) FrequencyBean(org.onebusaway.transit_data.model.schedule.FrequencyBean) StopTimeEntry(org.onebusaway.transit_data_federation.services.transit_graph.StopTimeEntry) BlockStopTimeEntry(org.onebusaway.transit_data_federation.services.transit_graph.BlockStopTimeEntry) BlockInstance(org.onebusaway.transit_data_federation.services.blocks.BlockInstance) TimepointPredictionRecord(org.onebusaway.realtime.api.TimepointPredictionRecord) StopBean(org.onebusaway.transit_data.model.StopBean) TripStatusBean(org.onebusaway.transit_data.model.trips.TripStatusBean) ServiceAlertBean(org.onebusaway.transit_data.model.service_alerts.ServiceAlertBean) BlockStopTimeEntry(org.onebusaway.transit_data_federation.services.transit_graph.BlockStopTimeEntry)

Example 24 with FrequencyEntry

use of org.onebusaway.transit_data_federation.services.transit_graph.FrequencyEntry in project onebusaway-application-modules by camsys.

the class BlockCalendarServiceImpl method findFrequencyBlockTripsInRange.

private void findFrequencyBlockTripsInRange(FrequencyServiceIntervalBlock serviceIntervalIndex, Date serviceDate, Date timeFrom, Date timeTo, List<BlockTripEntry> trips, List<FrequencyEntry> frequencies, Collection<BlockInstance> instances) {
    int scheduledTimeFrom = (int) ((timeFrom.getTime() - serviceDate.getTime()) / 1000);
    int scheduledTimeTo = (int) ((timeTo.getTime() - serviceDate.getTime()) / 1000);
    int indexFrom = index(Arrays.binarySearch(serviceIntervalIndex.getEndTimes(), scheduledTimeFrom));
    int indexTo = index(Arrays.binarySearch(serviceIntervalIndex.getStartTimes(), scheduledTimeTo));
    for (int in = indexFrom; in < indexTo; in++) {
        BlockTripEntry trip = trips.get(in);
        BlockConfigurationEntry block = trip.getBlockConfiguration();
        FrequencyEntry frequency = frequencies.get(in);
        InstanceState state = new InstanceState(serviceDate.getTime(), frequency);
        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) FrequencyEntry(org.onebusaway.transit_data_federation.services.transit_graph.FrequencyEntry) BlockConfigurationEntry(org.onebusaway.transit_data_federation.services.transit_graph.BlockConfigurationEntry)

Example 25 with FrequencyEntry

use of org.onebusaway.transit_data_federation.services.transit_graph.FrequencyEntry in project onebusaway-application-modules by camsys.

the class BlockCalendarServiceImpl method handleFrequencyBlockIndex.

private Collection<BlockInstance> handleFrequencyBlockIndex(FrequencyBlockTripIndex index, Date timeFrom, Date timeTo, Collection<BlockInstance> instances) {
    List<BlockTripEntry> trips = index.getTrips();
    List<FrequencyEntry> frequencies = index.getFrequencies();
    FrequencyServiceIntervalBlock serviceIntervalBlock = index.getServiceIntervalBlock();
    ServiceInterval serviceInterval = serviceIntervalBlock.getRange();
    Collection<Date> serviceDates = _calendarService.getServiceDatesWithinRange(index.getServiceIds(), serviceInterval, timeFrom, timeTo);
    for (Date serviceDate : serviceDates) {
        findFrequencyBlockTripsInRange(serviceIntervalBlock, serviceDate, timeFrom, timeTo, trips, frequencies, instances);
    }
    return instances;
}
Also used : FrequencyServiceIntervalBlock(org.onebusaway.transit_data_federation.services.blocks.FrequencyServiceIntervalBlock) BlockTripEntry(org.onebusaway.transit_data_federation.services.transit_graph.BlockTripEntry) ServiceInterval(org.onebusaway.gtfs.model.calendar.ServiceInterval) FrequencyEntry(org.onebusaway.transit_data_federation.services.transit_graph.FrequencyEntry) Date(java.util.Date)

Aggregations

FrequencyEntry (org.onebusaway.transit_data_federation.services.transit_graph.FrequencyEntry)28 BlockTripEntry (org.onebusaway.transit_data_federation.services.transit_graph.BlockTripEntry)12 ArrayList (java.util.ArrayList)10 BlockConfigurationEntry (org.onebusaway.transit_data_federation.services.transit_graph.BlockConfigurationEntry)8 BlockStopTimeEntry (org.onebusaway.transit_data_federation.services.transit_graph.BlockStopTimeEntry)7 TripEntry (org.onebusaway.transit_data_federation.services.transit_graph.TripEntry)7 AgencyAndId (org.onebusaway.gtfs.model.AgencyAndId)6 BlockEntryImpl (org.onebusaway.transit_data_federation.impl.transit_graph.BlockEntryImpl)5 TripEntryImpl (org.onebusaway.transit_data_federation.impl.transit_graph.TripEntryImpl)5 FrequencyBlockStopTimeEntry (org.onebusaway.transit_data_federation.services.transit_graph.FrequencyBlockStopTimeEntry)5 Date (java.util.Date)4 Test (org.junit.Test)4 FrequencyBean (org.onebusaway.transit_data.model.schedule.FrequencyBean)4 TripBean (org.onebusaway.transit_data.model.trips.TripBean)4 StopTimeInstance (org.onebusaway.transit_data_federation.model.StopTimeInstance)4 BlockInstance (org.onebusaway.transit_data_federation.services.blocks.BlockInstance)4 FrequencyBlockStopTimeIndex (org.onebusaway.transit_data_federation.services.blocks.FrequencyBlockStopTimeIndex)4 FrequencyServiceIntervalBlock (org.onebusaway.transit_data_federation.services.blocks.FrequencyServiceIntervalBlock)4 InstanceState (org.onebusaway.transit_data_federation.services.blocks.InstanceState)4 HashMap (java.util.HashMap)3