From 06a3edbbf2a772d43a8b776117d68b4a230bc899 Mon Sep 17 00:00:00 2001 From: Christian Biasuzzi Date: Thu, 17 Nov 2016 16:32:42 +0100 Subject: [PATCH 1/6] updates these itools with the new configurable csv formatter: print-online-workflow-violations, print-online-workflow-postcontingency-violations, print-online-workflow-postcontingency-loadflow, print-online-workflow-summary --- ...neWorkflowPostContingencyLoadflowTool.java | 191 ++++++----- ...WorkflowPostContingencyViolationsTool.java | 238 ++++++-------- .../PrintOnlineWorkflowSummaryTable.java | 299 +++++++++--------- .../PrintOnlineWorkflowViolationsTool.java | 263 +++++++-------- 4 files changed, 454 insertions(+), 537 deletions(-) diff --git a/online-workflow/src/main/java/eu/itesla_project/online/tools/PrintOnlineWorkflowPostContingencyLoadflowTool.java b/online-workflow/src/main/java/eu/itesla_project/online/tools/PrintOnlineWorkflowPostContingencyLoadflowTool.java index 0e0f3a81..9e165b27 100644 --- a/online-workflow/src/main/java/eu/itesla_project/online/tools/PrintOnlineWorkflowPostContingencyLoadflowTool.java +++ b/online-workflow/src/main/java/eu/itesla_project/online/tools/PrintOnlineWorkflowPostContingencyLoadflowTool.java @@ -1,13 +1,15 @@ /** * Copyright (c) 2016, All partners of the iTesla project (http://www.itesla-project.eu/consortium) + * Copyright (c) 2016, RTE (http://www.rte-france.com) * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ package eu.itesla_project.online.tools; -import com.csvreader.CsvWriter; import com.google.auto.service.AutoService; +import eu.itesla_project.commons.io.SystemOutStreamWriter; +import eu.itesla_project.commons.io.table.*; import eu.itesla_project.commons.tools.Command; import eu.itesla_project.commons.tools.Tool; import eu.itesla_project.modules.online.OnlineConfig; @@ -15,12 +17,13 @@ import org.apache.commons.cli.CommandLine; import org.apache.commons.cli.Option; import org.apache.commons.cli.Options; -import org.nocrala.tools.texttablefmt.BorderStyle; -import org.nocrala.tools.texttablefmt.CellStyle; -import org.nocrala.tools.texttablefmt.Table; import java.io.IOException; -import java.io.StringWriter; +import java.io.Writer; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; import java.util.Arrays; import java.util.Map; @@ -31,6 +34,7 @@ @AutoService(Tool.class) public class PrintOnlineWorkflowPostContingencyLoadflowTool implements Tool { + private static final String TABLE_TITLE = "online-workflow-postcontingency-loadflow"; private static Command COMMAND = new Command() { @Override @@ -68,8 +72,10 @@ public Options getOptions() { .argName("CONTINGENCY") .build()); options.addOption(Option.builder().longOpt("csv") - .desc("export in csv format") - .build()); + .desc("export in csv format to a file") + .hasArg() + .argName("FILE") + .build()); return options; } @@ -88,104 +94,85 @@ public Command getCommand() { @Override public void run(CommandLine line) throws Exception { OnlineConfig config = OnlineConfig.load(); - OnlineDb onlinedb = config.getOnlineDbFactoryClass().newInstance().create(); String workflowId = line.getOptionValue("workflow"); - if ( line.hasOption("state") ) { - Integer stateId = Integer.parseInt(line.getOptionValue("state")); - Map loadflowConvergence = onlinedb.getPostContingencyLoadflowConvergence(workflowId, stateId); - if ( loadflowConvergence != null && !loadflowConvergence.keySet().isEmpty() ) { - Table table = new Table(3, BorderStyle.CLASSIC_WIDE); - StringWriter content = new StringWriter(); - CsvWriter cvsWriter = new CsvWriter(content, ','); - printHeaders(table, cvsWriter); - String[] contingencyIds = loadflowConvergence.keySet().toArray(new String[loadflowConvergence.keySet().size()]); - Arrays.sort(contingencyIds); - for(String contingencyId : contingencyIds) { - Boolean loadflowConverge = loadflowConvergence.get(contingencyId); - printValues(table, cvsWriter, stateId, contingencyId, loadflowConverge); - } - cvsWriter.flush(); - printOutput(table, content, line.hasOption("csv")); - cvsWriter.close(); - } else - System.out.println("\nNo post contingency loadflow data for workflow "+workflowId+" and state "+stateId); - } else if ( line.hasOption("contingency") ) { - String contingencyId = line.getOptionValue("contingency"); - Map loadflowConvergence = onlinedb.getPostContingencyLoadflowConvergence(workflowId, contingencyId); - if ( loadflowConvergence != null && !loadflowConvergence.keySet().isEmpty() ) { - Table table = new Table(3, BorderStyle.CLASSIC_WIDE); - StringWriter content = new StringWriter(); - CsvWriter cvsWriter = new CsvWriter(content, ','); - printHeaders(table, cvsWriter); - Integer[] stateIds = loadflowConvergence.keySet().toArray(new Integer[loadflowConvergence.keySet().size()]); - Arrays.sort(stateIds); - for(Integer stateId : stateIds) { - Boolean loadflowConverge = loadflowConvergence.get(stateId); - printValues(table, cvsWriter, stateId, contingencyId, loadflowConverge); - } - cvsWriter.flush(); - printOutput(table, content, line.hasOption("csv")); - cvsWriter.close(); - } else - System.out.println("\nNo post contingency loadflow data for workflow "+workflowId+" and contingency "+contingencyId); - } else { - Map> loadflowConvergence = onlinedb.getPostContingencyLoadflowConvergence(workflowId); - if ( loadflowConvergence != null && !loadflowConvergence.keySet().isEmpty() ) { - Table table = new Table(3, BorderStyle.CLASSIC_WIDE); - StringWriter content = new StringWriter(); - CsvWriter cvsWriter = new CsvWriter(content, ','); - printHeaders(table, cvsWriter); - Integer[] stateIds = loadflowConvergence.keySet().toArray(new Integer[loadflowConvergence.keySet().size()]); - Arrays.sort(stateIds); - for(Integer stateId : stateIds) { - Map stateLoadflowConvergence = loadflowConvergence.get(stateId); - if ( stateLoadflowConvergence != null && !stateLoadflowConvergence.keySet().isEmpty() ) { - String[] contingencyIds = stateLoadflowConvergence.keySet().toArray(new String[stateLoadflowConvergence.keySet().size()]); - Arrays.sort(contingencyIds); - for(String contingencyId : contingencyIds) { - Boolean loadflowConverge = stateLoadflowConvergence.get(contingencyId); - printValues(table, cvsWriter, stateId, contingencyId, loadflowConverge); - } - } - } - cvsWriter.flush(); - printOutput(table, content, line.hasOption("csv")); - cvsWriter.close(); - } else - System.out.println("\nNo post contingency loadflow data for workflow "+workflowId); + TableFormatterConfig tableFormatterConfig=TableFormatterConfig.load(); + Writer writer=null; + Path csvFile = null; + TableFormatterFactory formatterFactory=null; + if (line.hasOption("csv")) { + formatterFactory=new CsvTableFormatterFactory(); + csvFile = Paths.get(line.getOptionValue("csv")); + writer=Files.newBufferedWriter(csvFile, StandardCharsets.UTF_8); + } else { + formatterFactory=new AsciiTableFormatterFactory(); + writer=new SystemOutStreamWriter(); } - onlinedb.close(); - } - - private void printHeaders(Table table, CsvWriter cvsWriter) throws IOException { - String[] headers = new String[3]; - int i = 0; - table.addCell("State", new CellStyle(CellStyle.HorizontalAlign.center)); - headers[i++] = "State"; - table.addCell("Contingency", new CellStyle(CellStyle.HorizontalAlign.center)); - headers[i++] = "Contingency"; - table.addCell("Loadflow Convergence", new CellStyle(CellStyle.HorizontalAlign.center)); - headers[i++] = "Loadflow Convergence"; - cvsWriter.writeRecord(headers); + try (OnlineDb onlinedb = config.getOnlineDbFactoryClass().newInstance().create()) { + if (line.hasOption("state")) { + Integer stateId = Integer.parseInt(line.getOptionValue("state")); + Map loadflowConvergence = onlinedb.getPostContingencyLoadflowConvergence(workflowId, stateId); + if (loadflowConvergence != null && !loadflowConvergence.keySet().isEmpty()) { + try (TableFormatter formatter = createFormatter(formatterFactory, tableFormatterConfig, writer)) { + String[] contingencyIds = loadflowConvergence.keySet().toArray(new String[loadflowConvergence.keySet().size()]); + Arrays.sort(contingencyIds); + for (String contingencyId : contingencyIds) { + Boolean loadflowConverge = loadflowConvergence.get(contingencyId); + printValues(formatter, stateId, contingencyId, loadflowConverge); + } + } + } else + System.out.println("\nNo post contingency loadflow data for workflow " + workflowId + " and state " + stateId); + } else if (line.hasOption("contingency")) { + String contingencyId = line.getOptionValue("contingency"); + Map loadflowConvergence = onlinedb.getPostContingencyLoadflowConvergence(workflowId, contingencyId); + if (loadflowConvergence != null && !loadflowConvergence.keySet().isEmpty()) { + try (TableFormatter formatter = createFormatter(formatterFactory, tableFormatterConfig, writer)) { + Integer[] stateIds = loadflowConvergence.keySet().toArray(new Integer[loadflowConvergence.keySet().size()]); + Arrays.sort(stateIds); + for (Integer stateId : stateIds) { + Boolean loadflowConverge = loadflowConvergence.get(stateId); + printValues(formatter, stateId, contingencyId, loadflowConverge); + } + } + } else + System.out.println("\nNo post contingency loadflow data for workflow " + workflowId + " and contingency " + contingencyId); + } else { + Map> loadflowConvergence = onlinedb.getPostContingencyLoadflowConvergence(workflowId); + if (loadflowConvergence != null && !loadflowConvergence.keySet().isEmpty()) { + try (TableFormatter formatter = createFormatter(formatterFactory, tableFormatterConfig, writer)) { + Integer[] stateIds = loadflowConvergence.keySet().toArray(new Integer[loadflowConvergence.keySet().size()]); + Arrays.sort(stateIds); + for (Integer stateId : stateIds) { + Map stateLoadflowConvergence = loadflowConvergence.get(stateId); + if (stateLoadflowConvergence != null && !stateLoadflowConvergence.keySet().isEmpty()) { + String[] contingencyIds = stateLoadflowConvergence.keySet().toArray(new String[stateLoadflowConvergence.keySet().size()]); + Arrays.sort(contingencyIds); + for (String contingencyId : contingencyIds) { + Boolean loadflowConverge = stateLoadflowConvergence.get(contingencyId); + printValues(formatter, stateId, contingencyId, loadflowConverge); + } + } + } + } + } else + System.out.println("\nNo post contingency loadflow data for workflow " + workflowId); + } + } } - - private void printValues(Table table, CsvWriter cvsWriter, Integer stateId, String contingencyId, Boolean loadflowConverge) throws IOException { - String[] values = new String[3]; - int i = 0; - table.addCell(Integer.toString(stateId), new CellStyle(CellStyle.HorizontalAlign.right)); - values[i++] = Integer.toString(stateId); - table.addCell(contingencyId, new CellStyle(CellStyle.HorizontalAlign.left)); - values[i++] = contingencyId; - table.addCell(loadflowConverge.toString(), new CellStyle(CellStyle.HorizontalAlign.left)); - values[i++] = loadflowConverge.toString(); - cvsWriter.writeRecord(values); + + private TableFormatter createFormatter(TableFormatterFactory formatterFactory, TableFormatterConfig config, Writer writer) throws IOException { + TableFormatter formatter = formatterFactory.create(writer, + TABLE_TITLE, + config, + new Column("State"), + new Column("Contingency"), + new Column("Loadflow Convergence")); + return formatter; } - - private void printOutput(Table table, StringWriter content, boolean csv) { - if ( csv ) - System.out.println(content.toString()); - else - System.out.println(table.render()); + + private void printValues(TableFormatter formatter, Integer stateId, String contingencyId, Boolean loadflowConverge) throws IOException { + formatter.writeCell(stateId); + formatter.writeCell(contingencyId); + formatter.writeCell(loadflowConverge); } - } diff --git a/online-workflow/src/main/java/eu/itesla_project/online/tools/PrintOnlineWorkflowPostContingencyViolationsTool.java b/online-workflow/src/main/java/eu/itesla_project/online/tools/PrintOnlineWorkflowPostContingencyViolationsTool.java index cfd5e283..19c410ae 100644 --- a/online-workflow/src/main/java/eu/itesla_project/online/tools/PrintOnlineWorkflowPostContingencyViolationsTool.java +++ b/online-workflow/src/main/java/eu/itesla_project/online/tools/PrintOnlineWorkflowPostContingencyViolationsTool.java @@ -8,7 +8,11 @@ package eu.itesla_project.online.tools; import java.io.IOException; -import java.io.StringWriter; +import java.io.Writer; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; import java.util.Arrays; import java.util.Collections; import java.util.Comparator; @@ -17,14 +21,11 @@ import java.util.Set; import java.util.stream.Collectors; +import eu.itesla_project.commons.io.SystemOutStreamWriter; +import eu.itesla_project.commons.io.table.*; import org.apache.commons.cli.CommandLine; import org.apache.commons.cli.Option; import org.apache.commons.cli.Options; -import org.nocrala.tools.texttablefmt.BorderStyle; -import org.nocrala.tools.texttablefmt.CellStyle; -import org.nocrala.tools.texttablefmt.Table; - -import com.csvreader.CsvWriter; import com.google.auto.service.AutoService; import eu.itesla_project.commons.tools.Command; @@ -42,6 +43,7 @@ @AutoService(Tool.class) public class PrintOnlineWorkflowPostContingencyViolationsTool implements Tool { + private static final String TABLE_TITLE = "online-workflow-postcontingency-violations"; private static Command COMMAND = new Command() { @Override @@ -84,7 +86,9 @@ public Options getOptions() { .argName("VIOLATION_TYPE,VIOLATION_TYPE,...") .build()); options.addOption(Option.builder().longOpt("csv") - .desc("export in csv format") + .desc("export in csv format to a file") + .hasArg() + .argName("FILE") .build()); return options; } @@ -104,7 +108,6 @@ public Command getCommand() { @Override public void run(CommandLine line) throws Exception { OnlineConfig config = OnlineConfig.load(); - OnlineDb onlinedb = config.getOnlineDbFactoryClass().newInstance().create(); String workflowId = line.getOptionValue("workflow"); LimitViolationFilter violationsFilter = null; if (line.hasOption("type")) { @@ -113,116 +116,104 @@ public void run(CommandLine line) throws Exception { .collect(Collectors.toSet()); violationsFilter = new LimitViolationFilter(limitViolationTypes, 0); } - if ( line.hasOption("state") && line.hasOption("contingency")) { - Integer stateId = Integer.parseInt(line.getOptionValue("state")); - String contingencyId = line.getOptionValue("contingency"); - List violations = onlinedb.getPostContingencyViolations(workflowId, stateId, contingencyId); - if ( violations != null && !violations.isEmpty() ) { - Table table = new Table(8, BorderStyle.CLASSIC_WIDE); - StringWriter content = new StringWriter(); - CsvWriter cvsWriter = new CsvWriter(content, ','); - printHeaders(table, cvsWriter); - printValues(table, cvsWriter, stateId, contingencyId, violations, violationsFilter); - printOutput(table, content, line.hasOption("csv")); - cvsWriter.close(); - } else - System.out.println("\nNo post contingency violations for workflow "+workflowId+", contingency "+contingencyId+" and state "+stateId); - } else if ( line.hasOption("state") ) { - Integer stateId = Integer.parseInt(line.getOptionValue("state")); - Map> stateViolations = onlinedb.getPostContingencyViolations(workflowId, stateId); - if ( stateViolations != null && !stateViolations.keySet().isEmpty() ) { - Table table = new Table(8, BorderStyle.CLASSIC_WIDE); - StringWriter content = new StringWriter(); - CsvWriter cvsWriter = new CsvWriter(content, ','); - printHeaders(table, cvsWriter); - String[] contingencyIds = stateViolations.keySet().toArray(new String[stateViolations.keySet().size()]); - Arrays.sort(contingencyIds); - for(String contingencyId : contingencyIds) { - List violations = stateViolations.get(contingencyId); - if ( violations != null && !violations.isEmpty() ) { - printValues(table, cvsWriter, stateId, contingencyId, violations, violationsFilter); - } - } - cvsWriter.flush(); - printOutput(table, content, line.hasOption("csv")); - cvsWriter.close(); - } else - System.out.println("\nNo post contingency violations for workflow "+workflowId+" and state "+stateId); - } else if ( line.hasOption("contingency") ) { - String contingencyId = line.getOptionValue("contingency"); - Map> contingencyViolations = onlinedb.getPostContingencyViolations(workflowId, contingencyId); - if ( contingencyViolations != null && !contingencyViolations.keySet().isEmpty() ) { - Table table = new Table(8, BorderStyle.CLASSIC_WIDE); - StringWriter content = new StringWriter(); - CsvWriter cvsWriter = new CsvWriter(content, ','); - printHeaders(table, cvsWriter); - Integer[] stateIds = contingencyViolations.keySet().toArray(new Integer[contingencyViolations.keySet().size()]); - Arrays.sort(stateIds); - for(Integer stateId : stateIds) { - List violations = contingencyViolations.get(stateId); - if ( violations != null && !violations.isEmpty() ) { - printValues(table, cvsWriter, stateId, contingencyId, violations, violationsFilter); + TableFormatterConfig tableFormatterConfig=TableFormatterConfig.load(); + Writer writer=null; + Path csvFile = null; + TableFormatterFactory formatterFactory=null; + try (OnlineDb onlinedb = config.getOnlineDbFactoryClass().newInstance().create()) { + if (line.hasOption("csv")) { + formatterFactory = new CsvTableFormatterFactory(); + csvFile = Paths.get(line.getOptionValue("csv")); + writer = Files.newBufferedWriter(csvFile, StandardCharsets.UTF_8); + } else { + formatterFactory = new AsciiTableFormatterFactory(); + writer = new SystemOutStreamWriter(); + } + if (line.hasOption("state") && line.hasOption("contingency")) { + Integer stateId = Integer.parseInt(line.getOptionValue("state")); + String contingencyId = line.getOptionValue("contingency"); + List violations = onlinedb.getPostContingencyViolations(workflowId, stateId, contingencyId); + if (violations != null && !violations.isEmpty()) { + try (TableFormatter formatter = createFormatter(formatterFactory, tableFormatterConfig, writer)) { + printValues(formatter, stateId, contingencyId, violations, violationsFilter); } - } - cvsWriter.flush(); - printOutput(table, content, line.hasOption("csv")); - cvsWriter.close(); - } else - System.out.println("\nNo post contingency violations for workflow "+workflowId+" and contingency "+contingencyId); - } else { - Map>> wfViolations = onlinedb.getPostContingencyViolations(workflowId); - if ( wfViolations != null && !wfViolations.keySet().isEmpty() ) { - Table table = new Table(8, BorderStyle.CLASSIC_WIDE); - StringWriter content = new StringWriter(); - CsvWriter cvsWriter = new CsvWriter(content, ','); - printHeaders(table, cvsWriter); - Integer[] stateIds = wfViolations.keySet().toArray(new Integer[wfViolations.keySet().size()]); - Arrays.sort(stateIds); - for(Integer stateId : stateIds) { - Map> stateViolations = wfViolations.get(stateId); - if ( stateViolations != null && !stateViolations.keySet().isEmpty() ) { + } else + System.out.println("\nNo post contingency violations for workflow " + workflowId + ", contingency " + contingencyId + " and state " + stateId); + } else if (line.hasOption("state")) { + Integer stateId = Integer.parseInt(line.getOptionValue("state")); + Map> stateViolations = onlinedb.getPostContingencyViolations(workflowId, stateId); + if (stateViolations != null && !stateViolations.keySet().isEmpty()) { + try (TableFormatter formatter = createFormatter(formatterFactory, tableFormatterConfig, writer)) { String[] contingencyIds = stateViolations.keySet().toArray(new String[stateViolations.keySet().size()]); Arrays.sort(contingencyIds); - for(String contingencyId : contingencyIds) { + for (String contingencyId : contingencyIds) { List violations = stateViolations.get(contingencyId); - if ( violations != null && !violations.isEmpty() ) { - printValues(table, cvsWriter, stateId, contingencyId, violations, violationsFilter); + if (violations != null && !violations.isEmpty()) { + printValues(formatter, stateId, contingencyId, violations, violationsFilter); } } } - } - cvsWriter.flush(); - printOutput(table, content, line.hasOption("csv")); - cvsWriter.close(); - } else - System.out.println("\nNo post contingency violations for workflow "+workflowId); + } else + System.out.println("\nNo post contingency violations for workflow " + workflowId + " and state " + stateId); + } else if (line.hasOption("contingency")) { + String contingencyId = line.getOptionValue("contingency"); + Map> contingencyViolations = onlinedb.getPostContingencyViolations(workflowId, contingencyId); + if (contingencyViolations != null && !contingencyViolations.keySet().isEmpty()) { + try (TableFormatter formatter = createFormatter(formatterFactory, tableFormatterConfig, writer)) { + Integer[] stateIds = contingencyViolations.keySet().toArray(new Integer[contingencyViolations.keySet().size()]); + Arrays.sort(stateIds); + for (Integer stateId : stateIds) { + List violations = contingencyViolations.get(stateId); + if (violations != null && !violations.isEmpty()) { + printValues(formatter, stateId, contingencyId, violations, violationsFilter); + } + } + } + } else + System.out.println("\nNo post contingency violations for workflow " + workflowId + " and contingency " + contingencyId); + } else { + Map>> wfViolations = onlinedb.getPostContingencyViolations(workflowId); + if (wfViolations != null && !wfViolations.keySet().isEmpty()) { + try (TableFormatter formatter = createFormatter(formatterFactory, tableFormatterConfig, writer)) { + Integer[] stateIds = wfViolations.keySet().toArray(new Integer[wfViolations.keySet().size()]); + Arrays.sort(stateIds); + for (Integer stateId : stateIds) { + Map> stateViolations = wfViolations.get(stateId); + if (stateViolations != null && !stateViolations.keySet().isEmpty()) { + String[] contingencyIds = stateViolations.keySet().toArray(new String[stateViolations.keySet().size()]); + Arrays.sort(contingencyIds); + for (String contingencyId : contingencyIds) { + List violations = stateViolations.get(contingencyId); + if (violations != null && !violations.isEmpty()) { + printValues(formatter, stateId, contingencyId, violations, violationsFilter); + } + } + } + } + } + } else + System.out.println("\nNo post contingency violations for workflow " + workflowId); + } } - onlinedb.close(); } - private void printHeaders(Table table, CsvWriter cvsWriter) throws IOException { - String[] headers = new String[8]; - int i = 0; - table.addCell("State", new CellStyle(CellStyle.HorizontalAlign.center)); - headers[i++] = "State"; - table.addCell("Contingency", new CellStyle(CellStyle.HorizontalAlign.center)); - headers[i++] = "Contingency"; - table.addCell("Equipment", new CellStyle(CellStyle.HorizontalAlign.center)); - headers[i++] = "Equipment"; - table.addCell("Type", new CellStyle(CellStyle.HorizontalAlign.center)); - headers[i++] = "Type"; - table.addCell("Value", new CellStyle(CellStyle.HorizontalAlign.center)); - headers[i++] = "Value"; - table.addCell("Limit", new CellStyle(CellStyle.HorizontalAlign.center)); - headers[i++] = "Limit"; - table.addCell("Limit Reduction", new CellStyle(CellStyle.HorizontalAlign.center)); - headers[i++] = "Limit Reduction"; - table.addCell("Voltage Level", new CellStyle(CellStyle.HorizontalAlign.center)); - headers[i++] = "Voltage Level"; - cvsWriter.writeRecord(headers); + private TableFormatter createFormatter(TableFormatterFactory formatterFactory, TableFormatterConfig config, Writer writer) throws IOException { + TableFormatter formatter = formatterFactory.create(writer, + TABLE_TITLE, + config, + new Column("State"), + new Column("Contingency"), + new Column("Equipment"), + new Column("Type"), + new Column("Value"), + new Column("Limit"), + new Column("Limit reduction"), + new Column("Voltage Level")); + return formatter; } - private void printValues(Table table, CsvWriter cvsWriter, Integer stateId, String contingencyId, List violations, + + private void printValues(TableFormatter formatter, Integer stateId, String contingencyId, List violations, LimitViolationFilter violationsFilter) throws IOException { if ( violationsFilter != null ) violations = violationsFilter.apply(violations); @@ -232,33 +223,14 @@ public int compare(LimitViolation o1, LimitViolation o2) { } }); for (LimitViolation violation : violations) { - String[] values = new String[8]; - int i = 0; - table.addCell(Integer.toString(stateId), new CellStyle(CellStyle.HorizontalAlign.right)); - values[i++] = Integer.toString(stateId); - table.addCell(contingencyId, new CellStyle(CellStyle.HorizontalAlign.left)); - values[i++] = contingencyId; - table.addCell(violation.getSubject().getId(), new CellStyle(CellStyle.HorizontalAlign.left)); - values[i++] = violation.getSubject().getId(); - table.addCell(violation.getLimitType().name(), new CellStyle(CellStyle.HorizontalAlign.left)); - values[i++] = violation.getLimitType().name(); - table.addCell(Float.toString(violation.getValue()), new CellStyle(CellStyle.HorizontalAlign.right)); - values[i++] = Float.toString(violation.getValue()); - table.addCell(Float.toString(violation.getLimit()), new CellStyle(CellStyle.HorizontalAlign.right)); - values[i++] = Float.toString(violation.getLimit()); - table.addCell(Float.toString(violation.getLimitReduction()), new CellStyle(CellStyle.HorizontalAlign.right)); - values[i++] = Float.toString(violation.getLimitReduction()); - table.addCell(Float.toString(violation.getBaseVoltage()), new CellStyle(CellStyle.HorizontalAlign.right)); - values[i++] = Float.toString(violation.getBaseVoltage()); - cvsWriter.writeRecord(values); + formatter.writeCell(stateId); + formatter.writeCell(contingencyId); + formatter.writeCell(violation.getSubject().getId()); + formatter.writeCell(violation.getLimitType().name()); + formatter.writeCell(violation.getValue()); + formatter.writeCell(violation.getLimit()); + formatter.writeCell(violation.getLimitReduction()); + formatter.writeCell(violation.getBaseVoltage()); } } - - private void printOutput(Table table, StringWriter content, boolean csv) { - if ( csv ) - System.out.println(content.toString()); - else - System.out.println(table.render()); - } - } diff --git a/online-workflow/src/main/java/eu/itesla_project/online/tools/PrintOnlineWorkflowSummaryTable.java b/online-workflow/src/main/java/eu/itesla_project/online/tools/PrintOnlineWorkflowSummaryTable.java index 962c586b..885e0542 100644 --- a/online-workflow/src/main/java/eu/itesla_project/online/tools/PrintOnlineWorkflowSummaryTable.java +++ b/online-workflow/src/main/java/eu/itesla_project/online/tools/PrintOnlineWorkflowSummaryTable.java @@ -1,13 +1,15 @@ /** * Copyright (c) 2016, All partners of the iTesla project (http://www.itesla-project.eu/consortium) + * Copyright (c) 2016, RTE (http://www.rte-france.com) * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ package eu.itesla_project.online.tools; -import com.csvreader.CsvWriter; import com.google.auto.service.AutoService; +import eu.itesla_project.commons.io.SystemOutStreamWriter; +import eu.itesla_project.commons.io.table.*; import eu.itesla_project.commons.tools.Command; import eu.itesla_project.commons.tools.Tool; import eu.itesla_project.iidm.network.Network; @@ -20,8 +22,10 @@ import org.joda.time.DateTime; import org.joda.time.Interval; -import java.io.FileWriter; import java.io.IOException; +import java.io.Writer; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.util.*; @@ -35,7 +39,8 @@ public class PrintOnlineWorkflowSummaryTable implements Tool { private static final String EMPTY_CONTINGENCY_ID = "Empty-Contingency"; - + private static final String TABLE_TITLE = "online-workflow-summary"; + private static Command COMMAND = new Command() { @Override @@ -77,9 +82,8 @@ public Options getOptions() { .argName("BASECASE") .build()); options.addOption(Option.builder().longOpt("output-file") - .desc("the ouptput file") + .desc("export in csv format to a file") .hasArg() - .required() .argName("FILE") .build()); return options; @@ -99,72 +103,76 @@ public Command getCommand() { @Override public void run(CommandLine line) throws Exception { - Path outputCsvFile = Paths.get(line.getOptionValue("output-file")); OnlineConfig config = OnlineConfig.load(); - OnlineDb onlinedb = config.getOnlineDbFactoryClass().newInstance().create(); - List workflowsIds = new ArrayList(); - if ( line.hasOption("workflow") ) - workflowsIds.add(line.getOptionValue("workflow")); - else if ( line.hasOption("workflows") ) - workflowsIds = Arrays.asList(line.getOptionValue("workflows").split(",")); - else if ( line.hasOption("basecase") ) { - DateTime basecaseDate = DateTime.parse(line.getOptionValue("basecase")); - workflowsIds = onlinedb.listWorkflows(basecaseDate).stream().map(OnlineWorkflowDetails::getWorkflowId).collect(Collectors.toList()); - } else if ( line.hasOption("basecases-interval") ) { - Interval basecasesInterval = Interval.parse(line.getOptionValue("basecases-interval")); - workflowsIds = onlinedb.listWorkflows(basecasesInterval).stream().map(OnlineWorkflowDetails::getWorkflowId).collect(Collectors.toList()); - } else { - System.out.println("You must specify workflow(s) or basecase(s)"); - return; - } - Collections.sort(workflowsIds, new Comparator() { - public int compare(String o1, String o2) { - return o1.compareTo(o2); - } - }); - System.out.println("Printing violations and failues of workflows " + workflowsIds); - try (FileWriter content = new FileWriter(outputCsvFile.toFile())) { - CsvWriter cvsWriter = null; - try { - // print headers - cvsWriter = new CsvWriter(content, ','); - String[] headers = new String[]{"WorkflowId", - "Basecase", - "Contingency", - "State", - "FailureStep", - "FailureDescription", - "ViolationType", - "Violation", - "ViolationStep", - "Equipment", - "Value", - "Limit"}; - cvsWriter.writeRecord(headers); - // cycle over the workflows - for (String workflowId : workflowsIds) { - System.out.println("Printing violations and failures of workflow " + workflowId); - Network basecase = onlinedb.getState(workflowId, 0); - String basecaseId = basecase.getId(); - // print pre-contingency violations - printPrecontingencyViolations(workflowId, basecaseId, onlinedb, cvsWriter); - // print contingencies violations - printContingenciesViolations(workflowId, basecaseId, onlinedb, basecase, cvsWriter); + try (OnlineDb onlinedb = config.getOnlineDbFactoryClass().newInstance().create()) { + List workflowsIds = new ArrayList(); + if ( line.hasOption("workflow") ) + workflowsIds.add(line.getOptionValue("workflow")); + else if ( line.hasOption("workflows") ) + workflowsIds = Arrays.asList(line.getOptionValue("workflows").split(",")); + else if ( line.hasOption("basecase") ) { + DateTime basecaseDate = DateTime.parse(line.getOptionValue("basecase")); + workflowsIds = onlinedb.listWorkflows(basecaseDate).stream().map(OnlineWorkflowDetails::getWorkflowId).collect(Collectors.toList()); + } else if ( line.hasOption("basecases-interval") ) { + Interval basecasesInterval = Interval.parse(line.getOptionValue("basecases-interval")); + workflowsIds = onlinedb.listWorkflows(basecasesInterval).stream().map(OnlineWorkflowDetails::getWorkflowId).collect(Collectors.toList()); + } else { + System.out.println("You must specify workflow(s) or basecase(s)"); + return; + } + TableFormatterConfig tableFormatterConfig=TableFormatterConfig.load(); + Writer writer=null; + Path csvFile = null; + TableFormatterFactory formatterFactory=null; + if (line.hasOption("output-file")) { + formatterFactory=new CsvTableFormatterFactory(); + csvFile = Paths.get(line.getOptionValue("output-file")); + writer=Files.newBufferedWriter(csvFile, StandardCharsets.UTF_8); + } else { + formatterFactory=new AsciiTableFormatterFactory(); + writer=new SystemOutStreamWriter(); + } + Collections.sort(workflowsIds, new Comparator() { + public int compare(String o1, String o2) { + return o1.compareTo(o2); + } + }); + System.out.println("Printing violations and failues of workflows " + workflowsIds); + + try (TableFormatter formatter = formatterFactory.create(writer, + TABLE_TITLE, + tableFormatterConfig, + new Column("WorkflowId"), + new Column("Basecase"), + new Column("Contingency"), + new Column("State"), + new Column("FailureStep"), + new Column("FailureDescription"), + new Column("ViolationType"), + new Column("Violation"), + new Column("ViolationStep"), + new Column("Equipment"), + new Column("Value"), + new Column("Limit")) ) { + try { + // cycle over the workflows + for (String workflowId : workflowsIds) { + System.out.println("Printing violations and failures of workflow " + workflowId); + Network basecase = onlinedb.getState(workflowId, 0); + String basecaseId = basecase.getId(); + // print pre-contingency violations + printPrecontingencyViolations(workflowId, basecaseId, onlinedb, formatter); + // print contingencies violations + printContingenciesViolations(workflowId, basecaseId, onlinedb, basecase, formatter); + } + } catch (IOException e) { + throw e; } - cvsWriter.flush(); - } catch (IOException e) { - throw e; - } finally { - if ( cvsWriter!=null ) - cvsWriter.close(); - onlinedb.close(); } - } catch (IOException e1) { - throw e1; } } - private void printPrecontingencyViolations(String workflowId, String basecaseId, OnlineDb onlinedb, CsvWriter cvsWriter) throws IOException { + private void printPrecontingencyViolations(String workflowId, String basecaseId, OnlineDb onlinedb, TableFormatter formatter) throws IOException { Map>> wfViolations = onlinedb.getViolations(workflowId); Map statesProcessingStatus = onlinedb.getStatesProcessingStatus(workflowId); if ( wfViolations != null && !wfViolations.keySet().isEmpty() ) { @@ -177,21 +185,18 @@ private void printPrecontingencyViolations(String workflowId, String basecaseId, && !stateprocessingStatus.getStatus().isEmpty() ) { for(String step : stateprocessingStatus.getStatus().keySet()) { if ( OnlineTaskStatus.valueOf(stateprocessingStatus.getStatus().get(step)) == OnlineTaskStatus.FAILED ) { - String[] values = new String[12]; - int i = 0; - values[i++] = workflowId; - values[i++] = basecaseId; - values[i++] = EMPTY_CONTINGENCY_ID; - values[i++] = Integer.toString(stateId); - values[i++] = step; - values[i++] = stateprocessingStatus.getDetail(); - values[i++] = ""; - values[i++] = ""; - values[i++] = ""; - values[i++] = ""; - values[i++] = ""; - values[i++] = ""; - cvsWriter.writeRecord(values); + formatter.writeCell(workflowId); + formatter.writeCell(basecaseId); + formatter.writeCell(EMPTY_CONTINGENCY_ID); + formatter.writeCell(stateId); + formatter.writeCell(step); + formatter.writeCell(stateprocessingStatus.getDetail()); + formatter.writeEmptyCell(); + formatter.writeEmptyCell(); + formatter.writeEmptyCell(); + formatter.writeEmptyCell(); + formatter.writeEmptyCell(); + formatter.writeEmptyCell(); break; } } @@ -202,7 +207,7 @@ private void printPrecontingencyViolations(String workflowId, String basecaseId, Arrays.sort(steps); for(OnlineStep step : steps) { List violations = stateViolations.get(step); - printViolations(workflowId, basecaseId, EMPTY_CONTINGENCY_ID, stateId, step, violations, cvsWriter); + printViolations(workflowId, basecaseId, EMPTY_CONTINGENCY_ID, stateId, step, violations, formatter); } } } @@ -210,7 +215,7 @@ private void printPrecontingencyViolations(String workflowId, String basecaseId, } private void printViolations(String workflowId, String basecaseId, String contingencyId, Integer stateId, OnlineStep step, - List violations, CsvWriter cvsWriter) throws IOException { + List violations, TableFormatter formatter) throws IOException { if (violations != null && !violations.isEmpty()) { Collections.sort(violations, new Comparator() { public int compare(LimitViolation o1, LimitViolation o2) { @@ -219,28 +224,25 @@ public int compare(LimitViolation o1, LimitViolation o2) { }); for (LimitViolation violation : violations) { if (violations != null && !violations.isEmpty()) { - String[] values = new String[12]; - int i = 0; - values[i++] = workflowId; - values[i++] = basecaseId; - values[i++] = contingencyId; - values[i++] = Integer.toString(stateId); - values[i++] = ""; - values[i++] = ""; - values[i++] = ViolationType.STEADY_STATE.name(); - values[i++] = violation.getLimitType().name(); - values[i++] = step.name(); - values[i++] = violation.getSubject().getId(); - values[i++] = Float.toString(violation.getValue()); - values[i++] = Float.toString(violation.getLimit()); - cvsWriter.writeRecord(values); + formatter.writeCell(workflowId); + formatter.writeCell(basecaseId); + formatter.writeCell(contingencyId); + formatter.writeCell(stateId); + formatter.writeEmptyCell(); + formatter.writeEmptyCell(); + formatter.writeCell(ViolationType.STEADY_STATE.name()); + formatter.writeCell(violation.getLimitType().name()); + formatter.writeCell(step.name()); + formatter.writeCell(violation.getSubject().getId()); + formatter.writeCell(violation.getValue()); + formatter.writeCell(violation.getLimit()); } } } } private void printContingenciesViolations(String workflowId, String basecaseId, OnlineDb onlinedb, - Network basecase, CsvWriter cvsWriter) throws IOException { + Network basecase, TableFormatter formatter) throws IOException { int states = onlinedb.getWorkflowParameters(workflowId).getStates(); OnlineWorkflowRulesResults wfWcaRulesResults = onlinedb.getWcaRulesResults(workflowId); OnlineWorkflowRulesResults wfMclaRulesResults = onlinedb.getRulesResults(workflowId); @@ -258,45 +260,42 @@ public int compare(String o1, String o2) { }); for (String contingency : contingencies) { for (int stateId = 0; stateId < states; stateId++) { - printFailures(loadflowConvergence, workflowId, basecaseId, contingency, stateId, cvsWriter); - printRulesResults(wfWcaRulesResults, workflowId, basecaseId, contingency, stateId, ViolationType.WCA_RULE, cvsWriter); - printRulesResults(wfMclaRulesResults, workflowId, basecaseId, contingency, stateId, ViolationType.MCLA_RULE, cvsWriter); - printSimulationResults(wfResults, workflowId, basecaseId, contingency, stateId, ViolationType.SECURITY_INDEX, cvsWriter); - printPostcontingencyViolations(workflowId, basecaseId, contingency, stateId, wfViolations, cvsWriter); + printFailures(loadflowConvergence, workflowId, basecaseId, contingency, stateId, formatter); + printRulesResults(wfWcaRulesResults, workflowId, basecaseId, contingency, stateId, ViolationType.WCA_RULE, formatter); + printRulesResults(wfMclaRulesResults, workflowId, basecaseId, contingency, stateId, ViolationType.MCLA_RULE, formatter); + printSimulationResults(wfResults, workflowId, basecaseId, contingency, stateId, ViolationType.SECURITY_INDEX, formatter); + printPostcontingencyViolations(workflowId, basecaseId, contingency, stateId, wfViolations, formatter); } } } private void printFailures(Map> loadflowConvergence, String workflowId, String basecaseId, - String contingencyId, Integer stateId, CsvWriter cvsWriter) throws IOException { + String contingencyId, Integer stateId, TableFormatter formatter) throws IOException { if ( loadflowConvergence != null && !loadflowConvergence.isEmpty() && loadflowConvergence.containsKey(stateId) && !loadflowConvergence.get(stateId).isEmpty() && loadflowConvergence.get(stateId).containsKey(contingencyId)) { if ( !loadflowConvergence.get(stateId).get(contingencyId) ) { - String[] values = new String[12]; - int i = 0; - values[i++] = workflowId; - values[i++] = basecaseId; - values[i++] = contingencyId; - values[i++] = Integer.toString(stateId); - values[i++] = OnlineStep.POSTCONTINGENCY_LOAD_FLOW.name(); - values[i++] = "Post contingency load flow does not converge"; - values[i++] = ""; - values[i++] = ""; - values[i++] = ""; - values[i++] = ""; - values[i++] = ""; - values[i++] = ""; - cvsWriter.writeRecord(values); + formatter.writeCell(workflowId); + formatter.writeCell(basecaseId); + formatter.writeCell(contingencyId); + formatter.writeCell(stateId); + formatter.writeCell(OnlineStep.POSTCONTINGENCY_LOAD_FLOW.name()); + formatter.writeCell("Post contingency load flow does not converge"); + formatter.writeEmptyCell(); + formatter.writeEmptyCell(); + formatter.writeEmptyCell(); + formatter.writeEmptyCell(); + formatter.writeEmptyCell(); + formatter.writeEmptyCell(); } } } private void printRulesResults(OnlineWorkflowRulesResults wfMclaRulesResults, String workflowId, String basecaseId, String contingencyId, - Integer stateId, ViolationType violationType, CsvWriter cvsWriter) throws IOException { + Integer stateId, ViolationType violationType, TableFormatter formatter) throws IOException { if ( wfMclaRulesResults != null && wfMclaRulesResults.getContingenciesWithSecurityRulesResults() != null && !wfMclaRulesResults.getContingenciesWithSecurityRulesResults().isEmpty() @@ -305,28 +304,25 @@ private void printRulesResults(OnlineWorkflowRulesResults wfMclaRulesResults, St && !wfMclaRulesResults.getStateResults(contingencyId, stateId).isEmpty()) { for (String index : wfMclaRulesResults.getStateResults(contingencyId, stateId).keySet()) { if ( !wfMclaRulesResults.getStateResults(contingencyId, stateId).get(index) ) { - String[] values = new String[12]; - int i=0; - values[i++] = workflowId; - values[i++] = basecaseId; - values[i++] = contingencyId; - values[i++] = Integer.toString(stateId); - values[i++] = ""; - values[i++] = ""; - values[i++] = violationType.name(); - values[i++] = index; - values[i++] = OnlineStep.MONTE_CARLO_LIKE_APPROACH.name(); - values[i++] = ""; - values[i++] = ""; - values[i++] = ""; - cvsWriter.writeRecord(values); + formatter.writeCell(workflowId); + formatter.writeCell(basecaseId); + formatter.writeCell(contingencyId); + formatter.writeCell(stateId); + formatter.writeEmptyCell(); + formatter.writeEmptyCell(); + formatter.writeCell(violationType.name()); + formatter.writeCell(index); + formatter.writeCell(OnlineStep.MONTE_CARLO_LIKE_APPROACH.name()); + formatter.writeEmptyCell(); + formatter.writeEmptyCell(); + formatter.writeEmptyCell(); } } } } private void printSimulationResults(OnlineWorkflowResults wfResults, String workflowId, String basecaseId, String contingencyId, - Integer stateId, ViolationType violationType, CsvWriter cvsWriter) throws IOException { + Integer stateId, ViolationType violationType, TableFormatter formatter) throws IOException { if (wfResults != null && wfResults.getUnsafeContingencies() != null && !wfResults.getUnsafeContingencies().isEmpty() @@ -335,28 +331,25 @@ private void printSimulationResults(OnlineWorkflowResults wfResults, String work && !wfResults.getIndexesData(contingencyId, stateId).isEmpty()) { for (String index : wfResults.getIndexesData(contingencyId, stateId).keySet()) { if ( !wfResults.getIndexesData(contingencyId, stateId).get(index) ) { - String[] values = new String[12]; - int i = 0; - values[i++] = workflowId; - values[i++] = basecaseId; - values[i++] = contingencyId; - values[i++] = Integer.toString(stateId); - values[i++] = ""; - values[i++] = ""; - values[i++] = violationType.name(); - values[i++] = index; - values[i++] = OnlineStep.TIME_DOMAIN_SIMULATION.name(); - values[i++] = ""; - values[i++] = ""; - values[i++] = ""; - cvsWriter.writeRecord(values); + formatter.writeCell(workflowId); + formatter.writeCell(basecaseId); + formatter.writeCell(contingencyId); + formatter.writeCell(stateId); + formatter.writeEmptyCell(); + formatter.writeEmptyCell(); + formatter.writeCell(violationType.name()); + formatter.writeCell(index); + formatter.writeCell(OnlineStep.TIME_DOMAIN_SIMULATION.name()); + formatter.writeEmptyCell(); + formatter.writeEmptyCell(); + formatter.writeEmptyCell(); } } } } private void printPostcontingencyViolations(String workflowId, String basecaseId, String contingencyId, Integer stateId, - Map>> wfViolations, CsvWriter cvsWriter) throws IOException { + Map>> wfViolations, TableFormatter formatter) throws IOException { if ( wfViolations!= null && !wfViolations.isEmpty() && wfViolations.containsKey(stateId) @@ -364,7 +357,7 @@ private void printPostcontingencyViolations(String workflowId, String basecaseId && !wfViolations.get(stateId).isEmpty() && wfViolations.get(stateId).containsKey(contingencyId)) { List violations = wfViolations.get(stateId).get(contingencyId); - printViolations(workflowId, basecaseId, contingencyId, stateId, OnlineStep.POSTCONTINGENCY_LOAD_FLOW, violations, cvsWriter); + printViolations(workflowId, basecaseId, contingencyId, stateId, OnlineStep.POSTCONTINGENCY_LOAD_FLOW, violations, formatter); } } } diff --git a/online-workflow/src/main/java/eu/itesla_project/online/tools/PrintOnlineWorkflowViolationsTool.java b/online-workflow/src/main/java/eu/itesla_project/online/tools/PrintOnlineWorkflowViolationsTool.java index b13956b5..4d5cd79a 100644 --- a/online-workflow/src/main/java/eu/itesla_project/online/tools/PrintOnlineWorkflowViolationsTool.java +++ b/online-workflow/src/main/java/eu/itesla_project/online/tools/PrintOnlineWorkflowViolationsTool.java @@ -7,26 +7,9 @@ */ package eu.itesla_project.online.tools; -import java.io.IOException; -import java.io.StringWriter; -import java.util.Arrays; -import java.util.Collections; -import java.util.Comparator; -import java.util.List; -import java.util.Map; -import java.util.Set; -import java.util.stream.Collectors; - -import org.apache.commons.cli.CommandLine; -import org.apache.commons.cli.Option; -import org.apache.commons.cli.Options; -import org.nocrala.tools.texttablefmt.BorderStyle; -import org.nocrala.tools.texttablefmt.CellStyle; -import org.nocrala.tools.texttablefmt.Table; - -import com.csvreader.CsvWriter; import com.google.auto.service.AutoService; - +import eu.itesla_project.commons.io.SystemOutStreamWriter; +import eu.itesla_project.commons.io.table.*; import eu.itesla_project.commons.tools.Command; import eu.itesla_project.commons.tools.Tool; import eu.itesla_project.modules.online.OnlineConfig; @@ -35,14 +18,26 @@ import eu.itesla_project.security.LimitViolation; import eu.itesla_project.security.LimitViolationFilter; import eu.itesla_project.security.LimitViolationType; +import org.apache.commons.cli.CommandLine; +import org.apache.commons.cli.Option; +import org.apache.commons.cli.Options; + +import java.io.IOException; +import java.io.Writer; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.*; +import java.util.stream.Collectors; /** - * * @author Quinary */ @AutoService(Tool.class) public class PrintOnlineWorkflowViolationsTool implements Tool { + private static final String TABLE_TITLE = "online-workflow-violations"; private static Command COMMAND = new Command() { @Override @@ -85,7 +80,9 @@ public Options getOptions() { .argName("VIOLATION_TYPE,VIOLATION_TYPE,...") .build()); options.addOption(Option.builder().longOpt("csv") - .desc("export in csv format") + .desc("export in csv format to a file") + .hasArg() + .argName("FILE") .build()); return options; } @@ -105,7 +102,6 @@ public Command getCommand() { @Override public void run(CommandLine line) throws Exception { OnlineConfig config = OnlineConfig.load(); - OnlineDb onlinedb = config.getOnlineDbFactoryClass().newInstance().create(); String workflowId = line.getOptionValue("workflow"); LimitViolationFilter violationsFilter = null; if (line.hasOption("type")) { @@ -114,118 +110,106 @@ public void run(CommandLine line) throws Exception { .collect(Collectors.toSet()); violationsFilter = new LimitViolationFilter(limitViolationTypes, 0); } - if ( line.hasOption("state") && line.hasOption("step")) { - Integer stateId = Integer.parseInt(line.getOptionValue("state")); - OnlineStep step = OnlineStep.valueOf(line.getOptionValue("step")); - List violations = onlinedb.getViolations(workflowId, stateId, step); - if ( violations != null && !violations.isEmpty() ) { - Table table = new Table(8, BorderStyle.CLASSIC_WIDE); - StringWriter content = new StringWriter(); - CsvWriter cvsWriter = new CsvWriter(content, ','); - printHeaders(table, cvsWriter); - printValues(table, cvsWriter, stateId, step, violations, violationsFilter); - printOutput(table, content, line.hasOption("csv")); - cvsWriter.close(); - } else - System.out.println("\nNo violations for workflow "+workflowId+", step "+step.name()+" and state "+stateId); - } else if ( line.hasOption("state") ) { - Integer stateId = Integer.parseInt(line.getOptionValue("state")); - Map> stateViolations = onlinedb.getViolations(workflowId, stateId); - if ( stateViolations != null && !stateViolations.keySet().isEmpty() ) { - Table table = new Table(8, BorderStyle.CLASSIC_WIDE); - StringWriter content = new StringWriter(); - CsvWriter cvsWriter = new CsvWriter(content, ','); - printHeaders(table, cvsWriter); - OnlineStep[] steps = stateViolations.keySet().toArray(new OnlineStep[stateViolations.keySet().size()]); - Arrays.sort(steps); - for(OnlineStep step : steps) { - List violations = stateViolations.get(step); - if ( violations != null && !violations.isEmpty() ) { - printValues(table, cvsWriter, stateId, step, violations, violationsFilter); - } - } - cvsWriter.flush(); - printOutput(table, content, line.hasOption("csv")); - cvsWriter.close(); - } else - System.out.println("\nNo violations for workflow "+workflowId+" and state "+stateId); - } else if ( line.hasOption("step") ) { - OnlineStep step = OnlineStep.valueOf(line.getOptionValue("step")); - Map> stepViolations = onlinedb.getViolations(workflowId, step); - if ( stepViolations != null && !stepViolations.keySet().isEmpty() ) { - Table table = new Table(8, BorderStyle.CLASSIC_WIDE); - StringWriter content = new StringWriter(); - CsvWriter cvsWriter = new CsvWriter(content, ','); - printHeaders(table, cvsWriter); - Integer[] stateIds = stepViolations.keySet().toArray(new Integer[stepViolations.keySet().size()]); - Arrays.sort(stateIds); - for(Integer stateId : stateIds) { - List violations = stepViolations.get(stateId); - if ( violations != null && !violations.isEmpty() ) { - printValues(table, cvsWriter, stateId, step, violations, violationsFilter); + + TableFormatterConfig tableFormatterConfig = TableFormatterConfig.load(); + Writer writer = null; + Path csvFile = null; + TableFormatterFactory formatterFactory = null; + if (line.hasOption("csv")) { + formatterFactory = new CsvTableFormatterFactory(); + csvFile = Paths.get(line.getOptionValue("csv")); + writer = Files.newBufferedWriter(csvFile, StandardCharsets.UTF_8); + } else { + formatterFactory = new AsciiTableFormatterFactory(); + writer = new SystemOutStreamWriter(); + } + try (OnlineDb onlinedb = config.getOnlineDbFactoryClass().newInstance().create()) { + if (line.hasOption("state") && line.hasOption("step")) { + Integer stateId = Integer.parseInt(line.getOptionValue("state")); + OnlineStep step = OnlineStep.valueOf(line.getOptionValue("step")); + List violations = onlinedb.getViolations(workflowId, stateId, step); + if (violations != null && !violations.isEmpty()) { + try (TableFormatter formatter = createFormatter(formatterFactory, tableFormatterConfig, writer)) { + printValues(formatter, stateId, step, violations, violationsFilter); } - } - cvsWriter.flush(); - printOutput(table, content, line.hasOption("csv")); - cvsWriter.close(); - } else - System.out.println("\nNo violations for workflow "+workflowId+" and step "+step); - } else { - Map>> wfViolations = onlinedb.getViolations(workflowId); - if ( wfViolations != null && !wfViolations.keySet().isEmpty() ) { - Table table = new Table(8, BorderStyle.CLASSIC_WIDE); - StringWriter content = new StringWriter(); - CsvWriter cvsWriter = new CsvWriter(content, ','); - printHeaders(table, cvsWriter); - Integer[] stateIds = wfViolations.keySet().toArray(new Integer[wfViolations.keySet().size()]); - Arrays.sort(stateIds); - for(Integer stateId : stateIds) { - Map> stateViolations = wfViolations.get(stateId); - if ( stateViolations != null && !stateViolations.keySet().isEmpty() ) { + } else + System.out.println("\nNo violations for workflow " + workflowId + ", step " + step.name() + " and state " + stateId); + } else if (line.hasOption("state")) { + Integer stateId = Integer.parseInt(line.getOptionValue("state")); + Map> stateViolations = onlinedb.getViolations(workflowId, stateId); + if (stateViolations != null && !stateViolations.keySet().isEmpty()) { + try (TableFormatter formatter = createFormatter(formatterFactory, tableFormatterConfig, writer)) { OnlineStep[] steps = stateViolations.keySet().toArray(new OnlineStep[stateViolations.keySet().size()]); Arrays.sort(steps); - for(OnlineStep step : steps) { + for (OnlineStep step : steps) { List violations = stateViolations.get(step); - if ( violations != null && !violations.isEmpty() ) { - printValues(table, cvsWriter, stateId, step, violations, violationsFilter); + if (violations != null && !violations.isEmpty()) { + printValues(formatter, stateId, step, violations, violationsFilter); + } + } + } + } else + System.out.println("\nNo violations for workflow " + workflowId + " and state " + stateId); + } else if (line.hasOption("step")) { + OnlineStep step = OnlineStep.valueOf(line.getOptionValue("step")); + Map> stepViolations = onlinedb.getViolations(workflowId, step); + if (stepViolations != null && !stepViolations.keySet().isEmpty()) { + try (TableFormatter formatter = createFormatter(formatterFactory, tableFormatterConfig, writer)) { + Integer[] stateIds = stepViolations.keySet().toArray(new Integer[stepViolations.keySet().size()]); + Arrays.sort(stateIds); + for (Integer stateId : stateIds) { + List violations = stepViolations.get(stateId); + if (violations != null && !violations.isEmpty()) { + printValues(formatter, stateId, step, violations, violationsFilter); } } } - } - cvsWriter.flush(); - printOutput(table, content, line.hasOption("csv")); - cvsWriter.close(); - } else - System.out.println("\nNo violations for workflow "+workflowId); + } else + System.out.println("\nNo violations for workflow " + workflowId + " and step " + step); + } else { + Map>> wfViolations = onlinedb.getViolations(workflowId); + if (wfViolations != null && !wfViolations.keySet().isEmpty()) { + try (TableFormatter formatter = createFormatter(formatterFactory, tableFormatterConfig, writer)) { + Integer[] stateIds = wfViolations.keySet().toArray(new Integer[wfViolations.keySet().size()]); + Arrays.sort(stateIds); + for (Integer stateId : stateIds) { + Map> stateViolations = wfViolations.get(stateId); + if (stateViolations != null && !stateViolations.keySet().isEmpty()) { + OnlineStep[] steps = stateViolations.keySet().toArray(new OnlineStep[stateViolations.keySet().size()]); + Arrays.sort(steps); + for (OnlineStep step : steps) { + List violations = stateViolations.get(step); + if (violations != null && !violations.isEmpty()) { + printValues(formatter, stateId, step, violations, violationsFilter); + } + } + } + } + } + } else + System.out.println("\nNo violations for workflow " + workflowId); + } } - onlinedb.close(); } - private void printHeaders(Table table, CsvWriter cvsWriter) throws IOException { - String[] headers = new String[8]; - int i = 0; - table.addCell("State", new CellStyle(CellStyle.HorizontalAlign.center)); - headers[i++] = "State"; - table.addCell("Step", new CellStyle(CellStyle.HorizontalAlign.center)); - headers[i++] = "Step"; - table.addCell("Equipment", new CellStyle(CellStyle.HorizontalAlign.center)); - headers[i++] = "Equipment"; - table.addCell("Type", new CellStyle(CellStyle.HorizontalAlign.center)); - headers[i++] = "Type"; - table.addCell("Value", new CellStyle(CellStyle.HorizontalAlign.center)); - headers[i++] = "Value"; - table.addCell("Limit", new CellStyle(CellStyle.HorizontalAlign.center)); - headers[i++] = "Limit"; - table.addCell("Limit Reduction", new CellStyle(CellStyle.HorizontalAlign.center)); - headers[i++] = "Limit Reduction"; - table.addCell("Voltage Level", new CellStyle(CellStyle.HorizontalAlign.center)); - headers[i++] = "Voltage Level"; - cvsWriter.writeRecord(headers); + private TableFormatter createFormatter(TableFormatterFactory formatterFactory, TableFormatterConfig config, Writer writer) throws IOException { + TableFormatter formatter = formatterFactory.create(writer, + TABLE_TITLE, + config, + new Column("State"), + new Column("Step"), + new Column("Equipment"), + new Column("Type"), + new Column("Value"), + new Column("Limit"), + new Column("Limit reduction"), + new Column("Voltage Level")); + return formatter; } - private void printValues(Table table, CsvWriter cvsWriter, Integer stateId, OnlineStep step, List violations, - LimitViolationFilter violationsFilter) throws IOException { - if ( violationsFilter != null ) + private void printValues(TableFormatter formatter, Integer stateId, OnlineStep step, List violations, + LimitViolationFilter violationsFilter) throws IOException { + if (violationsFilter != null) violations = violationsFilter.apply(violations); Collections.sort(violations, new Comparator() { public int compare(LimitViolation o1, LimitViolation o2) { @@ -233,33 +217,14 @@ public int compare(LimitViolation o1, LimitViolation o2) { } }); for (LimitViolation violation : violations) { - String[] values = new String[8]; - int i = 0; - table.addCell(Integer.toString(stateId), new CellStyle(CellStyle.HorizontalAlign.right)); - values[i++] = Integer.toString(stateId); - table.addCell(step.name(), new CellStyle(CellStyle.HorizontalAlign.left)); - values[i++] = step.name(); - table.addCell(violation.getSubject().getId(), new CellStyle(CellStyle.HorizontalAlign.left)); - values[i++] = violation.getSubject().getId(); - table.addCell(violation.getLimitType().name(), new CellStyle(CellStyle.HorizontalAlign.left)); - values[i++] = violation.getLimitType().name(); - table.addCell(Float.toString(violation.getValue()), new CellStyle(CellStyle.HorizontalAlign.right)); - values[i++] = Float.toString(violation.getValue()); - table.addCell(Float.toString(violation.getLimit()), new CellStyle(CellStyle.HorizontalAlign.right)); - values[i++] = Float.toString(violation.getLimit()); - table.addCell(Float.toString(violation.getLimitReduction()), new CellStyle(CellStyle.HorizontalAlign.right)); - values[i++] = Float.toString(violation.getLimitReduction()); - table.addCell(Float.toString(violation.getBaseVoltage()), new CellStyle(CellStyle.HorizontalAlign.right)); - values[i++] = Float.toString(violation.getBaseVoltage()); - cvsWriter.writeRecord(values); + formatter.writeCell(stateId); + formatter.writeCell(step.name()); + formatter.writeCell(violation.getSubject().getId()); + formatter.writeCell(violation.getLimitType().name()); + formatter.writeCell(violation.getValue()); + formatter.writeCell(violation.getLimit()); + formatter.writeCell(violation.getLimitReduction()); + formatter.writeCell(violation.getBaseVoltage()); } } - - private void printOutput(Table table, StringWriter content, boolean csv) { - if ( csv ) - System.out.println(content.toString()); - else - System.out.println(table.render()); - } - } From a7884418d5f2640d4a88f224b888cf09bb0d9aa3 Mon Sep 17 00:00:00 2001 From: massimoferraro Date: Fri, 18 Nov 2016 16:21:31 +0100 Subject: [PATCH 2/6] WCA: Added preventive actions to 'domains' task input --- .../iidm/export/ampl/AmplExportConfig.java | 29 +- .../iidm/export/ampl/AmplExporter.java | 3 +- .../iidm/export/ampl/AmplNetworkWriter.java | 81 +- .../iidm/export/ampl/AmplSubset.java | 4 +- .../export/ampl/AmplNetworkWriterTest.java | 2 +- ...ContingenciesAndActionsDatabaseClient.java | 1961 ++++++++--------- .../src/main/resources/xsd/actions.xsd | 2 +- ...ContingenciesAndActionsDatabaseClient.java | 11 +- ...ContingenciesAndActionsDatabaseClient.java | 8 + ...ingenciesAndActionsDatabaseClientImpl.java | 22 + ...ContingenciesAndActionsDatabaseClient.java | 18 +- .../wca/ContingencyDbFacade.java | 3 + .../wca/SimpleContingencyDbFacade.java | 43 +- .../java/eu/itesla_project/wca/WCAImpl.java | 122 +- 14 files changed, 1204 insertions(+), 1105 deletions(-) diff --git a/ampl-export/src/main/java/eu/itesla_project/iidm/export/ampl/AmplExportConfig.java b/ampl-export/src/main/java/eu/itesla_project/iidm/export/ampl/AmplExportConfig.java index 5c220441..20cf332a 100644 --- a/ampl-export/src/main/java/eu/itesla_project/iidm/export/ampl/AmplExportConfig.java +++ b/ampl-export/src/main/java/eu/itesla_project/iidm/export/ampl/AmplExportConfig.java @@ -1,5 +1,6 @@ /** * Copyright (c) 2016, All partners of the iTesla project (http://www.itesla-project.eu/consortium) + * Copyright (c) 2016, RTE (http://www.rte-france.com) * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. @@ -17,20 +18,38 @@ public enum ExportScope { ONLY_MAIN_CC_AND_CONNECTABLE_GENERATORS_AND_SHUNTS, ONLY_MAIN_CC_AND_CONNECTABLE_GENERATORS_AND_SHUNTS_AND_ALL_LOADS, } + + public enum ExportActionType { + CURATIVE("curative"), + PREVENTIVE("preventive"); + + private final String label; + + private ExportActionType(String label) { + this.label = label; + } + + public String getLabel() { + return label; + } + } private ExportScope exportScope; private boolean exportXNodes; + private ExportActionType actionType; + private boolean exportRatioTapChangerVoltageTarget; - public AmplExportConfig(ExportScope exportScope, boolean exportXNodes) { - this(exportScope, exportXNodes, false); + public AmplExportConfig(ExportScope exportScope, boolean exportXNodes, ExportActionType actionType) { + this(exportScope, exportXNodes, actionType, false); } - public AmplExportConfig(ExportScope exportScope, boolean exportXNodes, boolean exportRatioTapChangerVoltageTarget) { + public AmplExportConfig(ExportScope exportScope, boolean exportXNodes, ExportActionType actionType, boolean exportRatioTapChangerVoltageTarget) { this.exportScope = exportScope; this.exportXNodes = exportXNodes; + this.actionType = actionType; this.exportRatioTapChangerVoltageTarget = exportRatioTapChangerVoltageTarget; } @@ -50,6 +69,10 @@ public void setExportXNodes(boolean exportXNodes) { this.exportXNodes = exportXNodes; } + public ExportActionType getActionType() { + return actionType; + } + public boolean isExportRatioTapChangerVoltageTarget() { return exportRatioTapChangerVoltageTarget; } diff --git a/ampl-export/src/main/java/eu/itesla_project/iidm/export/ampl/AmplExporter.java b/ampl-export/src/main/java/eu/itesla_project/iidm/export/ampl/AmplExporter.java index 0d7453a0..c08bf4e0 100644 --- a/ampl-export/src/main/java/eu/itesla_project/iidm/export/ampl/AmplExporter.java +++ b/ampl-export/src/main/java/eu/itesla_project/iidm/export/ampl/AmplExporter.java @@ -1,5 +1,6 @@ /** * Copyright (c) 2016, All partners of the iTesla project (http://www.itesla-project.eu/consortium) + * Copyright (c) 2016, RTE (http://www.rte-france.com) * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. @@ -37,7 +38,7 @@ public void export(Network network, Properties parameters, DataSource dataSource throw new ITeslaException("network is null"); } try { - new AmplNetworkWriter(network, dataSource, new AmplExportConfig(AmplExportConfig.ExportScope.ONLY_MAIN_CC_AND_CONNECTABLE_GENERATORS_AND_SHUNTS, false)).write(); + new AmplNetworkWriter(network, dataSource, new AmplExportConfig(AmplExportConfig.ExportScope.ONLY_MAIN_CC_AND_CONNECTABLE_GENERATORS_AND_SHUNTS, false, AmplExportConfig.ExportActionType.CURATIVE)).write(); } catch (IOException e) { throw new ITeslaException(e); } diff --git a/ampl-export/src/main/java/eu/itesla_project/iidm/export/ampl/AmplNetworkWriter.java b/ampl-export/src/main/java/eu/itesla_project/iidm/export/ampl/AmplNetworkWriter.java index 12d56b21..f3bdcfb5 100644 --- a/ampl-export/src/main/java/eu/itesla_project/iidm/export/ampl/AmplNetworkWriter.java +++ b/ampl-export/src/main/java/eu/itesla_project/iidm/export/ampl/AmplNetworkWriter.java @@ -1,5 +1,6 @@ /** * Copyright (c) 2016, All partners of the iTesla project (http://www.itesla-project.eu/consortium) + * Copyright (c) 2016, RTE (http://www.rte-france.com) * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. @@ -41,7 +42,7 @@ public class AmplNetworkWriter implements AmplConstants { private final int faultNum; - private final int curativeActionNum; + private final int actionNum; private final DataSource dataSource; @@ -65,11 +66,11 @@ private static class AmplExportContext { } - public AmplNetworkWriter(Network network, DataSource dataSource, int faultNum, int curativeActionNum, + public AmplNetworkWriter(Network network, DataSource dataSource, int faultNum, int actionNum, boolean append, StringToIntMapper mapper, AmplExportConfig config) { this.network = network; this.faultNum = faultNum; - this.curativeActionNum = curativeActionNum; + this.actionNum = actionNum; this.dataSource = dataSource; this.append = append; this.mapper = mapper; @@ -123,7 +124,7 @@ private void writeSubstations(AmplExportContext context) throws IOException { new Column("minV (pu)"), new Column("maxV (pu)"), new Column("fault"), - new Column("curative"), + new Column(config.getActionType().getLabel()), new Column("country"), new Column("id"), new Column("description"))) { @@ -142,7 +143,7 @@ private void writeSubstations(AmplExportContext context) throws IOException { .writeCell(minV) .writeCell(maxV) .writeCell(faultNum) - .writeCell(curativeActionNum) + .writeCell(actionNum) .writeCell(vl.getSubstation().getCountry().toString()) .writeCell(vl.getId()) .writeCell(vl.getName()); @@ -164,7 +165,7 @@ private void writeSubstations(AmplExportContext context) throws IOException { .writeCell(Float.NaN) .writeCell(Float.NaN) .writeCell(faultNum) - .writeCell(curativeActionNum) + .writeCell(actionNum) .writeCell(vl1.getSubstation().getCountry().toString()) .writeCell(vlId) .writeEmptyCell(); @@ -187,7 +188,7 @@ private void writeSubstations(AmplExportContext context) throws IOException { .writeCell(minV) .writeCell(maxV) .writeCell(faultNum) - .writeCell(curativeActionNum) + .writeCell(actionNum) .writeCell(vl.getSubstation().getCountry().toString()) .writeCell(dl.getId() + "_voltageLevel") .writeEmptyCell(); @@ -207,7 +208,7 @@ private void writeSubstations(AmplExportContext context) throws IOException { .writeCell(Float.NaN) .writeCell(Float.NaN) .writeCell(faultNum) - .writeCell(curativeActionNum) + .writeCell(actionNum) .writeCell(XNODE_COUNTRY_NAME) .writeCell(AmplUtil.getXnodeBusId(tieLine) + "_voltageLevel") .writeEmptyCell(); @@ -246,7 +247,7 @@ private void writeBuses(AmplExportContext context) throws IOException { new Column("p (MW)"), new Column("q (MVar)"), new Column("fault"), - new Column("curative"))) { + new Column(config.getActionType().getLabel()))) { for (Bus b : AmplUtil.getBuses(network)) { int ccNum = ConnectedComponents.getCcNum(b); // skip buses not in the main connected component @@ -269,7 +270,7 @@ private void writeBuses(AmplExportContext context) throws IOException { .writeCell(b.getP()) .writeCell(b.getQ()) .writeCell(faultNum) - .writeCell(curativeActionNum); + .writeCell(actionNum); } // 3 windings transformers middle bus for (ThreeWindingsTransformer twt : network.getThreeWindingsTransformers()) { @@ -306,7 +307,7 @@ private void writeBuses(AmplExportContext context) throws IOException { .writeCell(0f) .writeCell(0f) .writeCell(faultNum) - .writeCell(curativeActionNum); + .writeCell(actionNum); } // dangling line middle bus for (DanglingLine dl : network.getDanglingLines()) { @@ -341,7 +342,7 @@ private void writeBuses(AmplExportContext context) throws IOException { .writeCell(0f) // 0 MW injected at dangling line internal bus .writeCell(0f) // 0 MVar injected at dangling line internal bus .writeCell(faultNum) - .writeCell(curativeActionNum); + .writeCell(actionNum); } if (config.isExportXNodes()) { for (Line l : network.getLines()) { @@ -376,7 +377,7 @@ private void writeBuses(AmplExportContext context) throws IOException { .writeCell(0f) .writeCell(0f) .writeCell(faultNum) - .writeCell(curativeActionNum); + .writeCell(actionNum); } } } @@ -423,7 +424,7 @@ private void writeBranches(AmplExportContext context) throws IOException { new Column("patl2 (A)"), new Column("merged"), new Column("fault"), - new Column("curative"), + new Column(config.getActionType().getLabel()), new Column("id"), new Column("description"))) { for (Line l : network.getLines()) { @@ -497,7 +498,7 @@ private void writeBranches(AmplExportContext context) throws IOException { .writeCell(Float.NaN) .writeCell(merged) .writeCell(faultNum) - .writeCell(curativeActionNum) + .writeCell(actionNum) .writeCell(half1Id) .writeCell(tl.getHalf1().getName()); formatter.writeCell(half2Num) @@ -523,7 +524,7 @@ private void writeBranches(AmplExportContext context) throws IOException { .writeCell(getPermanentLimit(l.getCurrentLimits2())) .writeCell(merged) .writeCell(faultNum) - .writeCell(curativeActionNum) + .writeCell(actionNum) .writeCell(half2Id) .writeCell(tl.getHalf2().getName()); } else { @@ -550,7 +551,7 @@ private void writeBranches(AmplExportContext context) throws IOException { .writeCell(getPermanentLimit(l.getCurrentLimits2())) .writeCell(merged) .writeCell(faultNum) - .writeCell(curativeActionNum) + .writeCell(actionNum) .writeCell(id) .writeCell(l.getName()); } @@ -624,7 +625,7 @@ private void writeBranches(AmplExportContext context) throws IOException { .writeCell(getPermanentLimit(twt.getCurrentLimits2())) .writeCell(false) // TODO to update .writeCell(faultNum) - .writeCell(curativeActionNum) + .writeCell(actionNum) .writeCell(id) .writeCell(twt.getName()); } @@ -722,7 +723,7 @@ private void writeBranches(AmplExportContext context) throws IOException { .writeCell(getPermanentLimit(twt.getLeg1().getCurrentLimits())) .writeCell(false) .writeCell(faultNum) - .writeCell(curativeActionNum) + .writeCell(actionNum) .writeCell(id1) .writeEmptyCell(); } @@ -750,7 +751,7 @@ private void writeBranches(AmplExportContext context) throws IOException { .writeCell(Float.NaN) .writeCell(false) .writeCell(faultNum) - .writeCell(curativeActionNum) + .writeCell(actionNum) .writeCell(id2) .writeEmptyCell(); } @@ -778,7 +779,7 @@ private void writeBranches(AmplExportContext context) throws IOException { .writeCell(Float.NaN) .writeCell(false) .writeCell(faultNum) - .writeCell(curativeActionNum) + .writeCell(actionNum) .writeCell(id3) .writeEmptyCell(); } @@ -836,7 +837,7 @@ private void writeBranches(AmplExportContext context) throws IOException { .writeCell(patl) .writeCell(false) .writeCell(faultNum) - .writeCell(curativeActionNum) + .writeCell(actionNum) .writeCell(id) .writeCell(dl.getName()); } @@ -856,7 +857,7 @@ private void writeTapChangerTable() throws IOException { new Column("x (pu)"), new Column("angle (rad)"), new Column("fault"), - new Column("curative"))) { + new Column(config.getActionType().getLabel()))) { for (TwoWindingsTransformer twt : network.getTwoWindingsTransformers()) { Terminal t2 = twt.getTerminal2(); float vb2 = t2.getVoltageLevel().getNominalV(); @@ -875,7 +876,7 @@ private void writeTapChangerTable() throws IOException { .writeCell(x) .writeCell(0f) .writeCell(faultNum) - .writeCell(curativeActionNum); + .writeCell(actionNum); } } PhaseTapChanger ptc = twt.getPhaseTapChanger(); @@ -891,7 +892,7 @@ private void writeTapChangerTable() throws IOException { .writeCell(x) .writeCell((float) Math.toRadians(step.getAlpha())) .writeCell(faultNum) - .writeCell(curativeActionNum); + .writeCell(actionNum); } } } @@ -915,7 +916,7 @@ private void writeTapChangerTable() throws IOException { .writeCell(x) .writeCell(0f) .writeCell(faultNum) - .writeCell(curativeActionNum); + .writeCell(actionNum); } } RatioTapChanger rtc3 = twt.getLeg3().getRatioTapChanger(); @@ -931,7 +932,7 @@ private void writeTapChangerTable() throws IOException { .writeCell(x) .writeCell(0f) .writeCell(faultNum) - .writeCell(curativeActionNum); + .writeCell(actionNum); } } } @@ -950,7 +951,7 @@ private void writeRatioTapChanger(TableFormatter formatter, String rtcId, formatter.writeCell(rtc.getTargetV()); } formatter.writeCell(faultNum) - .writeCell(curativeActionNum) + .writeCell(actionNum) .writeCell(rtcId); } @@ -964,7 +965,7 @@ private void writeRatioTapChangers() throws IOException { columns.add(new Column("targetV")); } columns.add(new Column("fault")); - columns.add(new Column("curative")); + columns.add(new Column(config.getActionType().getLabel())); columns.add(new Column("id")); try (TableFormatter formatter = new AmplDatTableFormatter( new OutputStreamWriter(dataSource.newOutputStream("_network_rtc", "txt", append), StandardCharsets.UTF_8), @@ -1009,7 +1010,7 @@ private void writePhaseTapChangers() throws IOException { new Column("tap"), new Column("table"), new Column("fault"), - new Column("curative"), + new Column(config.getActionType().getLabel()), new Column("id"))) { for (TwoWindingsTransformer twt : network.getTwoWindingsTransformers()) { PhaseTapChanger ptc = twt.getPhaseTapChanger(); @@ -1022,7 +1023,7 @@ private void writePhaseTapChangers() throws IOException { .writeCell(ptc.getTapPosition() - ptc.getLowTapPosition() + 1) .writeCell(tcsNum) .writeCell(faultNum) - .writeCell(curativeActionNum) + .writeCell(actionNum) .writeCell(ptcId); } } @@ -1055,7 +1056,7 @@ private void writeLoads(AmplExportContext context) throws IOException { new Column("p (MW)"), new Column("q (MVar)"), new Column("fault"), - new Column("curative"), + new Column(config.getActionType().getLabel()), new Column("id"), new Column("description"))) { List skipped = new ArrayList<>(); @@ -1082,7 +1083,7 @@ private void writeLoads(AmplExportContext context) throws IOException { .writeCell(l.getP0()) .writeCell(l.getQ0()) .writeCell(faultNum) - .writeCell(curativeActionNum) + .writeCell(actionNum) .writeCell(id) .writeCell(l.getName()); } @@ -1103,7 +1104,7 @@ private void writeLoads(AmplExportContext context) throws IOException { .writeCell(dl.getP0()) .writeCell(dl.getQ0()) .writeCell(faultNum) - .writeCell(curativeActionNum) + .writeCell(actionNum) .writeCell(dl.getId() + "_load") .writeEmptyCell(); } @@ -1143,7 +1144,7 @@ private void writeShunts(AmplExportContext context) throws IOException { new Column("inter. points"), new Column("b (pu)"), new Column("fault"), - new Column("curative"), + new Column(config.getActionType().getLabel()), new Column("id"), new Column("description"))) { List skipped = new ArrayList<>(); @@ -1188,7 +1189,7 @@ private void writeShunts(AmplExportContext context) throws IOException { .writeCell(points) .writeCell(b) .writeCell(faultNum) - .writeCell(curativeActionNum) + .writeCell(actionNum) .writeCell(id) .writeCell(sc.getName()); } @@ -1220,7 +1221,7 @@ private void writeGenerators(AmplExportContext context) throws IOException { new Column("targetP (MW)"), new Column("targetQ (MVar)"), new Column("fault"), - new Column("curative"), + new Column(config.getActionType().getLabel()), new Column("id"), new Column("description"))) { List skipped = new ArrayList<>(); @@ -1267,7 +1268,7 @@ private void writeGenerators(AmplExportContext context) throws IOException { .writeCell(g.getTargetP()) .writeCell(g.getTargetQ()) .writeCell(faultNum) - .writeCell(curativeActionNum) + .writeCell(actionNum) .writeCell(id) .writeCell(g.getName()); } @@ -1288,7 +1289,7 @@ private void writeTemporaryCurrentLimits(CurrentLimits limits, TableFormatter fo .writeCell(tl.getValue()) .writeCell(tl.getAcceptableDuration()) .writeCell(faultNum) - .writeCell(curativeActionNum); + .writeCell(actionNum); } } @@ -1305,7 +1306,7 @@ private void writeTemporaryCurrentLimits() throws IOException { new Column("limit (A)"), new Column("accept. duration (s)"), new Column("fault"), - new Column("curative"))) { + new Column(config.getActionType().getLabel()))) { for (Line l : network.getLines()) { String branchId = l.getId(); if (l.getCurrentLimits1() != null) { diff --git a/ampl-export/src/main/java/eu/itesla_project/iidm/export/ampl/AmplSubset.java b/ampl-export/src/main/java/eu/itesla_project/iidm/export/ampl/AmplSubset.java index da61a26c..414319a8 100644 --- a/ampl-export/src/main/java/eu/itesla_project/iidm/export/ampl/AmplSubset.java +++ b/ampl-export/src/main/java/eu/itesla_project/iidm/export/ampl/AmplSubset.java @@ -1,5 +1,6 @@ /** * Copyright (c) 2016, All partners of the iTesla project (http://www.itesla-project.eu/consortium) + * Copyright (c) 2016, RTE (http://www.rte-france.com) * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. @@ -26,7 +27,8 @@ public enum AmplSubset implements IntCounter { TEMPORARY_CURRENT_LIMIT(1), THREE_WINDINGS_TRANSFO(1), FAULT(1), - CURATIVE_ACTION(1); + CURATIVE_ACTION(1), + PREVENTIVE_ACTION(1); private final int initialValue; diff --git a/ampl-export/src/test/java/eu/itesla_project/iidm/export/ampl/AmplNetworkWriterTest.java b/ampl-export/src/test/java/eu/itesla_project/iidm/export/ampl/AmplNetworkWriterTest.java index ed673183..658195a3 100644 --- a/ampl-export/src/test/java/eu/itesla_project/iidm/export/ampl/AmplNetworkWriterTest.java +++ b/ampl-export/src/test/java/eu/itesla_project/iidm/export/ampl/AmplNetworkWriterTest.java @@ -33,7 +33,7 @@ public void write() throws Exception { Network network = EurostagTutorialExample1Factory.create(); MemDataSource dataSource = new MemDataSource(); - new AmplNetworkWriter(network, dataSource, new AmplExportConfig(AmplExportConfig.ExportScope.ALL, true)) + new AmplNetworkWriter(network, dataSource, new AmplExportConfig(AmplExportConfig.ExportScope.ALL, true, AmplExportConfig.ExportActionType.CURATIVE)) .write(); assertEqualsToRef(dataSource, "_network_substations", "eurostag-tutorial-example1-substations.txt"); diff --git a/iidm-actions-contingencies-xml-client/src/main/java/eu/itesla_project/iidm/actions_contingencies/xml/XmlFileContingenciesAndActionsDatabaseClient.java b/iidm-actions-contingencies-xml-client/src/main/java/eu/itesla_project/iidm/actions_contingencies/xml/XmlFileContingenciesAndActionsDatabaseClient.java index adda2ca6..db51b771 100644 --- a/iidm-actions-contingencies-xml-client/src/main/java/eu/itesla_project/iidm/actions_contingencies/xml/XmlFileContingenciesAndActionsDatabaseClient.java +++ b/iidm-actions-contingencies-xml-client/src/main/java/eu/itesla_project/iidm/actions_contingencies/xml/XmlFileContingenciesAndActionsDatabaseClient.java @@ -1,5 +1,6 @@ /** * Copyright (c) 2016, All partners of the iTesla project (http://www.itesla-project.eu/consortium) + * Copyright (c) 2016, RTE (http://www.rte-france.com) * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. @@ -17,8 +18,10 @@ import java.util.Iterator; import java.util.List; import java.util.Map; +import java.util.Objects; import java.util.Set; import java.util.TreeMap; +import java.util.stream.Collectors; import javax.xml.XMLConstants; import javax.xml.bind.JAXBContext; @@ -27,27 +30,57 @@ import javax.xml.validation.Schema; import javax.xml.validation.SchemaFactory; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.xml.sax.SAXException; + import eu.itesla_project.contingency.ContingencyElement; import eu.itesla_project.contingency.ContingencyImpl; import eu.itesla_project.contingency.GeneratorContingency; import eu.itesla_project.contingency.LineContingency; -import eu.itesla_project.iidm.actions_contingencies.xml.mapping.*; import eu.itesla_project.iidm.actions_contingencies.xml.mapping.Action; +import eu.itesla_project.iidm.actions_contingencies.xml.mapping.ActionCtgAssociations; import eu.itesla_project.iidm.actions_contingencies.xml.mapping.ActionPlan; +import eu.itesla_project.iidm.actions_contingencies.xml.mapping.ActionsContingencies; +import eu.itesla_project.iidm.actions_contingencies.xml.mapping.And; +import eu.itesla_project.iidm.actions_contingencies.xml.mapping.Association; import eu.itesla_project.iidm.actions_contingencies.xml.mapping.Constraint; import eu.itesla_project.iidm.actions_contingencies.xml.mapping.Contingency; +import eu.itesla_project.iidm.actions_contingencies.xml.mapping.ElementaryAction; +import eu.itesla_project.iidm.actions_contingencies.xml.mapping.Equipment; +import eu.itesla_project.iidm.actions_contingencies.xml.mapping.GenerationOperation; +import eu.itesla_project.iidm.actions_contingencies.xml.mapping.LineOperation; import eu.itesla_project.iidm.actions_contingencies.xml.mapping.LogicalExpression; +import eu.itesla_project.iidm.actions_contingencies.xml.mapping.Operand; +import eu.itesla_project.iidm.actions_contingencies.xml.mapping.Or; +import eu.itesla_project.iidm.actions_contingencies.xml.mapping.PstOperation; +import eu.itesla_project.iidm.actions_contingencies.xml.mapping.Redispatching; +import eu.itesla_project.iidm.actions_contingencies.xml.mapping.SwitchOperation; +import eu.itesla_project.iidm.actions_contingencies.xml.mapping.Then; import eu.itesla_project.iidm.actions_contingencies.xml.mapping.VoltageLevel; import eu.itesla_project.iidm.actions_contingencies.xml.mapping.Zone; -import eu.itesla_project.modules.contingencies.*; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.xml.sax.SAXException; - +import eu.itesla_project.iidm.actions_contingencies.xml.mapping.Zones; import eu.itesla_project.iidm.network.Line; import eu.itesla_project.iidm.network.Network; import eu.itesla_project.iidm.network.TieLine; +import eu.itesla_project.modules.contingencies.ActionElement; +import eu.itesla_project.modules.contingencies.ActionPlanOption; +import eu.itesla_project.modules.contingencies.ActionsContingenciesAssociation; +import eu.itesla_project.modules.contingencies.BinaryOperator; +import eu.itesla_project.modules.contingencies.ConstraintType; +import eu.itesla_project.modules.contingencies.ContingenciesAndActionsDatabaseClient; +import eu.itesla_project.modules.contingencies.GenerationRedispatching; +import eu.itesla_project.modules.contingencies.GeneratorStartAction; +import eu.itesla_project.modules.contingencies.GeneratorStopAction; +import eu.itesla_project.modules.contingencies.LineTrippingAction; +import eu.itesla_project.modules.contingencies.OperatorType; +import eu.itesla_project.modules.contingencies.Scenario; +import eu.itesla_project.modules.contingencies.ShuntAction; +import eu.itesla_project.modules.contingencies.SwitchClosingAction; +import eu.itesla_project.modules.contingencies.SwitchOpeningAction; +import eu.itesla_project.modules.contingencies.TapChangeAction; +import eu.itesla_project.modules.contingencies.TransformerOpeningAction; +import eu.itesla_project.modules.contingencies.UnaryOperator; import eu.itesla_project.modules.contingencies.impl.ActionImpl; import eu.itesla_project.modules.contingencies.impl.ActionPlanImpl; import eu.itesla_project.modules.contingencies.impl.ActionsContingenciesAssociationImpl; @@ -63,907 +96,745 @@ */ public class XmlFileContingenciesAndActionsDatabaseClient implements ContingenciesAndActionsDatabaseClient { - private static final Logger LOGGER = LoggerFactory.getLogger(XmlFileContingenciesAndActionsDatabaseClient.class); - - private ActionsContingencies actionContingencies; - private Map zonesMapping = new HashMap(); - - public XmlFileContingenciesAndActionsDatabaseClient(Path file) - throws JAXBException, SAXException { - - JAXBContext jaxbContext = JAXBContext - .newInstance(ActionsContingencies.class); - Unmarshaller jaxbMarshaller = jaxbContext.createUnmarshaller(); - - SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); - URL res=XmlFileContingenciesAndActionsDatabaseClient.class.getClassLoader().getResource("xsd/actions.xsd"); - Schema schema = sf.newSchema(res); - jaxbMarshaller.setSchema(schema); - - actionContingencies = (ActionsContingencies) jaxbMarshaller - .unmarshal(file.toFile()); - - - } - - - - - @Override - public List getScenarios() - { - - return Collections.emptyList(); - /* - List scenarios = new ArrayList(); - List actionIds = new ArrayList(); - for (Contingency c : actionContingencies.getContingencies() - .getContingency()) { - List actions = getActionsByContingencyId(c.getName()); - - for (Action a : actions) { - ActionPlan ap = this.getActionPlan(a.getId()); - if (ap != null) { - List