use of org.onebusaway.transit_data_federation.impl.transit_graph.TripEntryImpl in project onebusaway-application-modules by camsys.
the class TripEntriesFactory method processTrip.
private TripEntryImpl processTrip(TransitGraphImpl graph, Trip trip) {
List<StopTime> stopTimes = _gtfsDao.getStopTimesForTrip(trip);
// A trip without stop times is a trip we don't care about
if (stopTimes.isEmpty())
return null;
ShapePoints shapePoints = null;
if (trip.getShapeId() != null)
shapePoints = _shapePointsHelper.getShapePointsForShapeId(trip.getShapeId());
Agency agency = trip.getRoute().getAgency();
TimeZone tz = TimeZone.getTimeZone(agency.getTimezone());
LocalizedServiceId lsid = new LocalizedServiceId(trip.getServiceId(), tz);
TripEntryImpl tripEntry = new TripEntryImpl();
tripEntry.setId(trip.getId());
tripEntry.setDirectionId(unique(trip.getDirectionId()));
tripEntry.setServiceId(unique(lsid));
// back it up
if (!(shapePoints == null || shapePoints.isEmpty()))
tripEntry.setShapeId(unique(trip.getShapeId()));
List<StopTimeEntryImpl> stopTimesForTrip = _stopTimeEntriesFactory.processStopTimes(graph, stopTimes, tripEntry, shapePoints);
// Also: only set the trip if there are stops for it
if (stopTimesForTrip == null || stopTimesForTrip.size() < 2) {
_log.error("trip " + trip.getId() + " missing stops!");
return null;
}
double tripDistance = getTripDistance(stopTimesForTrip, shapePoints);
tripEntry.setTotalTripDistance(tripDistance);
tripEntry.setStopTimes(cast(stopTimesForTrip));
graph.putTripEntry(tripEntry);
return tripEntry;
}
use of org.onebusaway.transit_data_federation.impl.transit_graph.TripEntryImpl in project onebusaway-application-modules by camsys.
the class DistanceAlongShapeLibraryTest method readStopTimes.
private List<StopTimeEntryImpl> readStopTimes(String key) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(getClass().getResourceAsStream("DistancesAlongShapeLibraryTest-" + key)));
String line = null;
Map<String, StopEntryImpl> stops = new HashMap<String, StopEntryImpl>();
int index = 0;
TripEntryImpl trip = UnitTestingSupport.trip("trip");
List<StopTimeEntryImpl> stopTimes = new ArrayList<StopTimeEntryImpl>();
while ((line = reader.readLine()) != null) {
String[] tokens = line.split(" ");
String stopId = tokens[0];
double lat = Double.parseDouble(tokens[1]);
double lon = Double.parseDouble(tokens[2]);
StopEntryImpl stop = stops.get(stopId);
if (stop == null) {
stop = UnitTestingSupport.stop(stopId, lat, lon);
stops.put(stopId, stop);
}
StopTimeEntryImpl stopTime = UnitTestingSupport.stopTime(index, stop, trip, index, index, Double.NaN);
stopTimes.add(stopTime);
}
reader.close();
return stopTimes;
}
use of org.onebusaway.transit_data_federation.impl.transit_graph.TripEntryImpl in project onebusaway-application-modules by camsys.
the class StopTimeEntriesFactoryTest method testThreeInARowDuplicateRemoval.
@Test
public void testThreeInARowDuplicateRemoval() {
TransitGraphImpl graph = new TransitGraphImpl();
Stop stopA = new Stop();
stopA.setId(aid("stopA"));
graph.putStopEntry(stop("stopA", 47.672207391799056, -122.387855896286));
Stop stopB = new Stop();
stopB.setId(aid("stopB"));
graph.putStopEntry(stop("stopB", 47.66852277218285, -122.3853882639923));
Stop stopC = new Stop();
stopC.setId(aid("stopC"));
graph.putStopEntry(stop("stopC", 47.66847942216854, -122.37545336180114));
Stop stopD = new Stop();
stopD.setId(aid("stopD"));
graph.putStopEntry(stop("stopD", 47.66947942216854, -122.37545336180114));
graph.refreshStopMapping();
Agency agency = new Agency();
agency.setId("1");
agency.setTimezone("America/Los_Angeles");
Route route = new Route();
route.setAgency(agency);
Trip trip = new Trip();
trip.setRoute(route);
trip.setServiceId(aid("serviceId"));
TripEntryImpl tripEntry = trip("trip");
StopTime stA = new StopTime();
stA.setId(100);
stA.setArrivalTime(time(9, 00));
stA.setDepartureTime(time(9, 05));
stA.setStopSequence(100);
stA.setStop(stopA);
stA.setTrip(trip);
StopTime stB = new StopTime();
stB.setId(101);
stB.setArrivalTime(time(9, 00));
stB.setDepartureTime(time(9, 05));
stB.setStopSequence(101);
stB.setStop(stopB);
stB.setTrip(trip);
StopTime stC = new StopTime();
stC.setId(102);
stC.setArrivalTime(time(9, 00));
stC.setDepartureTime(time(9, 05));
stC.setStopSequence(102);
stC.setStop(stopC);
stC.setTrip(trip);
StopTime stD = new StopTime();
stD.setId(103);
stD.setArrivalTime(time(11, 00));
stD.setDepartureTime(time(11, 05));
stD.setStopSequence(103);
stD.setStop(stopD);
stD.setTrip(trip);
StopTimeEntriesFactory factory = new StopTimeEntriesFactory();
factory.setDistanceAlongShapeLibrary(new DistanceAlongShapeLibrary());
List<StopTime> stopTimes = Arrays.asList(stA, stB, stC, stD);
ShapePointsFactory shapePointsFactory = new ShapePointsFactory();
shapePointsFactory.addPoint(47.673840100841396, -122.38756621771239);
shapePointsFactory.addPoint(47.668667271970484, -122.38756621771239);
shapePointsFactory.addPoint(47.66868172192725, -122.3661729186096);
shapePointsFactory.addPoint(47.66947942216854, -122.37545336180114);
ShapePoints shapePoints = shapePointsFactory.create();
List<StopTimeEntryImpl> entries = factory.processStopTimes(graph, stopTimes, tripEntry, shapePoints);
assertEquals(stopTimes.size(), entries.size());
StopTimeEntryImpl entry = entries.get(0);
assertEquals(0, entry.getAccumulatedSlackTime());
assertEquals(time(9, 00), entry.getArrivalTime());
assertEquals(time(9, 05), entry.getDepartureTime());
assertEquals(0, entry.getSequence());
assertEquals(181.5, entry.getShapeDistTraveled(), 0.1);
assertEquals(5 * 60, entry.getSlackTime());
entry = entries.get(1);
assertEquals(5 * 60, entry.getAccumulatedSlackTime());
assertEquals(time(9, 28, 45), entry.getArrivalTime());
assertEquals(time(9, 28, 45), entry.getDepartureTime());
assertEquals(1, entry.getSequence());
assertEquals(738.7, entry.getShapeDistTraveled(), 0.1);
assertEquals(0, entry.getSlackTime());
entry = entries.get(2);
assertEquals(5 * 60, entry.getAccumulatedSlackTime());
assertEquals(time(10, 00, 34), entry.getArrivalTime());
assertEquals(time(10, 00, 34), entry.getDepartureTime());
assertEquals(2, entry.getSequence());
assertEquals(1484.5, entry.getShapeDistTraveled(), 0.1);
assertEquals(0, entry.getSlackTime());
entry = entries.get(3);
assertEquals(5 * 60, entry.getAccumulatedSlackTime());
assertEquals(time(11, 00), entry.getArrivalTime());
assertEquals(time(11, 05), entry.getDepartureTime());
assertEquals(3, entry.getSequence());
assertEquals(2877.69, entry.getShapeDistTraveled(), 0.1);
assertEquals(60 * 5, entry.getSlackTime());
}
use of org.onebusaway.transit_data_federation.impl.transit_graph.TripEntryImpl in project onebusaway-application-modules by camsys.
the class TripEntriesFactoryTest method test.
@Test
public void test() {
GtfsRelationalDao gtfsDao = Mockito.mock(GtfsRelationalDao.class);
Agency agency = new Agency();
agency.setId("1");
agency.setTimezone("America/Los_Angeles");
// gtfsDao.saveEntity(agency);
Route route = new Route();
route.setId(new AgencyAndId("1", "routeA"));
route.setAgency(agency);
Mockito.when(gtfsDao.getAllRoutes()).thenReturn(Arrays.asList(route));
AgencyAndId shapeId = new AgencyAndId("1", "shapeId");
Trip trip = new Trip();
trip.setId(new AgencyAndId("1", "tripA"));
trip.setRoute(route);
trip.setServiceId(new AgencyAndId("1", "serviceId"));
trip.setShapeId(shapeId);
Mockito.when(gtfsDao.getTripsForRoute(route)).thenReturn(Arrays.asList(trip));
Stop stopA = new Stop();
stopA.setId(aid("stopA"));
StopTime stA = new StopTime();
stA.setId(100);
stA.setArrivalTime(time(9, 00));
stA.setDepartureTime(time(9, 05));
stA.setStopSequence(100);
stA.setStop(stopA);
stA.setTrip(trip);
Stop stopB = new Stop();
stopB.setId(aid("stopB"));
StopTime stB = new StopTime();
stB.setId(101);
stB.setArrivalTime(time(10, 00));
stB.setDepartureTime(time(10, 05));
stB.setStopSequence(102);
stB.setStop(stopB);
stB.setTrip(trip);
Mockito.when(gtfsDao.getStopTimesForTrip(trip)).thenReturn(Arrays.asList(stA, stB));
TransitGraphImpl graph = new TransitGraphImpl();
graph.putStopEntry(stop("stopA", 47.672207391799056, -122.387855896286));
graph.putStopEntry(stop("stopB", 47.66852277218285, -122.3853882639923));
RouteEntryImpl routeEntry = route("routeA");
graph.putRouteEntry(routeEntry);
graph.initialize();
ShapePointsFactory shapePointsFactory = new ShapePointsFactory();
shapePointsFactory.addPoint(47.673840100841396, -122.38756621771239);
shapePointsFactory.addPoint(47.668667271970484, -122.38756621771239);
shapePointsFactory.addPoint(47.66868172192725, -122.3661729186096);
ShapePoints shapePoints = shapePointsFactory.create();
ShapePointHelper shapePointHelper = Mockito.mock(ShapePointHelper.class);
Mockito.when(shapePointHelper.getShapePointsForShapeId(shapeId)).thenReturn(shapePoints);
TripEntriesFactory factory = new TripEntriesFactory();
factory.setGtfsDao(gtfsDao);
factory.setShapePointHelper(shapePointHelper);
factory.setUniqueService(new UniqueServiceImpl());
StopTimeEntriesFactory stopTimeEntriesFactory = new StopTimeEntriesFactory();
stopTimeEntriesFactory.setDistanceAlongShapeLibrary(new DistanceAlongShapeLibrary());
factory.setStopTimeEntriesFactory(stopTimeEntriesFactory);
factory.processTrips(graph);
TripEntryImpl entry = graph.getTripEntryForId(trip.getId());
assertEquals(trip.getId(), entry.getId());
assertEquals(route.getId(), entry.getRoute().getId());
assertEquals(lsid("serviceId"), entry.getServiceId());
assertEquals(trip.getShapeId(), entry.getShapeId());
assertEquals(2177.1, entry.getTotalTripDistance(), 0.1);
List<StopTimeEntry> stopTimes = entry.getStopTimes();
assertEquals(2, stopTimes.size());
for (StopTimeEntry stopTime : stopTimes) {
assertSame(entry, stopTime.getTrip());
}
}
use of org.onebusaway.transit_data_federation.impl.transit_graph.TripEntryImpl 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());
}
Aggregations