BlockIndexFactoryServiceImpl implements BlockIndexFactoryService { public List<FrequencyBlockTripIndex> createFrequencyTripIndices( Iterable<BlockEntry> blocks) { List<FrequencyBlockTripIndex> allIndices = new ArrayList<FrequencyBlockTripIndex>(); int logInterval = LoggingIntervalUtil.getAppropriateLoggingInterval(allIndices.size()); Map<BlockSequenceKey, List<BlockTripEntry>> blockTripsByKey = new FactoryMap<BlockSequenceKey, List<BlockTripEntry>>( new ArrayList<BlockTripEntry>()); if (_verbose) _log.info("grouping frequency blocks by sequence key"); int tripCount = 0; for (BlockEntry block : blocks) { if (block.getConfigurations().isEmpty()) { _log.warn("block has no configurations: " + block.getId()); continue; } if (!BlockLibrary.isFrequencyBased(block)) continue; for (BlockConfigurationEntry blockConfiguration : block.getConfigurations()) { for (BlockTripEntry blockTrip : blockConfiguration.getTrips()) { BlockSequenceKey key = getBlockTripAsTripSequenceKey(blockTrip); blockTripsByKey.get(key).add(blockTrip); tripCount++; } } } if (_verbose) _log.info("frequency groups found: " + blockTripsByKey.size() + " out of trips: " + tripCount); int count = 0; for (List<BlockTripEntry> tripsWithSameSequence : blockTripsByKey.values()) { if (_verbose && count % logInterval == 0) _log.info("frequency groups processed: " + count + "/" + blockTripsByKey.size()); count++; List<FrequencyTripGroup> groupedTrips = ensureFrequencyTripGroups(tripsWithSameSequence); for (FrequencyTripGroup group : groupedTrips) { group.trimToSize(); FrequencyBlockTripIndex index = createFrequencyIndexForTrips( group.getTrips(), group.getFrequencies()); allIndices.add(index); } } return allIndices; } BlockIndexFactoryServiceImpl();  BlockIndexFactoryServiceImpl(boolean verbose); @Autowired void setAgencyService(AgencyService agencyService); @Autowired void setAgencyBeanService(AgencyBeanService agencyBeanService); void setVerbose(boolean verbose); void setMaxSlackBetweenConsecutiveTrips(
      int maxSlackBetweenConsecutiveTrips); void setMaxScheduledTimeBetweenConsecutiveTrips(
      int maxScheduledTimeBetweenConsecutiveTrips); @PostConstruct void setup(); List<BlockTripIndexData> createTripData(Iterable<BlockEntry> blocks); List<BlockLayoverIndexData> createLayoverData(
      Iterable<BlockEntry> blocks); List<FrequencyBlockTripIndexData> createFrequencyTripData(
      Iterable<BlockEntry> blocks); List<BlockTripIndex> createTripIndices(Iterable<BlockEntry> blocks); List<BlockLayoverIndex> createLayoverIndices(
      Iterable<BlockEntry> blocks); List<FrequencyBlockTripIndex> createFrequencyTripIndices(
      Iterable<BlockEntry> blocks); List<BlockSequenceIndex> createSequenceIndices(
      Iterable<BlockEntry> blocks); BlockTripIndex createTripIndexForGroupOfBlockTrips(
      List<BlockTripEntry> blocks); BlockLayoverIndex createLayoverIndexForGroupOfBlockTrips(
      List<BlockTripEntry> trips); FrequencyBlockTripIndex createFrequencyIndexForTrips(
      List<BlockTripEntry> trips, List<FrequencyEntry> frequencies); BlockSequenceIndex createSequenceIndexForGroupOfBlockSequences(
      List<BlockSequence> sequences);  }
@Test public void testFrequencies() { BlockIndexFactoryServiceImpl factory = new BlockIndexFactoryServiceImpl(); StopEntryImpl stopA = stop("a", 47.0, -122.0); StopEntryImpl stopB = stop("b", 47.1, -122.1); BlockEntryImpl blockA = block("a"); TripEntryImpl tripA = trip("a", "s1"); stopTime(0, stopA, tripA, 0, 10, 0); stopTime(0, stopB, tripA, 20, 20, 0); FrequencyEntry freqA1 = frequency(time(6, 00), time(9, 00), 10, 0); FrequencyEntry freqA2 = frequency(time(15, 00), time(18, 00), 10, 0); List<FrequencyEntry> freqsA = Arrays.asList(freqA1, freqA2); linkBlockTrips(blockA, freqsA, tripA); BlockEntryImpl blockB = block("b"); TripEntryImpl tripB = trip("b", "s1"); stopTime(0, stopA, tripB, 20, 30, 0); stopTime(0, stopB, tripB, 50, 50, 0); FrequencyEntry freqB1 = frequency(time(9, 00), time(15, 00), 20, 0); FrequencyEntry freqB2 = frequency(time(18, 00), time(21, 00), 20, 0); List<FrequencyEntry> freqsB = Arrays.asList(freqB1, freqB2); linkBlockTrips(blockB, freqsB, tripB); List<FrequencyBlockTripIndex> allIndices = factory.createFrequencyTripIndices(Arrays.asList( (BlockEntry) blockB, blockA)); assertEquals(1, allIndices.size()); List<FrequencyBlockTripIndex> indices = grep(allIndices, aid("a")); assertEquals(1, indices.size()); FrequencyBlockTripIndex index = indices.get(0); List<TripEntry> trips = trips(index.getTrips()); assertEquals(4, trips.size()); assertEquals(tripA, trips.get(0)); assertEquals(tripB, trips.get(1)); assertEquals(tripA, trips.get(2)); assertEquals(tripB, trips.get(3)); List<FrequencyEntry> freqs = index.getFrequencies(); assertEquals(Arrays.asList(freqA1, freqB1, freqA2, freqB2), freqs); ServiceIdActivation serviceIds = index.getServiceIds(); assertEquals(1, serviceIds.getActiveServiceIds().size()); assertTrue(serviceIds.getActiveServiceIds().contains(lsid("s1"))); FrequencyServiceIntervalBlock intervalBlock = index.getServiceIntervalBlock(); assertTrue(Arrays.equals(new int[] { time(6, 0), time(9, 0), time(15, 0), time(18, 0)}, intervalBlock.getStartTimes())); assertTrue(Arrays.equals(new int[] { time(9, 0), time(15, 0), time(18, 0), time(21, 0)}, intervalBlock.getEndTimes())); }
@Test public void testOverlappingFrequencies() { BlockIndexFactoryServiceImpl factory = new BlockIndexFactoryServiceImpl(); StopEntryImpl stopA = stop("a", 47.0, -122.0); StopEntryImpl stopB = stop("b", 47.1, -122.1); BlockEntryImpl blockA = block("a"); TripEntryImpl tripA = trip("a", "s1"); stopTime(0, stopA, tripA, 0, 10, 0); stopTime(0, stopB, tripA, 20, 20, 0); FrequencyEntry freqA1 = frequency(time(6, 00), time(9, 00), 10, 0); FrequencyEntry freqA2 = frequency(time(15, 00), time(18, 00), 10, 0); List<FrequencyEntry> freqsA = Arrays.asList(freqA1, freqA2); linkBlockTrips(blockA, freqsA, tripA); BlockEntryImpl blockB = block("b"); TripEntryImpl tripB = trip("b", "s1"); stopTime(0, stopA, tripB, 20, 30, 0); stopTime(0, stopB, tripB, 50, 50, 0); FrequencyEntry freqB1 = frequency(time(8, 00), time(14, 00), 20, 0); FrequencyEntry freqB2 = frequency(time(17, 00), time(20, 00), 20, 0); List<FrequencyEntry> freqsB = Arrays.asList(freqB1, freqB2); linkBlockTrips(blockB, freqsB, tripB); List<FrequencyBlockTripIndex> allIndices = factory.createFrequencyTripIndices(Arrays.asList( (BlockEntry) blockB, blockA)); assertEquals(2, allIndices.size()); List<FrequencyBlockTripIndex> indices = grep(allIndices, aid("a")); assertEquals(1, indices.size()); FrequencyBlockTripIndex index = indices.get(0); List<TripEntry> trips = trips(index.getTrips()); assertEquals(2, trips.size()); assertEquals(tripA, trips.get(0)); assertEquals(tripA, trips.get(1)); List<FrequencyEntry> freqs = index.getFrequencies(); assertEquals(Arrays.asList(freqA1, freqA2), freqs); ServiceIdActivation serviceIds = index.getServiceIds(); assertEquals(1, serviceIds.getActiveServiceIds().size()); assertTrue(serviceIds.getActiveServiceIds().contains(lsid("s1"))); FrequencyServiceIntervalBlock intervalBlock = index.getServiceIntervalBlock(); assertTrue(Arrays.equals(new int[] {time(6, 0), time(15, 0)}, intervalBlock.getStartTimes())); assertTrue(Arrays.equals(new int[] {time(9, 0), time(18, 0)}, intervalBlock.getEndTimes())); indices = grep(allIndices, aid("b")); assertEquals(1, indices.size()); index = indices.get(0); trips = trips(index.getTrips()); assertEquals(2, trips.size()); assertEquals(tripB, trips.get(0)); assertEquals(tripB, trips.get(1)); freqs = index.getFrequencies(); assertEquals(Arrays.asList(freqB1, freqB2), freqs); serviceIds = index.getServiceIds(); assertEquals(1, serviceIds.getActiveServiceIds().size()); assertTrue(serviceIds.getActiveServiceIds().contains(lsid("s1"))); intervalBlock = index.getServiceIntervalBlock(); assertTrue(Arrays.equals(new int[] {time(8, 0), time(17, 0)}, intervalBlock.getStartTimes())); assertTrue(Arrays.equals(new int[] {time(14, 0), time(20, 0)}, intervalBlock.getEndTimes())); }