GenerateNarrativesTask implements Runnable { public void generateStopNarratives(NarrativeProviderImpl provider) { Map<AgencyAndId, List<ProjectedPoint>> shapePointCache = new HashMap<AgencyAndId, List<ProjectedPoint>>(); int index = 0; Collection<Stop> allStops = _gtfsDao.getAllStops(); Map<AgencyAndId, Stop> stopsById = MappingLibrary.mapToValue(allStops, "id"); int logInterval = LoggingIntervalUtil.getAppropriateLoggingInterval(allStops.size()); for (StopEntry stopEntry : _transitGraphDao.getAllStops()) { if (index % logInterval == 0) _log.info("stops=" + index); index++; Stop stop = stopsById.get(stopEntry.getId()); StopNarrative.Builder narrative = StopNarrative.builder(); narrative.setCode(deduplicate(stop.getCode())); narrative.setDescription(deduplicate(stop.getDesc())); narrative.setName(deduplicate(stop.getName())); narrative.setUrl(deduplicate(stop.getUrl())); String direction = computeStopDirection(provider, shapePointCache, stop, stopEntry); narrative.setDirection(deduplicate(direction)); provider.setNarrativeForStop(stopEntry.getId(), narrative.create()); } }  @Autowired void setBundle(FederatedTransitDataBundle bundle); @Autowired void setGtfsDao(GtfsRelationalDao gtfsDao); @Autowired void setTransitGraphDao(TransitGraphDao transitGraphDao); @Autowired void setBlockIndexService(BlockIndexService blockIndexService); @Autowired void setModifications(Modifications modifications); @Autowired void setShapePointHelper(ShapePointHelper shapePointHelper); @Autowired void setUniqueService(UniqueService uniqueService); @Autowired void setRefreshService(RefreshService refreshService); void setStopDirectionStandardDeviationThreshold(
      double stopDirectionStandardDeviationThreshold); @Override void run(); void generateAgencyNarratives(NarrativeProviderImpl provider); void generateRouteNarratives(NarrativeProviderImpl provider); void generateShapePointNarratives(NarrativeProviderImpl provider); void generateStopNarratives(NarrativeProviderImpl provider); void generateTripNarratives(NarrativeProviderImpl provider);  }
@Test public void testGenerateStopNarratives() { StopEntry stopEntry = stop("stopA", 47.0, -122.0); Mockito.when(_transitGraphDao.getAllStops()).thenReturn( Arrays.asList(stopEntry)); Stop stop = new Stop(); stop.setId(stopEntry.getId()); stop.setCode("A"); stop.setDesc("Stop A is the best"); stop.setLat(stopEntry.getStopLat()); stop.setLon(stopEntry.getStopLon()); stop.setName("Stop A"); stop.setUrl("http: Mockito.when(_gtfsDao.getAllStops()).thenReturn(Arrays.asList(stop)); List<BlockStopTimeIndex> indices = Collections.emptyList(); Mockito.when(_blockIndexService.getStopTimeIndicesForStop(stopEntry)).thenReturn( indices); _task.generateStopNarratives(_provider); StopNarrative narrative = _provider.getNarrativeForStopId(stopEntry.getId()); assertEquals(stop.getCode(), narrative.getCode()); assertEquals(stop.getDesc(), narrative.getDescription()); assertNull(narrative.getDirection()); assertEquals(stop.getLocationType(), narrative.getLocationType()); assertEquals(stop.getName(), narrative.getName()); assertEquals(stop.getUrl(), narrative.getUrl()); }
@Test public void testGenerateStopNarrativesWithHardCodedDirection() { StopEntry stopEntry = stop("stopA", 47.0, -122.0); Mockito.when(_transitGraphDao.getAllStops()).thenReturn( Arrays.asList(stopEntry)); Stop stop = new Stop(); stop.setId(stopEntry.getId()); stop.setDirection("west"); Mockito.when(_gtfsDao.getAllStops()).thenReturn(Arrays.asList(stop)); List<BlockStopTimeIndex> indices = Collections.emptyList(); Mockito.when(_blockIndexService.getStopTimeIndicesForStop(stopEntry)).thenReturn( indices); _task.generateStopNarratives(_provider); StopNarrative narrative = _provider.getNarrativeForStopId(stopEntry.getId()); assertEquals("W", narrative.getDirection()); }
@Test public void testGenerateStopNarrativesWithCalculatedDirection() { StopEntryImpl stopEntry = stop("stopA", 47.663146, -122.300928); Mockito.when(_transitGraphDao.getAllStops()).thenReturn( Arrays.asList((StopEntry) stopEntry)); Stop stop = new Stop(); stop.setId(stopEntry.getId()); Mockito.when(_gtfsDao.getAllStops()).thenReturn(Arrays.asList(stop)); AgencyAndId shapeId = aid("shapeA"); ShapePointsFactory factory = new ShapePointsFactory(); factory.addPoint(47.661225, -122.3009201); factory.addPoint(47.664375, -122.3008986); ShapePoints shapePoints = factory.create(); _provider.setShapePointsForId(shapeId, shapePoints); TripEntryImpl trip = trip("trip"); trip.setShapeId(shapeId); StopTimeEntryImpl stopTime = stopTime(0, stopEntry, trip, 0, 0.0); stopTime.setShapePointIndex(0); BlockStopTimeEntry blockStopTime = Mockito.mock(BlockStopTimeEntry.class); Mockito.when(blockStopTime.getStopTime()).thenReturn(stopTime); BlockStopTimeIndex index = Mockito.mock(BlockStopTimeIndex.class); Mockito.when(index.getStopTimes()).thenReturn(Arrays.asList(blockStopTime)); List<BlockStopTimeIndex> indices = Arrays.asList(index); Mockito.when(_blockIndexService.getStopTimeIndicesForStop(stopEntry)).thenReturn( indices); _task.generateStopNarratives(_provider); StopNarrative narrative = _provider.getNarrativeForStopId(stopEntry.getId()); assertEquals("N", narrative.getDirection()); }
@Test public void testGenerateStopNarrativesWithConflictingDirections() { StopEntryImpl stopEntry = stop("stopA", 47.663146, -122.300928); Mockito.when(_transitGraphDao.getAllStops()).thenReturn( Arrays.asList((StopEntry) stopEntry)); Stop stop = new Stop(); stop.setId(stopEntry.getId()); Mockito.when(_gtfsDao.getAllStops()).thenReturn(Arrays.asList(stop)); AgencyAndId shapeIdA = aid("shapeA"); ShapePointsFactory factoryA = new ShapePointsFactory(); factoryA.addPoint(47.661225, -122.3009201); factoryA.addPoint(47.664375, -122.3008986); ShapePoints shapePointsA = factoryA.create(); _provider.setShapePointsForId(shapeIdA, shapePointsA); AgencyAndId shapeIdB = aid("shapeB"); ShapePointsFactory factoryB = new ShapePointsFactory(); factoryB.addPoint(47.664375, -122.3008986); factoryB.addPoint(47.661225, -122.3009201); ShapePoints shapePointsB = factoryB.create(); _provider.setShapePointsForId(shapeIdB, shapePointsB); TripEntryImpl tripA = trip("tripA"); tripA.setShapeId(shapeIdA); TripEntryImpl tripB = trip("tripB"); tripB.setShapeId(shapeIdB); StopTimeEntryImpl stopTimeA = stopTime(0, stopEntry, tripA, 0, 0.0); stopTimeA.setShapePointIndex(0); StopTimeEntryImpl stopTimeB = stopTime(0, stopEntry, tripB, 0, 0.0); stopTimeB.setShapePointIndex(0); BlockStopTimeEntry blockStopTimeA = Mockito.mock(BlockStopTimeEntry.class); Mockito.when(blockStopTimeA.getStopTime()).thenReturn(stopTimeA); BlockStopTimeEntry blockStopTimeB = Mockito.mock(BlockStopTimeEntry.class); Mockito.when(blockStopTimeB.getStopTime()).thenReturn(stopTimeB); BlockStopTimeIndex index = Mockito.mock(BlockStopTimeIndex.class); Mockito.when(index.getStopTimes()).thenReturn( Arrays.asList(blockStopTimeA, blockStopTimeB)); List<BlockStopTimeIndex> indices = Arrays.asList(index); Mockito.when(_blockIndexService.getStopTimeIndicesForStop(stopEntry)).thenReturn( indices); _task.generateStopNarratives(_provider); StopNarrative narrative = _provider.getNarrativeForStopId(stopEntry.getId()); assertNull(narrative.getDirection()); }