WCAHistoLimits implements  WCAConstants { public void write(DataSource dataSource, StringToIntMapper<AmplSubset> mapper) throws IOException { try (Writer writer = new OutputStreamWriter(dataSource.newOutputStream(HISTO_LOADS_FILE_SUFFIX, TXT_EXT, false), StandardCharsets.UTF_8); TableFormatter formatter = new AmplDatTableFormatter( writer, "loads historical data " + histoInterval, AmplConstants.INVALID_FLOAT_VALUE, true, AmplConstants.LOCALE, new Column("num"), new Column("min p (MW)"), new Column("max p (MW)"), new Column("id"))) { for (Map.Entry<String, Range<Float>> e : loadLimits.entrySet()) { String id = e.getKey(); Range<Float> range = e.getValue(); int num = mapper.getInt(AmplSubset.LOAD, id); formatter.writeCell(num) .writeCell(range.lowerEndpoint()) .writeCell(range.upperEndpoint()) .writeCell(id); } for (Map.Entry<String, Range<Float>> e : danglingLineLimits.entrySet()) { String id = e.getKey(); Range<Float> range = e.getValue(); int num = mapper.getInt(AmplSubset.LOAD, id); formatter.writeCell(num) .writeCell(range.lowerEndpoint()) .writeCell(range.upperEndpoint()) .writeCell(id + "_load"); } } try (Writer writer = new OutputStreamWriter(dataSource.newOutputStream(HISTO_GENERATORS_FILE_SUFFIX, TXT_EXT, false), StandardCharsets.UTF_8); TableFormatter formatter = new AmplDatTableFormatter( writer, "generators historical data " + histoInterval, AmplConstants.INVALID_FLOAT_VALUE, true, AmplConstants.LOCALE, new Column("num"), new Column("min p (MW)"), new Column("max p (MW)"), new Column("id"))) { for (Map.Entry<String, Range<Float>> e : generatorLimits.entrySet()) { String id = e.getKey(); Range<Float> range = e.getValue(); int num = mapper.getInt(AmplSubset.GENERATOR, id); formatter.writeCell(num) .writeCell(range.lowerEndpoint()) .writeCell(range.upperEndpoint()) .writeCell(id); } } } WCAHistoLimits(Interval histoInterval); void load(Network network, HistoDbClient histoDbClient); void write(DataSource dataSource, StringToIntMapper<AmplSubset> mapper);  }
@Test public void testWrite() throws IOException, InterruptedException { Interval histoInterval = Interval.parse("2013-01-01T00:00:00+01:00/2013-01-31T23:59:00+01:00"); Network network = NetworkTest1Factory.create(); HistoDbClient histoDbClient = Mockito.mock(HistoDbClient.class); HistoDbStats histoDbStats = new HistoDbStats(); histoDbStats.setValue(HistoDbStatsType.MIN, new HistoDbNetworkAttributeId(network.getLoads().iterator().next().getId(), HistoDbAttr.P), 0f); histoDbStats.setValue(HistoDbStatsType.MAX, new HistoDbNetworkAttributeId(network.getLoads().iterator().next().getId(), HistoDbAttr.P), 20f); histoDbStats.setValue(HistoDbStatsType.MIN, new HistoDbNetworkAttributeId(network.getGenerators().iterator().next().getId(), HistoDbAttr.P), 200f); histoDbStats.setValue(HistoDbStatsType.MAX, new HistoDbNetworkAttributeId(network.getGenerators().iterator().next().getId(), HistoDbAttr.P), 900f); Mockito.when(histoDbClient.queryStats(Matchers.anySet(), Matchers.eq(histoInterval), Matchers.eq(HistoDbHorizon.SN), Matchers.eq(true))) .thenReturn(histoDbStats); MemDataSource dataSource = new MemDataSource(); StringToIntMapper<AmplSubset> mapper = new StringToIntMapper<>(AmplSubset.class); AmplUtil.fillMapper(mapper, network); WCAHistoLimits histoLimits = new WCAHistoLimits(histoInterval); histoLimits.load(network, histoDbClient); histoLimits.write(dataSource, mapper); String fileContent = String.join(System.lineSeparator(), "#loads historical data " + histoInterval, "#\"num\" \"min p (MW)\" \"max p (MW)\" \"id\"", "1 0.00000 20.0000 \""+ network.getLoads().iterator().next().getId() + "\""); assertEquals(fileContent, new String(dataSource.getData(WCAConstants.HISTO_LOADS_FILE_SUFFIX, WCAConstants.TXT_EXT), StandardCharsets.UTF_8).trim()); fileContent = String.join(System.lineSeparator(), "#generators historical data " + histoInterval, "#\"num\" \"min p (MW)\" \"max p (MW)\" \"id\"", "1 200.000 900.000 \""+ network.getGenerators().iterator().next().getId() + "\""); assertEquals(fileContent, new String(dataSource.getData(WCAConstants.HISTO_GENERATORS_FILE_SUFFIX, WCAConstants.TXT_EXT), StandardCharsets.UTF_8).trim()); }