use of org.onebusaway.transit_data_federation.services.realtime.ArrivalAndDepartureInstance in project onebusaway-application-modules by camsys.
the class ArrivalsAndDeparturesBeanServiceImplTest method test.
@Test
public void test() {
long t = dateAsLong("2010-10-05 16:30");
long serviceDate = dateAsLong("2010-10-05 00:00");
int minutesBefore = 5;
int minutesAfter = 30;
StopEntryImpl stopA = stop("stopA", 47.0, -122.0);
StopEntryImpl stopB = stop("stopB", 47.0, -122.0);
Mockito.when(_transitGraphDao.getStopEntryForId(stopA.getId(), true)).thenReturn(stopA);
Mockito.when(_transitGraphDao.getStopEntryForId(stopB.getId(), true)).thenReturn(stopB);
/**
**
* Block A
***
*/
BlockEntryImpl blockA = block("blockA");
TripEntryImpl tripA = trip("tripA", "sA", 3000);
stopTime(0, stopA, tripA, time(16, 30), time(16, 35), 1000);
StopTimeEntryImpl stopTimeAB = stopTime(1, stopB, tripA, time(16, 40), time(16, 45), 2000);
BlockConfigurationEntry blockConfigA = blockConfiguration(blockA, serviceIds(lsids("sA"), lsids()), tripA);
BlockStopTimeEntry bstAA = blockConfigA.getStopTimes().get(0);
BlockStopTimeEntry bstAB = blockConfigA.getStopTimes().get(1);
/**
**
* Block B
***
*/
BlockEntryImpl blockB = block("blockB");
TripEntryImpl tripB = trip("tripB", "sA", 3000);
stopTime(2, stopA, tripB, time(16, 40), time(16, 45), 1000);
StopTimeEntryImpl stopTimeBB = stopTime(3, stopB, tripB, time(16, 50), time(16, 55), 2000);
BlockConfigurationEntry blockConfigB = blockConfiguration(blockB, serviceIds(lsids("sA"), lsids()), tripB);
BlockStopTimeEntry bstBA = blockConfigB.getStopTimes().get(0);
BlockStopTimeEntry bstBB = blockConfigB.getStopTimes().get(1);
/**
**
*
***
*/
BlockInstance blockInstanceA = new BlockInstance(blockConfigA, serviceDate);
long lastUpdateTime = dateAsLong("2010-10-05 16:15");
BlockLocation blockLocationA = new BlockLocation();
blockLocationA.setActiveTrip(bstAA.getTrip());
blockLocationA.setBlockInstance(blockInstanceA);
blockLocationA.setClosestStop(bstAA);
blockLocationA.setDistanceAlongBlock(500);
blockLocationA.setInService(true);
blockLocationA.setLastUpdateTime(lastUpdateTime);
blockLocationA.setNextStop(bstAA);
blockLocationA.setPredicted(true);
blockLocationA.setScheduledDistanceAlongBlock(600);
blockLocationA.setScheduleDeviation(10 * 60);
blockLocationA.setVehicleId(aid("vehicle"));
/**
**
*
***
*/
BlockInstance blockInstanceB = new BlockInstance(blockConfigB, serviceDate);
BlockLocation blockLocationB = new BlockLocation();
blockLocationB.setActiveTrip(bstBA.getTrip());
blockLocationB.setBlockInstance(blockInstanceA);
blockLocationB.setClosestStop(bstBA);
blockLocationB.setDistanceAlongBlock(400);
blockLocationB.setInService(true);
blockLocationB.setNextStop(bstAA);
blockLocationB.setPredicted(false);
blockLocationB.setScheduledDistanceAlongBlock(400);
/**
**
*
***
*/
long stopTimeFrom = t - minutesBefore * 60 * 1000;
long stopTimeTo = t + minutesAfter * 60 * 1000;
StopTimeInstance sti1 = new StopTimeInstance(bstAB, blockInstanceA.getState());
ArrivalAndDepartureInstance in1 = new ArrivalAndDepartureInstance(sti1);
in1.setBlockLocation(blockLocationA);
in1.setPredictedArrivalTime((long) (in1.getScheduledArrivalTime() + 5 * 60 * 1000));
in1.setPredictedDepartureTime((long) (in1.getScheduledDepartureTime()));
StopTimeInstance sti2 = new StopTimeInstance(bstBB, blockInstanceB.getState());
ArrivalAndDepartureInstance in2 = new ArrivalAndDepartureInstance(sti2);
in2.setBlockLocation(blockLocationB);
TargetTime target = new TargetTime(t, t);
Mockito.when(_arrivalAndDepartureService.getArrivalsAndDeparturesForStopInTimeRange(stopB, target, stopTimeFrom, stopTimeTo)).thenReturn(Arrays.asList(in1, in2));
/**
**
*
***
*/
Builder stopTimeNarrative = StopTimeNarrative.builder();
stopTimeNarrative.setStopHeadsign("Downtown");
Mockito.when(_narrativeService.getStopTimeForEntry(stopTimeAB)).thenReturn(stopTimeNarrative.create());
stopTimeNarrative = StopTimeNarrative.builder();
stopTimeNarrative.setRouteShortName("XX");
Mockito.when(_narrativeService.getStopTimeForEntry(stopTimeBB)).thenReturn(stopTimeNarrative.create());
/**
**
*
***
*/
StopBean stopABean = new StopBean();
stopABean.setId("1_stopA");
Mockito.when(_stopBeanService.getStopForId(stopA.getId())).thenReturn(stopABean);
StopBean stopBBean = new StopBean();
stopBBean.setId("1_stopB");
Mockito.when(_stopBeanService.getStopForId(stopB.getId())).thenReturn(stopBBean);
/**
**
*
***
*/
TripBean tripABean = new TripBean();
Mockito.when(_tripBeanService.getTripForId(aid("tripA"))).thenReturn(tripABean);
TripBean tripBBean = new TripBean();
Mockito.when(_tripBeanService.getTripForId(aid("tripB"))).thenReturn(tripBBean);
/**
**
*
***
*/
TripStatusBean tripStatusBeanA = new TripStatusBean();
TripStatusBean tripStatusBeanB = new TripStatusBean();
Mockito.when(_tripDetailsBeanService.getBlockLocationAsStatusBean(blockLocationA, t)).thenReturn(tripStatusBeanA);
Mockito.when(_tripDetailsBeanService.getBlockLocationAsStatusBean(blockLocationB, t)).thenReturn(tripStatusBeanB);
/**
**
*
***
*/
ArrivalsAndDeparturesQueryBean query = new ArrivalsAndDeparturesQueryBean();
query.setTime(t);
query.setMinutesBefore(minutesBefore);
query.setMinutesAfter(minutesAfter);
query.setFrequencyMinutesBefore(minutesBefore);
query.setFrequencyMinutesAfter(minutesAfter);
List<ArrivalAndDepartureBean> arrivalsAndDepartures = _service.getArrivalsAndDeparturesByStopId(stopB.getId(), query);
assertEquals(2, arrivalsAndDepartures.size());
ArrivalAndDepartureBean bean = arrivalsAndDepartures.get(0);
assertEquals(1500, bean.getDistanceFromStop(), 0.0);
assertEquals(lastUpdateTime, bean.getLastUpdateTime().longValue());
assertEquals(1, bean.getNumberOfStopsAway());
assertEquals(dateAsLong("2010-10-05 16:45"), bean.getPredictedArrivalTime());
assertEquals(dateAsLong("2010-10-05 16:45"), bean.getPredictedDepartureTime());
assertNull(bean.getRouteShortName());
assertEquals(dateAsLong("2010-10-05 16:40"), bean.getScheduledArrivalTime());
assertEquals(dateAsLong("2010-10-05 16:45"), bean.getScheduledDepartureTime());
assertEquals(serviceDate, bean.getServiceDate());
assertEquals("default", bean.getStatus());
assertSame(stopBBean, bean.getStop());
assertSame(tripABean, bean.getTrip());
assertSame(tripStatusBeanA, bean.getTripStatus());
assertEquals("Downtown", bean.getTripHeadsign());
assertEquals("1_vehicle", bean.getVehicleId());
bean = arrivalsAndDepartures.get(1);
assertEquals(1600, bean.getDistanceFromStop(), 0.0);
assertNull(bean.getLastUpdateTime());
assertEquals(1, bean.getNumberOfStopsAway());
assertEquals(0L, bean.getPredictedArrivalTime());
assertEquals(0L, bean.getPredictedDepartureTime());
assertEquals("XX", bean.getRouteShortName());
assertEquals(dateAsLong("2010-10-05 16:50"), bean.getScheduledArrivalTime());
assertEquals(dateAsLong("2010-10-05 16:55"), bean.getScheduledDepartureTime());
assertEquals(serviceDate, bean.getServiceDate());
assertEquals("default", bean.getStatus());
assertSame(stopBBean, bean.getStop());
assertSame(tripBBean, bean.getTrip());
assertSame(tripStatusBeanB, bean.getTripStatus());
assertNull(bean.getTripHeadsign());
assertNull(bean.getVehicleId());
}
use of org.onebusaway.transit_data_federation.services.realtime.ArrivalAndDepartureInstance 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;
}
use of org.onebusaway.transit_data_federation.services.realtime.ArrivalAndDepartureInstance in project onebusaway-application-modules by camsys.
the class ArrivalAndDepartureServiceImpl method getNextStopArrivalAndDeparture.
@Override
public ArrivalAndDepartureInstance getNextStopArrivalAndDeparture(ArrivalAndDepartureInstance instance) {
BlockStopTimeEntry stopTime = instance.getBlockStopTime();
BlockTripEntry trip = stopTime.getTrip();
BlockConfigurationEntry blockConfig = trip.getBlockConfiguration();
List<BlockStopTimeEntry> stopTimes = blockConfig.getStopTimes();
int index = stopTime.getBlockSequence() + 1;
if (index >= stopTimes.size())
return null;
BlockStopTimeEntry nextStopTime = stopTimes.get(index);
InstanceState state = instance.getStopTimeInstance().getState();
ArrivalAndDepartureTime scheduledTime = ArrivalAndDepartureTime.getScheduledTime(state, nextStopTime);
if (state.getFrequency() != null) {
StopTimeEntry nStopTime = nextStopTime.getStopTime();
int betweenStopDelta = nStopTime.getArrivalTime() - stopTime.getStopTime().getDepartureTime();
int atStopDelta = nStopTime.getDepartureTime() - nStopTime.getArrivalTime();
long scheduledArrivalTime = instance.getScheduledDepartureTime() + betweenStopDelta * 1000;
long scheduledDepartureTime = scheduledArrivalTime + atStopDelta * 1000;
scheduledTime.setArrivalTime(scheduledArrivalTime);
scheduledTime.setDepartureTime(scheduledDepartureTime);
}
StopTimeInstance nextStopTimeInstance = new StopTimeInstance(stopTime, state);
ArrivalAndDepartureInstance nextInstance = new ArrivalAndDepartureInstance(nextStopTimeInstance, scheduledTime);
if (instance.isPredictedDepartureTimeSet()) {
int scheduledDeviation = (int) ((instance.getPredictedDepartureTime() - instance.getScheduledDepartureTime()) / 1000);
int arrivalDeviation = propagateScheduleDeviationForwardBetweenStops(stopTime, nextStopTime, scheduledDeviation);
int departureDeviation = propagateScheduleDeviationForwardAcrossStop(nextStopTime, arrivalDeviation);
setPredictedArrivalTimeForInstance(nextInstance, nextInstance.getScheduledArrivalTime() + arrivalDeviation * 1000);
setPredictedDepartureTimeForInstance(nextInstance, nextInstance.getScheduledDepartureTime() + departureDeviation * 1000);
}
return nextInstance;
}
use of org.onebusaway.transit_data_federation.services.realtime.ArrivalAndDepartureInstance in project onebusaway-application-modules by camsys.
the class ArrivalAndDepartureServiceImpl method getNextScheduledBlockTripDeparturesForStop.
@Override
public List<ArrivalAndDepartureInstance> getNextScheduledBlockTripDeparturesForStop(StopEntry stop, long time, boolean includePrivateService) {
List<StopTimeInstance> stopTimes = _stopTimeService.getNextBlockSequenceDeparturesForStop(stop, time, includePrivateService);
List<ArrivalAndDepartureInstance> instances = new ArrayList<ArrivalAndDepartureInstance>();
for (StopTimeInstance sti : stopTimes) {
ArrivalAndDepartureInstance instance = createArrivalAndDepartureForStopTimeInstance(sti, time);
instances.add(instance);
}
return instances;
}
use of org.onebusaway.transit_data_federation.services.realtime.ArrivalAndDepartureInstance in project onebusaway-application-modules by camsys.
the class ArrivalAndDepartureServiceImpl method createArrivalAndDepartureForStopTimeInstance.
private ArrivalAndDepartureInstance createArrivalAndDepartureForStopTimeInstance(StopTimeInstance sti, long prevFrequencyTime) {
ArrivalAndDepartureInstance instance = createArrivalAndDeparture(sti, prevFrequencyTime, sti.getFrequencyOffset());
instance.setBlockSequence(sti.getBlockSequence());
return instance;
}
Aggregations