use of org.onebusaway.realtime.api.TimepointPredictionRecord in project onebusaway-application-modules by camsys.
the class ArrivalAndDepartureServiceImpl method setPredictedTimesFromTimepointPredictionRecords.
private boolean setPredictedTimesFromTimepointPredictionRecords(ArrivalAndDepartureInstance instance, BlockLocation blockLocation, long targetTime) {
List<TimepointPredictionRecord> records = blockLocation.getTimepointPredictions();
if (records == null)
return false;
// Find the right timepoint prediction record. We need to make sure that
// there are the proper number of records if the trip loops and stopSequence
// is not set.
int stopSequence = instance.getBlockStopTime().getStopTime().getSequence();
int gtfsSequence = instance.getBlockStopTime().getStopTime().getGtfsSequence();
int totalCandidates = 0;
// index (with respect to stop sequence) among stops
int thisStopIndex = 0;
// with the same ID
List<BlockStopTimeEntry> stopTimes = instance.getBlockTrip().getStopTimes();
for (int i = 0; i < stopTimes.size(); i++) {
BlockStopTimeEntry stopTime = stopTimes.get(i);
StopTimeEntry stop = stopTime.getStopTime();
if (stop.getStop().getId().equals(instance.getStop().getId())) {
totalCandidates++;
if (stop.getSequence() < stopSequence)
thisStopIndex++;
}
}
int tprTotalCandidates = 0;
int tprStopIndex = 0;
boolean success = false;
for (TimepointPredictionRecord tpr : records) {
boolean tripMatches = tpr.getTripId().equals(instance.getBlockTrip().getTrip().getId());
boolean stopMatches = tpr.getTimepointId().equals(instance.getStop().getId());
boolean sequenceMatches = tpr.getStopSequence() > 0 && tpr.getStopSequence() == gtfsSequence;
if (!tripMatches || !stopMatches)
continue;
if (sequenceMatches || tprStopIndex == thisStopIndex) {
success = true;
long arrivalTime = tpr.getTimepointPredictedArrivalTime();
long departureTime = tpr.getTimepointPredictedDepartureTime();
if (departureTime <= 0) {
int slack = instance.getBlockStopTime().getStopTime().getSlackTime();
departureTime = arrivalTime + slack * 1000;
}
setPredictedDepartureTimeForInstance(instance, departureTime);
/*
* if arrivalTime is -1 be polite to clients and serve departureTime
*/
if (arrivalTime == -1) {
setPredictedArrivalTimeForInstance(instance, departureTime);
} else {
setPredictedArrivalTimeForInstance(instance, arrivalTime);
}
if (sequenceMatches)
return true;
} else if (tprStopIndex < thisStopIndex)
tprStopIndex++;
tprTotalCandidates++;
}
if (success && totalCandidates == tprTotalCandidates && tprStopIndex == thisStopIndex)
return true;
// Clear out prediction times if we didn't end up finding the proper number
// of records
setPredictedArrivalTimeForInstance(instance, 0);
setPredictedDepartureTimeForInstance(instance, 0);
return false;
}
use of org.onebusaway.realtime.api.TimepointPredictionRecord in project onebusaway-application-modules by camsys.
the class ArrivalAndDepartureServiceImplTest method testGetArrivalsAndDeparturesForStopInTimeRange10.
/**
* This method tests loop routes (the same stop is visited more
* than once in the same trip) when stop_sequence is missing in the real-time feed.
* It applies the prediction if there is more than one prediction in the same trip
* (i.e., the stop_id can be disambiguated).
*
* Test configuration: There are three different loop trips and each trip has 3 stops
* Time point predictions does not have stop sequences. First 2 stops in middle trip
* have predictions.
*
* Current time = 14:00
* Schedule time Real-time from feed GTFS stop_sequence trip_id
* Stop A 13:30 ----- 0 t1
* Stop B 13:45 ----- 1 t1
* Stop A 13:55 ----- 2 t1
*
* Stop A 14:05 14:10 0 t2
* Stop B 14:15 14:25 1 t2
* Stop A 14:25 ----- 2 t2
*
* Stop A 14:30 ----- 0 t3
* Stop B 14:45 ----- 1 t3
* Stop A 14:55 ----- 2 t3
*/
@Test
public void testGetArrivalsAndDeparturesForStopInTimeRange10() {
// Override the current time with a later time than the time point
// predictions
mCurrentTime = dateAsLong("2015-07-23 14:00");
// Set time point predictions for stop A
TimepointPredictionRecord tprA = new TimepointPredictionRecord();
tprA.setTimepointId(mStopA.getId());
long tprATime = createPredictedTime(time(14, 10));
tprA.setTimepointPredictedArrivalTime(tprATime);
tprA.setTripId(mTrip2.getId());
// Set time point predictions for stop B
TimepointPredictionRecord tprB = new TimepointPredictionRecord();
tprB.setTimepointId(mStopB.getId());
long tprBTime = createPredictedTime(time(14, 25));
tprB.setTimepointPredictedArrivalTime(tprBTime);
tprB.setTripId(mTrip2.getId());
// Call ArrivalsAndDeparturesForStopInTimeRange method in
// ArrivalAndDepartureServiceImpl
List<ArrivalAndDepartureInstance> arrivalsAndDepartures = getArrivalsAndDeparturesForLoopRouteInTimeRangeByTimepointPredictionRecordWithMultipleTrips(Arrays.asList(tprA, tprB), mStopB);
long predictedArrivalTimeStopAA = getPredictedArrivalTimeByStopIdAndSequence(arrivalsAndDepartures, mStopA.getId(), mTrip1.getId(), 0);
long predictedArrivalTimeStopAB = getPredictedArrivalTimeByStopIdAndSequence(arrivalsAndDepartures, mStopB.getId(), mTrip1.getId(), 1);
long predictedArrivalTimeStopAC = getPredictedArrivalTimeByStopIdAndSequence(arrivalsAndDepartures, mStopA.getId(), mTrip1.getId(), 2);
/**
* Check the upstream stops and make sure no propagation happening.
*/
assertEquals(predictedArrivalTimeStopAA, 0);
assertEquals(predictedArrivalTimeStopAB, 0);
assertEquals(predictedArrivalTimeStopAC, 0);
/**
* Make sure the predictedArrivalTime for stop A (the last stop) and the stop B in
* the trip B is exactly the same as TimepointPredictionRecord.
*/
long predictedArrivalTimeBA = getPredictedArrivalTimeByStopIdAndSequence(arrivalsAndDepartures, mStopA.getId(), mTrip2.getId(), 0);
long predictedArrivalTimeBB = getPredictedArrivalTimeByStopIdAndSequence(arrivalsAndDepartures, mStopB.getId(), mTrip2.getId(), 1);
assertEquals(tprA.getTimepointPredictedArrivalTime(), predictedArrivalTimeBA);
assertEquals(tprB.getTimepointPredictedArrivalTime(), predictedArrivalTimeBB);
/**
* Make sure the predictions happening downstream based on the last stop
* of the trip B
*/
long scheduledArrivalTimeBB = getScheduledArrivalTimeByStopId(mTrip2, mStopB.getId(), 1);
// Calculate the delay of the last stop A in trip B
long delta = TimeUnit.MILLISECONDS.toSeconds(predictedArrivalTimeBB) - scheduledArrivalTimeBB;
long scheduledArrivalTimeStopBC = getScheduledArrivalTimeByStopId(mTrip2, mStopA.getId(), 2);
long predictedArrivalTimeBC = getPredictedArrivalTimeByStopIdAndSequence(arrivalsAndDepartures, mStopA.getId(), mTrip2.getId(), 2);
long scheduledArrivalTimeStopCA = getScheduledArrivalTimeByStopId(mTrip3, mStopA.getId(), 0);
long predictedArrivalTimeCA = getPredictedArrivalTimeByStopIdAndSequence(arrivalsAndDepartures, mStopA.getId(), mTrip3.getId(), 0);
long scheduledArrivalTimeStopCB = getScheduledArrivalTimeByStopId(mTrip3, mStopB.getId(), 1);
long predictedArrivalTimeCB = getPredictedArrivalTimeByStopIdAndSequence(arrivalsAndDepartures, mStopB.getId(), mTrip3.getId(), 1);
long scheduledArrivalTimeStopCC = getScheduledArrivalTimeByStopId(mTrip3, mStopA.getId(), 2);
long predictedArrivalTimeCC = getPredictedArrivalTimeByStopIdAndSequence(arrivalsAndDepartures, mStopA.getId(), mTrip3.getId(), 2);
assertEquals(scheduledArrivalTimeStopBC + delta, TimeUnit.MILLISECONDS.toSeconds(predictedArrivalTimeBC));
assertEquals(scheduledArrivalTimeStopCA + delta, TimeUnit.MILLISECONDS.toSeconds(predictedArrivalTimeCA));
assertEquals(scheduledArrivalTimeStopCB + delta, TimeUnit.MILLISECONDS.toSeconds(predictedArrivalTimeCB));
assertEquals(scheduledArrivalTimeStopCC + delta, TimeUnit.MILLISECONDS.toSeconds(predictedArrivalTimeCC));
}
use of org.onebusaway.realtime.api.TimepointPredictionRecord in project onebusaway-application-modules by camsys.
the class ArrivalAndDepartureServiceImplTest method testGetArrivalsAndDeparturesForStopInTimeRange03.
/**
* This method tests upstream time point predictions with only a predicted
* *departure* time.
*
* Test configuration: There is only one bus stop (Stop A) which has the real
* time departure time (time point prediction). In this case
* getArrivalsAndDeparturesForStopInTimeRange() should return the time point
* prediction for Stop A's departure time, which replaces the scheduled time
* from GTFS for this stop. For Stop B, the upstream departure prediction for
* Stop A should be propagated down to Stop B, and this deviation should be
* used to calculate Stop B's arrival and departure times.
*
* Current time = 13:00
* Schedule Arrival time Schedule Departure time Real-time departure time
* Stop A 13:30 13:35 13:30
* Stop B 13:45 13:50 ----
*
* When requesting arrival estimate for Stop A, result should be 0 (note this
* isn't currently supported - see FIXME in method body).
*
* When requesting departure estimate for Stop A, result should be exactly
* same with the real-time feed's departure time for Stop A.
*
* When requesting arrival and departure estimate for Stop B, the result
* should be 5 min less then the scheduled arrival and departure times.
* Because the upstream stop departs 5 min early, OBA should subtract this 5
* min deviation from the downstream scheduled values.
*/
@Test
public void testGetArrivalsAndDeparturesForStopInTimeRange03() {
// Set time point predictions for stop A
TimepointPredictionRecord tprA = new TimepointPredictionRecord();
tprA.setTimepointId(mStopA.getId());
long tprATime = createPredictedTime(time(13, 30));
tprA.setTimepointPredictedDepartureTime(tprATime);
tprA.setTripId(mTrip1.getId());
// Call ArrivalsAndDeparturesForStopInTimeRange method in
// ArrivalAndDepartureServiceImpl
List<ArrivalAndDepartureInstance> arrivalsAndDepartures = getArrivalsAndDeparturesForStopInTimeRangeByTimepointPredictionRecord(Arrays.asList(tprA));
long predictedArrivalTimeStopA = getPredictedArrivalTimeByStopId(arrivalsAndDepartures, mStopA.getId());
long predictedDepartureTimeStopA = getPredictedDepartureTimeByStopId(arrivalsAndDepartures, mStopA.getId());
/**
* Check if the predictedDepartureTime is exactly the same with
* TimepointPrediction.
*/
assertEquals(tprA.getTimepointPredictedDepartureTime(), predictedDepartureTimeStopA);
/**
* FIXME - Fully support both real-time arrival and departure times for each
* stop in OBA
*
* We're currently limited by OBA's internal data model which contains only
* one deviation per stop. By GTFS-rt spec, if no real-time arrival
* information is given for a stop, then the scheduled arrival should be
* used. In our case here, we're getting a real-time departure for Stop A
* (and no real-time arrival time for Stop A), but then we're showing the
* real-time departure info for Stop A as the real-time arrival time for
* Stop A. So, we're effectively propagating the real-time value backwards
* within the same stop. The correct value for predictedArrivalTimeStopA is
* actually 0, because we don't have any real-time arrival information for
* Stop A (or upstream of Stop A).
*
* So, the below assertion is currently commented out, as it fails. Future
* work should overhaul OBA's data model to support more than one real-time
* deviation per stop. When this is correctly implemented, the below
* assertion should be uncommented and it should pass.
*/
/*
* TODO: for backward compatiability we did not implement this. Discuss!
*/
// assertEquals(0, predictedArrivalTimeStopA);
/**
* Test for Stop B
*/
long predictedArrivalTimeStopB = getPredictedArrivalTimeByStopId(arrivalsAndDepartures, mStopB.getId());
long predictedDepartureTimeStopB = getPredictedDepartureTimeByStopId(arrivalsAndDepartures, mStopB.getId());
long scheduledDepartureTimeForStopA = getScheduledDepartureTimeByStopId(mTrip1, mStopA.getId());
long scheduledArrivalTimeForStopB = getScheduledArrivalTimeByStopId(mTrip1, mStopB.getId());
long scheduledDepartureTimeForStopB = getScheduledDepartureTimeByStopId(mTrip1, mStopB.getId());
// Calculate the departure time difference from the upstream stop
long deltaB = (scheduledDepartureTimeForStopA - TimeUnit.MILLISECONDS.toSeconds(predictedDepartureTimeStopA));
/**
* Check if the predictedArrivalTime is 5 min less then the scheduled
* arrival time for stop B.
*/
assertEquals(TimeUnit.MINUTES.toSeconds(5), deltaB);
assertEquals(scheduledArrivalTimeForStopB - deltaB, TimeUnit.MILLISECONDS.toSeconds(predictedArrivalTimeStopB));
/**
* Check if the predictedDepartureTime is 5 min less then the scheduled
* departure time for stop B.
*/
assertEquals(scheduledDepartureTimeForStopB - deltaB, TimeUnit.MILLISECONDS.toSeconds(predictedDepartureTimeStopB));
}
use of org.onebusaway.realtime.api.TimepointPredictionRecord in project onebusaway-application-modules by camsys.
the class ArrivalAndDepartureServiceImplTest method testGetArrivalsAndDeparturesForStopInTimeRange13.
/**
* This method tests loop routes (the same stop is visited more
* than once in the same trip) when stop_sequence is missing in the real-time feed.
* It applies the prediction if there is more than one prediction in the same trip
* (i.e., the stop_id can be disambiguated).
*
* Test configuration: There are three different loop trips and each trip has 3 stops.
* Time point predictions does not have stop sequences, and therefore the update for
* each trip Stop A (in trip 1 and trip 2) is ambiguous and cannot be matched. This
* test also ensures the we aren't accidentally recognizing two adjacent updates in
* the block as being part of the same trip. As a result, the updates should be dropped
* and schedule time should be provided.
*
* Current time = 14:00
* Schedule time Real-time from feed GTFS stop_sequence trip_id
* Stop A 13:30 ----- 0 t1
* Stop B 13:45 ----- 1 t1
* Stop A 13:55 14:00 2 t1
*
* Stop A 14:05 14:10 0 t2
* Stop B 14:15 ----- 1 t2
* Stop A 14:25 ----- 2 t2
*
* Stop A 14:30 ----- 0 t3
* Stop B 14:45 ----- 1 t3
* Stop A 14:55 ----- 2 t3
*/
@Test
public void testGetArrivalsAndDeparturesForStopInTimeRange13() {
// Override the current time with a later time than the time point
// predictions
mCurrentTime = dateAsLong("2015-07-23 14:00");
// Set time point predictions for trip 1 stop A
TimepointPredictionRecord tpr1A = new TimepointPredictionRecord();
tpr1A.setTimepointId(mStopA.getId());
long tprAATime = createPredictedTime(time(14, 00));
tpr1A.setTimepointPredictedArrivalTime(tprAATime);
tpr1A.setTripId(mTrip1.getId());
// Set time point predictions for trip 2 stop A
TimepointPredictionRecord tpr2A = new TimepointPredictionRecord();
tpr2A.setTimepointId(mStopA.getId());
long tprATime = createPredictedTime(time(14, 10));
tpr2A.setTimepointPredictedArrivalTime(tprATime);
tpr2A.setTripId(mTrip2.getId());
// Call ArrivalsAndDeparturesForStopInTimeRange method in
// ArrivalAndDepartureServiceImpl
List<ArrivalAndDepartureInstance> arrivalsAndDepartures = getArrivalsAndDeparturesForLoopRouteInTimeRangeByTimepointPredictionRecordWithMultipleTrips(Arrays.asList(tpr1A, tpr2A), mStopB);
// First trip in block
long predictedArrivalTimeStop1A = getPredictedArrivalTimeByStopIdAndSequence(arrivalsAndDepartures, mStopA.getId(), mTrip1.getId(), 0);
long predictedArrivalTimeStop1B = getPredictedArrivalTimeByStopIdAndSequence(arrivalsAndDepartures, mStopB.getId(), mTrip1.getId(), 1);
long predictedArrivalTimeStop1C = getPredictedArrivalTimeByStopIdAndSequence(arrivalsAndDepartures, mStopA.getId(), mTrip1.getId(), 2);
// Second trip in block
long predictedArrivalTimeStop2A = getPredictedArrivalTimeByStopIdAndSequence(arrivalsAndDepartures, mStopA.getId(), mTrip2.getId(), 0);
long predictedArrivalTimeStop2B = getPredictedArrivalTimeByStopIdAndSequence(arrivalsAndDepartures, mStopB.getId(), mTrip2.getId(), 1);
long predictedArrivalTimeStop2C = getPredictedArrivalTimeByStopIdAndSequence(arrivalsAndDepartures, mStopA.getId(), mTrip2.getId(), 2);
// Third trip in block
long predictedArrivalTimeStop3A = getPredictedArrivalTimeByStopIdAndSequence(arrivalsAndDepartures, mStopA.getId(), mTrip3.getId(), 0);
long predictedArrivalTimeStop3B = getPredictedArrivalTimeByStopIdAndSequence(arrivalsAndDepartures, mStopB.getId(), mTrip3.getId(), 1);
long predictedArrivalTimeStop3C = getPredictedArrivalTimeByStopIdAndSequence(arrivalsAndDepartures, mStopA.getId(), mTrip3.getId(), 2);
/**
* Check the all stops and make sure no real-time information has been applied.
*/
assertEquals(predictedArrivalTimeStop1A, 0);
assertEquals(predictedArrivalTimeStop1B, 0);
assertEquals(predictedArrivalTimeStop1C, 0);
assertEquals(predictedArrivalTimeStop2A, 0);
assertEquals(predictedArrivalTimeStop2B, 0);
assertEquals(predictedArrivalTimeStop2C, 0);
assertEquals(predictedArrivalTimeStop3A, 0);
assertEquals(predictedArrivalTimeStop3B, 0);
assertEquals(predictedArrivalTimeStop3C, 0);
}
use of org.onebusaway.realtime.api.TimepointPredictionRecord in project onebusaway-application-modules by camsys.
the class ArrivalAndDepartureServiceImplTest method testGetArrivalsAndDeparturesForStopInTimeRange04.
/**
* This method tests upstream time point predictions with both predicted
* arrival and departure times.
*
* Test configuration: Time point predictions are upstream and include the
* current stop_id, which means that the bus hasn't passed the bus stop yet.
* There is only one bus stop (Stop A) which has the real time arrival and
* departure times (time point prediction). In this case
* getArrivalsAndDeparturesForStopInTimeRange() should return absolute time
* point prediction for Stop A's departure time, which replaces the scheduled
* time from GTFS for this stop. Stop B's predictions should be derived from
* the upstream predictions provided for Stop A.
*
* Current time = 13:00
* Schedule Arrival time Schedule Departure time Real-time arrival time Real-time departure time
* Stop A 13:30 13:35 13:20 13:30
* Stop B 13:45 13:50 ----- -----
*
* When requesting arrival estimate for Stop A, result should be 13:20
* (predicted real-time arrival time). Note that this currently isn't support
* - see FIXME statement in method body.
*
* When requesting departure estimate for Stop A, result should be 13:30
* (predicted real-time departure time).
*
* When requesting arrival and departure estimates for Stop B, results should
* be 5 min less then the scheduled arrival and departure times. Because the
* upstream Stop A departs 5 min early, OBA should subtract this 5 min from
* the downstream estimates.
*/
@Test
public void testGetArrivalsAndDeparturesForStopInTimeRange04() {
// Set time point predictions for stop A
TimepointPredictionRecord tprA = new TimepointPredictionRecord();
tprA.setTimepointId(mStopA.getId());
long tprADepartureTime = createPredictedTime(time(13, 30));
tprA.setTimepointPredictedDepartureTime(tprADepartureTime);
long tprAArrivalTime = createPredictedTime(time(13, 20));
tprA.setTimepointPredictedArrivalTime(tprAArrivalTime);
tprA.setTripId(mTrip1.getId());
// Call ArrivalsAndDeparturesForStopInTimeRange method in
// ArrivalAndDepartureServiceImpl
List<ArrivalAndDepartureInstance> arrivalsAndDepartures = getArrivalsAndDeparturesForStopInTimeRangeByTimepointPredictionRecord(Arrays.asList(tprA));
long predictedDepartureTimeStopA = getPredictedDepartureTimeByStopId(arrivalsAndDepartures, mStopA.getId());
/**
* Check if the predictedDepartureTime is exactly the same with
* TimepointPrediction.
*/
assertEquals(tprA.getTimepointPredictedDepartureTime(), predictedDepartureTimeStopA);
/**
* OBA's data model now supports more than one real-time
* deviation per stop.
*/
long predictedArrivalTimeStopA = getPredictedArrivalTimeByStopId(arrivalsAndDepartures, mStopA.getId());
assertEquals(TimeUnit.MILLISECONDS.toSeconds(tprA.getTimepointPredictedArrivalTime()), TimeUnit.MILLISECONDS.toSeconds(predictedArrivalTimeStopA));
/**
* Test for Stop B
*/
long scheduledDepartureTimeForStopA = getScheduledDepartureTimeByStopId(mTrip1, mStopA.getId());
long predictedArrivalTimeStopB = getPredictedArrivalTimeByStopId(arrivalsAndDepartures, mStopB.getId());
long predictedDepartureTimeStopB = getPredictedDepartureTimeByStopId(arrivalsAndDepartures, mStopB.getId());
long scheduledArrivalTimeForStopB = getScheduledArrivalTimeByStopId(mTrip1, mStopB.getId());
long scheduledDepartureTimeForStopB = getScheduledDepartureTimeByStopId(mTrip1, mStopB.getId());
// Calculate the departure time difference from the upstream stop
long deltaB = scheduledDepartureTimeForStopA - TimeUnit.MILLISECONDS.toSeconds(predictedDepartureTimeStopA);
/**
* Check if the predictedDepartureTime is 5 min less then the scheduled
* departure time for stop B.
*/
assertEquals(TimeUnit.MINUTES.toSeconds(5), deltaB);
assertEquals(scheduledDepartureTimeForStopB - deltaB, TimeUnit.MILLISECONDS.toSeconds(predictedDepartureTimeStopB));
/**
* Check if the predictedArrivalTime is 5 min less then the scheduled
* arrival time for stop B.
*/
assertEquals(scheduledArrivalTimeForStopB - deltaB, TimeUnit.MILLISECONDS.toSeconds(predictedArrivalTimeStopB));
}
Aggregations