private Column[] getColumns() { return new Column[] { new Column("Contingency"), new Column("Status" + RESULT + "1"), new Column("Status" + RESULT + "2"), new Column("Equipment"), new Column("End"), new Column("ViolationType"), new Column("ViolationName" + RESULT + "1"), new Column("Value" + RESULT + "1"), new Column("Limit" + RESULT + "1"), new Column("ViolationName" + RESULT + "2"), new Column("Value" + RESULT + "2"), new Column("Limit" + RESULT + "2"), new Column("Actions" + RESULT + "1"), new Column("Actions" + RESULT + "2"), new Column("Comparison") }; }
private void writeTapChangerTable() throws IOException { try (Writer writer = new OutputStreamWriter(dataSource.newOutputStream("_network_tct", "txt", append), StandardCharsets.UTF_8); TableFormatter formatter = new AmplDatTableFormatter(writer, getTableTitle("Tap changer table"), AmplConstants.INVALID_FLOAT_VALUE, !append, AmplConstants.LOCALE, new Column(VARIANT), new Column("num"), new Column("tap"), new Column("var ratio"), new Column("x (pu)"), new Column("angle (rad)"), new Column(FAULT), new Column(config.getActionType().getLabel()))) { writeTwoWindingsTransformerTapChangerTable(formatter); writeThreeWindingsTransformerTapChangerTable(formatter); } }
private void writeCurrentLimits() throws IOException { try (Writer writer = new OutputStreamWriter(dataSource.newOutputStream("_network_limits", "txt", append), StandardCharsets.UTF_8); TableFormatter formatter = new AmplDatTableFormatter(writer, getTableTitle("Temporary current limits"), AmplConstants.INVALID_FLOAT_VALUE, !append, AmplConstants.LOCALE, new Column(VARIANT), new Column("num"), new Column("branch"), new Column("side"), new Column("limit (A)"), new Column("accept. duration (s)"), new Column(FAULT), new Column(config.getActionType().getLabel()))) { writeBranchCurrentLimits(formatter); writeThreeWindingsTransformerCurrentLimits(formatter); writeDanglingLineCurrentLimits(formatter); } }
private void writeBuses(AmplExportContext context) throws IOException { try (Writer writer = new OutputStreamWriter(dataSource.newOutputStream("_network_buses", "txt", append), StandardCharsets.UTF_8); TableFormatter formatter = new AmplDatTableFormatter(writer, getTableTitle("Buses"), AmplConstants.INVALID_FLOAT_VALUE, !append, AmplConstants.LOCALE, new Column(VARIANT), new Column("num"), new Column(SUBSTATION), new Column("cc"), new Column("v (pu)"), new Column("theta (rad)"), new Column("p (MW)"), new Column("q (MVar)"), new Column(FAULT), new Column(config.getActionType().getLabel()), new Column("id"))) { writeBuses(context, formatter); writeThreeWindingsTransformerMiddleBuses(context, formatter); writeDanglingLineMiddleBuses(context, formatter); if (config.isExportXNodes()) { writeTieLineMiddleBuses(context, formatter); } } }
private void exportViolations(Path folder, String file, String title, WCALoadflowResult loadflowResult, List<LimitViolation> violations) throws IOException { Path violationsPath = folder.resolve(file); Column[] columns = { new Column("Basecase"), new Column("FailureStep"), new Column("FailureDescription"), new Column("ViolationType"), new Column("Equipment"), new Column("Value"), new Column("Limit"), new Column("Country"), new Column("BaseVoltage"), new Column("Side"), }; CsvTableFormatterFactory factory = new CsvTableFormatterFactory(); try (Writer writer = Files.newBufferedWriter(violationsPath, StandardCharsets.UTF_8); TableFormatter formatter = factory.create(writer, title, tableFormatterConfig, columns)) { writeViolations(formatter, null, loadflowResult, violations); } }
private void exportPreventiveActionsApplication(Path folder) throws IOException { LOGGER.info("Exporting preventive action application report of basecase {} to file {}", basecase, folder + File.separator + POST_PREVENTIVE_ACTIONS_FILE); Path violationsPath = folder.resolve(POST_PREVENTIVE_ACTIONS_FILE); Column[] columns = { new Column("Basecase"), new Column("ActionId"), new Column("ViolatedEquipment"), new Column("ViolationType"), new Column("FailureStep"), new Column("FailureDescription"), new Column("ViolationRemoved"), new Column("ActionApplied"), new Column("Comment") }; CsvTableFormatterFactory factory = new CsvTableFormatterFactory(); try (Writer writer = Files.newBufferedWriter(violationsPath, StandardCharsets.UTF_8); TableFormatter formatter = factory.create(writer, POST_PREVENTIVE_ACTIONS_TITLE, tableFormatterConfig, columns)) { writeActionsApplications(formatter, null, new ArrayList<>(preventiveActionsApplication.values())); } }
private void writeTrustIntervals(Uncertainties uncertainties, DataSource dataSource) throws IOException { try (Writer writer = new OutputStreamWriter(dataSource.newOutputStream(TRUST_INTERVAL_FILE_SUFFIX, TXT_EXT, false), StandardCharsets.UTF_8); AmplDatTableFormatter formatter = new AmplDatTableFormatter( writer, "Trust intervals", INVALID_FLOAT_VALUE, true, LOCALE, new Column("var. num"), new Column("min"), new Column("max"))) { for (int varNum = 0; varNum < uncertainties.min.length; varNum++) { formatter.writeCell(varNum + 1) .writeCell(uncertainties.min[varNum]) .writeCell(uncertainties.max[varNum]); } } }
private void writeMeans(Uncertainties uncertainties, DataSource dataSource, StringToIntMapper<AmplSubset> mapper) throws IOException { try (Writer writer = new OutputStreamWriter(dataSource.newOutputStream(MEANS_FILE_SUFFIX, TXT_EXT, false), StandardCharsets.UTF_8); AmplDatTableFormatter formatter = new AmplDatTableFormatter( writer, "Means", INVALID_FLOAT_VALUE, true, LOCALE, new Column("inj. type"), new Column("inj. num"), new Column("mean"))) { for (int i = 0; i < uncertainties.means.length; i++) { StochasticInjection inj = uncertainties.injections.get(i); formatter.writeCell(inj.getType().toChar()) .writeCell(getNum(inj, mapper)) .writeCell(uncertainties.means[i]); } } }
static String getTableString(PlatformConfig platformConfig) { try (StringWriter writer = new StringWriter()) { try (TableFormatter formatter = new AsciiTableFormatter(writer, "Powsybl versions", TableFormatterConfig.load(platformConfig), new Column("Repository name"), new Column("Maven project version"), new Column("Git branch"), new Column("Git version"), new Column("Build timestamp"))) { list().forEach(version -> { try { formatter.writeCell(version.getRepositoryName()) .writeCell(version.getMavenProjectVersion()) .writeCell(version.getGitBranch()) .writeCell(version.getGitVersion()) .writeCell(new DateTime(version.getBuildTimestamp()).toString()); } catch (IOException e) { throw new UncheckedIOException(e); } }); } return writer.toString(); } catch (IOException e) { throw new UncheckedIOException(e); } }
private void printLoadFlowResult(LoadFlowResult result, Writer writer, TableFormatterFactory formatterFactory, TableFormatterConfig formatterConfig) { try (TableFormatter formatter = formatterFactory.create(writer, "loadflow results", formatterConfig, new Column("Result"), new Column("Metrics"))) { formatter.writeCell(result.isOk()); formatter.writeCell(result.getMetrics().toString()); } catch (IOException e) { throw new UncheckedIOException(e); } }
public static void writeContingencies(Collection<Contingency> contingencies, DataSource dataSource, StringToIntMapper<AmplSubset> mapper) { Objects.requireNonNull(contingencies); Objects.requireNonNull(dataSource); Objects.requireNonNull(mapper); try (Writer writer = new OutputStreamWriter(dataSource.newOutputStream(WCAConstants.FAULTS_FILE_SUFFIX, WCAConstants.TXT_EXT, false), StandardCharsets.UTF_8); TableFormatter formatter = new AmplDatTableFormatter( writer, "Contingencies", AmplConstants.INVALID_FLOAT_VALUE, true, AmplConstants.LOCALE, new Column("num"), new Column("id"))) { for (Contingency contingency : contingencies) { int contingencyNum = mapper.getInt(AmplSubset.FAULT, contingency.getId()); formatter.writeCell(contingencyNum) .writeCell(contingency.getId()); } } catch (IOException e) { throw new RuntimeException(e); } }
private static void prettyPrint(Multimap<String, SecurityIndex> securityIndexesPerContingency, PrintStream out) { List<Column> columns = new ArrayList<>(SecurityIndexType.values().length + 1); columns.add(new Column("Contingency")); for (SecurityIndexType securityIndexType : SecurityIndexType.values()) { columns.add(new Column(securityIndexType.toString())); } Column[] arrayColumns = columns.toArray(new Column[0]); Writer writer = new OutputStreamWriter(out); try (AbstractTableFormatter formatter = new AsciiTableFormatter(writer, null, arrayColumns)) { for (Map.Entry<String, Collection<SecurityIndex>> entry : securityIndexesPerContingency.asMap().entrySet()) { String contingencyId = entry.getKey(); formatter.writeCell(contingencyId); for (String str : toRow(entry.getValue())) { formatter.writeCell(str); } } } catch (IOException e) { throw new UncheckedIOException(e); } }
public void writeCsv(Writer writer, Locale locale, char separator, boolean writeHeaders) throws IOException { Objects.requireNonNull(writer); TableFormatterConfig formatterConfig = new TableFormatterConfig(locale, separator, "", writeHeaders, false); CsvTableFormatterFactory csvTableFormatterFactory = new CsvTableFormatterFactory(); Column[] cols = headers.stream().map(h -> new Column(h.getName())).toArray(sz -> new Column[sz]); try (TableFormatter formatter = csvTableFormatterFactory.create(writer, "", formatterConfig, cols)) { records.forEach(r -> { r.getValues().forEach(a -> { try { if ("".equals(a.toString())) { formatter.writeEmptyCell(); } else { formatter.writeCell(a.toString()); } } catch (IOException e) { throw new UncheckedIOException(e); } }); }); writer.flush(); } }
@Override public void run(CommandLine line, ToolRunningContext context) throws Exception { Collection<PluginInfo> pluginInfos = Plugins.getPluginInfos(); Writer writer = new OutputStreamWriter(context.getOutputStream()); AsciiTableFormatterFactory asciiTableFormatterFactory = new AsciiTableFormatterFactory(); try (TableFormatter formatter = asciiTableFormatterFactory.create(writer, "Plugins", new TableFormatterConfig(), new Column("Plugin type name"), new Column("Available plugin IDs"))) { pluginInfos.forEach(p -> { try { formatter.writeCell(p.getPluginName()); formatter.writeCell(String.join(", ", Plugins.getPluginImplementationsIds(p))); } catch (IOException e) { throw new UncheckedIOException(e); } }); } } }
@Test public void testEmptyLines() throws IOException { ByteArrayOutputStream bos = new ByteArrayOutputStream(); try (Writer writer = new OutputStreamWriter(bos, StandardCharsets.UTF_8); AsciiTableFormatter formatter = new AsciiTableFormatter(writer, null, config, new Column("column1").setColspan(4), new Column("column2").setColspan(2))) { formatter.writeCell("Line:1 Cell:1") .writeCell("Line:1 Cell:2") .writeCell("Line:1 Cell:3") .writeEmptyLine(); } assertEquals("+--------------------------------------------------+---------+\n" + "| column1 | column2 |\n" + "+--------------------------------------------------+---------+\n" + "| Line:1 Cell:1 | Line:1 Cell:2 | Line:1 Cell:3 | | | |\n" + "+---------------+---------------+---------------+--+--+------+" + System.lineSeparator(), new String(bos.toByteArray(), StandardCharsets.UTF_8)); } }
@Test public void testAsciiTableFormatter1() throws IOException { ByteArrayOutputStream bos = new ByteArrayOutputStream(); Writer writer = new OutputStreamWriter(bos, StandardCharsets.UTF_8); try (AsciiTableFormatter formatter = new AsciiTableFormatter(writer, null, config, new Column("column1").setColspan(2).setHorizontalAlignment(HorizontalAlignment.CENTER).setTitleHorizontalAlignment(HorizontalAlignment.CENTER), new Column("column2").setHorizontalAlignment(HorizontalAlignment.CENTER).setTitleHorizontalAlignment(HorizontalAlignment.CENTER))) { formatter.writeCell("Line:1 Cell:1", 2) .writeCell("Line:1 Cell:2", 1) .writeCell("Line:2 Cell:1", 1) .writeCell("Line:2 Cell:2", 1) .writeCell("Line:2 Cell:3", 1); } assertEquals("+-------------------------------+---------------+\n" + "| column1 | column2 |\n" + "+-------------------------------+---------------+\n" + "| Line:1 Cell:1 | Line:1 Cell:2 |\n" + "| Line:2 Cell:1 | Line:2 Cell:2 | Line:2 Cell:3 |\n" + "+---------------+---------------+---------------+" + System.lineSeparator(), new String(bos.toByteArray(), StandardCharsets.UTF_8)); writer.close(); }
@Test public void testUnauthorizedColspan() throws IOException { thrown.expect(IllegalArgumentException.class); thrown.expectMessage("You have exceded the authorized colspan"); ByteArrayOutputStream bos = new ByteArrayOutputStream(); try (Writer writer = new OutputStreamWriter(bos, StandardCharsets.UTF_8); AsciiTableFormatter formatter = new AsciiTableFormatter(writer, null, config, new Column("column1").setColspan(4).setHorizontalAlignment(HorizontalAlignment.CENTER), new Column("column2").setColspan(2).setHorizontalAlignment(HorizontalAlignment.CENTER))) { formatter.writeCell("Line:1 Cell:1", 1) .writeCell("Line:1 Cell:2", 1) .writeCell("Line:1 Cell:3", 1) .writeCell("Line:1 Cell:4", 2) .writeCell("Line:1 Cell:5", 1); } }
@Test public void testAsciiTableFormatter2() throws IOException { ByteArrayOutputStream bos = new ByteArrayOutputStream(); Writer writer = new OutputStreamWriter(bos, StandardCharsets.UTF_8); try (AsciiTableFormatter formatter = new AsciiTableFormatter(writer, null, config, new Column("column1").setColspan(4).setHorizontalAlignment(HorizontalAlignment.CENTER).setTitleHorizontalAlignment(HorizontalAlignment.CENTER), new Column("column2").setColspan(2).setHorizontalAlignment(HorizontalAlignment.CENTER).setTitleHorizontalAlignment(HorizontalAlignment.CENTER))) { formatter.writeCell("Line:1 Cell:1", 1) .writeCell("Line:1 Cell:2", 1) .writeCell("Line:1 Cell:3", 1) .writeCell("Line:1 Cell:4", 1) .writeCell("Line:1 Cell:5", 1) .writeCell("Line:1 Cell:6", 1); } assertEquals("+---------------------------------------------------------------+-------------------------------+\n" + "| column1 | column2 |\n" + "+---------------------------------------------------------------+-------------------------------+\n" + "| Line:1 Cell:1 | Line:1 Cell:2 | Line:1 Cell:3 | Line:1 Cell:4 | Line:1 Cell:5 | Line:1 Cell:6 |\n" + "+---------------+---------------+---------------+---------------+---------------+---------------+" + System.lineSeparator(), new String(bos.toByteArray(), StandardCharsets.UTF_8)); writer.close(); }
@Test public void testNegativeColspan() { thrown.expect(IllegalArgumentException.class); thrown.expectMessage("colspan must be greater than 0"); new Column("column").setColspan(-5); }
@Test public void testPositiveColspan() { Column column = new Column("column"); assertEquals(1, column.getColspan()); column.setColspan(2); assertEquals(2, column.getColspan()); }