From d9b20e7782f7a5c948d9c919362206940f866280 Mon Sep 17 00:00:00 2001 From: Leclerc Clement Date: Wed, 23 Oct 2024 11:12:26 +0200 Subject: [PATCH 01/38] add xiidm to ucte element's id conversion Signed-off-by: Leclerc Clement --- .../ucte/converter/DefaultNamingStrategy.java | 209 +++++++++++++++++- .../ucte/converter/NamingStrategy.java | 2 + .../ucte/converter/NamingStrategyTest.java | 8 + .../ucte/converter/UcteExporterTest.java | 6 +- .../src/test/resources/network.xiidm | 42 ++++ 5 files changed, 260 insertions(+), 7 deletions(-) create mode 100644 ucte/ucte-converter/src/test/resources/network.xiidm diff --git a/ucte/ucte-converter/src/main/java/com/powsybl/ucte/converter/DefaultNamingStrategy.java b/ucte/ucte-converter/src/main/java/com/powsybl/ucte/converter/DefaultNamingStrategy.java index 79fbc719893..b88f7e7de94 100644 --- a/ucte/ucte-converter/src/main/java/com/powsybl/ucte/converter/DefaultNamingStrategy.java +++ b/ucte/ucte-converter/src/main/java/com/powsybl/ucte/converter/DefaultNamingStrategy.java @@ -10,11 +10,14 @@ import com.google.auto.service.AutoService; import com.powsybl.iidm.network.*; +import com.powsybl.ucte.network.UcteCountryCode; import com.powsybl.ucte.network.UcteElementId; import com.powsybl.ucte.network.UcteNodeCode; +import com.powsybl.ucte.network.UcteVoltageLevelCode; + +import java.util.*; +import java.util.stream.StreamSupport; -import java.util.HashMap; -import java.util.Map; /** * A {@link NamingStrategy} implementation that ensures the conformity of IDs with the UCTE-DEF format @@ -25,7 +28,6 @@ public class DefaultNamingStrategy implements NamingStrategy { private final Map ucteNodeIds = new HashMap<>(); - private final Map ucteElementIds = new HashMap<>(); @Override @@ -33,6 +35,207 @@ public String getName() { return "Default"; } + + /** + * Initializes the network by converting network elements names to UCTE format if needed. + * + * @param network The network to check + */ + @Override + public void init(Network network) { + //add condition to test network is already ucte format + convertToUcte(network); + } + + /** + * Converts all network elements IDs to UCTE format. This includes: + * - Converting bus IDs using country codes from their substations + * - Converting line IDs + * - Converting two-winding transformer IDs + * The conversion process: + * 1. For each substation, retrieves its country code (defaults to "XX" if not found) + * 2. Processes all buses within the substation's voltage levels + * 3. Processes all lines in the network + * 4. Processes all two-winding transformers + * + * @param network The network whose elements need to be converted to UCTE format + */ + private void convertToUcte(Network network) { + + // Process all substations and their buses + // For each substation, get country name and convert all bus IDs to UCTE format + network.getSubstationStream() + .forEach(substation -> { + String countryCode = substation.getCountry() + .map(Country::getName) + .orElse("XX"); + + substation.getVoltageLevelStream() + .flatMap(vl -> StreamSupport.stream(vl.getBusBreakerView().getBuses().spliterator(), false)) + .forEach(bus -> { + generateUcteNodeId(countryCode, bus); + System.out.println("BUS : " + bus.getId() + "--> " + ucteNodeIds.get(bus.getId())); + }); + }); + + // Convert all line IDs to UCTE format + network.getLineStream() + .forEach(line -> { + generateUcteElementId(line); + System.out.println("LINE : " + line.getId() + "--> " + ucteElementIds.get(line.getId())); + }); + + // Convert all two-winding transformer IDs to UCTE format + network.getTwoWindingsTransformerStream() + .forEach(transformer -> { + generateUcteElementId(transformer); + System.out.println("TwoWindingsTrans : " + transformer.getId() + "--> " + ucteElementIds.get(transformer.getId())); + }); + + } + + /** + * Generates a UCTE node identifier for a given bus and stores it in the ucteNodeIds map. + * The UCTE node format follows the pattern: C_NNNNN_V where: + * - length : 8 + * - char[0] : country code + * - char[1-5]: First 5 characters of bus ID (padded with '_' if shorter) + * - char[6] : Voltage level code followed by + * - char[7] : letter or figure for differentiating bus bars (optional) -> actually replace by '_' + * + * @param country The country name of the bus's substation + * @param bus The bus for which to generate the UCTE node ID + * @throws UcteException if the generated ID is not a valid UCTE node identifier + */ + private void generateUcteNodeId(String country, Bus bus) { + String busId = bus.getId(); + + // Skip if this bus ID has already been processed + if (ucteNodeIds.containsKey(busId)) { + return; + } + // Initialize StringBuilder with fixed capacity of 8 for UCTE format + StringBuilder nameBuilder = new StringBuilder(8); + // get ucte country code with the country name + nameBuilder.append(getCountryCode(country)); + // Format bus ID to fill chars[1-5] + String fomatedId = busId.length() >= 5 + ? busId.substring(0, 5) + : String.format("%-5s", busId).replace(' ', '_'); + nameBuilder.append(fomatedId); + // Add voltage level code (char[6]) and trailing underscore (char[7]) + nameBuilder.append(getVoltageLevelCode(bus.getVoltageLevel().getNominalV())).append('_'); + String name = nameBuilder.toString(); + // Validate and store the generated UCTE node code + UcteNodeCode nodeCode = UcteNodeCode.parseUcteNodeCode(name) + .orElseThrow(() -> new UcteException("Invalid UCTE node identifier: " + name)); + ucteNodeIds.put(busId, nodeCode); + } + + /** + * Generates a UCTE element identifier for a given two-winding transformer and stores it in the ucteElementIds map. + * The UCTE element format is constructed from the two connected buses: AAAAAAAA_BBBBBBBB_N where: + * - AAAAAAAA: UCTE node ID of the first bus (8 chars) + * - BBBBBBBB: UCTE node ID of the second bus (8 chars) + * - N: Order code (actually 1 for transformers) + * The original ID format is: busId1_busId2 + * + * @param transformer The two-winding transformer for which to generate the UCTE element ID + * @throws UcteException if the generated ID is not a valid UCTE element identifier + */ + private void generateUcteElementId(TwoWindingsTransformer transformer) { + + // Get bus IDs from both terminals of the transformer + String busId1 = transformer.getTerminal1().getBusBreakerView().getBus().getId(); + String busId2 = transformer.getTerminal2().getBusBreakerView().getBus().getId(); + + // Create original ID by concatenating both bus IDs with underscore + String originalId = new StringBuilder(busId1.length() + busId2.length() + 1) + .append(busId1) + .append('_') + .append(busId2) + .toString(); + + // Skip if this transformer ID has already been processed + if (ucteElementIds.containsKey(originalId)) {return;} + + // Create UCTE element ID using previously converted UCTE node IDs + // '1' is used as order code for transformers + UcteElementId elementId = new UcteElementId(ucteNodeIds.get(busId1), ucteNodeIds.get(busId2), '1'); + + // Validate and store the generated UCTE element ID + ucteElementIds.computeIfAbsent(originalId, k -> UcteElementId.parseUcteElementId(elementId.toString()).orElseThrow(() -> new UcteException("Invalid UCTE node identifier: " + k))); + + } + + /** + * Generates a UCTE element identifier for a given line and stores it in the ucteElementIds map. + * The UCTE element format is constructed from the two connected buses: AAAAAAAA_BBBBBBBB_N where: + * - AAAAAAAA: UCTE node ID of the first bus (8 chars) + * - BBBBBBBB: UCTE node ID of the second bus (8 chars) + * - N: Order code + * The original ID format is: busId1_busId2_orderCode + * + * @param line The two-winding transformer for which to generate the UCTE element ID + * @throws UcteException if the generated ID is not a valid UCTE element identifier + */ + private void generateUcteElementId(Line line) { + + // Get bus IDs from both terminals of the line ang get the orderCode (last char of the id) + String busId1 = line.getTerminal1().getBusBreakerView().getBus().getId(); + String busId2 = line.getTerminal2().getBusBreakerView().getBus().getId(); + char orderCode = line.getId().charAt(line.getId().length() - 1); + + // Create original ID by concatenating both bus IDs and orderCode with underscore + String originalId = new StringBuilder(busId1.length() + busId2.length() + 1) + .append(busId1) + .append('_') + .append(busId2) + .append('_') + .append(orderCode) + .toString(); + + // Skip if this line ID has already been processed + if (ucteElementIds.containsKey(originalId)) {return;} + // Create UCTE element ID using previously converted UCTE node IDs + UcteElementId elementId = new UcteElementId(ucteNodeIds.get(busId1), ucteNodeIds.get(busId2), orderCode); + // Validate and store the generated UCTE element ID + ucteElementIds.computeIfAbsent(originalId, k -> UcteElementId.parseUcteElementId(elementId.toString()).orElseThrow(() -> new UcteException("Invalid UCTE node identifier: " + k))); + + } + + /** + * Retrieves the UCTE single-character country code from a full country name. + * Searches through UcteCountryCode enum to find a matching country name (case-insensitive). + * + * @param code The full country name to convert + * @return The single character UCTE country code, or 'X' if country is not found + */ + private static char getCountryCode(String code) { + for(UcteCountryCode countryCode : UcteCountryCode.values()) { + if(code.equalsIgnoreCase(countryCode.getPrettyName())) { + return countryCode.getUcteCode(); + } + } + return 'X'; + } + + /** + * Retrieves the UCTE voltage level code based on a nominal voltage value. + * + * @param voltage The nominal voltage value in kV + * @return A character representing the UCTE voltage level code ('0' to 'N') + * @throws IllegalArgumentException if no matching voltage level code is found + */ + public static char getVoltageLevelCode(double voltage) { + for (UcteVoltageLevelCode code : UcteVoltageLevelCode.values()) { + if (code.getVoltageLevel() == (int) voltage) { + return (char) ('0' + code.ordinal()); + } + } + throw new IllegalArgumentException("No voltage level code found for " + voltage + " kV"); + } + @Override public UcteNodeCode getUcteNodeCode(String id) { return ucteNodeIds.computeIfAbsent(id, k -> UcteNodeCode.parseUcteNodeCode(k).orElseThrow(() -> new UcteException("Invalid UCTE node identifier: " + k))); diff --git a/ucte/ucte-converter/src/main/java/com/powsybl/ucte/converter/NamingStrategy.java b/ucte/ucte-converter/src/main/java/com/powsybl/ucte/converter/NamingStrategy.java index eb925831124..5c6fddf784e 100644 --- a/ucte/ucte-converter/src/main/java/com/powsybl/ucte/converter/NamingStrategy.java +++ b/ucte/ucte-converter/src/main/java/com/powsybl/ucte/converter/NamingStrategy.java @@ -19,6 +19,8 @@ public interface NamingStrategy { String getName(); + void init(Network n); + UcteNodeCode getUcteNodeCode(String id); UcteNodeCode getUcteNodeCode(Bus bus); diff --git a/ucte/ucte-converter/src/test/java/com/powsybl/ucte/converter/NamingStrategyTest.java b/ucte/ucte-converter/src/test/java/com/powsybl/ucte/converter/NamingStrategyTest.java index 00736736edd..f4815515f09 100644 --- a/ucte/ucte-converter/src/test/java/com/powsybl/ucte/converter/NamingStrategyTest.java +++ b/ucte/ucte-converter/src/test/java/com/powsybl/ucte/converter/NamingStrategyTest.java @@ -27,6 +27,14 @@ */ class NamingStrategyTest { + @Test + void initTest() { + ResourceDataSource dataSource = new ResourceDataSource("network", new ResourceSet("/", "network.xiidm")); + Network n1 = Network.read(dataSource); + NamingStrategy s = new DefaultNamingStrategy(); + s.init(n1); + } + @Test void testUcteCode() { NamingStrategy strategy = new DefaultNamingStrategy(); diff --git a/ucte/ucte-converter/src/test/java/com/powsybl/ucte/converter/UcteExporterTest.java b/ucte/ucte-converter/src/test/java/com/powsybl/ucte/converter/UcteExporterTest.java index c8d21f65c45..6e2a2a42e26 100644 --- a/ucte/ucte-converter/src/test/java/com/powsybl/ucte/converter/UcteExporterTest.java +++ b/ucte/ucte-converter/src/test/java/com/powsybl/ucte/converter/UcteExporterTest.java @@ -8,12 +8,9 @@ package com.powsybl.ucte.converter; import com.google.common.collect.ImmutableList; +import com.powsybl.commons.datasource.*; import com.powsybl.commons.test.AbstractSerDeTest; import com.powsybl.commons.PowsyblException; -import com.powsybl.commons.datasource.MemDataSource; -import com.powsybl.commons.datasource.ReadOnlyDataSource; -import com.powsybl.commons.datasource.ResourceDataSource; -import com.powsybl.commons.datasource.ResourceSet; import com.powsybl.iidm.network.*; import org.apache.commons.io.FilenameUtils; import org.junit.jupiter.api.Test; @@ -104,6 +101,7 @@ void testExporter() { assertEquals(2, exporter.getParameters().size()); } + @Test void testCouplerToXnodeImport() throws IOException { Network network = loadNetworkFromResourceFile("/couplerToXnodeExample.uct"); diff --git a/ucte/ucte-converter/src/test/resources/network.xiidm b/ucte/ucte-converter/src/test/resources/network.xiidm new file mode 100644 index 00000000000..e2bdd7e4f8c --- /dev/null +++ b/ucte/ucte-converter/src/test/resources/network.xiidm @@ -0,0 +1,42 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file From 4d9e59d6c08459715a8e20767ecd692b8fde26fe Mon Sep 17 00:00:00 2001 From: Leclerc Clement Date: Wed, 23 Oct 2024 15:18:21 +0200 Subject: [PATCH 02/38] add unique NodeId Signed-off-by: Leclerc Clement --- .../ucte/converter/DefaultNamingStrategy.java | 193 ++++++------------ .../ucte/converter/NamingStrategyTest.java | 2 +- .../resources/{network.xiidm => network.xml} | 0 .../src/test/resources/network2.xml | 52 +++++ .../powsybl/ucte/network/UcteElementId.java | 3 +- .../powsybl/ucte/network/UcteNodeCode.java | 16 ++ 6 files changed, 135 insertions(+), 131 deletions(-) rename ucte/ucte-converter/src/test/resources/{network.xiidm => network.xml} (100%) create mode 100644 ucte/ucte-converter/src/test/resources/network2.xml diff --git a/ucte/ucte-converter/src/main/java/com/powsybl/ucte/converter/DefaultNamingStrategy.java b/ucte/ucte-converter/src/main/java/com/powsybl/ucte/converter/DefaultNamingStrategy.java index b88f7e7de94..3edb600a745 100644 --- a/ucte/ucte-converter/src/main/java/com/powsybl/ucte/converter/DefaultNamingStrategy.java +++ b/ucte/ucte-converter/src/main/java/com/powsybl/ucte/converter/DefaultNamingStrategy.java @@ -18,7 +18,6 @@ import java.util.*; import java.util.stream.StreamSupport; - /** * A {@link NamingStrategy} implementation that ensures the conformity of IDs with the UCTE-DEF format * @@ -35,10 +34,8 @@ public String getName() { return "Default"; } - /** * Initializes the network by converting network elements names to UCTE format if needed. - * * @param network The network to check */ @Override @@ -61,17 +58,14 @@ public void init(Network network) { * @param network The network whose elements need to be converted to UCTE format */ private void convertToUcte(Network network) { - - // Process all substations and their buses - // For each substation, get country name and convert all bus IDs to UCTE format - network.getSubstationStream() - .forEach(substation -> { - String countryCode = substation.getCountry() - .map(Country::getName) + network.getVoltageLevelStream() + .forEach(voltageLevel -> { + // Get country code from substation if exists, else use default + String countryCode = voltageLevel.getSubstation() + .map(s -> s.getCountry().map(Country::getName).orElse("XX")) .orElse("XX"); - substation.getVoltageLevelStream() - .flatMap(vl -> StreamSupport.stream(vl.getBusBreakerView().getBuses().spliterator(), false)) + StreamSupport.stream(voltageLevel.getBusBreakerView().getBuses().spliterator(), false) .forEach(bus -> { generateUcteNodeId(countryCode, bus); System.out.println("BUS : " + bus.getId() + "--> " + ucteNodeIds.get(bus.getId())); @@ -91,142 +85,84 @@ private void convertToUcte(Network network) { generateUcteElementId(transformer); System.out.println("TwoWindingsTrans : " + transformer.getId() + "--> " + ucteElementIds.get(transformer.getId())); }); - } - /** - * Generates a UCTE node identifier for a given bus and stores it in the ucteNodeIds map. - * The UCTE node format follows the pattern: C_NNNNN_V where: - * - length : 8 - * - char[0] : country code - * - char[1-5]: First 5 characters of bus ID (padded with '_' if shorter) - * - char[6] : Voltage level code followed by - * - char[7] : letter or figure for differentiating bus bars (optional) -> actually replace by '_' - * - * @param country The country name of the bus's substation - * @param bus The bus for which to generate the UCTE node ID - * @throws UcteException if the generated ID is not a valid UCTE node identifier - */ private void generateUcteNodeId(String country, Bus bus) { - String busId = bus.getId(); + String id = bus.getId(); + if (ucteElementIds.containsKey(id)) {return;} + char countryCode = getCountryCode(country); + char voltageLevelCode = getVoltageLevelCode(bus.getVoltageLevel().getNominalV()); + String formattedId = formatUniqueId(id); + UcteNodeCode nodeCode = UcteNodeCode.convertToUcteNodeId(countryCode, voltageLevelCode, formattedId).get(); + ucteNodeIds.put(id, nodeCode); + } - // Skip if this bus ID has already been processed - if (ucteNodeIds.containsKey(busId)) { - return; + private String formatUniqueId(String id) { + /* + id = "NHV1_NHV2_1_1_BUS"; + 1. parts = ["NHV1", "NHV2", "1", "1", "BUS"] + 2. unique = "NH" (2 first char) + 3. numbers = "1121" -> unique = "NH1" (last number) + 4. lastPart = "BUS" -> unique = "NH1BU" (2 first char) + 5. length = 5, else complete with _ + result= "NH1BU" + */ + String[] parts = id.split("_"); + StringBuilder unique = new StringBuilder(); + if (parts.length > 1) { + unique.append(parts[0].substring(0, 2)); + String numbers = id.replaceAll("[^0-9]", ""); + if (!numbers.isEmpty()) { + unique.append(numbers.charAt(numbers.length() - 1)); + } + String lastPart = parts[parts.length - 1].replaceAll("[0-9]", ""); + if (lastPart.length() > 0) { + unique.append(lastPart.substring(0, Math.min(2, lastPart.length()))); + } + } else { + unique.append(id.substring(0, Math.min(5, id.length()))); + } + while (unique.length() < 5) { + unique.append('_'); } - // Initialize StringBuilder with fixed capacity of 8 for UCTE format - StringBuilder nameBuilder = new StringBuilder(8); - // get ucte country code with the country name - nameBuilder.append(getCountryCode(country)); - // Format bus ID to fill chars[1-5] - String fomatedId = busId.length() >= 5 - ? busId.substring(0, 5) - : String.format("%-5s", busId).replace(' ', '_'); - nameBuilder.append(fomatedId); - // Add voltage level code (char[6]) and trailing underscore (char[7]) - nameBuilder.append(getVoltageLevelCode(bus.getVoltageLevel().getNominalV())).append('_'); - String name = nameBuilder.toString(); - // Validate and store the generated UCTE node code - UcteNodeCode nodeCode = UcteNodeCode.parseUcteNodeCode(name) - .orElseThrow(() -> new UcteException("Invalid UCTE node identifier: " + name)); - ucteNodeIds.put(busId, nodeCode); + return unique.substring(0, 5); } - /** - * Generates a UCTE element identifier for a given two-winding transformer and stores it in the ucteElementIds map. - * The UCTE element format is constructed from the two connected buses: AAAAAAAA_BBBBBBBB_N where: - * - AAAAAAAA: UCTE node ID of the first bus (8 chars) - * - BBBBBBBB: UCTE node ID of the second bus (8 chars) - * - N: Order code (actually 1 for transformers) - * The original ID format is: busId1_busId2 - * - * @param transformer The two-winding transformer for which to generate the UCTE element ID - * @throws UcteException if the generated ID is not a valid UCTE element identifier - */ - private void generateUcteElementId(TwoWindingsTransformer transformer) { - - // Get bus IDs from both terminals of the transformer - String busId1 = transformer.getTerminal1().getBusBreakerView().getBus().getId(); - String busId2 = transformer.getTerminal2().getBusBreakerView().getBus().getId(); - - // Create original ID by concatenating both bus IDs with underscore - String originalId = new StringBuilder(busId1.length() + busId2.length() + 1) - .append(busId1) - .append('_') - .append(busId2) - .toString(); - - // Skip if this transformer ID has already been processed - if (ucteElementIds.containsKey(originalId)) {return;} - - // Create UCTE element ID using previously converted UCTE node IDs - // '1' is used as order code for transformers - UcteElementId elementId = new UcteElementId(ucteNodeIds.get(busId1), ucteNodeIds.get(busId2), '1'); - - // Validate and store the generated UCTE element ID - ucteElementIds.computeIfAbsent(originalId, k -> UcteElementId.parseUcteElementId(elementId.toString()).orElseThrow(() -> new UcteException("Invalid UCTE node identifier: " + k))); + private void generateUcteElementId(String originalId, UcteNodeCode node1, UcteNodeCode node2, char orderCode) { + UcteElementId ucteElementId = new UcteElementId(node1, node2, orderCode); + ucteElementIds.put(originalId, ucteElementId); + } + private void generateUcteElementId(TwoWindingsTransformer transformer) { + String transformerId = transformer.getId(); + if (ucteElementIds.containsKey(transformerId)) { + return; + } + UcteNodeCode node1 = ucteNodeIds.get(transformer.getTerminal1().getBusBreakerView().getBus().getId()); + UcteNodeCode node2 = ucteNodeIds.get(transformer.getTerminal2().getBusBreakerView().getBus().getId()); + generateUcteElementId(transformerId, node1, node2, '1'); } - /** - * Generates a UCTE element identifier for a given line and stores it in the ucteElementIds map. - * The UCTE element format is constructed from the two connected buses: AAAAAAAA_BBBBBBBB_N where: - * - AAAAAAAA: UCTE node ID of the first bus (8 chars) - * - BBBBBBBB: UCTE node ID of the second bus (8 chars) - * - N: Order code - * The original ID format is: busId1_busId2_orderCode - * - * @param line The two-winding transformer for which to generate the UCTE element ID - * @throws UcteException if the generated ID is not a valid UCTE element identifier - */ private void generateUcteElementId(Line line) { - - // Get bus IDs from both terminals of the line ang get the orderCode (last char of the id) - String busId1 = line.getTerminal1().getBusBreakerView().getBus().getId(); - String busId2 = line.getTerminal2().getBusBreakerView().getBus().getId(); - char orderCode = line.getId().charAt(line.getId().length() - 1); - - // Create original ID by concatenating both bus IDs and orderCode with underscore - String originalId = new StringBuilder(busId1.length() + busId2.length() + 1) - .append(busId1) - .append('_') - .append(busId2) - .append('_') - .append(orderCode) - .toString(); - + String lineId = line.getId(); // Skip if this line ID has already been processed - if (ucteElementIds.containsKey(originalId)) {return;} - // Create UCTE element ID using previously converted UCTE node IDs - UcteElementId elementId = new UcteElementId(ucteNodeIds.get(busId1), ucteNodeIds.get(busId2), orderCode); - // Validate and store the generated UCTE element ID - ucteElementIds.computeIfAbsent(originalId, k -> UcteElementId.parseUcteElementId(elementId.toString()).orElseThrow(() -> new UcteException("Invalid UCTE node identifier: " + k))); - + if (ucteElementIds.containsKey(lineId)) {return;} + UcteNodeCode node1 = ucteNodeIds.get(line.getTerminal1().getBusBreakerView().getBus().getId()); + UcteNodeCode node2 = ucteNodeIds.get(line.getTerminal2().getBusBreakerView().getBus().getId()); + char orderCode = line.getId().charAt(line.getId().length() - 1); + generateUcteElementId(lineId,node1, node2, orderCode); } - /** - * Retrieves the UCTE single-character country code from a full country name. - * Searches through UcteCountryCode enum to find a matching country name (case-insensitive). - * - * @param code The full country name to convert - * @return The single character UCTE country code, or 'X' if country is not found - */ + private static char getCountryCode(String code) { - for(UcteCountryCode countryCode : UcteCountryCode.values()) { - if(code.equalsIgnoreCase(countryCode.getPrettyName())) { + for (UcteCountryCode countryCode : UcteCountryCode.values()) { + if (code.equalsIgnoreCase(countryCode.getPrettyName())) { return countryCode.getUcteCode(); } } return 'X'; } - /** - * Retrieves the UCTE voltage level code based on a nominal voltage value. - * - * @param voltage The nominal voltage value in kV - * @return A character representing the UCTE voltage level code ('0' to 'N') - * @throws IllegalArgumentException if no matching voltage level code is found - */ public static char getVoltageLevelCode(double voltage) { for (UcteVoltageLevelCode code : UcteVoltageLevelCode.values()) { if (code.getVoltageLevel() == (int) voltage) { @@ -270,5 +206,4 @@ public UcteElementId getUcteElementId(Branch branch) { public UcteElementId getUcteElementId(DanglingLine danglingLine) { return getUcteElementId(danglingLine.getId()); } - -} +} \ No newline at end of file diff --git a/ucte/ucte-converter/src/test/java/com/powsybl/ucte/converter/NamingStrategyTest.java b/ucte/ucte-converter/src/test/java/com/powsybl/ucte/converter/NamingStrategyTest.java index f4815515f09..03770d6e171 100644 --- a/ucte/ucte-converter/src/test/java/com/powsybl/ucte/converter/NamingStrategyTest.java +++ b/ucte/ucte-converter/src/test/java/com/powsybl/ucte/converter/NamingStrategyTest.java @@ -29,7 +29,7 @@ class NamingStrategyTest { @Test void initTest() { - ResourceDataSource dataSource = new ResourceDataSource("network", new ResourceSet("/", "network.xiidm")); + ResourceDataSource dataSource = new ResourceDataSource("network", new ResourceSet("/", "network.xml")); Network n1 = Network.read(dataSource); NamingStrategy s = new DefaultNamingStrategy(); s.init(n1); diff --git a/ucte/ucte-converter/src/test/resources/network.xiidm b/ucte/ucte-converter/src/test/resources/network.xml similarity index 100% rename from ucte/ucte-converter/src/test/resources/network.xiidm rename to ucte/ucte-converter/src/test/resources/network.xml diff --git a/ucte/ucte-converter/src/test/resources/network2.xml b/ucte/ucte-converter/src/test/resources/network2.xml new file mode 100644 index 00000000000..32df3266376 --- /dev/null +++ b/ucte/ucte-converter/src/test/resources/network2.xml @@ -0,0 +1,52 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/ucte/ucte-network/src/main/java/com/powsybl/ucte/network/UcteElementId.java b/ucte/ucte-network/src/main/java/com/powsybl/ucte/network/UcteElementId.java index 852af51fb65..ee9e8eb048a 100644 --- a/ucte/ucte-network/src/main/java/com/powsybl/ucte/network/UcteElementId.java +++ b/ucte/ucte-network/src/main/java/com/powsybl/ucte/network/UcteElementId.java @@ -16,7 +16,7 @@ public class UcteElementId implements Comparable { private static final List ORDER_CODES = Arrays.asList('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', - 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '_', '-', '.', ' '); + 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '_', '-', '.', ' '); private final UcteNodeCode nodeCode1; private final UcteNodeCode nodeCode2; @@ -110,6 +110,7 @@ public static boolean isUcteElementId(String id) { isOrderCode(id.charAt(18)); } + private static boolean isOrderCode(char orderCode) { /* Update to match modification on UCTE format diff --git a/ucte/ucte-network/src/main/java/com/powsybl/ucte/network/UcteNodeCode.java b/ucte/ucte-network/src/main/java/com/powsybl/ucte/network/UcteNodeCode.java index 8bc456fd47f..2b9529a2ed5 100644 --- a/ucte/ucte-network/src/main/java/com/powsybl/ucte/network/UcteNodeCode.java +++ b/ucte/ucte-network/src/main/java/com/powsybl/ucte/network/UcteNodeCode.java @@ -116,6 +116,22 @@ public static Optional parseUcteNodeCode(String id) { return Optional.ofNullable(ucteNodeCode); } + public static Optional convertToUcteNodeId(char countryCode, char voltageLevelCode, String id){ + // Initialize StringBuilder with fixed capacity of 8 for UCTE format + StringBuilder nameBuilder = new StringBuilder(8); + // get ucte country code with the country name + nameBuilder.append(countryCode); + // Format bus ID to fill chars[1-5] + String fomatedId = id.length() >= 5 + ? id.substring(0, 5) + : String.format("%-5s", id).replace(' ', '_'); + nameBuilder.append(fomatedId); + // Add voltage level code (char[6]) and trailing underscore (char[7]) + nameBuilder.append(voltageLevelCode).append('_'); + + return parseUcteNodeCode(nameBuilder.toString()); + } + public static boolean isUcteNodeId(String id) { return id != null && id.length() == 8 && From 7a1e787d819cfd8a858a47927a5540911b44022f Mon Sep 17 00:00:00 2001 From: Leclerc Clement Date: Wed, 23 Oct 2024 15:50:55 +0200 Subject: [PATCH 03/38] add unique NodeId Signed-off-by: Leclerc Clement --- .../src/main/java/com/powsybl/ucte/network/UcteElementId.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/ucte/ucte-network/src/main/java/com/powsybl/ucte/network/UcteElementId.java b/ucte/ucte-network/src/main/java/com/powsybl/ucte/network/UcteElementId.java index ee9e8eb048a..852af51fb65 100644 --- a/ucte/ucte-network/src/main/java/com/powsybl/ucte/network/UcteElementId.java +++ b/ucte/ucte-network/src/main/java/com/powsybl/ucte/network/UcteElementId.java @@ -16,7 +16,7 @@ public class UcteElementId implements Comparable { private static final List ORDER_CODES = Arrays.asList('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', - 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '_', '-', '.', ' '); + 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '_', '-', '.', ' '); private final UcteNodeCode nodeCode1; private final UcteNodeCode nodeCode2; @@ -110,7 +110,6 @@ public static boolean isUcteElementId(String id) { isOrderCode(id.charAt(18)); } - private static boolean isOrderCode(char orderCode) { /* Update to match modification on UCTE format From 0d55a0b7deb7257aeabc6b2bc037027d02a6773a Mon Sep 17 00:00:00 2001 From: Leclerc Clement Date: Wed, 23 Oct 2024 15:54:33 +0200 Subject: [PATCH 04/38] add unique NodeId Signed-off-by: Leclerc Clement --- .../java/com/powsybl/ucte/converter/NamingStrategyTest.java | 2 +- .../java/com/powsybl/ucte/converter/UcteExporterTest.java | 6 ++++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/ucte/ucte-converter/src/test/java/com/powsybl/ucte/converter/NamingStrategyTest.java b/ucte/ucte-converter/src/test/java/com/powsybl/ucte/converter/NamingStrategyTest.java index 03770d6e171..ddfcfa86bf6 100644 --- a/ucte/ucte-converter/src/test/java/com/powsybl/ucte/converter/NamingStrategyTest.java +++ b/ucte/ucte-converter/src/test/java/com/powsybl/ucte/converter/NamingStrategyTest.java @@ -29,7 +29,7 @@ class NamingStrategyTest { @Test void initTest() { - ResourceDataSource dataSource = new ResourceDataSource("network", new ResourceSet("/", "network.xml")); + ResourceDataSource dataSource = new ResourceDataSource("network2", new ResourceSet("/", "network2.xml")); Network n1 = Network.read(dataSource); NamingStrategy s = new DefaultNamingStrategy(); s.init(n1); diff --git a/ucte/ucte-converter/src/test/java/com/powsybl/ucte/converter/UcteExporterTest.java b/ucte/ucte-converter/src/test/java/com/powsybl/ucte/converter/UcteExporterTest.java index 6e2a2a42e26..c8d21f65c45 100644 --- a/ucte/ucte-converter/src/test/java/com/powsybl/ucte/converter/UcteExporterTest.java +++ b/ucte/ucte-converter/src/test/java/com/powsybl/ucte/converter/UcteExporterTest.java @@ -8,9 +8,12 @@ package com.powsybl.ucte.converter; import com.google.common.collect.ImmutableList; -import com.powsybl.commons.datasource.*; import com.powsybl.commons.test.AbstractSerDeTest; import com.powsybl.commons.PowsyblException; +import com.powsybl.commons.datasource.MemDataSource; +import com.powsybl.commons.datasource.ReadOnlyDataSource; +import com.powsybl.commons.datasource.ResourceDataSource; +import com.powsybl.commons.datasource.ResourceSet; import com.powsybl.iidm.network.*; import org.apache.commons.io.FilenameUtils; import org.junit.jupiter.api.Test; @@ -101,7 +104,6 @@ void testExporter() { assertEquals(2, exporter.getParameters().size()); } - @Test void testCouplerToXnodeImport() throws IOException { Network network = loadNetworkFromResourceFile("/couplerToXnodeExample.uct"); From d5d96136c74d800f451dccf506cddd3b48b30582 Mon Sep 17 00:00:00 2001 From: Leclerc Clement Date: Mon, 28 Oct 2024 10:31:15 +0100 Subject: [PATCH 05/38] change getVoltageLevelCode method Signed-off-by: Leclerc Clement --- .../ucte/converter/DefaultNamingStrategy.java | 12 +- .../ucte/converter/NamingStrategyTest.java | 2 +- .../src/test/resources/network3.xiidm | 1267 +++++++++++++++++ 3 files changed, 1279 insertions(+), 2 deletions(-) create mode 100644 ucte/ucte-converter/src/test/resources/network3.xiidm diff --git a/ucte/ucte-converter/src/main/java/com/powsybl/ucte/converter/DefaultNamingStrategy.java b/ucte/ucte-converter/src/main/java/com/powsybl/ucte/converter/DefaultNamingStrategy.java index 3edb600a745..e6a2d92f61d 100644 --- a/ucte/ucte-converter/src/main/java/com/powsybl/ucte/converter/DefaultNamingStrategy.java +++ b/ucte/ucte-converter/src/main/java/com/powsybl/ucte/converter/DefaultNamingStrategy.java @@ -163,9 +163,19 @@ private static char getCountryCode(String code) { return 'X'; } + + public static char getVoltageLevelCode(double voltage) { + //If voltage level neither than min UCTEVoltageLevel + if((int)voltage < UcteVoltageLevelCode.VL_27.getVoltageLevel()){ + return '7'; + } + //If voltage level gather than min UCTEVoltageLevel + if((int)voltage > UcteVoltageLevelCode.VL_750.getVoltageLevel()){ + return '0'; + } for (UcteVoltageLevelCode code : UcteVoltageLevelCode.values()) { - if (code.getVoltageLevel() == (int) voltage) { + if ((int) voltage >= code.getVoltageLevel()-10 && (int) voltage <= code.getVoltageLevel()+10) { return (char) ('0' + code.ordinal()); } } diff --git a/ucte/ucte-converter/src/test/java/com/powsybl/ucte/converter/NamingStrategyTest.java b/ucte/ucte-converter/src/test/java/com/powsybl/ucte/converter/NamingStrategyTest.java index ddfcfa86bf6..27d55bfa80d 100644 --- a/ucte/ucte-converter/src/test/java/com/powsybl/ucte/converter/NamingStrategyTest.java +++ b/ucte/ucte-converter/src/test/java/com/powsybl/ucte/converter/NamingStrategyTest.java @@ -29,7 +29,7 @@ class NamingStrategyTest { @Test void initTest() { - ResourceDataSource dataSource = new ResourceDataSource("network2", new ResourceSet("/", "network2.xml")); + ResourceDataSource dataSource = new ResourceDataSource("network3", new ResourceSet("/", "network3.xiidm")); Network n1 = Network.read(dataSource); NamingStrategy s = new DefaultNamingStrategy(); s.init(n1); diff --git a/ucte/ucte-converter/src/test/resources/network3.xiidm b/ucte/ucte-converter/src/test/resources/network3.xiidm new file mode 100644 index 00000000000..439c8cb9c29 --- /dev/null +++ b/ucte/ucte-converter/src/test/resources/network3.xiidm @@ -0,0 +1,1267 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + From 97b572a5d33d6780f837543dcfa59edd35decf0c Mon Sep 17 00:00:00 2001 From: Leclerc Clement Date: Tue, 29 Oct 2024 11:08:08 +0100 Subject: [PATCH 06/38] optimisation of checking elements Signed-off-by: Leclerc Clement --- .../ucte/converter/DefaultNamingStrategy.java | 295 +++++++++++------- .../ucte/converter/NamingStrategy.java | 2 +- .../ucte/converter/NamingStrategyTest.java | 5 +- .../src/test/resources/network4.xiidm | 147 +++++++++ .../powsybl/ucte/network/UcteNodeCode.java | 7 +- 5 files changed, 344 insertions(+), 112 deletions(-) create mode 100644 ucte/ucte-converter/src/test/resources/network4.xiidm diff --git a/ucte/ucte-converter/src/main/java/com/powsybl/ucte/converter/DefaultNamingStrategy.java b/ucte/ucte-converter/src/main/java/com/powsybl/ucte/converter/DefaultNamingStrategy.java index e6a2d92f61d..06a315d434f 100644 --- a/ucte/ucte-converter/src/main/java/com/powsybl/ucte/converter/DefaultNamingStrategy.java +++ b/ucte/ucte-converter/src/main/java/com/powsybl/ucte/converter/DefaultNamingStrategy.java @@ -16,7 +16,6 @@ import com.powsybl.ucte.network.UcteVoltageLevelCode; import java.util.*; -import java.util.stream.StreamSupport; /** * A {@link NamingStrategy} implementation that ensures the conformity of IDs with the UCTE-DEF format @@ -28,6 +27,7 @@ public class DefaultNamingStrategy implements NamingStrategy { private final Map ucteNodeIds = new HashMap<>(); private final Map ucteElementIds = new HashMap<>(); + private int namingCounter = 0; @Override public String getName() { @@ -39,147 +39,230 @@ public String getName() { * @param network The network to check */ @Override - public void init(Network network) { + public void networkInitialisation(Network network) { //add condition to test network is already ucte format - convertToUcte(network); + convertNetworkToUcte(network); } +/** + * Converts all network elements to UCTE format using a sequential naming scheme. + * The conversion process follows these steps: + *
    + *
  1. Reset the global naming counter to ensure unique identifiers
  2. + *
  3. Convert all elements within each voltage level + *
  4. Convert network-level elements + *
+ * + * Each element receives a unique sequential identifier during the conversion process. + * + * @param network the network to convert + */ + private void convertNetworkToUcte(Network network) { + //count for naming each element + namingCounter = 0; + // Convert voltage level elements + network.getVoltageLevelStream().forEach(voltageLevel -> + convertVoltageLevelElementsIdToUcte(voltageLevel)); + // Convert network elements + convertNetworkElementsIdToUcte(network); + } + + /** - * Converts all network elements IDs to UCTE format. This includes: - * - Converting bus IDs using country codes from their substations - * - Converting line IDs - * - Converting two-winding transformer IDs - * The conversion process: - * 1. For each substation, retrieves its country code (defaults to "XX" if not found) - * 2. Processes all buses within the substation's voltage levels - * 3. Processes all lines in the network - * 4. Processes all two-winding transformers + * Converts all voltageLevel elements to UCTE format using a sequential naming scheme. + * The conversion process follows these steps: + *
    + *
  1. Convert all elements within each voltage level (buses, generators, loads, shunts)
  2. + *
* - * @param network The network whose elements need to be converted to UCTE format + * Each element receives a unique sequential identifier during the conversion process. + * + * @param voltageLevel the voltageLevel to convert */ - private void convertToUcte(Network network) { - network.getVoltageLevelStream() - .forEach(voltageLevel -> { - // Get country code from substation if exists, else use default - String countryCode = voltageLevel.getSubstation() - .map(s -> s.getCountry().map(Country::getName).orElse("XX")) - .orElse("XX"); - - StreamSupport.stream(voltageLevel.getBusBreakerView().getBuses().spliterator(), false) - .forEach(bus -> { - generateUcteNodeId(countryCode, bus); - System.out.println("BUS : " + bus.getId() + "--> " + ucteNodeIds.get(bus.getId())); - }); - }); + private void convertVoltageLevelElementsIdToUcte(VoltageLevel voltageLevel) { + System.out.println(voltageLevel.getId()); - // Convert all line IDs to UCTE format - network.getLineStream() - .forEach(line -> { - generateUcteElementId(line); - System.out.println("LINE : " + line.getId() + "--> " + ucteElementIds.get(line.getId())); - }); + // Bus + voltageLevel.getBusBreakerView().getBusStream().forEach(bus -> { + UcteNodeCode ucteId = generateUcteNodeId(bus.getId(), voltageLevel, namingCounter++); + if (ucteId != null) { + bus.setName(ucteId.toString()); + logConversion("BUS", bus); + } + }); - // Convert all two-winding transformer IDs to UCTE format - network.getTwoWindingsTransformerStream() - .forEach(transformer -> { - generateUcteElementId(transformer); - System.out.println("TwoWindingsTrans : " + transformer.getId() + "--> " + ucteElementIds.get(transformer.getId())); + // Injection (generators, loads, shunts) + voltageLevel.getConnectableStream() + .filter(Injection.class::isInstance) + .map(Injection.class::cast) + .forEach(injection -> { + UcteNodeCode ucteId = generateUcteNodeId(injection.getId(), voltageLevel, namingCounter++); + injection.setName(ucteId.toString()); + logConversion(injection.getType().name(), injection); }); + } - private void generateUcteNodeId(String country, Bus bus) { - String id = bus.getId(); - if (ucteElementIds.containsKey(id)) {return;} - char countryCode = getCountryCode(country); - char voltageLevelCode = getVoltageLevelCode(bus.getVoltageLevel().getNominalV()); - String formattedId = formatUniqueId(id); - UcteNodeCode nodeCode = UcteNodeCode.convertToUcteNodeId(countryCode, voltageLevelCode, formattedId).get(); - ucteNodeIds.put(id, nodeCode); - } - - private String formatUniqueId(String id) { - /* - id = "NHV1_NHV2_1_1_BUS"; - 1. parts = ["NHV1", "NHV2", "1", "1", "BUS"] - 2. unique = "NH" (2 first char) - 3. numbers = "1121" -> unique = "NH1" (last number) - 4. lastPart = "BUS" -> unique = "NH1BU" (2 first char) - 5. length = 5, else complete with _ - result= "NH1BU" - */ - String[] parts = id.split("_"); - StringBuilder unique = new StringBuilder(); - if (parts.length > 1) { - unique.append(parts[0].substring(0, 2)); - String numbers = id.replaceAll("[^0-9]", ""); - if (!numbers.isEmpty()) { - unique.append(numbers.charAt(numbers.length() - 1)); - } - String lastPart = parts[parts.length - 1].replaceAll("[0-9]", ""); - if (lastPart.length() > 0) { - unique.append(lastPart.substring(0, Math.min(2, lastPart.length()))); - } - } else { - unique.append(id.substring(0, Math.min(5, id.length()))); - } - while (unique.length() < 5) { - unique.append('_'); - } - return unique.substring(0, 5); + /** + * Converts all network-level elements to UCTE format using a sequential naming scheme. + * The conversion process follows these steps: + *
    + *
  1. Convert network-level elements (lines, transformers)
  2. + *
+ * + * Each element receives a unique sequential identifier during the conversion process. + * + * @param network the network to convert + */ + private void convertNetworkElementsIdToUcte(Network network) { + network.getBranchStream().forEach(branch -> { + UcteElementId ucteId = generateUcteElementId(branch); + branch.setName(ucteId.toString()); + logConversion(branch.getType().name(), branch); + }); } - private void generateUcteElementId(String originalId, UcteNodeCode node1, UcteNodeCode node2, char orderCode) { - UcteElementId ucteElementId = new UcteElementId(node1, node2, orderCode); - ucteElementIds.put(originalId, ucteElementId); + private void logConversion(String elementType, Identifiable element) { + System.out.println(String.format("%s : %s--> %s", + elementType, + element.getId(), + element.getNameOrId())); } - private void generateUcteElementId(TwoWindingsTransformer transformer) { - String transformerId = transformer.getId(); - if (ucteElementIds.containsKey(transformerId)) { - return; + /** + * Generates a UCTE node code for a network element. + * The generation process involves: + *
    + *
  1. Checking for existing node ID to avoid duplicates
  2. + *
  3. Extracting country code from the voltage level
  4. + *
  5. Determining voltage level code based on nominal voltage
  6. + *
  7. Creating a new node ID using the first alphanumeric character of voltage level ID and counter
  8. + *
+ * + * The generated UCTE node code follows the format: [CountryCode][NodeId][VoltageLevelCode] + * + * @param nodeId original identifier of the network element + * @param voltageLevel voltage level containing the element + * @param counter sequential number to ensure unique identification + * @return generated UcteNodeCode, or null if nodeId already exists in ucteNodeIds + */ + private UcteNodeCode generateUcteNodeId(String nodeId, VoltageLevel voltageLevel, int counter) { + String busId = nodeId; + if (ucteNodeIds.containsKey(busId)) { + return null; } - UcteNodeCode node1 = ucteNodeIds.get(transformer.getTerminal1().getBusBreakerView().getBus().getId()); - UcteNodeCode node2 = ucteNodeIds.get(transformer.getTerminal2().getBusBreakerView().getBus().getId()); - generateUcteElementId(transformerId, node1, node2, '1'); + char countryCode = getCountryCode(voltageLevel); + char voltageLevelCode = getUcteVoltageLevelCode(voltageLevel.getNominalV()); + String newNodeId = voltageLevel.getId() + .replaceAll("[^A-Z0-9]", "") + .substring(0,1) + + counter; + + UcteNodeCode nodeCode = UcteNodeCode.convertToUcteNodeId(countryCode, voltageLevelCode, newNodeId) + .orElseThrow(() -> new IllegalStateException("Invalid UCTE node code generated for bus " + busId)); + + ucteNodeIds.put(busId, nodeCode); + return nodeCode; } - private void generateUcteElementId(Line line) { - String lineId = line.getId(); - // Skip if this line ID has already been processed - if (ucteElementIds.containsKey(lineId)) {return;} - UcteNodeCode node1 = ucteNodeIds.get(line.getTerminal1().getBusBreakerView().getBus().getId()); - UcteNodeCode node2 = ucteNodeIds.get(line.getTerminal2().getBusBreakerView().getBus().getId()); - char orderCode = line.getId().charAt(line.getId().length() - 1); - generateUcteElementId(lineId,node1, node2, orderCode); + + /** + * Generates a UCTE element identifier for a network branch element (line or transformer). + * The element ID is composed of two node codes and an order code. + * + *

The generated ID follows UCTE format specifications: + * [Node1Code][Node2Code][OrderCode] + * where: + *

    + *
  • Node1Code: UCTE code of the first terminal
  • + *
  • Node2Code: UCTE code of the second terminal
  • + *
  • OrderCode: Single character identifier
  • + *
+ * + * @param branch the element instance of branch + * @return the generated UCTE element identifier + */ + private UcteElementId generateUcteElementId(Branch branch) { + + String branchId = branch.getId(); + if (ucteElementIds.containsKey(branchId)) { + return null; + } + + UcteNodeCode node1 = ucteNodeIds.get(branch.getTerminal1().getBusBreakerView().getBus().getId()); + UcteNodeCode node2 = ucteNodeIds.get(branch.getTerminal2().getBusBreakerView().getBus().getId()); + + char orderCode = branch instanceof TwoWindingsTransformer ? + '1' : + branch.getId().charAt(branch.getId().length() - 1); + + UcteElementId ucteElementId = new UcteElementId(node1, node2, orderCode); + return ucteElementId; } + /** + * Extracts and converts a country code to UCTE format for a given voltage level. + * + *

The country code is determined through the following process: + *

    + *
  1. Attempts to get the country from the voltage level's substation
  2. + *
  3. Maps the country name to its corresponding UCTE code
  4. + *
  5. Returns 'X' as fallback if no valid mapping is found
  6. + *
+ * + * @param voltageLevel the voltage level from which to extract the country code + * @return the UCTE country code character, or 'X' if no valid country is found) + */ + private static char getCountryCode(VoltageLevel voltageLevel) { + + String country = voltageLevel.getSubstation() + .map(s->s.getCountry() + .map(Country::getName) + .orElse("XX")) + .orElse("XX"); - private static char getCountryCode(String code) { for (UcteCountryCode countryCode : UcteCountryCode.values()) { - if (code.equalsIgnoreCase(countryCode.getPrettyName())) { + if (country.equalsIgnoreCase(countryCode.getPrettyName())) { return countryCode.getUcteCode(); } } return 'X'; } + /** + * Determines the UCTE voltage level code for a given voltage value. + * The code is determined by finding the closest standard UCTE voltage level. + * + *

The method follows these rules: + *

    + *
  • For voltage < 27kV: returns '7'
  • + *
  • For voltage > 750kV: returns '0'
  • + *
  • For other voltages: returns code of the closest standard UCTE level
  • + *
+ * + * @param voltage the nominal voltage value in kV + * @return a character representing the UCTE voltage level code + */ + private static char getUcteVoltageLevelCode(double voltage) { - - public static char getVoltageLevelCode(double voltage) { - //If voltage level neither than min UCTEVoltageLevel - if((int)voltage < UcteVoltageLevelCode.VL_27.getVoltageLevel()){ + if (voltage < UcteVoltageLevelCode.VL_27.getVoltageLevel()) { return '7'; } - //If voltage level gather than min UCTEVoltageLevel - if((int)voltage > UcteVoltageLevelCode.VL_750.getVoltageLevel()){ + if (voltage > UcteVoltageLevelCode.VL_750.getVoltageLevel()) { return '0'; } + + UcteVoltageLevelCode closest = null; + double minDiff = Double.MAX_VALUE; + for (UcteVoltageLevelCode code : UcteVoltageLevelCode.values()) { - if ((int) voltage >= code.getVoltageLevel()-10 && (int) voltage <= code.getVoltageLevel()+10) { - return (char) ('0' + code.ordinal()); + double diff = Math.abs(voltage - code.getVoltageLevel()); + if (diff < minDiff) { + minDiff = diff; + closest = code; } } - throw new IllegalArgumentException("No voltage level code found for " + voltage + " kV"); + return closest != null ? (char) ('0' + closest.ordinal()) : '7'; } @Override diff --git a/ucte/ucte-converter/src/main/java/com/powsybl/ucte/converter/NamingStrategy.java b/ucte/ucte-converter/src/main/java/com/powsybl/ucte/converter/NamingStrategy.java index 5c6fddf784e..a41c81ecb60 100644 --- a/ucte/ucte-converter/src/main/java/com/powsybl/ucte/converter/NamingStrategy.java +++ b/ucte/ucte-converter/src/main/java/com/powsybl/ucte/converter/NamingStrategy.java @@ -19,7 +19,7 @@ public interface NamingStrategy { String getName(); - void init(Network n); + void networkInitialisation(Network n); UcteNodeCode getUcteNodeCode(String id); diff --git a/ucte/ucte-converter/src/test/java/com/powsybl/ucte/converter/NamingStrategyTest.java b/ucte/ucte-converter/src/test/java/com/powsybl/ucte/converter/NamingStrategyTest.java index 27d55bfa80d..6b7cd1a677e 100644 --- a/ucte/ucte-converter/src/test/java/com/powsybl/ucte/converter/NamingStrategyTest.java +++ b/ucte/ucte-converter/src/test/java/com/powsybl/ucte/converter/NamingStrategyTest.java @@ -8,6 +8,7 @@ package com.powsybl.ucte.converter; + import com.powsybl.commons.datasource.ResourceDataSource; import com.powsybl.commons.datasource.ResourceSet; import com.powsybl.iidm.network.*; @@ -17,6 +18,7 @@ import com.powsybl.ucte.network.UcteVoltageLevelCode; import org.junit.jupiter.api.Test; + import java.util.Properties; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -32,7 +34,8 @@ void initTest() { ResourceDataSource dataSource = new ResourceDataSource("network3", new ResourceSet("/", "network3.xiidm")); Network n1 = Network.read(dataSource); NamingStrategy s = new DefaultNamingStrategy(); - s.init(n1); + s.networkInitialisation(n1); + } @Test diff --git a/ucte/ucte-converter/src/test/resources/network4.xiidm b/ucte/ucte-converter/src/test/resources/network4.xiidm new file mode 100644 index 00000000000..74eddc98f99 --- /dev/null +++ b/ucte/ucte-converter/src/test/resources/network4.xiidm @@ -0,0 +1,147 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/ucte/ucte-network/src/main/java/com/powsybl/ucte/network/UcteNodeCode.java b/ucte/ucte-network/src/main/java/com/powsybl/ucte/network/UcteNodeCode.java index 2b9529a2ed5..cb61fea3a62 100644 --- a/ucte/ucte-network/src/main/java/com/powsybl/ucte/network/UcteNodeCode.java +++ b/ucte/ucte-network/src/main/java/com/powsybl/ucte/network/UcteNodeCode.java @@ -122,14 +122,13 @@ public static Optional convertToUcteNodeId(char countryCode, char // get ucte country code with the country name nameBuilder.append(countryCode); // Format bus ID to fill chars[1-5] - String fomatedId = id.length() >= 5 - ? id.substring(0, 5) - : String.format("%-5s", id).replace(' ', '_'); - nameBuilder.append(fomatedId); + nameBuilder.append(id); + while(nameBuilder.length() <= 5) nameBuilder.append('_'); // Add voltage level code (char[6]) and trailing underscore (char[7]) nameBuilder.append(voltageLevelCode).append('_'); return parseUcteNodeCode(nameBuilder.toString()); + } public static boolean isUcteNodeId(String id) { From 6a150141f942663e488d67c681359f907385b507 Mon Sep 17 00:00:00 2001 From: Leclerc Clement Date: Tue, 12 Nov 2024 11:07:14 +0100 Subject: [PATCH 07/38] create CounterNamingStrategy Signed-off-by: Leclerc Clement --- .../ucte/converter/CounterNamingStrategy.java | 291 ++++++++++++++++++ .../ucte/converter/DefaultNamingStrategy.java | 238 +------------- .../ucte/converter/NamingStrategy.java | 2 +- .../ucte/converter/NamingStrategyTest.java | 6 +- .../resources/{network.xml => network.xiidm} | 0 .../{network2.xml => network2.xiidm} | 0 .../src/test/resources/network3.xiidm | 2 +- .../src/test/resources/networku.xiidm | 42 +++ .../powsybl/ucte/network/UcteNodeCode.java | 14 - 9 files changed, 345 insertions(+), 250 deletions(-) create mode 100644 ucte/ucte-converter/src/main/java/com/powsybl/ucte/converter/CounterNamingStrategy.java rename ucte/ucte-converter/src/test/resources/{network.xml => network.xiidm} (100%) rename ucte/ucte-converter/src/test/resources/{network2.xml => network2.xiidm} (100%) create mode 100644 ucte/ucte-converter/src/test/resources/networku.xiidm diff --git a/ucte/ucte-converter/src/main/java/com/powsybl/ucte/converter/CounterNamingStrategy.java b/ucte/ucte-converter/src/main/java/com/powsybl/ucte/converter/CounterNamingStrategy.java new file mode 100644 index 00000000000..4f552de3f5f --- /dev/null +++ b/ucte/ucte-converter/src/main/java/com/powsybl/ucte/converter/CounterNamingStrategy.java @@ -0,0 +1,291 @@ +/** + * Copyright (c) 2019, 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/. + * SPDX-License-Identifier: MPL-2.0 + */ + +package com.powsybl.ucte.converter; + +import com.google.auto.service.AutoService; +import com.powsybl.iidm.network.*; +import com.powsybl.ucte.network.UcteCountryCode; +import com.powsybl.ucte.network.UcteElementId; +import com.powsybl.ucte.network.UcteNodeCode; +import com.powsybl.ucte.network.UcteVoltageLevelCode; + +import java.util.Comparator; +import java.util.HashMap; +import java.util.Map; + +/** + * A {@link NamingStrategy} implementation that ensures the conformity of IDs with the UCTE-DEF format + * + * @author Clément LECLERC {@literal } + */ +@AutoService(NamingStrategy.class) +public class CounterNamingStrategy implements NamingStrategy { + + private final Map ucteNodeIds = new HashMap<>(); + private final Map ucteElementIds = new HashMap<>(); + private int namingCounter; + private int nodeCounter; + + @Override + public String getName() { + return "CounterNamingStrategy"; + } + + +/** + * Converts all network elements to UCTE format using a sequential naming scheme. + * The conversion process follows these steps: + *
    + *
  1. Reset the global naming counter to ensure unique identifiers
  2. + *
  3. Convert all elements within each voltage level + *
  4. Convert network-level elements + *
+ * + * Each element receives a unique sequential identifier during the conversion process. + * + * @param network the network to convert + */ + @Override + public void convertNetworkToUcte(Network network) { + //count for naming each element + namingCounter = 0; + // Convert voltage level elements + network.getVoltageLevelStream().sorted(Comparator.comparing(VoltageLevel::getId)).forEach(voltageLevel -> + convertVoltageLevelElementsIdToUcte(voltageLevel)); + // Convert network elements + convertNetworkElementsIdToUcte(network); + } + + + /** + * Converts all voltageLevel elements to UCTE format using a sequential naming scheme. + * The conversion process follows these steps: + *
    + *
  1. Convert all elements within each voltage level (buses, generators, loads, shunts)
  2. + *
+ * + * Each element receives a unique sequential identifier during the conversion process. + * + * @param voltageLevel the voltageLevel to convert + */ + private void convertVoltageLevelElementsIdToUcte(VoltageLevel voltageLevel) { + System.out.println(voltageLevel.getId()); + namingCounter++; + nodeCounter=0; + + voltageLevel.getBusBreakerView().getBusStream().forEach(bus -> { + UcteNodeCode ucteId = generateUcteNodeId(bus.getId(), voltageLevel); + if (ucteId != null) { + bus.setName(ucteId.toString()); + logConversion("BUS", bus); + } + }); + + } + + /** + * Converts all network-level elements to UCTE format using a sequential naming scheme. + * The conversion process follows these steps: + *
    + *
  1. Convert network-level elements (lines, transformers)
  2. + *
+ * + * Each element receives a unique sequential identifier during the conversion process. + * + * @param network the network to convert + */ + private void convertNetworkElementsIdToUcte(Network network) { + network.getBranchStream().forEach(branch -> { + UcteElementId ucteId = generateUcteElementId(branch); + branch.setName(ucteId.toString()); + logConversion(branch.getType().name(), branch); + }); + } + + private void logConversion(String elementType, Identifiable element) { + System.out.println(String.format("%s : %s--> %s", + elementType, + element.getId(), + element.getNameOrId())); + } + + /** + * Generates a UCTE node code for a network element. + * The generation process involves: + *
    + *
  1. Checking for existing node ID to avoid duplicates
  2. + *
  3. Extracting country code from the voltage level
  4. + *
  5. Determining voltage level code based on nominal voltage
  6. + *
  7. Creating a new node ID using the first alphanumeric character of voltage level ID and counter
  8. + *
+ * + * The generated UCTE node code follows the format: [CountryCode][NodeId][VoltageLevelCode] + * + * @param nodeId original identifier of the network element + * @param voltageLevel voltage level containing the element + * @return generated UcteNodeCode, or null if nodeId already exists in ucteNodeIds + */ + private UcteNodeCode generateUcteNodeId(String nodeId, VoltageLevel voltageLevel) { + + if (ucteNodeIds.containsKey(nodeId)) { + return null; + } + nodeCounter++; + StringBuilder newNodeCode = new StringBuilder(8); + String newNodeId = String.format("%05d",namingCounter); + char countryCode = getCountryCode(voltageLevel); + char voltageLevelCode = getUcteVoltageLevelCode(voltageLevel.getNominalV()); + + newNodeCode + .append(countryCode) + .append(newNodeId) + .append(voltageLevelCode) + .append(nodeCounter); + + UcteNodeCode nodeCode = getUcteNodeCode(newNodeCode.toString()); + ucteNodeIds.put(nodeId, nodeCode); + return nodeCode; + } + + + /** + * Generates a UCTE element identifier for a network branch element (line or transformer). + * The element ID is composed of two node codes and an order code. + * + *

The generated ID follows UCTE format specifications: + * [Node1Code][Node2Code][OrderCode] + * where: + *

    + *
  • Node1Code: UCTE code of the first terminal
  • + *
  • Node2Code: UCTE code of the second terminal
  • + *
  • OrderCode: Single character identifier
  • + *
+ * + * @param branch the element instance of branch + * @return the generated UCTE element identifier + */ + private UcteElementId generateUcteElementId(Branch branch) { + + String branchId = branch.getId(); + if (ucteElementIds.containsKey(branchId)) { + return null; + } + + UcteNodeCode node1 = ucteNodeIds.get(branch.getTerminal1().getBusBreakerView().getBus().getId()); + UcteNodeCode node2 = ucteNodeIds.get(branch.getTerminal2().getBusBreakerView().getBus().getId()); + + char orderCode = branch instanceof TwoWindingsTransformer ? + '1' : + branch.getId().charAt(branch.getId().length() - 1); + + return new UcteElementId(node1, node2, orderCode); + } + + /** + * Extracts and converts a country code to UCTE format for a given voltage level. + * + *

The country code is determined through the following process: + *

    + *
  1. Attempts to get the country from the voltage level's substation
  2. + *
  3. Maps the country name to its corresponding UCTE code
  4. + *
  5. Returns 'X' as fallback if no valid mapping is found
  6. + *
+ * + * @param voltageLevel the voltage level from which to extract the country code + * @return the UCTE country code character, or 'X' if no valid country is found) + */ + private static char getCountryCode(VoltageLevel voltageLevel) { + + String country = voltageLevel.getSubstation() + .map(s->s.getCountry() + .map(Country::getName) + .orElse("XX")) + .orElse("XX"); + + for (UcteCountryCode countryCode : UcteCountryCode.values()) { + if (country.equalsIgnoreCase(countryCode.getPrettyName())) { + return countryCode.getUcteCode(); + } + } + return 'X'; + } + + /** + * Determines the UCTE voltage level code for a given voltage value. + * The code is determined by finding the closest standard UCTE voltage level. + * + *

The method follows these rules: + *

    + *
  • For voltage < 27kV: returns '7'
  • + *
  • For voltage > 750kV: returns '0'
  • + *
  • For other voltages: returns code of the closest standard UCTE level
  • + *
+ * + * @param voltage the nominal voltage value in kV + * @return a character representing the UCTE voltage level code + */ + private static char getUcteVoltageLevelCode(double voltage) { + + if (voltage < UcteVoltageLevelCode.VL_27.getVoltageLevel()) { + return '7'; + } + if (voltage > UcteVoltageLevelCode.VL_750.getVoltageLevel()) { + return '0'; + } + + UcteVoltageLevelCode closest = null; + double minDiff = Double.MAX_VALUE; + + for (UcteVoltageLevelCode code : UcteVoltageLevelCode.values()) { + double diff = Math.abs(voltage - code.getVoltageLevel()); + if (diff < minDiff) { + minDiff = diff; + closest = code; + } + } + return closest != null ? (char) ('0' + closest.ordinal()) : '7'; + } + + @Override + public UcteNodeCode getUcteNodeCode(String id) { + return ucteNodeIds.computeIfAbsent(id, k -> UcteNodeCode.parseUcteNodeCode(k).orElseThrow(() -> new UcteException("Invalid UCTE node identifier: " + k))); + } + + @Override + public UcteNodeCode getUcteNodeCode(Bus bus) { + return getUcteNodeCode(bus.getId()); + } + + @Override + public UcteNodeCode getUcteNodeCode(DanglingLine danglingLine) { + return getUcteNodeCode(danglingLine.getPairingKey()); + } + + @Override + public UcteElementId getUcteElementId(String id) { + return ucteElementIds.computeIfAbsent(id, k -> UcteElementId.parseUcteElementId(k).orElseThrow(() -> new UcteException("Invalid UCTE node identifier: " + k))); + } + + @Override + public UcteElementId getUcteElementId(Switch sw) { + return getUcteElementId(sw.getId()); + } + + @Override + public UcteElementId getUcteElementId(Branch branch) { + return getUcteElementId(branch.getId()); + } + + @Override + public UcteElementId getUcteElementId(DanglingLine danglingLine) { + return getUcteElementId(danglingLine.getId()); + } + + +} \ No newline at end of file diff --git a/ucte/ucte-converter/src/main/java/com/powsybl/ucte/converter/DefaultNamingStrategy.java b/ucte/ucte-converter/src/main/java/com/powsybl/ucte/converter/DefaultNamingStrategy.java index 06a315d434f..7640ecd3241 100644 --- a/ucte/ucte-converter/src/main/java/com/powsybl/ucte/converter/DefaultNamingStrategy.java +++ b/ucte/ucte-converter/src/main/java/com/powsybl/ucte/converter/DefaultNamingStrategy.java @@ -10,12 +10,11 @@ import com.google.auto.service.AutoService; import com.powsybl.iidm.network.*; -import com.powsybl.ucte.network.UcteCountryCode; import com.powsybl.ucte.network.UcteElementId; import com.powsybl.ucte.network.UcteNodeCode; -import com.powsybl.ucte.network.UcteVoltageLevelCode; -import java.util.*; +import java.util.HashMap; +import java.util.Map; /** * A {@link NamingStrategy} implementation that ensures the conformity of IDs with the UCTE-DEF format @@ -26,243 +25,17 @@ public class DefaultNamingStrategy implements NamingStrategy { private final Map ucteNodeIds = new HashMap<>(); + private final Map ucteElementIds = new HashMap<>(); - private int namingCounter = 0; @Override public String getName() { return "Default"; } - /** - * Initializes the network by converting network elements names to UCTE format if needed. - * @param network The network to check - */ @Override - public void networkInitialisation(Network network) { - //add condition to test network is already ucte format - convertNetworkToUcte(network); - } + public void convertNetworkToUcte(Network network) { -/** - * Converts all network elements to UCTE format using a sequential naming scheme. - * The conversion process follows these steps: - *
    - *
  1. Reset the global naming counter to ensure unique identifiers
  2. - *
  3. Convert all elements within each voltage level - *
  4. Convert network-level elements - *
- * - * Each element receives a unique sequential identifier during the conversion process. - * - * @param network the network to convert - */ - private void convertNetworkToUcte(Network network) { - //count for naming each element - namingCounter = 0; - // Convert voltage level elements - network.getVoltageLevelStream().forEach(voltageLevel -> - convertVoltageLevelElementsIdToUcte(voltageLevel)); - // Convert network elements - convertNetworkElementsIdToUcte(network); - } - - - /** - * Converts all voltageLevel elements to UCTE format using a sequential naming scheme. - * The conversion process follows these steps: - *
    - *
  1. Convert all elements within each voltage level (buses, generators, loads, shunts)
  2. - *
- * - * Each element receives a unique sequential identifier during the conversion process. - * - * @param voltageLevel the voltageLevel to convert - */ - private void convertVoltageLevelElementsIdToUcte(VoltageLevel voltageLevel) { - System.out.println(voltageLevel.getId()); - - // Bus - voltageLevel.getBusBreakerView().getBusStream().forEach(bus -> { - UcteNodeCode ucteId = generateUcteNodeId(bus.getId(), voltageLevel, namingCounter++); - if (ucteId != null) { - bus.setName(ucteId.toString()); - logConversion("BUS", bus); - } - }); - - // Injection (generators, loads, shunts) - voltageLevel.getConnectableStream() - .filter(Injection.class::isInstance) - .map(Injection.class::cast) - .forEach(injection -> { - UcteNodeCode ucteId = generateUcteNodeId(injection.getId(), voltageLevel, namingCounter++); - injection.setName(ucteId.toString()); - logConversion(injection.getType().name(), injection); - }); - - } - - /** - * Converts all network-level elements to UCTE format using a sequential naming scheme. - * The conversion process follows these steps: - *
    - *
  1. Convert network-level elements (lines, transformers)
  2. - *
- * - * Each element receives a unique sequential identifier during the conversion process. - * - * @param network the network to convert - */ - private void convertNetworkElementsIdToUcte(Network network) { - network.getBranchStream().forEach(branch -> { - UcteElementId ucteId = generateUcteElementId(branch); - branch.setName(ucteId.toString()); - logConversion(branch.getType().name(), branch); - }); - } - - private void logConversion(String elementType, Identifiable element) { - System.out.println(String.format("%s : %s--> %s", - elementType, - element.getId(), - element.getNameOrId())); - } - - /** - * Generates a UCTE node code for a network element. - * The generation process involves: - *
    - *
  1. Checking for existing node ID to avoid duplicates
  2. - *
  3. Extracting country code from the voltage level
  4. - *
  5. Determining voltage level code based on nominal voltage
  6. - *
  7. Creating a new node ID using the first alphanumeric character of voltage level ID and counter
  8. - *
- * - * The generated UCTE node code follows the format: [CountryCode][NodeId][VoltageLevelCode] - * - * @param nodeId original identifier of the network element - * @param voltageLevel voltage level containing the element - * @param counter sequential number to ensure unique identification - * @return generated UcteNodeCode, or null if nodeId already exists in ucteNodeIds - */ - private UcteNodeCode generateUcteNodeId(String nodeId, VoltageLevel voltageLevel, int counter) { - String busId = nodeId; - if (ucteNodeIds.containsKey(busId)) { - return null; - } - char countryCode = getCountryCode(voltageLevel); - char voltageLevelCode = getUcteVoltageLevelCode(voltageLevel.getNominalV()); - String newNodeId = voltageLevel.getId() - .replaceAll("[^A-Z0-9]", "") - .substring(0,1) - + counter; - - UcteNodeCode nodeCode = UcteNodeCode.convertToUcteNodeId(countryCode, voltageLevelCode, newNodeId) - .orElseThrow(() -> new IllegalStateException("Invalid UCTE node code generated for bus " + busId)); - - ucteNodeIds.put(busId, nodeCode); - return nodeCode; - } - - - /** - * Generates a UCTE element identifier for a network branch element (line or transformer). - * The element ID is composed of two node codes and an order code. - * - *

The generated ID follows UCTE format specifications: - * [Node1Code][Node2Code][OrderCode] - * where: - *

    - *
  • Node1Code: UCTE code of the first terminal
  • - *
  • Node2Code: UCTE code of the second terminal
  • - *
  • OrderCode: Single character identifier
  • - *
- * - * @param branch the element instance of branch - * @return the generated UCTE element identifier - */ - private UcteElementId generateUcteElementId(Branch branch) { - - String branchId = branch.getId(); - if (ucteElementIds.containsKey(branchId)) { - return null; - } - - UcteNodeCode node1 = ucteNodeIds.get(branch.getTerminal1().getBusBreakerView().getBus().getId()); - UcteNodeCode node2 = ucteNodeIds.get(branch.getTerminal2().getBusBreakerView().getBus().getId()); - - char orderCode = branch instanceof TwoWindingsTransformer ? - '1' : - branch.getId().charAt(branch.getId().length() - 1); - - UcteElementId ucteElementId = new UcteElementId(node1, node2, orderCode); - return ucteElementId; - } - - /** - * Extracts and converts a country code to UCTE format for a given voltage level. - * - *

The country code is determined through the following process: - *

    - *
  1. Attempts to get the country from the voltage level's substation
  2. - *
  3. Maps the country name to its corresponding UCTE code
  4. - *
  5. Returns 'X' as fallback if no valid mapping is found
  6. - *
- * - * @param voltageLevel the voltage level from which to extract the country code - * @return the UCTE country code character, or 'X' if no valid country is found) - */ - private static char getCountryCode(VoltageLevel voltageLevel) { - - String country = voltageLevel.getSubstation() - .map(s->s.getCountry() - .map(Country::getName) - .orElse("XX")) - .orElse("XX"); - - for (UcteCountryCode countryCode : UcteCountryCode.values()) { - if (country.equalsIgnoreCase(countryCode.getPrettyName())) { - return countryCode.getUcteCode(); - } - } - return 'X'; - } - - /** - * Determines the UCTE voltage level code for a given voltage value. - * The code is determined by finding the closest standard UCTE voltage level. - * - *

The method follows these rules: - *

    - *
  • For voltage < 27kV: returns '7'
  • - *
  • For voltage > 750kV: returns '0'
  • - *
  • For other voltages: returns code of the closest standard UCTE level
  • - *
- * - * @param voltage the nominal voltage value in kV - * @return a character representing the UCTE voltage level code - */ - private static char getUcteVoltageLevelCode(double voltage) { - - if (voltage < UcteVoltageLevelCode.VL_27.getVoltageLevel()) { - return '7'; - } - if (voltage > UcteVoltageLevelCode.VL_750.getVoltageLevel()) { - return '0'; - } - - UcteVoltageLevelCode closest = null; - double minDiff = Double.MAX_VALUE; - - for (UcteVoltageLevelCode code : UcteVoltageLevelCode.values()) { - double diff = Math.abs(voltage - code.getVoltageLevel()); - if (diff < minDiff) { - minDiff = diff; - closest = code; - } - } - return closest != null ? (char) ('0' + closest.ordinal()) : '7'; } @Override @@ -299,4 +72,5 @@ public UcteElementId getUcteElementId(Branch branch) { public UcteElementId getUcteElementId(DanglingLine danglingLine) { return getUcteElementId(danglingLine.getId()); } -} \ No newline at end of file + +} diff --git a/ucte/ucte-converter/src/main/java/com/powsybl/ucte/converter/NamingStrategy.java b/ucte/ucte-converter/src/main/java/com/powsybl/ucte/converter/NamingStrategy.java index a41c81ecb60..2309b46aae9 100644 --- a/ucte/ucte-converter/src/main/java/com/powsybl/ucte/converter/NamingStrategy.java +++ b/ucte/ucte-converter/src/main/java/com/powsybl/ucte/converter/NamingStrategy.java @@ -19,7 +19,7 @@ public interface NamingStrategy { String getName(); - void networkInitialisation(Network n); + void convertNetworkToUcte(Network network); UcteNodeCode getUcteNodeCode(String id); diff --git a/ucte/ucte-converter/src/test/java/com/powsybl/ucte/converter/NamingStrategyTest.java b/ucte/ucte-converter/src/test/java/com/powsybl/ucte/converter/NamingStrategyTest.java index 6b7cd1a677e..51778e58342 100644 --- a/ucte/ucte-converter/src/test/java/com/powsybl/ucte/converter/NamingStrategyTest.java +++ b/ucte/ucte-converter/src/test/java/com/powsybl/ucte/converter/NamingStrategyTest.java @@ -31,10 +31,12 @@ class NamingStrategyTest { @Test void initTest() { + // Chargement du réseau original ResourceDataSource dataSource = new ResourceDataSource("network3", new ResourceSet("/", "network3.xiidm")); Network n1 = Network.read(dataSource); - NamingStrategy s = new DefaultNamingStrategy(); - s.networkInitialisation(n1); + NamingStrategy s = new CounterNamingStrategy(); + s.convertNetworkToUcte(n1); + } diff --git a/ucte/ucte-converter/src/test/resources/network.xml b/ucte/ucte-converter/src/test/resources/network.xiidm similarity index 100% rename from ucte/ucte-converter/src/test/resources/network.xml rename to ucte/ucte-converter/src/test/resources/network.xiidm diff --git a/ucte/ucte-converter/src/test/resources/network2.xml b/ucte/ucte-converter/src/test/resources/network2.xiidm similarity index 100% rename from ucte/ucte-converter/src/test/resources/network2.xml rename to ucte/ucte-converter/src/test/resources/network2.xiidm diff --git a/ucte/ucte-converter/src/test/resources/network3.xiidm b/ucte/ucte-converter/src/test/resources/network3.xiidm index 439c8cb9c29..d648236289c 100644 --- a/ucte/ucte-converter/src/test/resources/network3.xiidm +++ b/ucte/ucte-converter/src/test/resources/network3.xiidm @@ -1,7 +1,7 @@ - + diff --git a/ucte/ucte-converter/src/test/resources/networku.xiidm b/ucte/ucte-converter/src/test/resources/networku.xiidm new file mode 100644 index 00000000000..b7ef818e14e --- /dev/null +++ b/ucte/ucte-converter/src/test/resources/networku.xiidm @@ -0,0 +1,42 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/ucte/ucte-network/src/main/java/com/powsybl/ucte/network/UcteNodeCode.java b/ucte/ucte-network/src/main/java/com/powsybl/ucte/network/UcteNodeCode.java index cb61fea3a62..a9c04e8a754 100644 --- a/ucte/ucte-network/src/main/java/com/powsybl/ucte/network/UcteNodeCode.java +++ b/ucte/ucte-network/src/main/java/com/powsybl/ucte/network/UcteNodeCode.java @@ -116,20 +116,6 @@ public static Optional parseUcteNodeCode(String id) { return Optional.ofNullable(ucteNodeCode); } - public static Optional convertToUcteNodeId(char countryCode, char voltageLevelCode, String id){ - // Initialize StringBuilder with fixed capacity of 8 for UCTE format - StringBuilder nameBuilder = new StringBuilder(8); - // get ucte country code with the country name - nameBuilder.append(countryCode); - // Format bus ID to fill chars[1-5] - nameBuilder.append(id); - while(nameBuilder.length() <= 5) nameBuilder.append('_'); - // Add voltage level code (char[6]) and trailing underscore (char[7]) - nameBuilder.append(voltageLevelCode).append('_'); - - return parseUcteNodeCode(nameBuilder.toString()); - - } public static boolean isUcteNodeId(String id) { return id != null && From 1ce1b1758471f2e3ee0bcf244af57572214b24ec Mon Sep 17 00:00:00 2001 From: Leclerc Clement Date: Tue, 12 Nov 2024 11:23:19 +0100 Subject: [PATCH 08/38] create CounterNamingStrategy Signed-off-by: Leclerc Clement --- .../src/main/java/com/powsybl/ucte/network/UcteNodeCode.java | 1 - 1 file changed, 1 deletion(-) diff --git a/ucte/ucte-network/src/main/java/com/powsybl/ucte/network/UcteNodeCode.java b/ucte/ucte-network/src/main/java/com/powsybl/ucte/network/UcteNodeCode.java index a9c04e8a754..8bc456fd47f 100644 --- a/ucte/ucte-network/src/main/java/com/powsybl/ucte/network/UcteNodeCode.java +++ b/ucte/ucte-network/src/main/java/com/powsybl/ucte/network/UcteNodeCode.java @@ -116,7 +116,6 @@ public static Optional parseUcteNodeCode(String id) { return Optional.ofNullable(ucteNodeCode); } - public static boolean isUcteNodeId(String id) { return id != null && id.length() == 8 && From dadc8525276cef04903d1b5ec766a91b9f32e254 Mon Sep 17 00:00:00 2001 From: Leclerc Clement Date: Tue, 12 Nov 2024 11:35:27 +0100 Subject: [PATCH 09/38] change nodeCounter incrementation loop Signed-off-by: Leclerc Clement --- .../ucte/converter/CounterNamingStrategy.java | 22 +++++++++---------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/ucte/ucte-converter/src/main/java/com/powsybl/ucte/converter/CounterNamingStrategy.java b/ucte/ucte-converter/src/main/java/com/powsybl/ucte/converter/CounterNamingStrategy.java index 4f552de3f5f..0d9bb80617f 100644 --- a/ucte/ucte-converter/src/main/java/com/powsybl/ucte/converter/CounterNamingStrategy.java +++ b/ucte/ucte-converter/src/main/java/com/powsybl/ucte/converter/CounterNamingStrategy.java @@ -30,7 +30,6 @@ public class CounterNamingStrategy implements NamingStrategy { private final Map ucteNodeIds = new HashMap<>(); private final Map ucteElementIds = new HashMap<>(); private int namingCounter; - private int nodeCounter; @Override public String getName() { @@ -77,15 +76,16 @@ public void convertNetworkToUcte(Network network) { private void convertVoltageLevelElementsIdToUcte(VoltageLevel voltageLevel) { System.out.println(voltageLevel.getId()); namingCounter++; - nodeCounter=0; + int nodeCounter=0; - voltageLevel.getBusBreakerView().getBusStream().forEach(bus -> { - UcteNodeCode ucteId = generateUcteNodeId(bus.getId(), voltageLevel); - if (ucteId != null) { - bus.setName(ucteId.toString()); - logConversion("BUS", bus); - } - }); + for(Bus bus : voltageLevel.getBusBreakerView().getBuses()){ + nodeCounter++; + UcteNodeCode ucteId = generateUcteNodeId(bus.getId(), voltageLevel,nodeCounter); + if (ucteId != null) { + bus.setName(ucteId.toString()); + logConversion("BUS", bus); + } + } } @@ -131,12 +131,11 @@ private void logConversion(String elementType, Identifiable element) { * @param voltageLevel voltage level containing the element * @return generated UcteNodeCode, or null if nodeId already exists in ucteNodeIds */ - private UcteNodeCode generateUcteNodeId(String nodeId, VoltageLevel voltageLevel) { + private UcteNodeCode generateUcteNodeId(String nodeId, VoltageLevel voltageLevel,int nodeCounter) { if (ucteNodeIds.containsKey(nodeId)) { return null; } - nodeCounter++; StringBuilder newNodeCode = new StringBuilder(8); String newNodeId = String.format("%05d",namingCounter); char countryCode = getCountryCode(voltageLevel); @@ -184,6 +183,7 @@ private UcteElementId generateUcteElementId(Branch branch) { '1' : branch.getId().charAt(branch.getId().length() - 1); + return new UcteElementId(node1, node2, orderCode); } From f0bf3339794b47c8896b339a98d626880599660d Mon Sep 17 00:00:00 2001 From: Leclerc Clement Date: Tue, 12 Nov 2024 13:03:42 +0100 Subject: [PATCH 10/38] change ElementId orderCode incrementation Signed-off-by: Leclerc Clement --- .../ucte/converter/CounterNamingStrategy.java | 25 +++++++++++++------ .../ucte/converter/NamingStrategyTest.java | 2 +- 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/ucte/ucte-converter/src/main/java/com/powsybl/ucte/converter/CounterNamingStrategy.java b/ucte/ucte-converter/src/main/java/com/powsybl/ucte/converter/CounterNamingStrategy.java index 0d9bb80617f..f32741a8c55 100644 --- a/ucte/ucte-converter/src/main/java/com/powsybl/ucte/converter/CounterNamingStrategy.java +++ b/ucte/ucte-converter/src/main/java/com/powsybl/ucte/converter/CounterNamingStrategy.java @@ -15,9 +15,8 @@ import com.powsybl.ucte.network.UcteNodeCode; import com.powsybl.ucte.network.UcteVoltageLevelCode; -import java.util.Comparator; -import java.util.HashMap; -import java.util.Map; +import java.util.*; + /** * A {@link NamingStrategy} implementation that ensures the conformity of IDs with the UCTE-DEF format @@ -122,13 +121,14 @@ private void logConversion(String elementType, Identifiable element) { *
  • Checking for existing node ID to avoid duplicates
  • *
  • Extracting country code from the voltage level
  • *
  • Determining voltage level code based on nominal voltage
  • - *
  • Creating a new node ID using the first alphanumeric character of voltage level ID and counter
  • + *
  • Creating a new node ID using namingCounter
  • * * * The generated UCTE node code follows the format: [CountryCode][NodeId][VoltageLevelCode] * * @param nodeId original identifier of the network element * @param voltageLevel voltage level containing the element + * @param nodeCounter code counter * @return generated UcteNodeCode, or null if nodeId already exists in ucteNodeIds */ private UcteNodeCode generateUcteNodeId(String nodeId, VoltageLevel voltageLevel,int nodeCounter) { @@ -171,6 +171,10 @@ private UcteNodeCode generateUcteNodeId(String nodeId, VoltageLevel voltageLevel */ private UcteElementId generateUcteElementId(Branch branch) { + int i=0; + List ORDER_CODES = Arrays.asList('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', + 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '_', '-', '.', ' '); + String branchId = branch.getId(); if (ucteElementIds.containsKey(branchId)) { return null; @@ -178,12 +182,19 @@ private UcteElementId generateUcteElementId(Branch branch) { UcteNodeCode node1 = ucteNodeIds.get(branch.getTerminal1().getBusBreakerView().getBus().getId()); UcteNodeCode node2 = ucteNodeIds.get(branch.getTerminal2().getBusBreakerView().getBus().getId()); + char orderCode = ORDER_CODES.get(0); - char orderCode = branch instanceof TwoWindingsTransformer ? - '1' : - branch.getId().charAt(branch.getId().length() - 1); + UcteElementId ucteElementId = new UcteElementId(node1, node2, orderCode); + while(ucteElementIds.containsValue(ucteElementId)) { + i++; + if(i < ORDER_CODES.size()) { + orderCode = ORDER_CODES.get(i); + } + ucteElementId = new UcteElementId(node1, node2, orderCode); + } + ucteElementIds.put(branchId, ucteElementId); return new UcteElementId(node1, node2, orderCode); } diff --git a/ucte/ucte-converter/src/test/java/com/powsybl/ucte/converter/NamingStrategyTest.java b/ucte/ucte-converter/src/test/java/com/powsybl/ucte/converter/NamingStrategyTest.java index 51778e58342..6a1db5bbc24 100644 --- a/ucte/ucte-converter/src/test/java/com/powsybl/ucte/converter/NamingStrategyTest.java +++ b/ucte/ucte-converter/src/test/java/com/powsybl/ucte/converter/NamingStrategyTest.java @@ -32,7 +32,7 @@ class NamingStrategyTest { @Test void initTest() { // Chargement du réseau original - ResourceDataSource dataSource = new ResourceDataSource("network3", new ResourceSet("/", "network3.xiidm")); + ResourceDataSource dataSource = new ResourceDataSource("network2", new ResourceSet("/", "network2.xiidm")); Network n1 = Network.read(dataSource); NamingStrategy s = new CounterNamingStrategy(); s.convertNetworkToUcte(n1); From 74ad50059072e9284afedc7c446ff81762bc51e6 Mon Sep 17 00:00:00 2001 From: Leclerc Clement Date: Wed, 13 Nov 2024 09:26:34 +0100 Subject: [PATCH 11/38] change UtceExporter exception Signed-off-by: Leclerc Clement --- .../ucte/converter/CounterNamingStrategy.java | 83 ++++++++++++------- .../ucte/converter/DefaultNamingStrategy.java | 2 +- .../ucte/converter/NamingStrategy.java | 2 +- .../powsybl/ucte/converter/UcteExporter.java | 4 +- .../ucte/converter/NamingStrategyTest.java | 20 +++-- 5 files changed, 68 insertions(+), 43 deletions(-) diff --git a/ucte/ucte-converter/src/main/java/com/powsybl/ucte/converter/CounterNamingStrategy.java b/ucte/ucte-converter/src/main/java/com/powsybl/ucte/converter/CounterNamingStrategy.java index f32741a8c55..0f15f5d604c 100644 --- a/ucte/ucte-converter/src/main/java/com/powsybl/ucte/converter/CounterNamingStrategy.java +++ b/ucte/ucte-converter/src/main/java/com/powsybl/ucte/converter/CounterNamingStrategy.java @@ -32,7 +32,7 @@ public class CounterNamingStrategy implements NamingStrategy { @Override public String getName() { - return "CounterNamingStrategy"; + return "Counter"; } @@ -50,7 +50,7 @@ public String getName() { * @param network the network to convert */ @Override - public void convertNetworkToUcte(Network network) { + public void initialiseNetwork(Network network) { //count for naming each element namingCounter = 0; // Convert voltage level elements @@ -60,7 +60,6 @@ public void convertNetworkToUcte(Network network) { convertNetworkElementsIdToUcte(network); } - /** * Converts all voltageLevel elements to UCTE format using a sequential naming scheme. * The conversion process follows these steps: @@ -74,17 +73,24 @@ public void convertNetworkToUcte(Network network) { */ private void convertVoltageLevelElementsIdToUcte(VoltageLevel voltageLevel) { System.out.println(voltageLevel.getId()); - namingCounter++; - int nodeCounter=0; - - for(Bus bus : voltageLevel.getBusBreakerView().getBuses()){ - nodeCounter++; - UcteNodeCode ucteId = generateUcteNodeId(bus.getId(), voltageLevel,nodeCounter); - if (ucteId != null) { - bus.setName(ucteId.toString()); - logConversion("BUS", bus); - } - } + int nodeCounter = 0; + + for (Bus bus : voltageLevel.getBusBreakerView().getBuses()) { + nodeCounter++; + UcteNodeCode ucteId = generateUcteNodeId(bus.getId(), voltageLevel, nodeCounter); + if (ucteId != null) { + bus.setName(ucteId.toString()); + logConversion("BUS", bus); + } + } + for (Switch s : voltageLevel.getBusBreakerView().getSwitches()) { + nodeCounter++; + UcteNodeCode ucteId = generateUcteNodeId(s.getId(), voltageLevel, nodeCounter); + if (ucteId != null) { + s.setName(ucteId.toString()); + logConversion("SWITCH", s); + } + } } @@ -108,10 +114,13 @@ private void convertNetworkElementsIdToUcte(Network network) { } private void logConversion(String elementType, Identifiable element) { - System.out.println(String.format("%s : %s--> %s", + System.out.println(String.format("%s : %s--> %s %s", elementType, element.getId(), - element.getNameOrId())); + ucteNodeIds.get(element.getId()), + ucteElementIds.get(element.getId()) + )); + } /** @@ -131,13 +140,13 @@ private void logConversion(String elementType, Identifiable element) { * @param nodeCounter code counter * @return generated UcteNodeCode, or null if nodeId already exists in ucteNodeIds */ - private UcteNodeCode generateUcteNodeId(String nodeId, VoltageLevel voltageLevel,int nodeCounter) { + private UcteNodeCode generateUcteNodeId(String nodeId, VoltageLevel voltageLevel, int nodeCounter) { if (ucteNodeIds.containsKey(nodeId)) { return null; } StringBuilder newNodeCode = new StringBuilder(8); - String newNodeId = String.format("%05d",namingCounter); + String newNodeId = String.format("%05d", ++namingCounter); char countryCode = getCountryCode(voltageLevel); char voltageLevelCode = getUcteVoltageLevelCode(voltageLevel.getNominalV()); @@ -147,12 +156,11 @@ private UcteNodeCode generateUcteNodeId(String nodeId, VoltageLevel voltageLevel .append(voltageLevelCode) .append(nodeCounter); - UcteNodeCode nodeCode = getUcteNodeCode(newNodeCode.toString()); + UcteNodeCode nodeCode = UcteNodeCode.parseUcteNodeCode(newNodeCode.toString()).get(); ucteNodeIds.put(nodeId, nodeCode); return nodeCode; } - /** * Generates a UCTE element identifier for a network branch element (line or transformer). * The element ID is composed of two node codes and an order code. @@ -171,8 +179,8 @@ private UcteNodeCode generateUcteNodeId(String nodeId, VoltageLevel voltageLevel */ private UcteElementId generateUcteElementId(Branch branch) { - int i=0; - List ORDER_CODES = Arrays.asList('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', + int i = 0; + List oderCode = Arrays.asList('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '_', '-', '.', ' '); String branchId = branch.getId(); @@ -182,20 +190,20 @@ private UcteElementId generateUcteElementId(Branch branch) { UcteNodeCode node1 = ucteNodeIds.get(branch.getTerminal1().getBusBreakerView().getBus().getId()); UcteNodeCode node2 = ucteNodeIds.get(branch.getTerminal2().getBusBreakerView().getBus().getId()); - char orderCode = ORDER_CODES.get(0); + char orderCode = oderCode.get(0); UcteElementId ucteElementId = new UcteElementId(node1, node2, orderCode); - while(ucteElementIds.containsValue(ucteElementId)) { + while (ucteElementIds.containsValue(ucteElementId)) { i++; - if(i < ORDER_CODES.size()) { - orderCode = ORDER_CODES.get(i); + if (i < oderCode.size()) { + orderCode = oderCode.get(i); } ucteElementId = new UcteElementId(node1, node2, orderCode); } ucteElementIds.put(branchId, ucteElementId); - return new UcteElementId(node1, node2, orderCode); + return ucteElementId; } /** @@ -214,7 +222,7 @@ private UcteElementId generateUcteElementId(Branch branch) { private static char getCountryCode(VoltageLevel voltageLevel) { String country = voltageLevel.getSubstation() - .map(s->s.getCountry() + .map(s -> s.getCountry() .map(Country::getName) .orElse("XX")) .orElse("XX"); @@ -265,7 +273,15 @@ private static char getUcteVoltageLevelCode(double voltage) { @Override public UcteNodeCode getUcteNodeCode(String id) { - return ucteNodeIds.computeIfAbsent(id, k -> UcteNodeCode.parseUcteNodeCode(k).orElseThrow(() -> new UcteException("Invalid UCTE node identifier: " + k))); + if (id == null) { + return null; + } + + UcteNodeCode code = ucteNodeIds.get(id); + if (code == null) { + throw new UcteException("No UCTE code found for id: " + id); + } + return code; } @Override @@ -280,7 +296,11 @@ public UcteNodeCode getUcteNodeCode(DanglingLine danglingLine) { @Override public UcteElementId getUcteElementId(String id) { - return ucteElementIds.computeIfAbsent(id, k -> UcteElementId.parseUcteElementId(k).orElseThrow(() -> new UcteException("Invalid UCTE node identifier: " + k))); + UcteElementId elementId = ucteElementIds.get(id); + if (elementId == null) { + throw new UcteException("No UCTE element id found for: " + id); + } + return elementId; } @Override @@ -298,5 +318,4 @@ public UcteElementId getUcteElementId(DanglingLine danglingLine) { return getUcteElementId(danglingLine.getId()); } - -} \ No newline at end of file +} diff --git a/ucte/ucte-converter/src/main/java/com/powsybl/ucte/converter/DefaultNamingStrategy.java b/ucte/ucte-converter/src/main/java/com/powsybl/ucte/converter/DefaultNamingStrategy.java index 7640ecd3241..9abd0c9e623 100644 --- a/ucte/ucte-converter/src/main/java/com/powsybl/ucte/converter/DefaultNamingStrategy.java +++ b/ucte/ucte-converter/src/main/java/com/powsybl/ucte/converter/DefaultNamingStrategy.java @@ -34,7 +34,7 @@ public String getName() { } @Override - public void convertNetworkToUcte(Network network) { + public void initialiseNetwork(Network network) { } diff --git a/ucte/ucte-converter/src/main/java/com/powsybl/ucte/converter/NamingStrategy.java b/ucte/ucte-converter/src/main/java/com/powsybl/ucte/converter/NamingStrategy.java index 2309b46aae9..5cbe47aeded 100644 --- a/ucte/ucte-converter/src/main/java/com/powsybl/ucte/converter/NamingStrategy.java +++ b/ucte/ucte-converter/src/main/java/com/powsybl/ucte/converter/NamingStrategy.java @@ -19,7 +19,7 @@ public interface NamingStrategy { String getName(); - void convertNetworkToUcte(Network network); + void initialiseNetwork(Network network); UcteNodeCode getUcteNodeCode(String id); diff --git a/ucte/ucte-converter/src/main/java/com/powsybl/ucte/converter/UcteExporter.java b/ucte/ucte-converter/src/main/java/com/powsybl/ucte/converter/UcteExporter.java index 2a41a45c4e2..869c54f3362 100644 --- a/ucte/ucte-converter/src/main/java/com/powsybl/ucte/converter/UcteExporter.java +++ b/ucte/ucte-converter/src/main/java/com/powsybl/ucte/converter/UcteExporter.java @@ -140,11 +140,13 @@ private static UcteNetwork createUcteNetwork(Network network, NamingStrategy nam network.getHvdcLineCount() > 0 || network.getThreeWindingsTransformerCount() > 0) { - throw new UcteException("This network contains unsupported equipments"); + //throw new UcteException("This network contains unsupported equipments"); } UcteExporterContext context = new UcteExporterContext(namingStrategy, combinePhaseAngleRegulation); + namingStrategy.initialiseNetwork(network); + UcteNetwork ucteNetwork = new UcteNetworkImpl(); ucteNetwork.setVersion(UcteFormatVersion.SECOND); diff --git a/ucte/ucte-converter/src/test/java/com/powsybl/ucte/converter/NamingStrategyTest.java b/ucte/ucte-converter/src/test/java/com/powsybl/ucte/converter/NamingStrategyTest.java index 6a1db5bbc24..e581f81c2cb 100644 --- a/ucte/ucte-converter/src/test/java/com/powsybl/ucte/converter/NamingStrategyTest.java +++ b/ucte/ucte-converter/src/test/java/com/powsybl/ucte/converter/NamingStrategyTest.java @@ -8,7 +8,9 @@ package com.powsybl.ucte.converter; - +import com.fasterxml.jackson.databind.annotation.JsonAppend; +import com.powsybl.commons.datasource.DataSource; +import com.powsybl.commons.datasource.DirectoryDataSource; import com.powsybl.commons.datasource.ResourceDataSource; import com.powsybl.commons.datasource.ResourceSet; import com.powsybl.iidm.network.*; @@ -18,7 +20,7 @@ import com.powsybl.ucte.network.UcteVoltageLevelCode; import org.junit.jupiter.api.Test; - +import java.nio.file.Path; import java.util.Properties; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -30,13 +32,15 @@ class NamingStrategyTest { @Test - void initTest() { - // Chargement du réseau original - ResourceDataSource dataSource = new ResourceDataSource("network2", new ResourceSet("/", "network2.xiidm")); + void ConvertStartegyTest(){ + ResourceDataSource dataSource = new ResourceDataSource("network3", new ResourceSet("/", "network3.xiidm")); Network n1 = Network.read(dataSource); - NamingStrategy s = new CounterNamingStrategy(); - s.convertNetworkToUcte(n1); - + UcteExporter exporter = new UcteExporter(); + Properties props = new Properties(); + props.put(UcteExporter.NAMING_STRATEGY,"Counter"); + //NamingStrategy s = new CounterNamingStrategy(); + //s.initialiseNetwork(n1); + //exporter.export(n1,props, new DirectoryDataSource(Path.of("./"),"test")); } From 92ab8f995b75990fdce8840fb5d212ce25fc4f9b Mon Sep 17 00:00:00 2001 From: Leclerc Clement Date: Wed, 13 Nov 2024 16:02:46 +0100 Subject: [PATCH 12/38] change Signed-off-by: Leclerc Clement --- .../ucte/converter/CounterNamingStrategy.java | 237 ++++++++++-------- .../ucte/converter/NamingStrategyTest.java | 17 -- 2 files changed, 137 insertions(+), 117 deletions(-) diff --git a/ucte/ucte-converter/src/main/java/com/powsybl/ucte/converter/CounterNamingStrategy.java b/ucte/ucte-converter/src/main/java/com/powsybl/ucte/converter/CounterNamingStrategy.java index 0f15f5d604c..334152093e4 100644 --- a/ucte/ucte-converter/src/main/java/com/powsybl/ucte/converter/CounterNamingStrategy.java +++ b/ucte/ucte-converter/src/main/java/com/powsybl/ucte/converter/CounterNamingStrategy.java @@ -29,13 +29,14 @@ public class CounterNamingStrategy implements NamingStrategy { private final Map ucteNodeIds = new HashMap<>(); private final Map ucteElementIds = new HashMap<>(); private int namingCounter; + private final List oderCode = Arrays.asList('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', + 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '_', '-', '.', ' '); @Override public String getName() { return "Counter"; } - /** * Converts all network elements to UCTE format using a sequential naming scheme. * The conversion process follows these steps: @@ -51,66 +52,35 @@ public String getName() { */ @Override public void initialiseNetwork(Network network) { - //count for naming each element namingCounter = 0; - // Convert voltage level elements - network.getVoltageLevelStream().sorted(Comparator.comparing(VoltageLevel::getId)).forEach(voltageLevel -> - convertVoltageLevelElementsIdToUcte(voltageLevel)); - // Convert network elements - convertNetworkElementsIdToUcte(network); - } + network.getSubstations().forEach(substation -> substation.getVoltageLevels().forEach(voltageLevel -> { - /** - * Converts all voltageLevel elements to UCTE format using a sequential naming scheme. - * The conversion process follows these steps: - *
      - *
    1. Convert all elements within each voltage level (buses, generators, loads, shunts)
    2. - *
    - * - * Each element receives a unique sequential identifier during the conversion process. - * - * @param voltageLevel the voltageLevel to convert - */ - private void convertVoltageLevelElementsIdToUcte(VoltageLevel voltageLevel) { - System.out.println(voltageLevel.getId()); - int nodeCounter = 0; - - for (Bus bus : voltageLevel.getBusBreakerView().getBuses()) { - nodeCounter++; - UcteNodeCode ucteId = generateUcteNodeId(bus.getId(), voltageLevel, nodeCounter); - if (ucteId != null) { - bus.setName(ucteId.toString()); + System.out.println(voltageLevel.getId()); + voltageLevel.getBusBreakerView().getBuses().forEach(bus -> { + generateUcteNodeId(bus.getId(), voltageLevel); logConversion("BUS", bus); - } - } - for (Switch s : voltageLevel.getBusBreakerView().getSwitches()) { - nodeCounter++; - UcteNodeCode ucteId = generateUcteNodeId(s.getId(), voltageLevel, nodeCounter); - if (ucteId != null) { - s.setName(ucteId.toString()); - logConversion("SWITCH", s); - } - } - - } + }); + voltageLevel.getBusBreakerView().getSwitches().forEach(sw -> { + if (sw.getKind().equals(SwitchKind.BREAKER)) { + generateUcteElementId(sw); + } else { + generateUcteNodeId(sw.getId(), voltageLevel); + } + logConversion("SW", sw); + }); + })); - /** - * Converts all network-level elements to UCTE format using a sequential naming scheme. - * The conversion process follows these steps: - *
      - *
    1. Convert network-level elements (lines, transformers)
    2. - *
    - * - * Each element receives a unique sequential identifier during the conversion process. - * - * @param network the network to convert - */ - private void convertNetworkElementsIdToUcte(Network network) { network.getBranchStream().forEach(branch -> { - UcteElementId ucteId = generateUcteElementId(branch); - branch.setName(ucteId.toString()); + generateUcteElementId(branch); logConversion(branch.getType().name(), branch); }); + + network.getDanglingLineStream().forEach(d -> { + generateUcteNodeId(d); + generateUcteElementId(d); + logConversion(d.getType().name(), d); + }); + } private void logConversion(String elementType, Identifiable element) { @@ -137,61 +107,92 @@ private void logConversion(String elementType, Identifiable element) { * * @param nodeId original identifier of the network element * @param voltageLevel voltage level containing the element - * @param nodeCounter code counter * @return generated UcteNodeCode, or null if nodeId already exists in ucteNodeIds */ - private UcteNodeCode generateUcteNodeId(String nodeId, VoltageLevel voltageLevel, int nodeCounter) { + private UcteNodeCode generateUcteNodeId(String nodeId, VoltageLevel voltageLevel) { if (ucteNodeIds.containsKey(nodeId)) { - return null; + return ucteNodeIds.get(nodeId); } + if (namingCounter > 9999) { + namingCounter = 0; + } + StringBuilder newNodeCode = new StringBuilder(8); String newNodeId = String.format("%05d", ++namingCounter); char countryCode = getCountryCode(voltageLevel); char voltageLevelCode = getUcteVoltageLevelCode(voltageLevel.getNominalV()); + char orderCode = oderCode.get(0); + int i = 0; newNodeCode .append(countryCode) .append(newNodeId) .append(voltageLevelCode) - .append(nodeCounter); + .append(orderCode); + while (ucteNodeIds.containsKey(newNodeId)) { + i++; + orderCode = oderCode.get(i); + newNodeCode.replace(7, 7, String.valueOf(orderCode)); + } - UcteNodeCode nodeCode = UcteNodeCode.parseUcteNodeCode(newNodeCode.toString()).get(); - ucteNodeIds.put(nodeId, nodeCode); - return nodeCode; + Optional nodeCode = UcteNodeCode.parseUcteNodeCode(newNodeCode.toString()); + if (!nodeCode.isPresent()) { + System.out.println("le code en erreur est : " + newNodeCode); + throw new IllegalArgumentException("Invalid node code: " + newNodeCode); + } + ucteNodeIds.put(nodeId, nodeCode.get()); + return nodeCode.get(); } - /** - * Generates a UCTE element identifier for a network branch element (line or transformer). - * The element ID is composed of two node codes and an order code. - * - *

    The generated ID follows UCTE format specifications: - * [Node1Code][Node2Code][OrderCode] - * where: - *

      - *
    • Node1Code: UCTE code of the first terminal
    • - *
    • Node2Code: UCTE code of the second terminal
    • - *
    • OrderCode: Single character identifier
    • - *
    - * - * @param branch the element instance of branch - * @return the generated UCTE element identifier - */ - private UcteElementId generateUcteElementId(Branch branch) { + private UcteNodeCode generateUcteNodeId(DanglingLine danglingLine) { + + String nodeId = danglingLine.getPairingKey(); + + if (ucteNodeIds.containsKey(nodeId)) { + return ucteNodeIds.get(nodeId); + } + + if (namingCounter > 9999) { + namingCounter = 0; + } + + StringBuilder newNodeCode = new StringBuilder(8); + String newNodeId = String.format("%05d", ++namingCounter); + char voltageLevelCode = getUcteVoltageLevelCode(danglingLine.getTerminal().getVoltageLevel().getNominalV()); + + newNodeCode + .append('X') + .append(newNodeId) + .append(voltageLevelCode) + .append('0'); int i = 0; - List oderCode = Arrays.asList('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', - 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '_', '-', '.', ' '); + while (ucteNodeIds.containsKey(newNodeId)) { + i++; + if (i >= oderCode.size()) { + throw new UcteException("Too many nodes with same prefix"); + } + newNodeCode.setCharAt(7, oderCode.get(i)); + } - String branchId = branch.getId(); - if (ucteElementIds.containsKey(branchId)) { - return null; + Optional nodeCode = UcteNodeCode.parseUcteNodeCode(newNodeCode.toString()); + if (!nodeCode.isPresent()) { + throw new IllegalArgumentException("Invalid node code: " + newNodeCode); } - UcteNodeCode node1 = ucteNodeIds.get(branch.getTerminal1().getBusBreakerView().getBus().getId()); - UcteNodeCode node2 = ucteNodeIds.get(branch.getTerminal2().getBusBreakerView().getBus().getId()); - char orderCode = oderCode.get(0); + ucteNodeIds.put(nodeId, nodeCode.get()); + return nodeCode.get(); + } + + private UcteElementId generateUcteElementId(String id, UcteNodeCode node1, UcteNodeCode node2) { + if (ucteElementIds.containsKey(id)) { + return ucteElementIds.get(id); + } + int i = 0; + + char orderCode = oderCode.get(0); UcteElementId ucteElementId = new UcteElementId(node1, node2, orderCode); while (ucteElementIds.containsValue(ucteElementId)) { @@ -202,23 +203,59 @@ private UcteElementId generateUcteElementId(Branch branch) { ucteElementId = new UcteElementId(node1, node2, orderCode); } - ucteElementIds.put(branchId, ucteElementId); + ucteElementIds.put(id, ucteElementId); return ucteElementId; } - /** - * Extracts and converts a country code to UCTE format for a given voltage level. - * - *

    The country code is determined through the following process: - *

      - *
    1. Attempts to get the country from the voltage level's substation
    2. - *
    3. Maps the country name to its corresponding UCTE code
    4. - *
    5. Returns 'X' as fallback if no valid mapping is found
    6. - *
    - * - * @param voltageLevel the voltage level from which to extract the country code - * @return the UCTE country code character, or 'X' if no valid country is found) - */ + private UcteElementId generateUcteElementId(Branch branch) { + if (ucteElementIds.containsKey(branch.getId())) { + return ucteElementIds.get(branch.getId()); + } + UcteNodeCode node1 = this.ucteNodeIds.get(branch.getTerminal1().getBusBreakerView().getBus().getId()); + UcteNodeCode node2 = this.ucteNodeIds.get(branch.getTerminal2().getBusBreakerView().getBus().getId()); + + return generateUcteElementId(branch.getId(), node1, node2); + } + + private UcteElementId generateUcteElementId(DanglingLine danglingLine) { + if (ucteElementIds.containsKey(danglingLine.getId())) { + return ucteElementIds.get(danglingLine.getId()); + } + + for (Terminal t : danglingLine.getTerminals()) { + System.out.println(t.getBusBreakerView().getBus().getId()); + System.out.println(danglingLine.getPairingKey()); + } + UcteNodeCode node1 = this.ucteNodeIds.get(danglingLine.getTerminal().getBusBreakerView().getBus().getId()); + UcteNodeCode node2 = this.ucteNodeIds.get(danglingLine.getPairingKey()); + + return generateUcteElementId(danglingLine.getId(), node1, node2); + } + + private UcteElementId generateUcteElementId(Switch sw) { + + if (ucteElementIds.containsKey(sw.getId())) { + return ucteElementIds.get(sw.getId()); + } + + UcteNodeCode node1 = generateUcteNodeId(sw.getId(), sw.getVoltageLevel()); + UcteNodeCode node2 = generateUcteNodeId(sw.getId(), sw.getVoltageLevel()); + return generateUcteElementId(sw.getId(), node1, node2); + } + + /** + * Extracts and converts a country code to UCTE format for a given voltage level. + * + *

    The country code is determined through the following process: + *

      + *
    1. Attempts to get the country from the voltage level's substation
    2. + *
    3. Maps the country name to its corresponding UCTE code
    4. + *
    5. Returns 'X' as fallback if no valid mapping is found
    6. + *
    + * + * @param voltageLevel the voltage level from which to extract the country code + * @return the UCTE country code character, or 'X' if no valid country is found) + */ private static char getCountryCode(VoltageLevel voltageLevel) { String country = voltageLevel.getSubstation() diff --git a/ucte/ucte-converter/src/test/java/com/powsybl/ucte/converter/NamingStrategyTest.java b/ucte/ucte-converter/src/test/java/com/powsybl/ucte/converter/NamingStrategyTest.java index e581f81c2cb..00736736edd 100644 --- a/ucte/ucte-converter/src/test/java/com/powsybl/ucte/converter/NamingStrategyTest.java +++ b/ucte/ucte-converter/src/test/java/com/powsybl/ucte/converter/NamingStrategyTest.java @@ -8,9 +8,6 @@ package com.powsybl.ucte.converter; -import com.fasterxml.jackson.databind.annotation.JsonAppend; -import com.powsybl.commons.datasource.DataSource; -import com.powsybl.commons.datasource.DirectoryDataSource; import com.powsybl.commons.datasource.ResourceDataSource; import com.powsybl.commons.datasource.ResourceSet; import com.powsybl.iidm.network.*; @@ -20,7 +17,6 @@ import com.powsybl.ucte.network.UcteVoltageLevelCode; import org.junit.jupiter.api.Test; -import java.nio.file.Path; import java.util.Properties; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -31,19 +27,6 @@ */ class NamingStrategyTest { - @Test - void ConvertStartegyTest(){ - ResourceDataSource dataSource = new ResourceDataSource("network3", new ResourceSet("/", "network3.xiidm")); - Network n1 = Network.read(dataSource); - UcteExporter exporter = new UcteExporter(); - Properties props = new Properties(); - props.put(UcteExporter.NAMING_STRATEGY,"Counter"); - //NamingStrategy s = new CounterNamingStrategy(); - //s.initialiseNetwork(n1); - //exporter.export(n1,props, new DirectoryDataSource(Path.of("./"),"test")); - - } - @Test void testUcteCode() { NamingStrategy strategy = new DefaultNamingStrategy(); From b8f8027ec72c4649ce543328079622b0c730d0bb Mon Sep 17 00:00:00 2001 From: Leclerc Clement Date: Mon, 18 Nov 2024 10:21:25 +0100 Subject: [PATCH 13/38] add LOGGER and update with change request Signed-off-by: Leclerc Clement --- .../ucte/converter/CounterNamingStrategy.java | 109 ++++++++---------- 1 file changed, 51 insertions(+), 58 deletions(-) diff --git a/ucte/ucte-converter/src/main/java/com/powsybl/ucte/converter/CounterNamingStrategy.java b/ucte/ucte-converter/src/main/java/com/powsybl/ucte/converter/CounterNamingStrategy.java index 334152093e4..94f9e79c811 100644 --- a/ucte/ucte-converter/src/main/java/com/powsybl/ucte/converter/CounterNamingStrategy.java +++ b/ucte/ucte-converter/src/main/java/com/powsybl/ucte/converter/CounterNamingStrategy.java @@ -1,5 +1,5 @@ /** - * Copyright (c) 2019, RTE (http://www.rte-france.com) + * Copyright (c) 2024, 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/. @@ -14,6 +14,8 @@ import com.powsybl.ucte.network.UcteElementId; import com.powsybl.ucte.network.UcteNodeCode; import com.powsybl.ucte.network.UcteVoltageLevelCode; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import java.util.*; @@ -29,36 +31,38 @@ public class CounterNamingStrategy implements NamingStrategy { private final Map ucteNodeIds = new HashMap<>(); private final Map ucteElementIds = new HashMap<>(); private int namingCounter; - private final List oderCode = Arrays.asList('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', + private static final List ORDER_CODES = Arrays.asList('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '_', '-', '.', ' '); + private static final Logger LOGGER = LoggerFactory.getLogger(CounterNamingStrategy.class); @Override public String getName() { return "Counter"; } -/** - * Converts all network elements to UCTE format using a sequential naming scheme. - * The conversion process follows these steps: - *
      - *
    1. Reset the global naming counter to ensure unique identifiers
    2. - *
    3. Convert all elements within each voltage level - *
    4. Convert network-level elements - *
    - * - * Each element receives a unique sequential identifier during the conversion process. - * - * @param network the network to convert - */ + /** + * Converts all network elements to UCTE format using a sequential naming scheme. + * The conversion process follows these steps: + *
      + *
    1. Reset the global naming counter to ensure unique identifiers
    2. + *
    3. Convert all elements within each voltage level + *
    4. Convert network-level elements + *
    + * + * Each element receives a unique sequential identifier during the conversion process. + * + * @param network the network to convert + */ @Override public void initialiseNetwork(Network network) { namingCounter = 0; network.getSubstations().forEach(substation -> substation.getVoltageLevels().forEach(voltageLevel -> { - System.out.println(voltageLevel.getId()); + LOGGER.info("VOLTAGE LEVEL : " + voltageLevel.getId()); + voltageLevel.getBusBreakerView().getBuses().forEach(bus -> { generateUcteNodeId(bus.getId(), voltageLevel); - logConversion("BUS", bus); + LOGGER.info("BUS " + bus.getId() + " : " + ucteNodeIds.get(bus.getId())); }); voltageLevel.getBusBreakerView().getSwitches().forEach(sw -> { if (sw.getKind().equals(SwitchKind.BREAKER)) { @@ -66,31 +70,20 @@ public void initialiseNetwork(Network network) { } else { generateUcteNodeId(sw.getId(), voltageLevel); } - logConversion("SW", sw); + LOGGER.info("SWITCH : " + sw.getId() + " : " + ucteNodeIds.get(sw.getId())); }); })); network.getBranchStream().forEach(branch -> { generateUcteElementId(branch); - logConversion(branch.getType().name(), branch); + LOGGER.info(branch.getType() + " " + branch.getId() + " : " + ucteElementIds.get(branch.getId())); }); network.getDanglingLineStream().forEach(d -> { generateUcteNodeId(d); generateUcteElementId(d); - logConversion(d.getType().name(), d); + LOGGER.info("DanglingLine " + d.getId() + " : " + ucteElementIds.get(d.getId())); }); - - } - - private void logConversion(String elementType, Identifiable element) { - System.out.println(String.format("%s : %s--> %s %s", - elementType, - element.getId(), - ucteNodeIds.get(element.getId()), - ucteElementIds.get(element.getId()) - )); - } /** @@ -111,10 +104,15 @@ private void logConversion(String elementType, Identifiable element) { */ private UcteNodeCode generateUcteNodeId(String nodeId, VoltageLevel voltageLevel) { + if (UcteNodeCode.isUcteNodeId(nodeId)) { + UcteNodeCode ucteNodeCode = UcteNodeCode.parseUcteNodeCode(nodeId).get(); + ucteNodeIds.put(nodeId, ucteNodeCode); + return ucteNodeCode; + } if (ucteNodeIds.containsKey(nodeId)) { return ucteNodeIds.get(nodeId); } - if (namingCounter > 9999) { + if (namingCounter > 99999) { namingCounter = 0; } @@ -122,7 +120,7 @@ private UcteNodeCode generateUcteNodeId(String nodeId, VoltageLevel voltageLevel String newNodeId = String.format("%05d", ++namingCounter); char countryCode = getCountryCode(voltageLevel); char voltageLevelCode = getUcteVoltageLevelCode(voltageLevel.getNominalV()); - char orderCode = oderCode.get(0); + char orderCode = ORDER_CODES.get(0); int i = 0; newNodeCode @@ -130,15 +128,14 @@ private UcteNodeCode generateUcteNodeId(String nodeId, VoltageLevel voltageLevel .append(newNodeId) .append(voltageLevelCode) .append(orderCode); - while (ucteNodeIds.containsKey(newNodeId)) { + while (ucteNodeIds.containsValue(newNodeCode)) { i++; - orderCode = oderCode.get(i); + orderCode = ORDER_CODES.get(i); newNodeCode.replace(7, 7, String.valueOf(orderCode)); } Optional nodeCode = UcteNodeCode.parseUcteNodeCode(newNodeCode.toString()); if (!nodeCode.isPresent()) { - System.out.println("le code en erreur est : " + newNodeCode); throw new IllegalArgumentException("Invalid node code: " + newNodeCode); } ucteNodeIds.put(nodeId, nodeCode.get()); @@ -170,10 +167,10 @@ private UcteNodeCode generateUcteNodeId(DanglingLine danglingLine) { int i = 0; while (ucteNodeIds.containsKey(newNodeId)) { i++; - if (i >= oderCode.size()) { + if (i >= ORDER_CODES.size()) { throw new UcteException("Too many nodes with same prefix"); } - newNodeCode.setCharAt(7, oderCode.get(i)); + newNodeCode.setCharAt(7, ORDER_CODES.get(i)); } Optional nodeCode = UcteNodeCode.parseUcteNodeCode(newNodeCode.toString()); @@ -192,13 +189,13 @@ private UcteElementId generateUcteElementId(String id, UcteNodeCode node1, UcteN } int i = 0; - char orderCode = oderCode.get(0); + char orderCode = ORDER_CODES.get(0); UcteElementId ucteElementId = new UcteElementId(node1, node2, orderCode); while (ucteElementIds.containsValue(ucteElementId)) { i++; - if (i < oderCode.size()) { - orderCode = oderCode.get(i); + if (i < ORDER_CODES.size()) { + orderCode = ORDER_CODES.get(i); } ucteElementId = new UcteElementId(node1, node2, orderCode); } @@ -222,10 +219,6 @@ private UcteElementId generateUcteElementId(DanglingLine danglingLine) { return ucteElementIds.get(danglingLine.getId()); } - for (Terminal t : danglingLine.getTerminals()) { - System.out.println(t.getBusBreakerView().getBus().getId()); - System.out.println(danglingLine.getPairingKey()); - } UcteNodeCode node1 = this.ucteNodeIds.get(danglingLine.getTerminal().getBusBreakerView().getBus().getId()); UcteNodeCode node2 = this.ucteNodeIds.get(danglingLine.getPairingKey()); @@ -243,19 +236,19 @@ private UcteElementId generateUcteElementId(Switch sw) { return generateUcteElementId(sw.getId(), node1, node2); } - /** - * Extracts and converts a country code to UCTE format for a given voltage level. - * - *

    The country code is determined through the following process: - *

      - *
    1. Attempts to get the country from the voltage level's substation
    2. - *
    3. Maps the country name to its corresponding UCTE code
    4. - *
    5. Returns 'X' as fallback if no valid mapping is found
    6. - *
    - * - * @param voltageLevel the voltage level from which to extract the country code - * @return the UCTE country code character, or 'X' if no valid country is found) - */ + /** + * Extracts and converts a country code to UCTE format for a given voltage level. + * + *

    The country code is determined through the following process: + *

      + *
    1. Attempts to get the country from the voltage level's substation
    2. + *
    3. Maps the country name to its corresponding UCTE code
    4. + *
    5. Returns 'X' as fallback if no valid mapping is found
    6. + *
    + * + * @param voltageLevel the voltage level from which to extract the country code + * @return the UCTE country code character, or 'X' if no valid country is found) + * */ private static char getCountryCode(VoltageLevel voltageLevel) { String country = voltageLevel.getSubstation() From 1dec4705994f3949236aaffb29ce2f9b880ba070 Mon Sep 17 00:00:00 2001 From: Leclerc Clement Date: Mon, 18 Nov 2024 11:41:22 +0100 Subject: [PATCH 14/38] change dangling lines code generation Signed-off-by: Leclerc Clement --- .../ucte/converter/CounterNamingStrategy.java | 64 ++++--------------- 1 file changed, 12 insertions(+), 52 deletions(-) diff --git a/ucte/ucte-converter/src/main/java/com/powsybl/ucte/converter/CounterNamingStrategy.java b/ucte/ucte-converter/src/main/java/com/powsybl/ucte/converter/CounterNamingStrategy.java index 94f9e79c811..7472e8ec933 100644 --- a/ucte/ucte-converter/src/main/java/com/powsybl/ucte/converter/CounterNamingStrategy.java +++ b/ucte/ucte-converter/src/main/java/com/powsybl/ucte/converter/CounterNamingStrategy.java @@ -65,12 +65,8 @@ public void initialiseNetwork(Network network) { LOGGER.info("BUS " + bus.getId() + " : " + ucteNodeIds.get(bus.getId())); }); voltageLevel.getBusBreakerView().getSwitches().forEach(sw -> { - if (sw.getKind().equals(SwitchKind.BREAKER)) { - generateUcteElementId(sw); - } else { - generateUcteNodeId(sw.getId(), voltageLevel); - } - LOGGER.info("SWITCH : " + sw.getId() + " : " + ucteNodeIds.get(sw.getId())); + generateUcteElementId(sw); + LOGGER.info("SWITCH : " + sw.getId() + " : " + ucteElementIds.get(sw.getId())); }); })); @@ -80,7 +76,6 @@ public void initialiseNetwork(Network network) { }); network.getDanglingLineStream().forEach(d -> { - generateUcteNodeId(d); generateUcteElementId(d); LOGGER.info("DanglingLine " + d.getId() + " : " + ucteElementIds.get(d.getId())); }); @@ -142,46 +137,6 @@ private UcteNodeCode generateUcteNodeId(String nodeId, VoltageLevel voltageLevel return nodeCode.get(); } - private UcteNodeCode generateUcteNodeId(DanglingLine danglingLine) { - - String nodeId = danglingLine.getPairingKey(); - - if (ucteNodeIds.containsKey(nodeId)) { - return ucteNodeIds.get(nodeId); - } - - if (namingCounter > 9999) { - namingCounter = 0; - } - - StringBuilder newNodeCode = new StringBuilder(8); - String newNodeId = String.format("%05d", ++namingCounter); - char voltageLevelCode = getUcteVoltageLevelCode(danglingLine.getTerminal().getVoltageLevel().getNominalV()); - - newNodeCode - .append('X') - .append(newNodeId) - .append(voltageLevelCode) - .append('0'); - - int i = 0; - while (ucteNodeIds.containsKey(newNodeId)) { - i++; - if (i >= ORDER_CODES.size()) { - throw new UcteException("Too many nodes with same prefix"); - } - newNodeCode.setCharAt(7, ORDER_CODES.get(i)); - } - - Optional nodeCode = UcteNodeCode.parseUcteNodeCode(newNodeCode.toString()); - if (!nodeCode.isPresent()) { - throw new IllegalArgumentException("Invalid node code: " + newNodeCode); - } - - ucteNodeIds.put(nodeId, nodeCode.get()); - return nodeCode.get(); - } - private UcteElementId generateUcteElementId(String id, UcteNodeCode node1, UcteNodeCode node2) { if (ucteElementIds.containsKey(id)) { @@ -219,8 +174,10 @@ private UcteElementId generateUcteElementId(DanglingLine danglingLine) { return ucteElementIds.get(danglingLine.getId()); } - UcteNodeCode node1 = this.ucteNodeIds.get(danglingLine.getTerminal().getBusBreakerView().getBus().getId()); - UcteNodeCode node2 = this.ucteNodeIds.get(danglingLine.getPairingKey()); + generateUcteNodeId(danglingLine.getPairingKey(), danglingLine.getTerminal().getVoltageLevel()); + generateUcteNodeId(danglingLine.getTerminal().getBusBreakerView().getBus().getId(), danglingLine.getTerminal().getVoltageLevel()); + UcteNodeCode node1 = this.ucteNodeIds.get(danglingLine.getPairingKey()); + UcteNodeCode node2 = this.ucteNodeIds.get(danglingLine.getTerminal().getBusBreakerView().getBus().getId()); return generateUcteElementId(danglingLine.getId(), node1, node2); } @@ -231,9 +188,12 @@ private UcteElementId generateUcteElementId(Switch sw) { return ucteElementIds.get(sw.getId()); } - UcteNodeCode node1 = generateUcteNodeId(sw.getId(), sw.getVoltageLevel()); - UcteNodeCode node2 = generateUcteNodeId(sw.getId(), sw.getVoltageLevel()); - return generateUcteElementId(sw.getId(), node1, node2); + Bus bus1 = sw.getVoltageLevel().getBusBreakerView().getBus1(sw.getId()); + Bus bus2 = sw.getVoltageLevel().getBusBreakerView().getBus2(sw.getId()); + UcteNodeCode u1 = generateUcteNodeId(bus1.getId(), bus1.getVoltageLevel()); + UcteNodeCode u2 = generateUcteNodeId(bus2.getId(), bus2.getVoltageLevel()); + + return generateUcteElementId(sw.getId(), u1, u2); } /** From cd42dcd025b7e8fc210da3fed14265a9f1e6dfad Mon Sep 17 00:00:00 2001 From: Leclerc Clement Date: Mon, 18 Nov 2024 14:30:06 +0100 Subject: [PATCH 15/38] change nodeID generation Signed-off-by: Leclerc Clement --- .../ucte/converter/CounterNamingStrategy.java | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/ucte/ucte-converter/src/main/java/com/powsybl/ucte/converter/CounterNamingStrategy.java b/ucte/ucte-converter/src/main/java/com/powsybl/ucte/converter/CounterNamingStrategy.java index 7472e8ec933..e5dc173e71e 100644 --- a/ucte/ucte-converter/src/main/java/com/powsybl/ucte/converter/CounterNamingStrategy.java +++ b/ucte/ucte-converter/src/main/java/com/powsybl/ucte/converter/CounterNamingStrategy.java @@ -59,7 +59,6 @@ public void initialiseNetwork(Network network) { network.getSubstations().forEach(substation -> substation.getVoltageLevels().forEach(voltageLevel -> { LOGGER.info("VOLTAGE LEVEL : " + voltageLevel.getId()); - voltageLevel.getBusBreakerView().getBuses().forEach(bus -> { generateUcteNodeId(bus.getId(), voltageLevel); LOGGER.info("BUS " + bus.getId() + " : " + ucteNodeIds.get(bus.getId())); @@ -107,12 +106,9 @@ private UcteNodeCode generateUcteNodeId(String nodeId, VoltageLevel voltageLevel if (ucteNodeIds.containsKey(nodeId)) { return ucteNodeIds.get(nodeId); } - if (namingCounter > 99999) { - namingCounter = 0; - } StringBuilder newNodeCode = new StringBuilder(8); - String newNodeId = String.format("%05d", ++namingCounter); + String newNodeId = generateIDFromCounter(); char countryCode = getCountryCode(voltageLevel); char voltageLevelCode = getUcteVoltageLevelCode(voltageLevel.getNominalV()); char orderCode = ORDER_CODES.get(0); @@ -225,6 +221,16 @@ private static char getCountryCode(VoltageLevel voltageLevel) { return 'X'; } + private String generateIDFromCounter() { + namingCounter++; + if (namingCounter > 99999) { + int baseNumber = (namingCounter - 100000) % 10000; + char letter = (char) ('A' + ((namingCounter - 100000) / 10000)); + return String.format("%04d%c", baseNumber, letter); + } + return String.format("%05d", namingCounter); + } + /** * Determines the UCTE voltage level code for a given voltage value. * The code is determined by finding the closest standard UCTE voltage level. From 609becee01636cf96846ddeb2142ea9f88685868 Mon Sep 17 00:00:00 2001 From: Leclerc Clement Date: Tue, 19 Nov 2024 17:31:46 +0100 Subject: [PATCH 16/38] delete LOGGER Signed-off-by: Leclerc Clement --- .../ucte/converter/CounterNamingStrategy.java | 65 +++++++------------ .../powsybl/ucte/converter/UcteExporter.java | 2 +- 2 files changed, 25 insertions(+), 42 deletions(-) diff --git a/ucte/ucte-converter/src/main/java/com/powsybl/ucte/converter/CounterNamingStrategy.java b/ucte/ucte-converter/src/main/java/com/powsybl/ucte/converter/CounterNamingStrategy.java index e5dc173e71e..92ab29e609d 100644 --- a/ucte/ucte-converter/src/main/java/com/powsybl/ucte/converter/CounterNamingStrategy.java +++ b/ucte/ucte-converter/src/main/java/com/powsybl/ucte/converter/CounterNamingStrategy.java @@ -9,13 +9,12 @@ package com.powsybl.ucte.converter; import com.google.auto.service.AutoService; +import com.powsybl.commons.PowsyblException; import com.powsybl.iidm.network.*; import com.powsybl.ucte.network.UcteCountryCode; import com.powsybl.ucte.network.UcteElementId; import com.powsybl.ucte.network.UcteNodeCode; import com.powsybl.ucte.network.UcteVoltageLevelCode; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; import java.util.*; @@ -31,9 +30,8 @@ public class CounterNamingStrategy implements NamingStrategy { private final Map ucteNodeIds = new HashMap<>(); private final Map ucteElementIds = new HashMap<>(); private int namingCounter; - private static final List ORDER_CODES = Arrays.asList('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', + private static final List ORDER_CODES = Arrays.asList('1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '_', '-', '.', ' '); - private static final Logger LOGGER = LoggerFactory.getLogger(CounterNamingStrategy.class); @Override public String getName() { @@ -58,25 +56,20 @@ public void initialiseNetwork(Network network) { namingCounter = 0; network.getSubstations().forEach(substation -> substation.getVoltageLevels().forEach(voltageLevel -> { - LOGGER.info("VOLTAGE LEVEL : " + voltageLevel.getId()); voltageLevel.getBusBreakerView().getBuses().forEach(bus -> { generateUcteNodeId(bus.getId(), voltageLevel); - LOGGER.info("BUS " + bus.getId() + " : " + ucteNodeIds.get(bus.getId())); }); voltageLevel.getBusBreakerView().getSwitches().forEach(sw -> { generateUcteElementId(sw); - LOGGER.info("SWITCH : " + sw.getId() + " : " + ucteElementIds.get(sw.getId())); }); })); network.getBranchStream().forEach(branch -> { generateUcteElementId(branch); - LOGGER.info(branch.getType() + " " + branch.getId() + " : " + ucteElementIds.get(branch.getId())); }); network.getDanglingLineStream().forEach(d -> { generateUcteElementId(d); - LOGGER.info("DanglingLine " + d.getId() + " : " + ucteElementIds.get(d.getId())); }); } @@ -99,9 +92,13 @@ public void initialiseNetwork(Network network) { private UcteNodeCode generateUcteNodeId(String nodeId, VoltageLevel voltageLevel) { if (UcteNodeCode.isUcteNodeId(nodeId)) { - UcteNodeCode ucteNodeCode = UcteNodeCode.parseUcteNodeCode(nodeId).get(); - ucteNodeIds.put(nodeId, ucteNodeCode); - return ucteNodeCode; + Optional ucteNodeCode = UcteNodeCode.parseUcteNodeCode(nodeId); + if (ucteNodeCode.isPresent()) { + ucteNodeIds.put(nodeId, ucteNodeCode.get()); + return ucteNodeCode.get(); + } else { + throw new UcteException("Invalid ucte node code: " + nodeId); + } } if (ucteNodeIds.containsKey(nodeId)) { return ucteNodeIds.get(nodeId); @@ -109,7 +106,7 @@ private UcteNodeCode generateUcteNodeId(String nodeId, VoltageLevel voltageLevel StringBuilder newNodeCode = new StringBuilder(8); String newNodeId = generateIDFromCounter(); - char countryCode = getCountryCode(voltageLevel); + char countryCode = getCountryCode(voltageLevel).getUcteCode(); char voltageLevelCode = getUcteVoltageLevelCode(voltageLevel.getNominalV()); char orderCode = ORDER_CODES.get(0); int i = 0; @@ -119,7 +116,8 @@ private UcteNodeCode generateUcteNodeId(String nodeId, VoltageLevel voltageLevel .append(newNodeId) .append(voltageLevelCode) .append(orderCode); - while (ucteNodeIds.containsValue(newNodeCode)) { + + while (ucteNodeIds.containsValue(newNodeCode.toString())) { i++; orderCode = ORDER_CODES.get(i); newNodeCode.replace(7, 7, String.valueOf(orderCode)); @@ -192,33 +190,18 @@ private UcteElementId generateUcteElementId(Switch sw) { return generateUcteElementId(sw.getId(), u1, u2); } - /** - * Extracts and converts a country code to UCTE format for a given voltage level. - * - *

    The country code is determined through the following process: - *

      - *
    1. Attempts to get the country from the voltage level's substation
    2. - *
    3. Maps the country name to its corresponding UCTE code
    4. - *
    5. Returns 'X' as fallback if no valid mapping is found
    6. - *
    - * - * @param voltageLevel the voltage level from which to extract the country code - * @return the UCTE country code character, or 'X' if no valid country is found) - * */ - private static char getCountryCode(VoltageLevel voltageLevel) { - - String country = voltageLevel.getSubstation() - .map(s -> s.getCountry() - .map(Country::getName) - .orElse("XX")) - .orElse("XX"); - - for (UcteCountryCode countryCode : UcteCountryCode.values()) { - if (country.equalsIgnoreCase(countryCode.getPrettyName())) { - return countryCode.getUcteCode(); - } + private UcteCountryCode getCountryCode(VoltageLevel voltageLevel) { + + UcteCountryCode ucteCountryCode; + Country country = voltageLevel.getSubstation().get().getCountry().orElseThrow(() -> new com.powsybl.ucte.network.UcteException("No country for this substation")); + try { + ucteCountryCode = UcteCountryCode.valueOf(country.name()); + } catch (IllegalArgumentException iae) { + throw new UcteException("No UCTE country code for " + country.getName()); } - return 'X'; + + return ucteCountryCode; + } private String generateIDFromCounter() { @@ -270,7 +253,7 @@ private static char getUcteVoltageLevelCode(double voltage) { @Override public UcteNodeCode getUcteNodeCode(String id) { if (id == null) { - return null; + throw new PowsyblException("ID is null" + id); } UcteNodeCode code = ucteNodeIds.get(id); diff --git a/ucte/ucte-converter/src/main/java/com/powsybl/ucte/converter/UcteExporter.java b/ucte/ucte-converter/src/main/java/com/powsybl/ucte/converter/UcteExporter.java index 869c54f3362..752cb67dc98 100644 --- a/ucte/ucte-converter/src/main/java/com/powsybl/ucte/converter/UcteExporter.java +++ b/ucte/ucte-converter/src/main/java/com/powsybl/ucte/converter/UcteExporter.java @@ -140,7 +140,7 @@ private static UcteNetwork createUcteNetwork(Network network, NamingStrategy nam network.getHvdcLineCount() > 0 || network.getThreeWindingsTransformerCount() > 0) { - //throw new UcteException("This network contains unsupported equipments"); + throw new UcteException("This network contains unsupported equipments"); } UcteExporterContext context = new UcteExporterContext(namingStrategy, combinePhaseAngleRegulation); From 8f159d3efd2f02ae8592a1052b6f293a5f736f52 Mon Sep 17 00:00:00 2001 From: clementleclerc Date: Thu, 21 Nov 2024 07:55:42 +0100 Subject: [PATCH 17/38] code optimisation Signed-off-by: clementleclerc --- .../ucte/converter/CounterNamingStrategy.java | 304 +++++++----------- 1 file changed, 118 insertions(+), 186 deletions(-) diff --git a/ucte/ucte-converter/src/main/java/com/powsybl/ucte/converter/CounterNamingStrategy.java b/ucte/ucte-converter/src/main/java/com/powsybl/ucte/converter/CounterNamingStrategy.java index 92ab29e609d..0e51267d4e0 100644 --- a/ucte/ucte-converter/src/main/java/com/powsybl/ucte/converter/CounterNamingStrategy.java +++ b/ucte/ucte-converter/src/main/java/com/powsybl/ucte/converter/CounterNamingStrategy.java @@ -1,11 +1,3 @@ -/** - * Copyright (c) 2024, 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/. - * SPDX-License-Identifier: MPL-2.0 - */ - package com.powsybl.ucte.converter; import com.google.auto.service.AutoService; @@ -18,148 +10,151 @@ import java.util.*; - -/** - * A {@link NamingStrategy} implementation that ensures the conformity of IDs with the UCTE-DEF format - * - * @author Clément LECLERC {@literal } - */ @AutoService(NamingStrategy.class) public class CounterNamingStrategy implements NamingStrategy { + private static final List ORDER_CODES = List.of('1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', + 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '_', '-', '.', ' '); + private static final String NO_COUNTRY_ERROR = "No country for this substation"; + private static final String NO_UCTE_CODE_ERROR = "No UCTE code found for id: %s"; + private static final String NO_UCTE_COUNTRY_ERROR = "No UCTE country code for %s"; + private static final String INVALID_NODE_CODE_ERROR = "Invalid ucte node code: %s"; + private static final char DEFAULT_VOLTAGE_CODE = '7'; + private static final int MAX_COUNTER = 99999; + private static final int BASE_LETTER_OFFSET = 100000; + private static final int MODULO_VALUE = 10000; private final Map ucteNodeIds = new HashMap<>(); private final Map ucteElementIds = new HashMap<>(); private int namingCounter; - private static final List ORDER_CODES = Arrays.asList('1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', - 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '_', '-', '.', ' '); @Override public String getName() { return "Counter"; } - /** - * Converts all network elements to UCTE format using a sequential naming scheme. - * The conversion process follows these steps: - *
      - *
    1. Reset the global naming counter to ensure unique identifiers
    2. - *
    3. Convert all elements within each voltage level - *
    4. Convert network-level elements - *
    - * - * Each element receives a unique sequential identifier during the conversion process. - * - * @param network the network to convert - */ @Override public void initialiseNetwork(Network network) { namingCounter = 0; - network.getSubstations().forEach(substation -> substation.getVoltageLevels().forEach(voltageLevel -> { - - voltageLevel.getBusBreakerView().getBuses().forEach(bus -> { - generateUcteNodeId(bus.getId(), voltageLevel); - }); - voltageLevel.getBusBreakerView().getSwitches().forEach(sw -> { - generateUcteElementId(sw); - }); - })); - - network.getBranchStream().forEach(branch -> { - generateUcteElementId(branch); - }); - - network.getDanglingLineStream().forEach(d -> { - generateUcteElementId(d); - }); + network.getSubstationStream() + .flatMap(s -> s.getVoltageLevelStream()) + .forEach(this::processVoltageLevel); + + network.getBranchStream().forEach(this::generateUcteElementId); + network.getDanglingLineStream().forEach(this::generateUcteElementId); } - /** - * Generates a UCTE node code for a network element. - * The generation process involves: - *
      - *
    1. Checking for existing node ID to avoid duplicates
    2. - *
    3. Extracting country code from the voltage level
    4. - *
    5. Determining voltage level code based on nominal voltage
    6. - *
    7. Creating a new node ID using namingCounter
    8. - *
    - * - * The generated UCTE node code follows the format: [CountryCode][NodeId][VoltageLevelCode] - * - * @param nodeId original identifier of the network element - * @param voltageLevel voltage level containing the element - * @return generated UcteNodeCode, or null if nodeId already exists in ucteNodeIds - */ - private UcteNodeCode generateUcteNodeId(String nodeId, VoltageLevel voltageLevel) { + private void processVoltageLevel(VoltageLevel voltageLevel) { + voltageLevel.getBusBreakerView().getBuses() + .forEach(bus -> generateUcteNodeId(bus.getId(), voltageLevel)); + voltageLevel.getBusBreakerView().getSwitches() + .forEach(this::generateUcteElementId); + } + private UcteNodeCode generateUcteNodeId(String nodeId, VoltageLevel voltageLevel) { if (UcteNodeCode.isUcteNodeId(nodeId)) { - Optional ucteNodeCode = UcteNodeCode.parseUcteNodeCode(nodeId); - if (ucteNodeCode.isPresent()) { - ucteNodeIds.put(nodeId, ucteNodeCode.get()); - return ucteNodeCode.get(); - } else { - throw new UcteException("Invalid ucte node code: " + nodeId); - } + return processExistingUcteNodeId(nodeId); } if (ucteNodeIds.containsKey(nodeId)) { return ucteNodeIds.get(nodeId); } + return createNewUcteNodeId(nodeId, voltageLevel); + } + private UcteNodeCode processExistingUcteNodeId(String nodeId) { + return UcteNodeCode.parseUcteNodeCode(nodeId) + .map(code -> { + ucteNodeIds.put(nodeId, code); + return code; + }) + .orElseThrow(() -> new UcteException(String.format(INVALID_NODE_CODE_ERROR, nodeId))); + } + + private UcteNodeCode createNewUcteNodeId(String nodeId, VoltageLevel voltageLevel) { StringBuilder newNodeCode = new StringBuilder(8); String newNodeId = generateIDFromCounter(); char countryCode = getCountryCode(voltageLevel).getUcteCode(); char voltageLevelCode = getUcteVoltageLevelCode(voltageLevel.getNominalV()); - char orderCode = ORDER_CODES.get(0); - int i = 0; - - newNodeCode - .append(countryCode) - .append(newNodeId) - .append(voltageLevelCode) - .append(orderCode); - - while (ucteNodeIds.containsValue(newNodeCode.toString())) { - i++; - orderCode = ORDER_CODES.get(i); - newNodeCode.replace(7, 7, String.valueOf(orderCode)); - } - Optional nodeCode = UcteNodeCode.parseUcteNodeCode(newNodeCode.toString()); - if (!nodeCode.isPresent()) { - throw new IllegalArgumentException("Invalid node code: " + newNodeCode); + return generateUniqueNodeCode(nodeId, newNodeCode, newNodeId, countryCode, voltageLevelCode); + } + + private UcteNodeCode generateUniqueNodeCode(String nodeId, StringBuilder newNodeCode, String newNodeId, + char countryCode, char voltageLevelCode) { + for (Character orderCode : ORDER_CODES) { + newNodeCode.setLength(0); + newNodeCode.append(countryCode) + .append(newNodeId) + .append(voltageLevelCode) + .append(orderCode); + + Optional nodeCode = UcteNodeCode.parseUcteNodeCode(newNodeCode.toString()); + if (nodeCode.isPresent() && !ucteNodeIds.containsValue(nodeCode.get())) { + ucteNodeIds.put(nodeId, nodeCode.get()); + return nodeCode.get(); + } } - ucteNodeIds.put(nodeId, nodeCode.get()); - return nodeCode.get(); + throw new UcteException("Unable to generate unique node code"); } - private UcteElementId generateUcteElementId(String id, UcteNodeCode node1, UcteNodeCode node2) { + private String generateIDFromCounter() { + namingCounter++; + if (namingCounter > MAX_COUNTER) { + int baseNumber = (namingCounter - BASE_LETTER_OFFSET) % MODULO_VALUE; + char letter = (char) ('A' + ((namingCounter - BASE_LETTER_OFFSET) / MODULO_VALUE)); + return String.format("%04d%c", baseNumber, letter); + } + return String.format("%05d", namingCounter); + } - if (ucteElementIds.containsKey(id)) { - return ucteElementIds.get(id); + private static char getUcteVoltageLevelCode(double voltage) { + if (voltage < UcteVoltageLevelCode.VL_27.getVoltageLevel()) { + return DEFAULT_VOLTAGE_CODE; + } + if (voltage > UcteVoltageLevelCode.VL_750.getVoltageLevel()) { + return '0'; } - int i = 0; - char orderCode = ORDER_CODES.get(0); - UcteElementId ucteElementId = new UcteElementId(node1, node2, orderCode); + return Arrays.stream(UcteVoltageLevelCode.values()) + .min(Comparator.comparingDouble(code -> + Math.abs(voltage - code.getVoltageLevel()))) + .map(code -> (char) ('0' + code.ordinal())) + .orElse(DEFAULT_VOLTAGE_CODE); + } - while (ucteElementIds.containsValue(ucteElementId)) { - i++; - if (i < ORDER_CODES.size()) { - orderCode = ORDER_CODES.get(i); - } - ucteElementId = new UcteElementId(node1, node2, orderCode); + private UcteCountryCode getCountryCode(VoltageLevel voltageLevel) { + Country country = voltageLevel.getSubstation() + .flatMap(Substation::getCountry) + .orElseThrow(() -> new UcteException(NO_COUNTRY_ERROR)); + + try { + return UcteCountryCode.valueOf(country.name()); + } catch (IllegalArgumentException e) { + throw new UcteException(String.format(NO_UCTE_COUNTRY_ERROR, country.getName())); } + } - ucteElementIds.put(id, ucteElementId); - return ucteElementId; + private UcteElementId generateUcteElementId(String id, UcteNodeCode node1, UcteNodeCode node2) { + if (ucteElementIds.containsKey(id)) { + return ucteElementIds.get(id); + } + + return ORDER_CODES.stream() + .map(orderCode -> new UcteElementId(node1, node2, orderCode)) + .filter(elementId -> !ucteElementIds.containsValue(elementId)) + .findFirst() + .map(elementId -> { + ucteElementIds.put(id, elementId); + return elementId; + }) + .orElseThrow(() -> new UcteException("Unable to generate unique element ID")); } private UcteElementId generateUcteElementId(Branch branch) { if (ucteElementIds.containsKey(branch.getId())) { return ucteElementIds.get(branch.getId()); } - UcteNodeCode node1 = this.ucteNodeIds.get(branch.getTerminal1().getBusBreakerView().getBus().getId()); - UcteNodeCode node2 = this.ucteNodeIds.get(branch.getTerminal2().getBusBreakerView().getBus().getId()); - + UcteNodeCode node1 = ucteNodeIds.get(branch.getTerminal1().getBusBreakerView().getBus().getId()); + UcteNodeCode node2 = ucteNodeIds.get(branch.getTerminal2().getBusBreakerView().getBus().getId()); return generateUcteElementId(branch.getId(), node1, node2); } @@ -168,99 +163,40 @@ private UcteElementId generateUcteElementId(DanglingLine danglingLine) { return ucteElementIds.get(danglingLine.getId()); } - generateUcteNodeId(danglingLine.getPairingKey(), danglingLine.getTerminal().getVoltageLevel()); - generateUcteNodeId(danglingLine.getTerminal().getBusBreakerView().getBus().getId(), danglingLine.getTerminal().getVoltageLevel()); - UcteNodeCode node1 = this.ucteNodeIds.get(danglingLine.getPairingKey()); - UcteNodeCode node2 = this.ucteNodeIds.get(danglingLine.getTerminal().getBusBreakerView().getBus().getId()); + Bus bus = danglingLine.getTerminal().getBusBreakerView().getBus(); + VoltageLevel voltageLevel = danglingLine.getTerminal().getVoltageLevel(); + + generateUcteNodeId(danglingLine.getPairingKey(), voltageLevel); + generateUcteNodeId(bus.getId(), voltageLevel); + + UcteNodeCode node1 = ucteNodeIds.get(danglingLine.getPairingKey()); + UcteNodeCode node2 = ucteNodeIds.get(bus.getId()); return generateUcteElementId(danglingLine.getId(), node1, node2); } private UcteElementId generateUcteElementId(Switch sw) { - if (ucteElementIds.containsKey(sw.getId())) { return ucteElementIds.get(sw.getId()); } - Bus bus1 = sw.getVoltageLevel().getBusBreakerView().getBus1(sw.getId()); - Bus bus2 = sw.getVoltageLevel().getBusBreakerView().getBus2(sw.getId()); + VoltageLevel.BusBreakerView view = sw.getVoltageLevel().getBusBreakerView(); + Bus bus1 = view.getBus1(sw.getId()); + Bus bus2 = view.getBus2(sw.getId()); + UcteNodeCode u1 = generateUcteNodeId(bus1.getId(), bus1.getVoltageLevel()); UcteNodeCode u2 = generateUcteNodeId(bus2.getId(), bus2.getVoltageLevel()); return generateUcteElementId(sw.getId(), u1, u2); } - private UcteCountryCode getCountryCode(VoltageLevel voltageLevel) { - - UcteCountryCode ucteCountryCode; - Country country = voltageLevel.getSubstation().get().getCountry().orElseThrow(() -> new com.powsybl.ucte.network.UcteException("No country for this substation")); - try { - ucteCountryCode = UcteCountryCode.valueOf(country.name()); - } catch (IllegalArgumentException iae) { - throw new UcteException("No UCTE country code for " + country.getName()); - } - - return ucteCountryCode; - - } - - private String generateIDFromCounter() { - namingCounter++; - if (namingCounter > 99999) { - int baseNumber = (namingCounter - 100000) % 10000; - char letter = (char) ('A' + ((namingCounter - 100000) / 10000)); - return String.format("%04d%c", baseNumber, letter); - } - return String.format("%05d", namingCounter); - } - - /** - * Determines the UCTE voltage level code for a given voltage value. - * The code is determined by finding the closest standard UCTE voltage level. - * - *

    The method follows these rules: - *

      - *
    • For voltage < 27kV: returns '7'
    • - *
    • For voltage > 750kV: returns '0'
    • - *
    • For other voltages: returns code of the closest standard UCTE level
    • - *
    - * - * @param voltage the nominal voltage value in kV - * @return a character representing the UCTE voltage level code - */ - private static char getUcteVoltageLevelCode(double voltage) { - - if (voltage < UcteVoltageLevelCode.VL_27.getVoltageLevel()) { - return '7'; - } - if (voltage > UcteVoltageLevelCode.VL_750.getVoltageLevel()) { - return '0'; - } - - UcteVoltageLevelCode closest = null; - double minDiff = Double.MAX_VALUE; - - for (UcteVoltageLevelCode code : UcteVoltageLevelCode.values()) { - double diff = Math.abs(voltage - code.getVoltageLevel()); - if (diff < minDiff) { - minDiff = diff; - closest = code; - } - } - return closest != null ? (char) ('0' + closest.ordinal()) : '7'; - } - @Override public UcteNodeCode getUcteNodeCode(String id) { if (id == null) { - throw new PowsyblException("ID is null" + id); + throw new PowsyblException("ID is null"); } - - UcteNodeCode code = ucteNodeIds.get(id); - if (code == null) { - throw new UcteException("No UCTE code found for id: " + id); - } - return code; + return Optional.ofNullable(ucteNodeIds.get(id)) + .orElseThrow(() -> new UcteException(String.format(NO_UCTE_CODE_ERROR, id))); } @Override @@ -275,11 +211,8 @@ public UcteNodeCode getUcteNodeCode(DanglingLine danglingLine) { @Override public UcteElementId getUcteElementId(String id) { - UcteElementId elementId = ucteElementIds.get(id); - if (elementId == null) { - throw new UcteException("No UCTE element id found for: " + id); - } - return elementId; + return Optional.ofNullable(ucteElementIds.get(id)) + .orElseThrow(() -> new UcteException(String.format("No UCTE element id found for: %s", id))); } @Override @@ -296,5 +229,4 @@ public UcteElementId getUcteElementId(Branch branch) { public UcteElementId getUcteElementId(DanglingLine danglingLine) { return getUcteElementId(danglingLine.getId()); } - } From d7f076ecd5ffcefe129d972d3077ae413c53813c Mon Sep 17 00:00:00 2001 From: clementleclerc Date: Thu, 21 Nov 2024 08:29:47 +0100 Subject: [PATCH 18/38] add CounterNamingStrategyTest Signed-off-by: clementleclerc --- .../converter/CounterNamingStrategyTest.java | 129 ++++++++++++++++++ 1 file changed, 129 insertions(+) create mode 100644 ucte/ucte-converter/src/test/java/com/powsybl/ucte/converter/CounterNamingStrategyTest.java diff --git a/ucte/ucte-converter/src/test/java/com/powsybl/ucte/converter/CounterNamingStrategyTest.java b/ucte/ucte-converter/src/test/java/com/powsybl/ucte/converter/CounterNamingStrategyTest.java new file mode 100644 index 00000000000..2645f6c7fbd --- /dev/null +++ b/ucte/ucte-converter/src/test/java/com/powsybl/ucte/converter/CounterNamingStrategyTest.java @@ -0,0 +1,129 @@ +package com.powsybl.ucte.converter; + +import com.powsybl.commons.PowsyblException; +import com.powsybl.iidm.network.*; +import com.powsybl.commons.datasource.ResourceDataSource; +import com.powsybl.commons.datasource.ResourceSet; +import com.powsybl.ucte.network.UcteNodeCode; +import com.powsybl.ucte.network.UcteElementId; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +class CounterNamingStrategyTest { + + private Network network; + private CounterNamingStrategy strategy; + + @BeforeEach + void setUp() { + ResourceDataSource dataSource = new ResourceDataSource("network", new ResourceSet("/", "network.xiidm")); + network = Network.read(dataSource); + strategy = new CounterNamingStrategy(); + strategy.initialiseNetwork(network); + } + + @Test + void testName() { + assertEquals("Counter", strategy.getName()); + } + + @Test + void testBasicNodeCodeGeneration() { + Bus genBus = network.getBusBreakerView().getBus("NGEN"); + Bus hv1Bus = network.getBusBreakerView().getBus("NHV1"); + Bus hv2Bus = network.getBusBreakerView().getBus("NHV2"); + Bus loadBus = network.getBusBreakerView().getBus("NLOAD"); + + UcteNodeCode genCode = strategy.getUcteNodeCode(genBus); + UcteNodeCode hv1Code = strategy.getUcteNodeCode(hv1Bus); + UcteNodeCode hv2Code = strategy.getUcteNodeCode(hv2Bus); + UcteNodeCode loadCode = strategy.getUcteNodeCode(loadBus); + + assertAll( + () -> assertEquals(8, genCode.toString().length()), + () -> assertEquals(8, hv1Code.toString().length()), + () -> assertEquals(8, hv2Code.toString().length()), + () -> assertEquals(8, loadCode.toString().length()) + ); + + assertAll( + () -> assertNotEquals(genCode, hv1Code), + () -> assertNotEquals(genCode, hv2Code), + () -> assertNotEquals(genCode, loadCode), + () -> assertNotEquals(hv1Code, hv2Code), + () -> assertNotEquals(hv1Code, loadCode), + () -> assertNotEquals(hv2Code, loadCode) + ); + } + + @Test + void testTransformerElementIds() { + Branch transformer1 = network.getBranch("NGEN_NHV1"); + Branch transformer2 = network.getBranch("NHV2_NLOAD"); + + UcteElementId id1 = strategy.getUcteElementId(transformer1); + UcteElementId id2 = strategy.getUcteElementId(transformer2); + + assertAll( + () -> assertNotNull(id1), + () -> assertNotNull(id2), + () -> assertEquals(19, id1.toString().length()), + () -> assertEquals(19, id2.toString().length()), + () -> assertNotEquals(id1, id2) + ); + } + + @Test + void testParallelLines() { + Branch line1 = network.getBranch("NHV1_NHV2_1"); + Branch line2 = network.getBranch("NHV1_NHV2_2"); + + UcteElementId id1 = strategy.getUcteElementId(line1); + UcteElementId id2 = strategy.getUcteElementId(line2); + + assertAll( + () -> assertNotNull(id1), + () -> assertNotNull(id2), + () -> assertEquals(19, id1.toString().length()), + () -> assertEquals(19, id2.toString().length()), + () -> assertNotEquals(id1, id2) + ); + } + + @Test + void testVoltageLevelCodes() { + Bus genBus = network.getBusBreakerView().getBus("NGEN"); // 27.0 kV + Bus hv1Bus = network.getBusBreakerView().getBus("NHV1"); // 380.0 kV + Bus loadBus = network.getBusBreakerView().getBus("NLOAD"); // 150.0 kV + + UcteNodeCode genCode = strategy.getUcteNodeCode(genBus); + UcteNodeCode hv1Code = strategy.getUcteNodeCode(hv1Bus); + UcteNodeCode loadCode = strategy.getUcteNodeCode(loadBus); + + assertAll( + () -> assertEquals('7', genCode.toString().charAt(6)), + () -> assertEquals('1', hv1Code.toString().charAt(6)), + () -> assertEquals('3', loadCode.toString().charAt(6)) + ); + } + + @Test + void testNullAndInvalidIds() { + assertAll( + () -> assertThrows(PowsyblException.class, () -> strategy.getUcteNodeCode((String) null)), + () -> assertThrows(PowsyblException.class, () -> strategy.getUcteElementId((String) null)), + () -> assertThrows(UcteException.class, () -> strategy.getUcteNodeCode("INVALID_ID")), + () -> assertThrows(UcteException.class, () -> strategy.getUcteElementId("INVALID_ID")) + ); + } + + @Test + void testCountryCode() { + Bus genBus = network.getBusBreakerView().getBus("NGEN"); + UcteNodeCode code = strategy.getUcteNodeCode(genBus); + assertEquals('F', code.toString().charAt(0)); // France + } + +} \ No newline at end of file From 82783ef0b68e6a064f1b157508c4e878f3009765 Mon Sep 17 00:00:00 2001 From: Leclerc Clement Date: Mon, 25 Nov 2024 15:36:51 +0100 Subject: [PATCH 19/38] code review Signed-off-by: Leclerc Clement --- .../ucte/converter/CounterNamingStrategy.java | 94 +++++------- .../powsybl/ucte/converter/UcteExporter.java | 3 +- .../converter/CounterNamingStrategyTest.java | 145 ++++++++++++++---- .../ucte/converter/NamingStrategyTest.java | 2 +- .../src/test/resources/network.xiidm | 24 +++ .../powsybl/ucte/network/UcteElementId.java | 2 +- .../ucte/network/UcteVoltageLevelCode.java | 18 +++ .../network/UcteVoltageLevelCodeTest.java | 9 ++ 8 files changed, 206 insertions(+), 91 deletions(-) diff --git a/ucte/ucte-converter/src/main/java/com/powsybl/ucte/converter/CounterNamingStrategy.java b/ucte/ucte-converter/src/main/java/com/powsybl/ucte/converter/CounterNamingStrategy.java index 0e51267d4e0..695caa4a39a 100644 --- a/ucte/ucte-converter/src/main/java/com/powsybl/ucte/converter/CounterNamingStrategy.java +++ b/ucte/ucte-converter/src/main/java/com/powsybl/ucte/converter/CounterNamingStrategy.java @@ -1,3 +1,10 @@ +/** + * Copyright (c) 2024, 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/. + * SPDX-License-Identifier: MPL-2.0 + */ package com.powsybl.ucte.converter; import com.google.auto.service.AutoService; @@ -10,18 +17,15 @@ import java.util.*; +/** + * @author Clément LECLERC {@literal } + */ @AutoService(NamingStrategy.class) public class CounterNamingStrategy implements NamingStrategy { - private static final List ORDER_CODES = List.of('1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', - 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '_', '-', '.', ' '); private static final String NO_COUNTRY_ERROR = "No country for this substation"; private static final String NO_UCTE_CODE_ERROR = "No UCTE code found for id: %s"; private static final String NO_UCTE_COUNTRY_ERROR = "No UCTE country code for %s"; private static final String INVALID_NODE_CODE_ERROR = "Invalid ucte node code: %s"; - private static final char DEFAULT_VOLTAGE_CODE = '7'; - private static final int MAX_COUNTER = 99999; - private static final int BASE_LETTER_OFFSET = 100000; - private static final int MODULO_VALUE = 10000; private final Map ucteNodeIds = new HashMap<>(); private final Map ucteElementIds = new HashMap<>(); @@ -35,8 +39,7 @@ public String getName() { @Override public void initialiseNetwork(Network network) { namingCounter = 0; - network.getSubstationStream() - .flatMap(s -> s.getVoltageLevelStream()) + network.getVoltageLevelStream() .forEach(this::processVoltageLevel); network.getBranchStream().forEach(this::generateUcteElementId); @@ -70,57 +73,30 @@ private UcteNodeCode processExistingUcteNodeId(String nodeId) { } private UcteNodeCode createNewUcteNodeId(String nodeId, VoltageLevel voltageLevel) { - StringBuilder newNodeCode = new StringBuilder(8); - String newNodeId = generateIDFromCounter(); + String newNodeId = String.format("%05d", namingCounter++); char countryCode = getCountryCode(voltageLevel).getUcteCode(); - char voltageLevelCode = getUcteVoltageLevelCode(voltageLevel.getNominalV()); + char voltageLevelCode = UcteVoltageLevelCode.voltageLevelCodeFromVoltage(voltageLevel.getNominalV()); - return generateUniqueNodeCode(nodeId, newNodeCode, newNodeId, countryCode, voltageLevelCode); + return generateUniqueNodeCode(nodeId, newNodeId, countryCode, voltageLevelCode); } - private UcteNodeCode generateUniqueNodeCode(String nodeId, StringBuilder newNodeCode, String newNodeId, + private UcteNodeCode generateUniqueNodeCode(String nodeId, String newNodeId, char countryCode, char voltageLevelCode) { - for (Character orderCode : ORDER_CODES) { - newNodeCode.setLength(0); - newNodeCode.append(countryCode) - .append(newNodeId) - .append(voltageLevelCode) - .append(orderCode); - - Optional nodeCode = UcteNodeCode.parseUcteNodeCode(newNodeCode.toString()); - if (nodeCode.isPresent() && !ucteNodeIds.containsValue(nodeCode.get())) { - ucteNodeIds.put(nodeId, nodeCode.get()); - return nodeCode.get(); + for (Character orderCode : UcteElementId.ORDER_CODES) { + + UcteNodeCode ucteNodeCode = new UcteNodeCode( + UcteCountryCode.fromUcteCode(countryCode), + newNodeId, + UcteVoltageLevelCode.voltageLevelCodeFromChar(voltageLevelCode), + orderCode); + if (!ucteNodeIds.containsValue(ucteNodeCode)) { + ucteNodeIds.put(nodeId, ucteNodeCode); + return ucteNodeCode; } } throw new UcteException("Unable to generate unique node code"); } - private String generateIDFromCounter() { - namingCounter++; - if (namingCounter > MAX_COUNTER) { - int baseNumber = (namingCounter - BASE_LETTER_OFFSET) % MODULO_VALUE; - char letter = (char) ('A' + ((namingCounter - BASE_LETTER_OFFSET) / MODULO_VALUE)); - return String.format("%04d%c", baseNumber, letter); - } - return String.format("%05d", namingCounter); - } - - private static char getUcteVoltageLevelCode(double voltage) { - if (voltage < UcteVoltageLevelCode.VL_27.getVoltageLevel()) { - return DEFAULT_VOLTAGE_CODE; - } - if (voltage > UcteVoltageLevelCode.VL_750.getVoltageLevel()) { - return '0'; - } - - return Arrays.stream(UcteVoltageLevelCode.values()) - .min(Comparator.comparingDouble(code -> - Math.abs(voltage - code.getVoltageLevel()))) - .map(code -> (char) ('0' + code.ordinal())) - .orElse(DEFAULT_VOLTAGE_CODE); - } - private UcteCountryCode getCountryCode(VoltageLevel voltageLevel) { Country country = voltageLevel.getSubstation() .flatMap(Substation::getCountry) @@ -138,7 +114,7 @@ private UcteElementId generateUcteElementId(String id, UcteNodeCode node1, UcteN return ucteElementIds.get(id); } - return ORDER_CODES.stream() + return UcteElementId.ORDER_CODES.stream() .map(orderCode -> new UcteElementId(node1, node2, orderCode)) .filter(elementId -> !ucteElementIds.containsValue(elementId)) .findFirst() @@ -163,16 +139,16 @@ private UcteElementId generateUcteElementId(DanglingLine danglingLine) { return ucteElementIds.get(danglingLine.getId()); } - Bus bus = danglingLine.getTerminal().getBusBreakerView().getBus(); - VoltageLevel voltageLevel = danglingLine.getTerminal().getVoltageLevel(); - - generateUcteNodeId(danglingLine.getPairingKey(), voltageLevel); - generateUcteNodeId(bus.getId(), voltageLevel); + UcteNodeCode code1; + UcteNodeCode code2; - UcteNodeCode node1 = ucteNodeIds.get(danglingLine.getPairingKey()); - UcteNodeCode node2 = ucteNodeIds.get(bus.getId()); - - return generateUcteElementId(danglingLine.getId(), node1, node2); + if (danglingLine.getPairingKey() != null) { + code1 = generateUcteNodeId(danglingLine.getPairingKey(), danglingLine.getTerminal().getVoltageLevel()); + } else { + code1 = generateUcteNodeId(danglingLine.getId(), danglingLine.getTerminal().getVoltageLevel()); + } + code2 = generateUcteNodeId(danglingLine.getTerminal().getBusView().getBus().getId(), danglingLine.getTerminal().getVoltageLevel()); + return generateUcteElementId(danglingLine.getId(), code1, code2); } private UcteElementId generateUcteElementId(Switch sw) { diff --git a/ucte/ucte-converter/src/main/java/com/powsybl/ucte/converter/UcteExporter.java b/ucte/ucte-converter/src/main/java/com/powsybl/ucte/converter/UcteExporter.java index 752cb67dc98..55ca23aeb2b 100644 --- a/ucte/ucte-converter/src/main/java/com/powsybl/ucte/converter/UcteExporter.java +++ b/ucte/ucte-converter/src/main/java/com/powsybl/ucte/converter/UcteExporter.java @@ -87,6 +87,7 @@ public void export(Network network, Properties parameters, DataSource dataSource String namingStrategyName = Parameter.readString(getFormat(), parameters, NAMING_STRATEGY_PARAMETER, defaultValueConfig); NamingStrategy namingStrategy = findNamingStrategy(namingStrategyName, NAMING_STRATEGY_SUPPLIERS.get()); + namingStrategy.initialiseNetwork(network); boolean combinePhaseAngleRegulation = Parameter.readBoolean(getFormat(), parameters, COMBINE_PHASE_ANGLE_REGULATION_PARAMETER, defaultValueConfig); UcteNetwork ucteNetwork = createUcteNetwork(network, namingStrategy, combinePhaseAngleRegulation); @@ -145,8 +146,6 @@ private static UcteNetwork createUcteNetwork(Network network, NamingStrategy nam UcteExporterContext context = new UcteExporterContext(namingStrategy, combinePhaseAngleRegulation); - namingStrategy.initialiseNetwork(network); - UcteNetwork ucteNetwork = new UcteNetworkImpl(); ucteNetwork.setVersion(UcteFormatVersion.SECOND); diff --git a/ucte/ucte-converter/src/test/java/com/powsybl/ucte/converter/CounterNamingStrategyTest.java b/ucte/ucte-converter/src/test/java/com/powsybl/ucte/converter/CounterNamingStrategyTest.java index 2645f6c7fbd..a4b289c18dd 100644 --- a/ucte/ucte-converter/src/test/java/com/powsybl/ucte/converter/CounterNamingStrategyTest.java +++ b/ucte/ucte-converter/src/test/java/com/powsybl/ucte/converter/CounterNamingStrategyTest.java @@ -1,3 +1,10 @@ +/** + * Copyright (c) 2024, 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/. + * SPDX-License-Identifier: MPL-2.0 + */ package com.powsybl.ucte.converter; import com.powsybl.commons.PowsyblException; @@ -11,6 +18,9 @@ import static org.junit.jupiter.api.Assertions.*; +/** + * @author Clément LECLERC {@literal } + */ class CounterNamingStrategyTest { private Network network; @@ -21,7 +31,6 @@ void setUp() { ResourceDataSource dataSource = new ResourceDataSource("network", new ResourceSet("/", "network.xiidm")); network = Network.read(dataSource); strategy = new CounterNamingStrategy(); - strategy.initialiseNetwork(network); } @Test @@ -29,8 +38,36 @@ void testName() { assertEquals("Counter", strategy.getName()); } + @Test + void testInitialNetwork() { + assertThrows(UcteException.class, () -> strategy.getUcteNodeCode("NGEN")); + assertThrows(UcteException.class, () -> strategy.getUcteElementId("NHV1_NHV2_1")); + + strategy.initialiseNetwork(network); + + assertDoesNotThrow(() -> strategy.getUcteNodeCode("NGEN")); + assertDoesNotThrow(() -> strategy.getUcteElementId("NHV1_NHV2_1")); + } + + @Test + void testInitializationOrder() { + strategy.initialiseNetwork(network); + + UcteNodeCode firstBusCode = strategy.getUcteNodeCode(network.getBusBreakerView().getBus("NGEN")); + UcteNodeCode secondBusCode = strategy.getUcteNodeCode(network.getBusBreakerView().getBus("NHV1")); + + String firstNumericPart = firstBusCode.toString().substring(1, 6); + String secondNumericPart = secondBusCode.toString().substring(1, 6); + int firstNum = Integer.parseInt(firstNumericPart); + int secondNum = Integer.parseInt(secondNumericPart); + + assertTrue(secondNum > firstNum, "Les codes doivent être générés séquentiellement"); + } + @Test void testBasicNodeCodeGeneration() { + + strategy.initialiseNetwork(network); Bus genBus = network.getBusBreakerView().getBus("NGEN"); Bus hv1Bus = network.getBusBreakerView().getBus("NHV1"); Bus hv2Bus = network.getBusBreakerView().getBus("NHV2"); @@ -42,10 +79,10 @@ void testBasicNodeCodeGeneration() { UcteNodeCode loadCode = strategy.getUcteNodeCode(loadBus); assertAll( - () -> assertEquals(8, genCode.toString().length()), - () -> assertEquals(8, hv1Code.toString().length()), - () -> assertEquals(8, hv2Code.toString().length()), - () -> assertEquals(8, loadCode.toString().length()) + () -> assertTrue(UcteNodeCode.isUcteNodeId(genCode.toString())), + () -> assertTrue(UcteNodeCode.isUcteNodeId(hv1Code.toString())), + () -> assertTrue(UcteNodeCode.isUcteNodeId(hv2Code.toString())), + () -> assertTrue(UcteNodeCode.isUcteNodeId(loadCode.toString())) ); assertAll( @@ -59,24 +96,70 @@ void testBasicNodeCodeGeneration() { } @Test - void testTransformerElementIds() { + void testBranchElementIds() { + strategy.initialiseNetwork(network); + Branch transformer1 = network.getBranch("NGEN_NHV1"); Branch transformer2 = network.getBranch("NHV2_NLOAD"); + Branch line1 = network.getBranch("NHV1_NHV2_1"); + Branch line2 = network.getBranch("NHV1_NHV2_2"); - UcteElementId id1 = strategy.getUcteElementId(transformer1); - UcteElementId id2 = strategy.getUcteElementId(transformer2); + UcteElementId transformerId1 = strategy.getUcteElementId(transformer1); + UcteElementId transformerId2 = strategy.getUcteElementId(transformer2); + UcteElementId lineId1 = strategy.getUcteElementId(line1); + UcteElementId lineId2 = strategy.getUcteElementId(line2); assertAll( - () -> assertNotNull(id1), - () -> assertNotNull(id2), - () -> assertEquals(19, id1.toString().length()), - () -> assertEquals(19, id2.toString().length()), - () -> assertNotEquals(id1, id2) + () -> assertTrue(UcteElementId.isUcteElementId(transformerId1.toString())), + () -> assertTrue(UcteElementId.isUcteElementId(transformerId2.toString())), + () -> assertTrue(UcteElementId.isUcteElementId(lineId1.toString())), + () -> assertTrue(UcteElementId.isUcteElementId(lineId2.toString())), + () -> assertNotEquals(transformerId1, transformerId2), + () -> assertNotEquals(lineId1, lineId2) ); + + // Test cached values + assertEquals(transformerId1, strategy.getUcteElementId(transformer1)); + assertEquals(transformerId2, strategy.getUcteElementId(transformer2)); + } + + @Test + void testSwitchElementIds() { + strategy.initialiseNetwork(network); + Switch sw = network.getSwitch("NGEN-NGEN2"); + Switch sw2 = network.getSwitch("NGEN-NGEN3"); + UcteElementId swId = strategy.getUcteElementId(sw); + UcteElementId swId2 = strategy.getUcteElementId(sw2); + + assertAll( + () -> assertTrue(UcteElementId.isUcteElementId(swId.toString())), + () -> assertTrue(UcteElementId.isUcteElementId(swId2.toString())), + () -> assertNotEquals(swId, swId2) + ); + + } + + @Test + void testDanglingLineElementIds() { + strategy.initialiseNetwork(network); + DanglingLine dl1 = network.getDanglingLine("DL1"); + DanglingLine dl2 = network.getDanglingLine("DL2"); + UcteElementId dlId1 = strategy.getUcteElementId(dl1); + UcteElementId dlId2 = strategy.getUcteElementId(dl2); + + assertAll( + () -> assertTrue(UcteElementId.isUcteElementId(dlId1.toString())), + () -> assertTrue(UcteElementId.isUcteElementId(dlId1.toString())), + () -> assertNotEquals(dlId1, dlId2) + + ); + } @Test void testParallelLines() { + strategy.initialiseNetwork(network); + Branch line1 = network.getBranch("NHV1_NHV2_1"); Branch line2 = network.getBranch("NHV1_NHV2_2"); @@ -86,31 +169,35 @@ void testParallelLines() { assertAll( () -> assertNotNull(id1), () -> assertNotNull(id2), - () -> assertEquals(19, id1.toString().length()), - () -> assertEquals(19, id2.toString().length()), + () -> assertEquals(true, UcteElementId.isUcteElementId(id1.toString())), + () -> assertEquals(true, UcteElementId.isUcteElementId(id2.toString())), () -> assertNotEquals(id1, id2) ); } + @Test - void testVoltageLevelCodes() { - Bus genBus = network.getBusBreakerView().getBus("NGEN"); // 27.0 kV - Bus hv1Bus = network.getBusBreakerView().getBus("NHV1"); // 380.0 kV - Bus loadBus = network.getBusBreakerView().getBus("NLOAD"); // 150.0 kV + void testExistingUcteNodeCodes() { + strategy.initialiseNetwork(network); - UcteNodeCode genCode = strategy.getUcteNodeCode(genBus); - UcteNodeCode hv1Code = strategy.getUcteNodeCode(hv1Bus); - UcteNodeCode loadCode = strategy.getUcteNodeCode(loadBus); + Bus bus = network.getBusBreakerView().getBus("NGEN"); + UcteNodeCode firstCode = strategy.getUcteNodeCode(bus); + assertNotNull(firstCode); - assertAll( - () -> assertEquals('7', genCode.toString().charAt(6)), - () -> assertEquals('1', hv1Code.toString().charAt(6)), - () -> assertEquals('3', loadCode.toString().charAt(6)) - ); + UcteNodeCode existingCode = strategy.getUcteNodeCode(bus); + assertNotNull(existingCode); + assertEquals(firstCode, existingCode); + + UcteNodeCode presentCode = strategy.getUcteNodeCode(bus.getId()); + assertNotNull(presentCode); + assertEquals(firstCode, presentCode); + assertEquals(existingCode, presentCode); } @Test void testNullAndInvalidIds() { + strategy.initialiseNetwork(network); + assertAll( () -> assertThrows(PowsyblException.class, () -> strategy.getUcteNodeCode((String) null)), () -> assertThrows(PowsyblException.class, () -> strategy.getUcteElementId((String) null)), @@ -121,9 +208,11 @@ void testNullAndInvalidIds() { @Test void testCountryCode() { + strategy.initialiseNetwork(network); + Bus genBus = network.getBusBreakerView().getBus("NGEN"); UcteNodeCode code = strategy.getUcteNodeCode(genBus); assertEquals('F', code.toString().charAt(0)); // France } -} \ No newline at end of file +} diff --git a/ucte/ucte-converter/src/test/java/com/powsybl/ucte/converter/NamingStrategyTest.java b/ucte/ucte-converter/src/test/java/com/powsybl/ucte/converter/NamingStrategyTest.java index 00736736edd..f3e0cf045c0 100644 --- a/ucte/ucte-converter/src/test/java/com/powsybl/ucte/converter/NamingStrategyTest.java +++ b/ucte/ucte-converter/src/test/java/com/powsybl/ucte/converter/NamingStrategyTest.java @@ -1,5 +1,5 @@ /** - * Copyright (c) 2019, RTE (http://www.rte-france.com) + * Copyright (c) 2024, 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/. diff --git a/ucte/ucte-converter/src/test/resources/network.xiidm b/ucte/ucte-converter/src/test/resources/network.xiidm index e2bdd7e4f8c..e63065344c1 100644 --- a/ucte/ucte-converter/src/test/resources/network.xiidm +++ b/ucte/ucte-converter/src/test/resources/network.xiidm @@ -4,10 +4,22 @@ + + + + + + + + + + @@ -37,6 +49,18 @@
    + + + + + + + + + + + +
    \ No newline at end of file diff --git a/ucte/ucte-network/src/main/java/com/powsybl/ucte/network/UcteElementId.java b/ucte/ucte-network/src/main/java/com/powsybl/ucte/network/UcteElementId.java index 852af51fb65..fbcd3e4651b 100644 --- a/ucte/ucte-network/src/main/java/com/powsybl/ucte/network/UcteElementId.java +++ b/ucte/ucte-network/src/main/java/com/powsybl/ucte/network/UcteElementId.java @@ -15,7 +15,7 @@ */ public class UcteElementId implements Comparable { - private static final List ORDER_CODES = Arrays.asList('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', + public static final List ORDER_CODES = Arrays.asList('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '_', '-', '.', ' '); private final UcteNodeCode nodeCode1; diff --git a/ucte/ucte-network/src/main/java/com/powsybl/ucte/network/UcteVoltageLevelCode.java b/ucte/ucte-network/src/main/java/com/powsybl/ucte/network/UcteVoltageLevelCode.java index 1c03015b9f7..447fd690851 100644 --- a/ucte/ucte-network/src/main/java/com/powsybl/ucte/network/UcteVoltageLevelCode.java +++ b/ucte/ucte-network/src/main/java/com/powsybl/ucte/network/UcteVoltageLevelCode.java @@ -7,6 +7,9 @@ */ package com.powsybl.ucte.network; +import java.util.Arrays; +import java.util.Comparator; + /** * * @author Geoffroy Jamgotchian {@literal } @@ -75,6 +78,21 @@ public static UcteVoltageLevelCode voltageLevelCodeFromChar(char code) { return UcteVoltageLevelCode.values()[code - '0']; } + public static char voltageLevelCodeFromVoltage(double voltage) { + if (voltage < UcteVoltageLevelCode.VL_27.getVoltageLevel()) { + return '7'; + } + if (voltage > UcteVoltageLevelCode.VL_750.getVoltageLevel()) { + return '0'; + } + + return Arrays.stream(UcteVoltageLevelCode.values()) + .min(Comparator.comparingDouble(code -> + Math.abs(voltage - code.getVoltageLevel()))) + .map(code -> (char) ('0' + code.ordinal())) + .orElse('7'); + } + public static boolean isVoltageLevelCode(char character) { return character >= '0' && character <= '9'; } diff --git a/ucte/ucte-network/src/test/java/com/powsybl/ucte/network/UcteVoltageLevelCodeTest.java b/ucte/ucte-network/src/test/java/com/powsybl/ucte/network/UcteVoltageLevelCodeTest.java index d9582f02f97..859ec7061e5 100644 --- a/ucte/ucte-network/src/test/java/com/powsybl/ucte/network/UcteVoltageLevelCodeTest.java +++ b/ucte/ucte-network/src/test/java/com/powsybl/ucte/network/UcteVoltageLevelCodeTest.java @@ -29,5 +29,14 @@ void test() { assertEquals(27, UcteVoltageLevelCode.VL_27.getVoltageLevel()); assertEquals(330, UcteVoltageLevelCode.VL_330.getVoltageLevel()); assertEquals(500, UcteVoltageLevelCode.VL_500.getVoltageLevel()); + + assertEquals('0', UcteVoltageLevelCode.voltageLevelCodeFromVoltage(1000)); + assertEquals('1', UcteVoltageLevelCode.voltageLevelCodeFromVoltage(386)); + assertEquals('2', UcteVoltageLevelCode.voltageLevelCodeFromVoltage(220)); + assertEquals('2', UcteVoltageLevelCode.voltageLevelCodeFromVoltage(195)); + assertEquals('3', UcteVoltageLevelCode.voltageLevelCodeFromVoltage(150)); + assertEquals('4', UcteVoltageLevelCode.voltageLevelCodeFromVoltage(120)); + assertEquals('4', UcteVoltageLevelCode.voltageLevelCodeFromVoltage(125)); + assertEquals('7', UcteVoltageLevelCode.voltageLevelCodeFromVoltage(22)); } } From 4c9e2460e8260bc7ca2c26fab6440aa11a66c504 Mon Sep 17 00:00:00 2001 From: Leclerc Clement Date: Mon, 25 Nov 2024 16:12:23 +0100 Subject: [PATCH 20/38] changer danglingline getBusBreakerView() Signed-off-by: Leclerc Clement --- .../java/com/powsybl/ucte/converter/CounterNamingStrategy.java | 2 +- .../com/powsybl/ucte/converter/CounterNamingStrategyTest.java | 2 -- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/ucte/ucte-converter/src/main/java/com/powsybl/ucte/converter/CounterNamingStrategy.java b/ucte/ucte-converter/src/main/java/com/powsybl/ucte/converter/CounterNamingStrategy.java index 695caa4a39a..31eaeea4b56 100644 --- a/ucte/ucte-converter/src/main/java/com/powsybl/ucte/converter/CounterNamingStrategy.java +++ b/ucte/ucte-converter/src/main/java/com/powsybl/ucte/converter/CounterNamingStrategy.java @@ -147,7 +147,7 @@ private UcteElementId generateUcteElementId(DanglingLine danglingLine) { } else { code1 = generateUcteNodeId(danglingLine.getId(), danglingLine.getTerminal().getVoltageLevel()); } - code2 = generateUcteNodeId(danglingLine.getTerminal().getBusView().getBus().getId(), danglingLine.getTerminal().getVoltageLevel()); + code2 = generateUcteNodeId(danglingLine.getTerminal().getBusBreakerView().getBus().getId(), danglingLine.getTerminal().getVoltageLevel()); return generateUcteElementId(danglingLine.getId(), code1, code2); } diff --git a/ucte/ucte-converter/src/test/java/com/powsybl/ucte/converter/CounterNamingStrategyTest.java b/ucte/ucte-converter/src/test/java/com/powsybl/ucte/converter/CounterNamingStrategyTest.java index a4b289c18dd..b6e0ac53790 100644 --- a/ucte/ucte-converter/src/test/java/com/powsybl/ucte/converter/CounterNamingStrategyTest.java +++ b/ucte/ucte-converter/src/test/java/com/powsybl/ucte/converter/CounterNamingStrategyTest.java @@ -118,7 +118,6 @@ void testBranchElementIds() { () -> assertNotEquals(lineId1, lineId2) ); - // Test cached values assertEquals(transformerId1, strategy.getUcteElementId(transformer1)); assertEquals(transformerId2, strategy.getUcteElementId(transformer2)); } @@ -175,7 +174,6 @@ void testParallelLines() { ); } - @Test void testExistingUcteNodeCodes() { strategy.initialiseNetwork(network); From bbfbee31efb12d5d1cbb6df371243212a88e0bee Mon Sep 17 00:00:00 2001 From: Leclerc Clement Date: Mon, 25 Nov 2024 16:43:41 +0100 Subject: [PATCH 21/38] inverse node order for Transformaters Signed-off-by: Leclerc Clement --- .../com/powsybl/ucte/converter/CounterNamingStrategy.java | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/ucte/ucte-converter/src/main/java/com/powsybl/ucte/converter/CounterNamingStrategy.java b/ucte/ucte-converter/src/main/java/com/powsybl/ucte/converter/CounterNamingStrategy.java index 31eaeea4b56..9b571cf7bdc 100644 --- a/ucte/ucte-converter/src/main/java/com/powsybl/ucte/converter/CounterNamingStrategy.java +++ b/ucte/ucte-converter/src/main/java/com/powsybl/ucte/converter/CounterNamingStrategy.java @@ -125,12 +125,15 @@ private UcteElementId generateUcteElementId(String id, UcteNodeCode node1, UcteN .orElseThrow(() -> new UcteException("Unable to generate unique element ID")); } - private UcteElementId generateUcteElementId(Branch branch) { + private UcteElementId generateUcteElementId(Branch branch) { if (ucteElementIds.containsKey(branch.getId())) { return ucteElementIds.get(branch.getId()); } UcteNodeCode node1 = ucteNodeIds.get(branch.getTerminal1().getBusBreakerView().getBus().getId()); UcteNodeCode node2 = ucteNodeIds.get(branch.getTerminal2().getBusBreakerView().getBus().getId()); + if (branch instanceof TwoWindingsTransformer) { + return generateUcteElementId(branch.getId(), node2, node1); + } return generateUcteElementId(branch.getId(), node1, node2); } From 61995d8edc867b67a20fabc606d550dce01174d8 Mon Sep 17 00:00:00 2001 From: Leclerc Clement Date: Tue, 26 Nov 2024 14:07:00 +0100 Subject: [PATCH 22/38] change incrementation of namingcounter Signed-off-by: Leclerc Clement --- .../ucte/converter/CounterNamingStrategy.java | 87 +++++++++---------- .../converter/CounterNamingStrategyTest.java | 25 +++--- .../src/test/resources/network.xiidm | 1 + 3 files changed, 58 insertions(+), 55 deletions(-) diff --git a/ucte/ucte-converter/src/main/java/com/powsybl/ucte/converter/CounterNamingStrategy.java b/ucte/ucte-converter/src/main/java/com/powsybl/ucte/converter/CounterNamingStrategy.java index 9b571cf7bdc..e1cf608fe6f 100644 --- a/ucte/ucte-converter/src/main/java/com/powsybl/ucte/converter/CounterNamingStrategy.java +++ b/ucte/ucte-converter/src/main/java/com/powsybl/ucte/converter/CounterNamingStrategy.java @@ -25,7 +25,6 @@ public class CounterNamingStrategy implements NamingStrategy { private static final String NO_COUNTRY_ERROR = "No country for this substation"; private static final String NO_UCTE_CODE_ERROR = "No UCTE code found for id: %s"; private static final String NO_UCTE_COUNTRY_ERROR = "No UCTE country code for %s"; - private static final String INVALID_NODE_CODE_ERROR = "Invalid ucte node code: %s"; private final Map ucteNodeIds = new HashMap<>(); private final Map ucteElementIds = new HashMap<>(); @@ -47,54 +46,51 @@ public void initialiseNetwork(Network network) { } private void processVoltageLevel(VoltageLevel voltageLevel) { - voltageLevel.getBusBreakerView().getBuses() - .forEach(bus -> generateUcteNodeId(bus.getId(), voltageLevel)); + Iterator buslist = voltageLevel.getBusBreakerView().getBuses().iterator(); + for (int i = 0; buslist.hasNext(); i++) { + Bus bus = buslist.next(); + generateUcteNodeId(bus.getId(), voltageLevel, i); + } + voltageLevel.getBusBreakerView().getSwitches() .forEach(this::generateUcteElementId); + namingCounter++; } - private UcteNodeCode generateUcteNodeId(String nodeId, VoltageLevel voltageLevel) { + private UcteNodeCode generateUcteNodeId(String nodeId, VoltageLevel voltageLevel, int orderCodeIndex) { if (UcteNodeCode.isUcteNodeId(nodeId)) { - return processExistingUcteNodeId(nodeId); + return processExistingUcteNodeId(nodeId, orderCodeIndex); } if (ucteNodeIds.containsKey(nodeId)) { return ucteNodeIds.get(nodeId); } - return createNewUcteNodeId(nodeId, voltageLevel); + return createNewUcteNodeId(nodeId, voltageLevel, orderCodeIndex); } - private UcteNodeCode processExistingUcteNodeId(String nodeId) { - return UcteNodeCode.parseUcteNodeCode(nodeId) - .map(code -> { - ucteNodeIds.put(nodeId, code); - return code; - }) - .orElseThrow(() -> new UcteException(String.format(INVALID_NODE_CODE_ERROR, nodeId))); + private UcteNodeCode processExistingUcteNodeId(String nodeId, int orderCodeIndex) { + String newNodeId = nodeId.substring(0, nodeId.length() - 1) + orderCodeIndex; + UcteNodeCode nodeCode = UcteNodeCode.parseUcteNodeCode(newNodeId).get(); + ucteNodeIds.put(nodeId, nodeCode); + return nodeCode; } - private UcteNodeCode createNewUcteNodeId(String nodeId, VoltageLevel voltageLevel) { - String newNodeId = String.format("%05d", namingCounter++); + private UcteNodeCode createNewUcteNodeId(String nodeId, VoltageLevel voltageLevel, int orderCodeIndex) { + String newNodeId = String.format("%05d", namingCounter); char countryCode = getCountryCode(voltageLevel).getUcteCode(); char voltageLevelCode = UcteVoltageLevelCode.voltageLevelCodeFromVoltage(voltageLevel.getNominalV()); + if (orderCodeIndex > UcteElementId.ORDER_CODES.get(orderCodeIndex)) { + throw new PowsyblException("Order code index out of bounds"); + } + char orderCode = UcteElementId.ORDER_CODES.get(orderCodeIndex); - return generateUniqueNodeCode(nodeId, newNodeId, countryCode, voltageLevelCode); - } - - private UcteNodeCode generateUniqueNodeCode(String nodeId, String newNodeId, - char countryCode, char voltageLevelCode) { - for (Character orderCode : UcteElementId.ORDER_CODES) { + UcteNodeCode ucteNodeCode = new UcteNodeCode( + UcteCountryCode.fromUcteCode(countryCode), + newNodeId, + UcteVoltageLevelCode.voltageLevelCodeFromChar(voltageLevelCode), + orderCode); - UcteNodeCode ucteNodeCode = new UcteNodeCode( - UcteCountryCode.fromUcteCode(countryCode), - newNodeId, - UcteVoltageLevelCode.voltageLevelCodeFromChar(voltageLevelCode), - orderCode); - if (!ucteNodeIds.containsValue(ucteNodeCode)) { - ucteNodeIds.put(nodeId, ucteNodeCode); - return ucteNodeCode; - } - } - throw new UcteException("Unable to generate unique node code"); + ucteNodeIds.put(nodeId, ucteNodeCode); + return ucteNodeCode; } private UcteCountryCode getCountryCode(VoltageLevel voltageLevel) { @@ -114,15 +110,14 @@ private UcteElementId generateUcteElementId(String id, UcteNodeCode node1, UcteN return ucteElementIds.get(id); } - return UcteElementId.ORDER_CODES.stream() + UcteElementId uniqueElementId = UcteElementId.ORDER_CODES.stream() .map(orderCode -> new UcteElementId(node1, node2, orderCode)) .filter(elementId -> !ucteElementIds.containsValue(elementId)) .findFirst() - .map(elementId -> { - ucteElementIds.put(id, elementId); - return elementId; - }) .orElseThrow(() -> new UcteException("Unable to generate unique element ID")); + + ucteElementIds.put(id, uniqueElementId); + return uniqueElementId; } private UcteElementId generateUcteElementId(Branch branch) { @@ -131,9 +126,7 @@ private UcteElementId generateUcteElementId(Branch branch) { } UcteNodeCode node1 = ucteNodeIds.get(branch.getTerminal1().getBusBreakerView().getBus().getId()); UcteNodeCode node2 = ucteNodeIds.get(branch.getTerminal2().getBusBreakerView().getBus().getId()); - if (branch instanceof TwoWindingsTransformer) { - return generateUcteElementId(branch.getId(), node2, node1); - } + return generateUcteElementId(branch.getId(), node1, node2); } @@ -145,12 +138,13 @@ private UcteElementId generateUcteElementId(DanglingLine danglingLine) { UcteNodeCode code1; UcteNodeCode code2; + code1 = getUcteNodeCode(danglingLine.getTerminal().getBusBreakerView().getBus()); + if (danglingLine.getPairingKey() != null) { - code1 = generateUcteNodeId(danglingLine.getPairingKey(), danglingLine.getTerminal().getVoltageLevel()); + code2 = generateUcteNodeId(danglingLine.getPairingKey(), danglingLine.getTerminal().getVoltageLevel(), 0); } else { - code1 = generateUcteNodeId(danglingLine.getId(), danglingLine.getTerminal().getVoltageLevel()); + code2 = generateUcteNodeId(danglingLine.getId(), danglingLine.getTerminal().getVoltageLevel(), 0); } - code2 = generateUcteNodeId(danglingLine.getTerminal().getBusBreakerView().getBus().getId(), danglingLine.getTerminal().getVoltageLevel()); return generateUcteElementId(danglingLine.getId(), code1, code2); } @@ -163,8 +157,8 @@ private UcteElementId generateUcteElementId(Switch sw) { Bus bus1 = view.getBus1(sw.getId()); Bus bus2 = view.getBus2(sw.getId()); - UcteNodeCode u1 = generateUcteNodeId(bus1.getId(), bus1.getVoltageLevel()); - UcteNodeCode u2 = generateUcteNodeId(bus2.getId(), bus2.getVoltageLevel()); + UcteNodeCode u1 = getUcteNodeCode(bus1.getId()); + UcteNodeCode u2 = getUcteNodeCode(bus2.getId()); return generateUcteElementId(sw.getId(), u1, u2); } @@ -185,6 +179,9 @@ public UcteNodeCode getUcteNodeCode(Bus bus) { @Override public UcteNodeCode getUcteNodeCode(DanglingLine danglingLine) { + if (danglingLine.getPairingKey() == null) { + return getUcteNodeCode(danglingLine.getId()); + } return getUcteNodeCode(danglingLine.getPairingKey()); } diff --git a/ucte/ucte-converter/src/test/java/com/powsybl/ucte/converter/CounterNamingStrategyTest.java b/ucte/ucte-converter/src/test/java/com/powsybl/ucte/converter/CounterNamingStrategyTest.java index b6e0ac53790..eefd6fc46cf 100644 --- a/ucte/ucte-converter/src/test/java/com/powsybl/ucte/converter/CounterNamingStrategyTest.java +++ b/ucte/ucte-converter/src/test/java/com/powsybl/ucte/converter/CounterNamingStrategyTest.java @@ -50,18 +50,20 @@ void testInitialNetwork() { } @Test - void testInitializationOrder() { + void testVoltageLevelCounterNaming() { strategy.initialiseNetwork(network); UcteNodeCode firstBusCode = strategy.getUcteNodeCode(network.getBusBreakerView().getBus("NGEN")); - UcteNodeCode secondBusCode = strategy.getUcteNodeCode(network.getBusBreakerView().getBus("NHV1")); + UcteNodeCode secondBusCode = strategy.getUcteNodeCode(network.getBusBreakerView().getBus("NGEN2")); + UcteNodeCode thirdBusCode = strategy.getUcteNodeCode(network.getBusBreakerView().getBus("NGEN3")); - String firstNumericPart = firstBusCode.toString().substring(1, 6); - String secondNumericPart = secondBusCode.toString().substring(1, 6); - int firstNum = Integer.parseInt(firstNumericPart); - int secondNum = Integer.parseInt(secondNumericPart); + String firstIdPart = firstBusCode.toString().substring(0, 6); + String secondIdPart = secondBusCode.toString().substring(0, 6); + String thirdIdPart = thirdBusCode.toString().substring(0, 6); - assertTrue(secondNum > firstNum, "Les codes doivent être générés séquentiellement"); + assertEquals(firstIdPart, secondIdPart); + assertEquals(firstIdPart, thirdIdPart); + assertEquals(secondIdPart, thirdIdPart); } @Test @@ -72,17 +74,20 @@ void testBasicNodeCodeGeneration() { Bus hv1Bus = network.getBusBreakerView().getBus("NHV1"); Bus hv2Bus = network.getBusBreakerView().getBus("NHV2"); Bus loadBus = network.getBusBreakerView().getBus("NLOAD"); + Bus ucteBus = network.getBusBreakerView().getBus("F0000010"); UcteNodeCode genCode = strategy.getUcteNodeCode(genBus); UcteNodeCode hv1Code = strategy.getUcteNodeCode(hv1Bus); UcteNodeCode hv2Code = strategy.getUcteNodeCode(hv2Bus); UcteNodeCode loadCode = strategy.getUcteNodeCode(loadBus); + UcteNodeCode ucteCode = strategy.getUcteNodeCode(ucteBus); assertAll( () -> assertTrue(UcteNodeCode.isUcteNodeId(genCode.toString())), () -> assertTrue(UcteNodeCode.isUcteNodeId(hv1Code.toString())), () -> assertTrue(UcteNodeCode.isUcteNodeId(hv2Code.toString())), - () -> assertTrue(UcteNodeCode.isUcteNodeId(loadCode.toString())) + () -> assertTrue(UcteNodeCode.isUcteNodeId(loadCode.toString())), + () -> assertTrue(UcteNodeCode.isUcteNodeId(ucteCode.toString())) ); assertAll( @@ -168,8 +173,8 @@ void testParallelLines() { assertAll( () -> assertNotNull(id1), () -> assertNotNull(id2), - () -> assertEquals(true, UcteElementId.isUcteElementId(id1.toString())), - () -> assertEquals(true, UcteElementId.isUcteElementId(id2.toString())), + () -> assertTrue(UcteElementId.isUcteElementId(id1.toString())), + () -> assertTrue(UcteElementId.isUcteElementId(id2.toString())), () -> assertNotEquals(id1, id2) ); } diff --git a/ucte/ucte-converter/src/test/resources/network.xiidm b/ucte/ucte-converter/src/test/resources/network.xiidm index e63065344c1..ec583532206 100644 --- a/ucte/ucte-converter/src/test/resources/network.xiidm +++ b/ucte/ucte-converter/src/test/resources/network.xiidm @@ -6,6 +6,7 @@ + From 45ed4016caafa886444ea14302fd7a0d5d14d6d4 Mon Sep 17 00:00:00 2001 From: Leclerc Clement Date: Tue, 26 Nov 2024 15:58:20 +0100 Subject: [PATCH 23/38] retry change incrementation of namingcounter Signed-off-by: Leclerc Clement --- .../ucte/converter/CounterNamingStrategy.java | 9 ++++++--- .../ucte/converter/CounterNamingStrategyTest.java | 15 ++++++++++++++- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/ucte/ucte-converter/src/main/java/com/powsybl/ucte/converter/CounterNamingStrategy.java b/ucte/ucte-converter/src/main/java/com/powsybl/ucte/converter/CounterNamingStrategy.java index e1cf608fe6f..00e1f2364d2 100644 --- a/ucte/ucte-converter/src/main/java/com/powsybl/ucte/converter/CounterNamingStrategy.java +++ b/ucte/ucte-converter/src/main/java/com/powsybl/ucte/converter/CounterNamingStrategy.java @@ -69,9 +69,12 @@ private UcteNodeCode generateUcteNodeId(String nodeId, VoltageLevel voltageLevel private UcteNodeCode processExistingUcteNodeId(String nodeId, int orderCodeIndex) { String newNodeId = nodeId.substring(0, nodeId.length() - 1) + orderCodeIndex; - UcteNodeCode nodeCode = UcteNodeCode.parseUcteNodeCode(newNodeId).get(); - ucteNodeIds.put(nodeId, nodeCode); - return nodeCode; + Optional nodeCode = UcteNodeCode.parseUcteNodeCode(newNodeId); + if (nodeCode.isPresent()) { + ucteNodeIds.put(nodeId, nodeCode.get()); + return nodeCode.get(); + } + throw new PowsyblException(String.format(NO_UCTE_CODE_ERROR, newNodeId)); } private UcteNodeCode createNewUcteNodeId(String nodeId, VoltageLevel voltageLevel, int orderCodeIndex) { diff --git a/ucte/ucte-converter/src/test/java/com/powsybl/ucte/converter/CounterNamingStrategyTest.java b/ucte/ucte-converter/src/test/java/com/powsybl/ucte/converter/CounterNamingStrategyTest.java index eefd6fc46cf..5e726b63661 100644 --- a/ucte/ucte-converter/src/test/java/com/powsybl/ucte/converter/CounterNamingStrategyTest.java +++ b/ucte/ucte-converter/src/test/java/com/powsybl/ucte/converter/CounterNamingStrategyTest.java @@ -132,13 +132,17 @@ void testSwitchElementIds() { strategy.initialiseNetwork(network); Switch sw = network.getSwitch("NGEN-NGEN2"); Switch sw2 = network.getSwitch("NGEN-NGEN3"); + Switch sw3 = network.getSwitch("NGEN-NGEN3"); + UcteElementId swId = strategy.getUcteElementId(sw); UcteElementId swId2 = strategy.getUcteElementId(sw2); + UcteElementId swId3 = strategy.getUcteElementId(sw3); assertAll( () -> assertTrue(UcteElementId.isUcteElementId(swId.toString())), () -> assertTrue(UcteElementId.isUcteElementId(swId2.toString())), - () -> assertNotEquals(swId, swId2) + () -> assertNotEquals(swId, swId2), + () -> assertEquals(swId3, swId2) ); } @@ -160,6 +164,15 @@ void testDanglingLineElementIds() { } + @Test + void testDanglingLineNodeIds() { + strategy.initialiseNetwork(network); + DanglingLine dl1 = network.getDanglingLine("DL1"); + UcteNodeCode code1 = strategy.getUcteNodeCode(dl1); + + assertTrue(UcteNodeCode.isUcteNodeId(code1.toString())); + } + @Test void testParallelLines() { strategy.initialiseNetwork(network); From 84cf3c464a2a07870fc4cdba2cdd08000a4efd4b Mon Sep 17 00:00:00 2001 From: Leclerc Clement Date: Tue, 26 Nov 2024 17:18:57 +0100 Subject: [PATCH 24/38] change implementation Signed-off-by: Leclerc Clement --- .../converter/AbstractNamingStrategy.java | 74 +++++++++++++++++++ .../ucte/converter/CounterNamingStrategy.java | 50 +------------ .../ucte/converter/DefaultNamingStrategy.java | 51 +------------ .../ucte/converter/NamingStrategy.java | 4 +- 4 files changed, 78 insertions(+), 101 deletions(-) create mode 100644 ucte/ucte-converter/src/main/java/com/powsybl/ucte/converter/AbstractNamingStrategy.java diff --git a/ucte/ucte-converter/src/main/java/com/powsybl/ucte/converter/AbstractNamingStrategy.java b/ucte/ucte-converter/src/main/java/com/powsybl/ucte/converter/AbstractNamingStrategy.java new file mode 100644 index 00000000000..a5ae5ec0899 --- /dev/null +++ b/ucte/ucte-converter/src/main/java/com/powsybl/ucte/converter/AbstractNamingStrategy.java @@ -0,0 +1,74 @@ +/** + * Copyright (c) 2024, 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/. + * SPDX-License-Identifier: MPL-2.0 + */ +package com.powsybl.ucte.converter; + +import com.powsybl.iidm.network.*; +import com.powsybl.ucte.network.UcteElementId; +import com.powsybl.ucte.network.UcteNodeCode; + +import java.util.HashMap; +import java.util.Map; + +/** + * @author Clément LECLERC {@literal } + */ +public abstract class AbstractNamingStrategy implements NamingStrategy { + + protected static final String NO_COUNTRY_ERROR = "No country for this substation"; + protected static final String NO_UCTE_CODE_ERROR = "No UCTE code found for id: %s"; + protected static final String NO_UCTE_COUNTRY_ERROR = "No UCTE country code for %s"; + + protected final Map ucteNodeIds = new HashMap<>(); + protected final Map ucteElementIds = new HashMap<>(); + + @Override + public void initialiseNetwork(Network network) { + //Empty implementation by default + } + + @Override + public UcteNodeCode getUcteNodeCode(String id) { + return ucteNodeIds.computeIfAbsent(id, k -> UcteNodeCode.parseUcteNodeCode(k) + .orElseThrow(() -> new UcteException(NO_UCTE_CODE_ERROR + k))); + } + + @Override + public UcteNodeCode getUcteNodeCode(Bus bus) { + return getUcteNodeCode(bus.getId()); + } + + @Override + public UcteNodeCode getUcteNodeCode(DanglingLine danglingLine) { + if (danglingLine.getPairingKey() == null) { + return getUcteNodeCode(danglingLine.getId()); + } + return getUcteNodeCode(danglingLine.getPairingKey()); + } + + @Override + public UcteElementId getUcteElementId(String id) { + return ucteElementIds.computeIfAbsent(id, k -> UcteElementId.parseUcteElementId(k) + .orElseThrow(() -> new UcteException(NO_UCTE_CODE_ERROR + k))); + } + + @Override + public UcteElementId getUcteElementId(Switch sw) { + return getUcteElementId(sw.getId()); + } + + @Override + public UcteElementId getUcteElementId(Branch branch) { + return getUcteElementId(branch.getId()); + } + + @Override + public UcteElementId getUcteElementId(DanglingLine danglingLine) { + return getUcteElementId(danglingLine.getId()); + } +} + diff --git a/ucte/ucte-converter/src/main/java/com/powsybl/ucte/converter/CounterNamingStrategy.java b/ucte/ucte-converter/src/main/java/com/powsybl/ucte/converter/CounterNamingStrategy.java index 00e1f2364d2..f38a3a6ef1d 100644 --- a/ucte/ucte-converter/src/main/java/com/powsybl/ucte/converter/CounterNamingStrategy.java +++ b/ucte/ucte-converter/src/main/java/com/powsybl/ucte/converter/CounterNamingStrategy.java @@ -21,13 +21,8 @@ * @author Clément LECLERC {@literal } */ @AutoService(NamingStrategy.class) -public class CounterNamingStrategy implements NamingStrategy { - private static final String NO_COUNTRY_ERROR = "No country for this substation"; - private static final String NO_UCTE_CODE_ERROR = "No UCTE code found for id: %s"; - private static final String NO_UCTE_COUNTRY_ERROR = "No UCTE country code for %s"; +public class CounterNamingStrategy extends AbstractNamingStrategy { - private final Map ucteNodeIds = new HashMap<>(); - private final Map ucteElementIds = new HashMap<>(); private int namingCounter; @Override @@ -35,7 +30,6 @@ public String getName() { return "Counter"; } - @Override public void initialiseNetwork(Network network) { namingCounter = 0; network.getVoltageLevelStream() @@ -166,46 +160,4 @@ private UcteElementId generateUcteElementId(Switch sw) { return generateUcteElementId(sw.getId(), u1, u2); } - @Override - public UcteNodeCode getUcteNodeCode(String id) { - if (id == null) { - throw new PowsyblException("ID is null"); - } - return Optional.ofNullable(ucteNodeIds.get(id)) - .orElseThrow(() -> new UcteException(String.format(NO_UCTE_CODE_ERROR, id))); - } - - @Override - public UcteNodeCode getUcteNodeCode(Bus bus) { - return getUcteNodeCode(bus.getId()); - } - - @Override - public UcteNodeCode getUcteNodeCode(DanglingLine danglingLine) { - if (danglingLine.getPairingKey() == null) { - return getUcteNodeCode(danglingLine.getId()); - } - return getUcteNodeCode(danglingLine.getPairingKey()); - } - - @Override - public UcteElementId getUcteElementId(String id) { - return Optional.ofNullable(ucteElementIds.get(id)) - .orElseThrow(() -> new UcteException(String.format("No UCTE element id found for: %s", id))); - } - - @Override - public UcteElementId getUcteElementId(Switch sw) { - return getUcteElementId(sw.getId()); - } - - @Override - public UcteElementId getUcteElementId(Branch branch) { - return getUcteElementId(branch.getId()); - } - - @Override - public UcteElementId getUcteElementId(DanglingLine danglingLine) { - return getUcteElementId(danglingLine.getId()); - } } diff --git a/ucte/ucte-converter/src/main/java/com/powsybl/ucte/converter/DefaultNamingStrategy.java b/ucte/ucte-converter/src/main/java/com/powsybl/ucte/converter/DefaultNamingStrategy.java index 9abd0c9e623..b85bcd5c4eb 100644 --- a/ucte/ucte-converter/src/main/java/com/powsybl/ucte/converter/DefaultNamingStrategy.java +++ b/ucte/ucte-converter/src/main/java/com/powsybl/ucte/converter/DefaultNamingStrategy.java @@ -9,12 +9,7 @@ package com.powsybl.ucte.converter; import com.google.auto.service.AutoService; -import com.powsybl.iidm.network.*; -import com.powsybl.ucte.network.UcteElementId; -import com.powsybl.ucte.network.UcteNodeCode; -import java.util.HashMap; -import java.util.Map; /** * A {@link NamingStrategy} implementation that ensures the conformity of IDs with the UCTE-DEF format @@ -22,55 +17,11 @@ * @author Mathieu Bague {@literal } */ @AutoService(NamingStrategy.class) -public class DefaultNamingStrategy implements NamingStrategy { - - private final Map ucteNodeIds = new HashMap<>(); - - private final Map ucteElementIds = new HashMap<>(); +public class DefaultNamingStrategy extends AbstractNamingStrategy { @Override public String getName() { return "Default"; } - @Override - public void initialiseNetwork(Network network) { - - } - - @Override - public UcteNodeCode getUcteNodeCode(String id) { - return ucteNodeIds.computeIfAbsent(id, k -> UcteNodeCode.parseUcteNodeCode(k).orElseThrow(() -> new UcteException("Invalid UCTE node identifier: " + k))); - } - - @Override - public UcteNodeCode getUcteNodeCode(Bus bus) { - return getUcteNodeCode(bus.getId()); - } - - @Override - public UcteNodeCode getUcteNodeCode(DanglingLine danglingLine) { - return getUcteNodeCode(danglingLine.getPairingKey()); - } - - @Override - public UcteElementId getUcteElementId(String id) { - return ucteElementIds.computeIfAbsent(id, k -> UcteElementId.parseUcteElementId(k).orElseThrow(() -> new UcteException("Invalid UCTE node identifier: " + k))); - } - - @Override - public UcteElementId getUcteElementId(Switch sw) { - return getUcteElementId(sw.getId()); - } - - @Override - public UcteElementId getUcteElementId(Branch branch) { - return getUcteElementId(branch.getId()); - } - - @Override - public UcteElementId getUcteElementId(DanglingLine danglingLine) { - return getUcteElementId(danglingLine.getId()); - } - } diff --git a/ucte/ucte-converter/src/main/java/com/powsybl/ucte/converter/NamingStrategy.java b/ucte/ucte-converter/src/main/java/com/powsybl/ucte/converter/NamingStrategy.java index 5cbe47aeded..949d30072f5 100644 --- a/ucte/ucte-converter/src/main/java/com/powsybl/ucte/converter/NamingStrategy.java +++ b/ucte/ucte-converter/src/main/java/com/powsybl/ucte/converter/NamingStrategy.java @@ -17,9 +17,9 @@ */ public interface NamingStrategy { - String getName(); + public void initialiseNetwork(Network network); - void initialiseNetwork(Network network); + String getName(); UcteNodeCode getUcteNodeCode(String id); From e6182776618e309fb81fd7a9f4dd38fa1d4f93eb Mon Sep 17 00:00:00 2001 From: Leclerc Clement Date: Mon, 2 Dec 2024 09:28:15 +0100 Subject: [PATCH 25/38] change OrderCode list Signed-off-by: Leclerc Clement --- .../com/powsybl/ucte/converter/CounterNamingStrategy.java | 3 ++- .../main/java/com/powsybl/ucte/converter/UcteExporter.java | 4 ++-- .../src/main/java/com/powsybl/ucte/network/UcteElementId.java | 4 ++-- 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/ucte/ucte-converter/src/main/java/com/powsybl/ucte/converter/CounterNamingStrategy.java b/ucte/ucte-converter/src/main/java/com/powsybl/ucte/converter/CounterNamingStrategy.java index f38a3a6ef1d..64577cc33e4 100644 --- a/ucte/ucte-converter/src/main/java/com/powsybl/ucte/converter/CounterNamingStrategy.java +++ b/ucte/ucte-converter/src/main/java/com/powsybl/ucte/converter/CounterNamingStrategy.java @@ -30,6 +30,7 @@ public String getName() { return "Counter"; } + @Override public void initialiseNetwork(Network network) { namingCounter = 0; network.getVoltageLevelStream() @@ -117,7 +118,7 @@ private UcteElementId generateUcteElementId(String id, UcteNodeCode node1, UcteN return uniqueElementId; } - private UcteElementId generateUcteElementId(Branch branch) { + private UcteElementId generateUcteElementId(Branch branch) { if (ucteElementIds.containsKey(branch.getId())) { return ucteElementIds.get(branch.getId()); } diff --git a/ucte/ucte-converter/src/main/java/com/powsybl/ucte/converter/UcteExporter.java b/ucte/ucte-converter/src/main/java/com/powsybl/ucte/converter/UcteExporter.java index 55ca23aeb2b..a2f2af02e21 100644 --- a/ucte/ucte-converter/src/main/java/com/powsybl/ucte/converter/UcteExporter.java +++ b/ucte/ucte-converter/src/main/java/com/powsybl/ucte/converter/UcteExporter.java @@ -141,7 +141,7 @@ private static UcteNetwork createUcteNetwork(Network network, NamingStrategy nam network.getHvdcLineCount() > 0 || network.getThreeWindingsTransformerCount() > 0) { - throw new UcteException("This network contains unsupported equipments"); + //throw new UcteException("This network contains unsupported equipments"); } UcteExporterContext context = new UcteExporterContext(namingStrategy, combinePhaseAngleRegulation); @@ -183,7 +183,7 @@ private static void convertBus(UcteNetwork ucteNetwork, Bus bus, UcteExporterCon throw new UcteException("Too many generators connected to this bus"); } if (bus.getLoadStream().count() > 1) { - throw new UcteException("Too many loads connected to this bus"); + //throw new UcteException("Too many loads connected to this bus"); } UcteNodeCode ucteNodeCode = context.getNamingStrategy().getUcteNodeCode(bus); diff --git a/ucte/ucte-network/src/main/java/com/powsybl/ucte/network/UcteElementId.java b/ucte/ucte-network/src/main/java/com/powsybl/ucte/network/UcteElementId.java index fbcd3e4651b..daeee3fa849 100644 --- a/ucte/ucte-network/src/main/java/com/powsybl/ucte/network/UcteElementId.java +++ b/ucte/ucte-network/src/main/java/com/powsybl/ucte/network/UcteElementId.java @@ -15,8 +15,8 @@ */ public class UcteElementId implements Comparable { - public static final List ORDER_CODES = Arrays.asList('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', - 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '_', '-', '.', ' '); + public static final List ORDER_CODES = Arrays.asList('1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', + 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'); private final UcteNodeCode nodeCode1; private final UcteNodeCode nodeCode2; From 20e844ced9b672b812b5a2ff5b426cdedc5f790f Mon Sep 17 00:00:00 2001 From: Leclerc Clement Date: Mon, 2 Dec 2024 09:57:23 +0100 Subject: [PATCH 26/38] change OrderCode list Signed-off-by: Leclerc Clement --- .../main/java/com/powsybl/ucte/converter/UcteExporter.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ucte/ucte-converter/src/main/java/com/powsybl/ucte/converter/UcteExporter.java b/ucte/ucte-converter/src/main/java/com/powsybl/ucte/converter/UcteExporter.java index a2f2af02e21..55ca23aeb2b 100644 --- a/ucte/ucte-converter/src/main/java/com/powsybl/ucte/converter/UcteExporter.java +++ b/ucte/ucte-converter/src/main/java/com/powsybl/ucte/converter/UcteExporter.java @@ -141,7 +141,7 @@ private static UcteNetwork createUcteNetwork(Network network, NamingStrategy nam network.getHvdcLineCount() > 0 || network.getThreeWindingsTransformerCount() > 0) { - //throw new UcteException("This network contains unsupported equipments"); + throw new UcteException("This network contains unsupported equipments"); } UcteExporterContext context = new UcteExporterContext(namingStrategy, combinePhaseAngleRegulation); @@ -183,7 +183,7 @@ private static void convertBus(UcteNetwork ucteNetwork, Bus bus, UcteExporterCon throw new UcteException("Too many generators connected to this bus"); } if (bus.getLoadStream().count() > 1) { - //throw new UcteException("Too many loads connected to this bus"); + throw new UcteException("Too many loads connected to this bus"); } UcteNodeCode ucteNodeCode = context.getNamingStrategy().getUcteNodeCode(bus); From 9762869dbaf53ae172f5448bf10a339635249002 Mon Sep 17 00:00:00 2001 From: Leclerc Clement Date: Mon, 2 Dec 2024 15:06:29 +0100 Subject: [PATCH 27/38] change DanlingLine conversion Signed-off-by: Leclerc Clement --- .../converter/AbstractNamingStrategy.java | 9 +-- .../ucte/converter/CounterNamingStrategy.java | 58 ++++++++++--------- .../ucte/converter/DefaultNamingStrategy.java | 1 - .../ucte/converter/util/UcteConstants.java | 7 +++ .../converter/CounterNamingStrategyTest.java | 21 +++---- .../src/test/resources/network.xiidm | 8 ++- .../powsybl/ucte/network/UcteElementId.java | 2 +- 7 files changed, 57 insertions(+), 49 deletions(-) diff --git a/ucte/ucte-converter/src/main/java/com/powsybl/ucte/converter/AbstractNamingStrategy.java b/ucte/ucte-converter/src/main/java/com/powsybl/ucte/converter/AbstractNamingStrategy.java index a5ae5ec0899..3052e72eab3 100644 --- a/ucte/ucte-converter/src/main/java/com/powsybl/ucte/converter/AbstractNamingStrategy.java +++ b/ucte/ucte-converter/src/main/java/com/powsybl/ucte/converter/AbstractNamingStrategy.java @@ -8,6 +8,7 @@ package com.powsybl.ucte.converter; import com.powsybl.iidm.network.*; +import com.powsybl.ucte.converter.util.UcteConstants; import com.powsybl.ucte.network.UcteElementId; import com.powsybl.ucte.network.UcteNodeCode; @@ -19,10 +20,6 @@ */ public abstract class AbstractNamingStrategy implements NamingStrategy { - protected static final String NO_COUNTRY_ERROR = "No country for this substation"; - protected static final String NO_UCTE_CODE_ERROR = "No UCTE code found for id: %s"; - protected static final String NO_UCTE_COUNTRY_ERROR = "No UCTE country code for %s"; - protected final Map ucteNodeIds = new HashMap<>(); protected final Map ucteElementIds = new HashMap<>(); @@ -34,7 +31,7 @@ public void initialiseNetwork(Network network) { @Override public UcteNodeCode getUcteNodeCode(String id) { return ucteNodeIds.computeIfAbsent(id, k -> UcteNodeCode.parseUcteNodeCode(k) - .orElseThrow(() -> new UcteException(NO_UCTE_CODE_ERROR + k))); + .orElseThrow(() -> new UcteException(UcteConstants.NO_UCTE_CODE_ERROR + k))); } @Override @@ -53,7 +50,7 @@ public UcteNodeCode getUcteNodeCode(DanglingLine danglingLine) { @Override public UcteElementId getUcteElementId(String id) { return ucteElementIds.computeIfAbsent(id, k -> UcteElementId.parseUcteElementId(k) - .orElseThrow(() -> new UcteException(NO_UCTE_CODE_ERROR + k))); + .orElseThrow(() -> new UcteException(UcteConstants.NO_UCTE_CODE_ERROR + k))); } @Override diff --git a/ucte/ucte-converter/src/main/java/com/powsybl/ucte/converter/CounterNamingStrategy.java b/ucte/ucte-converter/src/main/java/com/powsybl/ucte/converter/CounterNamingStrategy.java index 64577cc33e4..2ed44ded469 100644 --- a/ucte/ucte-converter/src/main/java/com/powsybl/ucte/converter/CounterNamingStrategy.java +++ b/ucte/ucte-converter/src/main/java/com/powsybl/ucte/converter/CounterNamingStrategy.java @@ -10,6 +10,7 @@ import com.google.auto.service.AutoService; import com.powsybl.commons.PowsyblException; import com.powsybl.iidm.network.*; +import com.powsybl.ucte.converter.util.UcteConstants; import com.powsybl.ucte.network.UcteCountryCode; import com.powsybl.ucte.network.UcteElementId; import com.powsybl.ucte.network.UcteNodeCode; @@ -23,7 +24,7 @@ @AutoService(NamingStrategy.class) public class CounterNamingStrategy extends AbstractNamingStrategy { - private int namingCounter; + private int voltageLevelCounter; @Override public String getName() { @@ -32,7 +33,7 @@ public String getName() { @Override public void initialiseNetwork(Network network) { - namingCounter = 0; + voltageLevelCounter = 0; network.getVoltageLevelStream() .forEach(this::processVoltageLevel); @@ -49,37 +50,35 @@ private void processVoltageLevel(VoltageLevel voltageLevel) { voltageLevel.getBusBreakerView().getSwitches() .forEach(this::generateUcteElementId); - namingCounter++; + voltageLevelCounter++; } - private UcteNodeCode generateUcteNodeId(String nodeId, VoltageLevel voltageLevel, int orderCodeIndex) { - if (UcteNodeCode.isUcteNodeId(nodeId)) { - return processExistingUcteNodeId(nodeId, orderCodeIndex); + private UcteNodeCode generateUcteNodeId(String busId, VoltageLevel voltageLevel, int orderCodeIndex) { + if (UcteNodeCode.isUcteNodeId(busId)) { + return changeOrderCode(busId, orderCodeIndex); } - if (ucteNodeIds.containsKey(nodeId)) { - return ucteNodeIds.get(nodeId); - } - return createNewUcteNodeId(nodeId, voltageLevel, orderCodeIndex); + return createNewUcteNodeId(busId, voltageLevel, orderCodeIndex); } - private UcteNodeCode processExistingUcteNodeId(String nodeId, int orderCodeIndex) { - String newNodeId = nodeId.substring(0, nodeId.length() - 1) + orderCodeIndex; - Optional nodeCode = UcteNodeCode.parseUcteNodeCode(newNodeId); - if (nodeCode.isPresent()) { - ucteNodeIds.put(nodeId, nodeCode.get()); - return nodeCode.get(); + private UcteNodeCode changeOrderCode(String busId, int orderCodeIndex) { + UcteNodeCode nodeCode = UcteNodeCode.parseUcteNodeCode(busId).orElseThrow(); + UcteNodeCode newUcteNodeCode = new UcteNodeCode(nodeCode.getUcteCountryCode(), nodeCode.getGeographicalSpot(), nodeCode.getVoltageLevelCode(), UcteConstants.ORDER_CODES.get(0)); + if (orderCodeIndex > UcteConstants.ORDER_CODES.size()) { + throw new PowsyblException("Order code index out of bounds"); } - throw new PowsyblException(String.format(NO_UCTE_CODE_ERROR, newNodeId)); + newUcteNodeCode.setBusbar(UcteConstants.ORDER_CODES.get(orderCodeIndex)); + ucteNodeIds.put(busId, newUcteNodeCode); + return newUcteNodeCode; } - private UcteNodeCode createNewUcteNodeId(String nodeId, VoltageLevel voltageLevel, int orderCodeIndex) { - String newNodeId = String.format("%05d", namingCounter); + private UcteNodeCode createNewUcteNodeId(String busId, VoltageLevel voltageLevel, int orderCodeIndex) { + String newNodeId = String.format("%05d", voltageLevelCounter); char countryCode = getCountryCode(voltageLevel).getUcteCode(); char voltageLevelCode = UcteVoltageLevelCode.voltageLevelCodeFromVoltage(voltageLevel.getNominalV()); - if (orderCodeIndex > UcteElementId.ORDER_CODES.get(orderCodeIndex)) { + if (orderCodeIndex > UcteConstants.ORDER_CODES.size()) { throw new PowsyblException("Order code index out of bounds"); } - char orderCode = UcteElementId.ORDER_CODES.get(orderCodeIndex); + char orderCode = UcteConstants.ORDER_CODES.get(orderCodeIndex); UcteNodeCode ucteNodeCode = new UcteNodeCode( UcteCountryCode.fromUcteCode(countryCode), @@ -87,19 +86,19 @@ private UcteNodeCode createNewUcteNodeId(String nodeId, VoltageLevel voltageLeve UcteVoltageLevelCode.voltageLevelCodeFromChar(voltageLevelCode), orderCode); - ucteNodeIds.put(nodeId, ucteNodeCode); + ucteNodeIds.put(busId, ucteNodeCode); return ucteNodeCode; } private UcteCountryCode getCountryCode(VoltageLevel voltageLevel) { Country country = voltageLevel.getSubstation() .flatMap(Substation::getCountry) - .orElseThrow(() -> new UcteException(NO_COUNTRY_ERROR)); + .orElseThrow(() -> new UcteException(UcteConstants.NO_COUNTRY_ERROR)); try { return UcteCountryCode.valueOf(country.name()); } catch (IllegalArgumentException e) { - throw new UcteException(String.format(NO_UCTE_COUNTRY_ERROR, country.getName())); + throw new UcteException(String.format(UcteConstants.NO_UCTE_COUNTRY_ERROR, country.getName())); } } @@ -108,7 +107,7 @@ private UcteElementId generateUcteElementId(String id, UcteNodeCode node1, UcteN return ucteElementIds.get(id); } - UcteElementId uniqueElementId = UcteElementId.ORDER_CODES.stream() + UcteElementId uniqueElementId = UcteConstants.ORDER_CODES.stream() .map(orderCode -> new UcteElementId(node1, node2, orderCode)) .filter(elementId -> !ucteElementIds.containsValue(elementId)) .findFirst() @@ -138,8 +137,13 @@ private UcteElementId generateUcteElementId(DanglingLine danglingLine) { code1 = getUcteNodeCode(danglingLine.getTerminal().getBusBreakerView().getBus()); - if (danglingLine.getPairingKey() != null) { - code2 = generateUcteNodeId(danglingLine.getPairingKey(), danglingLine.getTerminal().getVoltageLevel(), 0); + if (danglingLine.getPairingKey() != null && UcteNodeCode.isUcteNodeId(danglingLine.getPairingKey())) { + UcteNodeCode pairingKeyId = UcteNodeCode.parseUcteNodeCode(danglingLine.getPairingKey()).orElseThrow(); + int orderCodeindex = UcteConstants.ORDER_CODES.indexOf(pairingKeyId.getBusbar()); + if (orderCodeindex == -1) { + throw new PowsyblException("No orderCode found for " + pairingKeyId); + } + code2 = generateUcteNodeId(danglingLine.getPairingKey(), danglingLine.getTerminal().getVoltageLevel(), orderCodeindex); } else { code2 = generateUcteNodeId(danglingLine.getId(), danglingLine.getTerminal().getVoltageLevel(), 0); } diff --git a/ucte/ucte-converter/src/main/java/com/powsybl/ucte/converter/DefaultNamingStrategy.java b/ucte/ucte-converter/src/main/java/com/powsybl/ucte/converter/DefaultNamingStrategy.java index b85bcd5c4eb..d4855e4aba2 100644 --- a/ucte/ucte-converter/src/main/java/com/powsybl/ucte/converter/DefaultNamingStrategy.java +++ b/ucte/ucte-converter/src/main/java/com/powsybl/ucte/converter/DefaultNamingStrategy.java @@ -23,5 +23,4 @@ public class DefaultNamingStrategy extends AbstractNamingStrategy { public String getName() { return "Default"; } - } diff --git a/ucte/ucte-converter/src/main/java/com/powsybl/ucte/converter/util/UcteConstants.java b/ucte/ucte-converter/src/main/java/com/powsybl/ucte/converter/util/UcteConstants.java index 10bb3766fb8..1c2be494809 100644 --- a/ucte/ucte-converter/src/main/java/com/powsybl/ucte/converter/util/UcteConstants.java +++ b/ucte/ucte-converter/src/main/java/com/powsybl/ucte/converter/util/UcteConstants.java @@ -7,6 +7,8 @@ */ package com.powsybl.ucte.converter.util; +import java.util.List; + /** * @author Sebastien Murgey {@literal } */ @@ -26,4 +28,9 @@ private UcteConstants() { public static final String ORDER_CODE = "orderCode"; public static final String POWER_PLANT_TYPE_PROPERTY_KEY = "powerPlantType"; public static final int DEFAULT_POWER_LIMIT = 9999; + public static final String NO_COUNTRY_ERROR = "No country for this substation"; + public static final String NO_UCTE_CODE_ERROR = "No UCTE code found for id: "; + public static final String NO_UCTE_COUNTRY_ERROR = "No UCTE country code for "; + public static final List ORDER_CODES = List.of('1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', + 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'); } diff --git a/ucte/ucte-converter/src/test/java/com/powsybl/ucte/converter/CounterNamingStrategyTest.java b/ucte/ucte-converter/src/test/java/com/powsybl/ucte/converter/CounterNamingStrategyTest.java index 5e726b63661..79a28a2cdad 100644 --- a/ucte/ucte-converter/src/test/java/com/powsybl/ucte/converter/CounterNamingStrategyTest.java +++ b/ucte/ucte-converter/src/test/java/com/powsybl/ucte/converter/CounterNamingStrategyTest.java @@ -74,7 +74,7 @@ void testBasicNodeCodeGeneration() { Bus hv1Bus = network.getBusBreakerView().getBus("NHV1"); Bus hv2Bus = network.getBusBreakerView().getBus("NHV2"); Bus loadBus = network.getBusBreakerView().getBus("NLOAD"); - Bus ucteBus = network.getBusBreakerView().getBus("F0000010"); + Bus ucteBus = network.getBusBreakerView().getBus("F0000014"); UcteNodeCode genCode = strategy.getUcteNodeCode(genBus); UcteNodeCode hv1Code = strategy.getUcteNodeCode(hv1Bus); @@ -82,6 +82,8 @@ void testBasicNodeCodeGeneration() { UcteNodeCode loadCode = strategy.getUcteNodeCode(loadBus); UcteNodeCode ucteCode = strategy.getUcteNodeCode(ucteBus); + System.out.println(ucteCode); + assertAll( () -> assertTrue(UcteNodeCode.isUcteNodeId(genCode.toString())), () -> assertTrue(UcteNodeCode.isUcteNodeId(hv1Code.toString())), @@ -152,27 +154,22 @@ void testDanglingLineElementIds() { strategy.initialiseNetwork(network); DanglingLine dl1 = network.getDanglingLine("DL1"); DanglingLine dl2 = network.getDanglingLine("DL2"); + DanglingLine dl3 = network.getDanglingLine("DL3"); UcteElementId dlId1 = strategy.getUcteElementId(dl1); UcteElementId dlId2 = strategy.getUcteElementId(dl2); + UcteElementId dlId3 = strategy.getUcteElementId(dl3); assertAll( () -> assertTrue(UcteElementId.isUcteElementId(dlId1.toString())), - () -> assertTrue(UcteElementId.isUcteElementId(dlId1.toString())), - () -> assertNotEquals(dlId1, dlId2) + () -> assertTrue(UcteElementId.isUcteElementId(dlId3.toString())), + () -> assertNotEquals(dlId1, dlId2), + () -> assertNotEquals(dlId1, dlId3), + () -> assertNotEquals(dlId2, dlId3) ); } - @Test - void testDanglingLineNodeIds() { - strategy.initialiseNetwork(network); - DanglingLine dl1 = network.getDanglingLine("DL1"); - UcteNodeCode code1 = strategy.getUcteNodeCode(dl1); - - assertTrue(UcteNodeCode.isUcteNodeId(code1.toString())); - } - @Test void testParallelLines() { strategy.initialiseNetwork(network); diff --git a/ucte/ucte-converter/src/test/resources/network.xiidm b/ucte/ucte-converter/src/test/resources/network.xiidm index ec583532206..078acfc506e 100644 --- a/ucte/ucte-converter/src/test/resources/network.xiidm +++ b/ucte/ucte-converter/src/test/resources/network.xiidm @@ -6,7 +6,7 @@ - + @@ -18,7 +18,11 @@ + bus="NGEN2" connectableBus="NGEN2" pairingKey="X0000011"> + + + diff --git a/ucte/ucte-network/src/main/java/com/powsybl/ucte/network/UcteElementId.java b/ucte/ucte-network/src/main/java/com/powsybl/ucte/network/UcteElementId.java index daeee3fa849..a6b4c43d8aa 100644 --- a/ucte/ucte-network/src/main/java/com/powsybl/ucte/network/UcteElementId.java +++ b/ucte/ucte-network/src/main/java/com/powsybl/ucte/network/UcteElementId.java @@ -15,7 +15,7 @@ */ public class UcteElementId implements Comparable { - public static final List ORDER_CODES = Arrays.asList('1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', + private static final List ORDER_CODES = List.of('1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'); private final UcteNodeCode nodeCode1; From e2d11b7e650297fb1b9cdef327e9f94327c9ccd2 Mon Sep 17 00:00:00 2001 From: Leclerc Clement Date: Mon, 2 Dec 2024 17:31:31 +0100 Subject: [PATCH 28/38] add orderCode getter Signed-off-by: Leclerc Clement --- .../ucte/converter/CounterNamingStrategy.java | 39 +++++++------------ .../ucte/converter/NamingStrategy.java | 2 +- .../converter/util/UcteConverterHelper.java | 11 +++++- .../converter/CounterNamingStrategyTest.java | 20 +++++++--- .../util/UcteConverterHelperTest.java | 16 +++++++- .../src/test/resources/network.xiidm | 1 + .../ucte/network/UcteVoltageLevelCode.java | 2 +- 7 files changed, 55 insertions(+), 36 deletions(-) diff --git a/ucte/ucte-converter/src/main/java/com/powsybl/ucte/converter/CounterNamingStrategy.java b/ucte/ucte-converter/src/main/java/com/powsybl/ucte/converter/CounterNamingStrategy.java index 2ed44ded469..812df6ccac8 100644 --- a/ucte/ucte-converter/src/main/java/com/powsybl/ucte/converter/CounterNamingStrategy.java +++ b/ucte/ucte-converter/src/main/java/com/powsybl/ucte/converter/CounterNamingStrategy.java @@ -8,9 +8,9 @@ package com.powsybl.ucte.converter; import com.google.auto.service.AutoService; -import com.powsybl.commons.PowsyblException; import com.powsybl.iidm.network.*; import com.powsybl.ucte.converter.util.UcteConstants; +import com.powsybl.ucte.converter.util.UcteConverterHelper; import com.powsybl.ucte.network.UcteCountryCode; import com.powsybl.ucte.network.UcteElementId; import com.powsybl.ucte.network.UcteNodeCode; @@ -45,7 +45,8 @@ private void processVoltageLevel(VoltageLevel voltageLevel) { Iterator buslist = voltageLevel.getBusBreakerView().getBuses().iterator(); for (int i = 0; buslist.hasNext(); i++) { Bus bus = buslist.next(); - generateUcteNodeId(bus.getId(), voltageLevel, i); + char orderCode = UcteConverterHelper.getOrderCode(i); + generateUcteNodeId(bus.getId(), voltageLevel, orderCode); } voltageLevel.getBusBreakerView().getSwitches() @@ -53,32 +54,24 @@ private void processVoltageLevel(VoltageLevel voltageLevel) { voltageLevelCounter++; } - private UcteNodeCode generateUcteNodeId(String busId, VoltageLevel voltageLevel, int orderCodeIndex) { + private UcteNodeCode generateUcteNodeId(String busId, VoltageLevel voltageLevel, char orderCode) { if (UcteNodeCode.isUcteNodeId(busId)) { - return changeOrderCode(busId, orderCodeIndex); + return changeOrderCode(busId, orderCode); } - return createNewUcteNodeId(busId, voltageLevel, orderCodeIndex); + return createNewUcteNodeId(busId, voltageLevel, orderCode); } - private UcteNodeCode changeOrderCode(String busId, int orderCodeIndex) { - UcteNodeCode nodeCode = UcteNodeCode.parseUcteNodeCode(busId).orElseThrow(); - UcteNodeCode newUcteNodeCode = new UcteNodeCode(nodeCode.getUcteCountryCode(), nodeCode.getGeographicalSpot(), nodeCode.getVoltageLevelCode(), UcteConstants.ORDER_CODES.get(0)); - if (orderCodeIndex > UcteConstants.ORDER_CODES.size()) { - throw new PowsyblException("Order code index out of bounds"); - } - newUcteNodeCode.setBusbar(UcteConstants.ORDER_CODES.get(orderCodeIndex)); - ucteNodeIds.put(busId, newUcteNodeCode); - return newUcteNodeCode; + private UcteNodeCode changeOrderCode(String busId, char orderCode) { + UcteNodeCode newNodeCode = UcteNodeCode.parseUcteNodeCode(busId).orElseThrow(); + newNodeCode.setBusbar(orderCode); + ucteNodeIds.put(busId, newNodeCode); + return newNodeCode; } - private UcteNodeCode createNewUcteNodeId(String busId, VoltageLevel voltageLevel, int orderCodeIndex) { + private UcteNodeCode createNewUcteNodeId(String busId, VoltageLevel voltageLevel, char orderCode) { String newNodeId = String.format("%05d", voltageLevelCounter); char countryCode = getCountryCode(voltageLevel).getUcteCode(); char voltageLevelCode = UcteVoltageLevelCode.voltageLevelCodeFromVoltage(voltageLevel.getNominalV()); - if (orderCodeIndex > UcteConstants.ORDER_CODES.size()) { - throw new PowsyblException("Order code index out of bounds"); - } - char orderCode = UcteConstants.ORDER_CODES.get(orderCodeIndex); UcteNodeCode ucteNodeCode = new UcteNodeCode( UcteCountryCode.fromUcteCode(countryCode), @@ -139,13 +132,9 @@ private UcteElementId generateUcteElementId(DanglingLine danglingLine) { if (danglingLine.getPairingKey() != null && UcteNodeCode.isUcteNodeId(danglingLine.getPairingKey())) { UcteNodeCode pairingKeyId = UcteNodeCode.parseUcteNodeCode(danglingLine.getPairingKey()).orElseThrow(); - int orderCodeindex = UcteConstants.ORDER_CODES.indexOf(pairingKeyId.getBusbar()); - if (orderCodeindex == -1) { - throw new PowsyblException("No orderCode found for " + pairingKeyId); - } - code2 = generateUcteNodeId(danglingLine.getPairingKey(), danglingLine.getTerminal().getVoltageLevel(), orderCodeindex); + code2 = generateUcteNodeId(danglingLine.getPairingKey(), danglingLine.getTerminal().getVoltageLevel(), pairingKeyId.getBusbar()); } else { - code2 = generateUcteNodeId(danglingLine.getId(), danglingLine.getTerminal().getVoltageLevel(), 0); + code2 = generateUcteNodeId(danglingLine.getId(), danglingLine.getTerminal().getVoltageLevel(), UcteConverterHelper.getOrderCode(0)); } return generateUcteElementId(danglingLine.getId(), code1, code2); } diff --git a/ucte/ucte-converter/src/main/java/com/powsybl/ucte/converter/NamingStrategy.java b/ucte/ucte-converter/src/main/java/com/powsybl/ucte/converter/NamingStrategy.java index 949d30072f5..3e2adcf5f9d 100644 --- a/ucte/ucte-converter/src/main/java/com/powsybl/ucte/converter/NamingStrategy.java +++ b/ucte/ucte-converter/src/main/java/com/powsybl/ucte/converter/NamingStrategy.java @@ -17,7 +17,7 @@ */ public interface NamingStrategy { - public void initialiseNetwork(Network network); + void initialiseNetwork(Network network); String getName(); diff --git a/ucte/ucte-converter/src/main/java/com/powsybl/ucte/converter/util/UcteConverterHelper.java b/ucte/ucte-converter/src/main/java/com/powsybl/ucte/converter/util/UcteConverterHelper.java index cc1b1f9c9e5..308d846ce3f 100644 --- a/ucte/ucte-converter/src/main/java/com/powsybl/ucte/converter/util/UcteConverterHelper.java +++ b/ucte/ucte-converter/src/main/java/com/powsybl/ucte/converter/util/UcteConverterHelper.java @@ -10,6 +10,7 @@ import com.powsybl.iidm.network.PhaseTapChanger; import com.powsybl.iidm.network.RatioTapChanger; import com.powsybl.iidm.network.TwoWindingsTransformer; +import com.powsybl.ucte.converter.UcteException; import com.powsybl.ucte.network.UcteAngleRegulation; import com.powsybl.ucte.network.UctePhaseRegulation; import org.apache.commons.math3.complex.Complex; @@ -120,6 +121,14 @@ public static Complex calculateAsymmAngleDuAndAngle(TwoWindingsTransformer twoWi } return ComplexUtils.polar2Complex(BigDecimal.valueOf(absDu).setScale(4, RoundingMode.HALF_UP).doubleValue(), - theta); + theta); } + + public static char getOrderCode(int index) { + if (index > UcteConstants.ORDER_CODES.size() || index < 0) { + throw new UcteException("Order code index out of bounds"); + } + return UcteConstants.ORDER_CODES.get(index); + } + } diff --git a/ucte/ucte-converter/src/test/java/com/powsybl/ucte/converter/CounterNamingStrategyTest.java b/ucte/ucte-converter/src/test/java/com/powsybl/ucte/converter/CounterNamingStrategyTest.java index 79a28a2cdad..70ce3d656b6 100644 --- a/ucte/ucte-converter/src/test/java/com/powsybl/ucte/converter/CounterNamingStrategyTest.java +++ b/ucte/ucte-converter/src/test/java/com/powsybl/ucte/converter/CounterNamingStrategyTest.java @@ -71,18 +71,25 @@ void testBasicNodeCodeGeneration() { strategy.initialiseNetwork(network); Bus genBus = network.getBusBreakerView().getBus("NGEN"); - Bus hv1Bus = network.getBusBreakerView().getBus("NHV1"); - Bus hv2Bus = network.getBusBreakerView().getBus("NHV2"); + Bus hv1Bus = network.getBusBreakerView().getBus("NGEN2"); + Bus hv2Bus = network.getBusBreakerView().getBus("NGEN3"); Bus loadBus = network.getBusBreakerView().getBus("NLOAD"); Bus ucteBus = network.getBusBreakerView().getBus("F0000014"); + Bus ucteBus2 = network.getBusBreakerView().getBus("F0000019"); UcteNodeCode genCode = strategy.getUcteNodeCode(genBus); UcteNodeCode hv1Code = strategy.getUcteNodeCode(hv1Bus); UcteNodeCode hv2Code = strategy.getUcteNodeCode(hv2Bus); UcteNodeCode loadCode = strategy.getUcteNodeCode(loadBus); UcteNodeCode ucteCode = strategy.getUcteNodeCode(ucteBus); + UcteNodeCode ucteCode2 = strategy.getUcteNodeCode(ucteBus2); + System.out.println(genCode); + System.out.println(hv1Code); + System.out.println(hv2Code); + System.out.println(loadCode); System.out.println(ucteCode); + System.out.println(ucteCode2); assertAll( () -> assertTrue(UcteNodeCode.isUcteNodeId(genCode.toString())), @@ -121,12 +128,13 @@ void testBranchElementIds() { () -> assertTrue(UcteElementId.isUcteElementId(transformerId2.toString())), () -> assertTrue(UcteElementId.isUcteElementId(lineId1.toString())), () -> assertTrue(UcteElementId.isUcteElementId(lineId2.toString())), + () -> assertNotEquals(transformerId1, transformerId2), - () -> assertNotEquals(lineId1, lineId2) - ); + () -> assertNotEquals(lineId1, lineId2), - assertEquals(transformerId1, strategy.getUcteElementId(transformer1)); - assertEquals(transformerId2, strategy.getUcteElementId(transformer2)); + () -> assertEquals(transformerId1, strategy.getUcteElementId(transformer1)), + () -> assertEquals(transformerId2, strategy.getUcteElementId(transformer2)) + ); } @Test diff --git a/ucte/ucte-converter/src/test/java/com/powsybl/ucte/converter/util/UcteConverterHelperTest.java b/ucte/ucte-converter/src/test/java/com/powsybl/ucte/converter/util/UcteConverterHelperTest.java index 0eb86fb1d39..03925fea03b 100644 --- a/ucte/ucte-converter/src/test/java/com/powsybl/ucte/converter/util/UcteConverterHelperTest.java +++ b/ucte/ucte-converter/src/test/java/com/powsybl/ucte/converter/util/UcteConverterHelperTest.java @@ -12,14 +12,14 @@ import com.powsybl.commons.datasource.ResourceSet; import com.powsybl.iidm.network.Network; import com.powsybl.iidm.network.NetworkFactory; +import com.powsybl.ucte.converter.UcteException; import com.powsybl.ucte.converter.UcteImporter; import org.apache.commons.math3.complex.Complex; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; import static com.powsybl.ucte.converter.util.UcteConverterHelper.*; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNotEquals; +import static org.junit.jupiter.api.Assertions.*; /** * @author Abdelsalem HEDHILI {@literal } @@ -90,4 +90,16 @@ void calculatePhaseDuTest2() { assertEquals(0.990, duRef6.abs(), 0.00001); assertEquals(90.00, Math.toDegrees(duRef6.getArgument()), 0.00001); // loss of one decimal with sign } + + @Test + void getOrderCodeTest() { + assertEquals('1', getOrderCode(0)); + assertEquals('A', getOrderCode(9)); + assertThrows(UcteException.class, () -> { + getOrderCode(-1); + }); + assertThrows(UcteException.class, () -> { + getOrderCode(50); + }); + } } diff --git a/ucte/ucte-converter/src/test/resources/network.xiidm b/ucte/ucte-converter/src/test/resources/network.xiidm index 078acfc506e..18bf35553e8 100644 --- a/ucte/ucte-converter/src/test/resources/network.xiidm +++ b/ucte/ucte-converter/src/test/resources/network.xiidm @@ -7,6 +7,7 @@ + diff --git a/ucte/ucte-network/src/main/java/com/powsybl/ucte/network/UcteVoltageLevelCode.java b/ucte/ucte-network/src/main/java/com/powsybl/ucte/network/UcteVoltageLevelCode.java index 447fd690851..0b25a723626 100644 --- a/ucte/ucte-network/src/main/java/com/powsybl/ucte/network/UcteVoltageLevelCode.java +++ b/ucte/ucte-network/src/main/java/com/powsybl/ucte/network/UcteVoltageLevelCode.java @@ -90,7 +90,7 @@ public static char voltageLevelCodeFromVoltage(double voltage) { .min(Comparator.comparingDouble(code -> Math.abs(voltage - code.getVoltageLevel()))) .map(code -> (char) ('0' + code.ordinal())) - .orElse('7'); + .orElseThrow(); } public static boolean isVoltageLevelCode(char character) { From 5c20757a18bc76defc2da1bd0b4296e150ae870b Mon Sep 17 00:00:00 2001 From: Leclerc Clement Date: Tue, 3 Dec 2024 08:58:12 +0100 Subject: [PATCH 29/38] delete system.out Signed-off-by: Leclerc Clement --- .../powsybl/ucte/converter/CounterNamingStrategyTest.java | 7 ------- 1 file changed, 7 deletions(-) diff --git a/ucte/ucte-converter/src/test/java/com/powsybl/ucte/converter/CounterNamingStrategyTest.java b/ucte/ucte-converter/src/test/java/com/powsybl/ucte/converter/CounterNamingStrategyTest.java index 70ce3d656b6..8b4ed5a80b9 100644 --- a/ucte/ucte-converter/src/test/java/com/powsybl/ucte/converter/CounterNamingStrategyTest.java +++ b/ucte/ucte-converter/src/test/java/com/powsybl/ucte/converter/CounterNamingStrategyTest.java @@ -84,13 +84,6 @@ void testBasicNodeCodeGeneration() { UcteNodeCode ucteCode = strategy.getUcteNodeCode(ucteBus); UcteNodeCode ucteCode2 = strategy.getUcteNodeCode(ucteBus2); - System.out.println(genCode); - System.out.println(hv1Code); - System.out.println(hv2Code); - System.out.println(loadCode); - System.out.println(ucteCode); - System.out.println(ucteCode2); - assertAll( () -> assertTrue(UcteNodeCode.isUcteNodeId(genCode.toString())), () -> assertTrue(UcteNodeCode.isUcteNodeId(hv1Code.toString())), From 696ca5119df57b61676324193f28e7d932b08371 Mon Sep 17 00:00:00 2001 From: Leclerc Clement Date: Tue, 3 Dec 2024 08:59:13 +0100 Subject: [PATCH 30/38] delete system.out Signed-off-by: Leclerc Clement --- .../powsybl/ucte/converter/CounterNamingStrategyTest.java | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/ucte/ucte-converter/src/test/java/com/powsybl/ucte/converter/CounterNamingStrategyTest.java b/ucte/ucte-converter/src/test/java/com/powsybl/ucte/converter/CounterNamingStrategyTest.java index 8b4ed5a80b9..76763d05821 100644 --- a/ucte/ucte-converter/src/test/java/com/powsybl/ucte/converter/CounterNamingStrategyTest.java +++ b/ucte/ucte-converter/src/test/java/com/powsybl/ucte/converter/CounterNamingStrategyTest.java @@ -89,7 +89,8 @@ void testBasicNodeCodeGeneration() { () -> assertTrue(UcteNodeCode.isUcteNodeId(hv1Code.toString())), () -> assertTrue(UcteNodeCode.isUcteNodeId(hv2Code.toString())), () -> assertTrue(UcteNodeCode.isUcteNodeId(loadCode.toString())), - () -> assertTrue(UcteNodeCode.isUcteNodeId(ucteCode.toString())) + () -> assertTrue(UcteNodeCode.isUcteNodeId(ucteCode.toString())), + () -> assertTrue(UcteNodeCode.isUcteNodeId(ucteCode2.toString())) ); assertAll( @@ -98,7 +99,8 @@ void testBasicNodeCodeGeneration() { () -> assertNotEquals(genCode, loadCode), () -> assertNotEquals(hv1Code, hv2Code), () -> assertNotEquals(hv1Code, loadCode), - () -> assertNotEquals(hv2Code, loadCode) + () -> assertNotEquals(hv2Code, loadCode), + () -> assertNotEquals(ucteCode2, ucteCode) ); } From b29e7b1158cf87f70b7b6a9357f2986af36ce42b Mon Sep 17 00:00:00 2001 From: Leclerc Clement Date: Tue, 3 Dec 2024 10:54:19 +0100 Subject: [PATCH 31/38] update CounterNamingStategyTest Signed-off-by: Leclerc Clement --- .../converter/CounterNamingStrategyTest.java | 70 +- .../src/test/resources/network.xiidm | 4 +- .../src/test/resources/network2.xiidm | 52 - .../src/test/resources/network3.xiidm | 1267 ----------------- .../src/test/resources/network4.xiidm | 147 -- .../src/test/resources/networku.xiidm | 42 - 6 files changed, 45 insertions(+), 1537 deletions(-) delete mode 100644 ucte/ucte-converter/src/test/resources/network2.xiidm delete mode 100644 ucte/ucte-converter/src/test/resources/network3.xiidm delete mode 100644 ucte/ucte-converter/src/test/resources/network4.xiidm delete mode 100644 ucte/ucte-converter/src/test/resources/networku.xiidm diff --git a/ucte/ucte-converter/src/test/java/com/powsybl/ucte/converter/CounterNamingStrategyTest.java b/ucte/ucte-converter/src/test/java/com/powsybl/ucte/converter/CounterNamingStrategyTest.java index 76763d05821..36f75e43ec2 100644 --- a/ucte/ucte-converter/src/test/java/com/powsybl/ucte/converter/CounterNamingStrategyTest.java +++ b/ucte/ucte-converter/src/test/java/com/powsybl/ucte/converter/CounterNamingStrategyTest.java @@ -71,36 +71,33 @@ void testBasicNodeCodeGeneration() { strategy.initialiseNetwork(network); Bus genBus = network.getBusBreakerView().getBus("NGEN"); - Bus hv1Bus = network.getBusBreakerView().getBus("NGEN2"); - Bus hv2Bus = network.getBusBreakerView().getBus("NGEN3"); + Bus genbus2 = network.getBusBreakerView().getBus("NGEN2"); + Bus ucteBus = network.getBusBreakerView().getBus("F0000079"); Bus loadBus = network.getBusBreakerView().getBus("NLOAD"); - Bus ucteBus = network.getBusBreakerView().getBus("F0000014"); - Bus ucteBus2 = network.getBusBreakerView().getBus("F0000019"); UcteNodeCode genCode = strategy.getUcteNodeCode(genBus); - UcteNodeCode hv1Code = strategy.getUcteNodeCode(hv1Bus); - UcteNodeCode hv2Code = strategy.getUcteNodeCode(hv2Bus); + UcteNodeCode genCode2 = strategy.getUcteNodeCode(genbus2); UcteNodeCode loadCode = strategy.getUcteNodeCode(loadBus); UcteNodeCode ucteCode = strategy.getUcteNodeCode(ucteBus); - UcteNodeCode ucteCode2 = strategy.getUcteNodeCode(ucteBus2); assertAll( () -> assertTrue(UcteNodeCode.isUcteNodeId(genCode.toString())), - () -> assertTrue(UcteNodeCode.isUcteNodeId(hv1Code.toString())), - () -> assertTrue(UcteNodeCode.isUcteNodeId(hv2Code.toString())), + () -> assertTrue(UcteNodeCode.isUcteNodeId(genCode2.toString())), () -> assertTrue(UcteNodeCode.isUcteNodeId(loadCode.toString())), () -> assertTrue(UcteNodeCode.isUcteNodeId(ucteCode.toString())), - () -> assertTrue(UcteNodeCode.isUcteNodeId(ucteCode2.toString())) - ); - assertAll( - () -> assertNotEquals(genCode, hv1Code), - () -> assertNotEquals(genCode, hv2Code), + () -> assertNotEquals(genCode, genCode2), () -> assertNotEquals(genCode, loadCode), - () -> assertNotEquals(hv1Code, hv2Code), - () -> assertNotEquals(hv1Code, loadCode), - () -> assertNotEquals(hv2Code, loadCode), - () -> assertNotEquals(ucteCode2, ucteCode) + () -> assertNotEquals(genCode, ucteCode), + () -> assertNotEquals(genCode2, loadCode), + () -> assertNotEquals(genCode2, ucteCode), + () -> assertNotEquals(ucteCode, loadCode), + + () -> assertEquals("F0000071", genCode.toString()), + () -> assertEquals("F0000072", genCode2.toString()), + () -> assertEquals("F0000074", ucteCode.toString()), + () -> assertEquals("F0000331", loadCode.toString()) + ); } @@ -128,7 +125,13 @@ void testBranchElementIds() { () -> assertNotEquals(lineId1, lineId2), () -> assertEquals(transformerId1, strategy.getUcteElementId(transformer1)), - () -> assertEquals(transformerId2, strategy.getUcteElementId(transformer2)) + () -> assertEquals(transformerId2, strategy.getUcteElementId(transformer2)), + + () -> assertEquals("F0000071 F0000111 1", transformerId1.toString()), + () -> assertEquals("F0000211 F0000331 1", transformerId2.toString()), + () -> assertEquals("F0000111 F0000211 1", lineId1.toString()), + () -> assertEquals("F0000111 F0000211 2", lineId2.toString()) + ); } @@ -138,18 +141,29 @@ void testSwitchElementIds() { Switch sw = network.getSwitch("NGEN-NGEN2"); Switch sw2 = network.getSwitch("NGEN-NGEN3"); Switch sw3 = network.getSwitch("NGEN-NGEN3"); + Switch sw4 = network.getSwitch("NGEN-NGEN4"); UcteElementId swId = strategy.getUcteElementId(sw); UcteElementId swId2 = strategy.getUcteElementId(sw2); UcteElementId swId3 = strategy.getUcteElementId(sw3); + UcteElementId swId4 = strategy.getUcteElementId(sw4); assertAll( () -> assertTrue(UcteElementId.isUcteElementId(swId.toString())), () -> assertTrue(UcteElementId.isUcteElementId(swId2.toString())), + () -> assertTrue(UcteElementId.isUcteElementId(swId3.toString())), + () -> assertTrue(UcteElementId.isUcteElementId(swId4.toString())), () -> assertNotEquals(swId, swId2), - () -> assertEquals(swId3, swId2) + () -> assertNotEquals(swId3, swId4), + () -> assertNotEquals(swId, swId4), + () -> assertNotEquals(swId2, swId4), + () -> assertEquals(swId3, swId2), + + () -> assertEquals("F0000071 F0000072 1", swId.toString()), + () -> assertEquals("F0000071 F0000073 1", swId2.toString()), + () -> assertEquals("F0000071 F0000073 1", swId3.toString()), + () -> assertEquals("F0000071 F0000073 2", swId4.toString()) ); - } @Test @@ -167,10 +181,12 @@ void testDanglingLineElementIds() { () -> assertTrue(UcteElementId.isUcteElementId(dlId3.toString())), () -> assertNotEquals(dlId1, dlId2), () -> assertNotEquals(dlId1, dlId3), - () -> assertNotEquals(dlId2, dlId3) + () -> assertNotEquals(dlId2, dlId3), + () -> assertEquals("F0000072 F0000671 1", dlId1.toString()), + () -> assertEquals("F0000072 X0000011 1", dlId2.toString()), + () -> assertEquals("F0000072 F0000671 2", dlId3.toString()) ); - } @Test @@ -184,11 +200,12 @@ void testParallelLines() { UcteElementId id2 = strategy.getUcteElementId(line2); assertAll( - () -> assertNotNull(id1), - () -> assertNotNull(id2), () -> assertTrue(UcteElementId.isUcteElementId(id1.toString())), () -> assertTrue(UcteElementId.isUcteElementId(id2.toString())), - () -> assertNotEquals(id1, id2) + () -> assertNotEquals(id1, id2), + + () -> assertEquals("F0000111 F0000211 1", id1.toString()), + () -> assertEquals("F0000111 F0000211 2", id2.toString()) ); } @@ -230,5 +247,4 @@ void testCountryCode() { UcteNodeCode code = strategy.getUcteNodeCode(genBus); assertEquals('F', code.toString().charAt(0)); // France } - } diff --git a/ucte/ucte-converter/src/test/resources/network.xiidm b/ucte/ucte-converter/src/test/resources/network.xiidm index 18bf35553e8..2a653a93ad3 100644 --- a/ucte/ucte-converter/src/test/resources/network.xiidm +++ b/ucte/ucte-converter/src/test/resources/network.xiidm @@ -6,10 +6,10 @@ - - + + diff --git a/ucte/ucte-converter/src/test/resources/network2.xiidm b/ucte/ucte-converter/src/test/resources/network2.xiidm deleted file mode 100644 index 32df3266376..00000000000 --- a/ucte/ucte-converter/src/test/resources/network2.xiidm +++ /dev/null @@ -1,52 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/ucte/ucte-converter/src/test/resources/network3.xiidm b/ucte/ucte-converter/src/test/resources/network3.xiidm deleted file mode 100644 index d648236289c..00000000000 --- a/ucte/ucte-converter/src/test/resources/network3.xiidm +++ /dev/null @@ -1,1267 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/ucte/ucte-converter/src/test/resources/network4.xiidm b/ucte/ucte-converter/src/test/resources/network4.xiidm deleted file mode 100644 index 74eddc98f99..00000000000 --- a/ucte/ucte-converter/src/test/resources/network4.xiidm +++ /dev/null @@ -1,147 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/ucte/ucte-converter/src/test/resources/networku.xiidm b/ucte/ucte-converter/src/test/resources/networku.xiidm deleted file mode 100644 index b7ef818e14e..00000000000 --- a/ucte/ucte-converter/src/test/resources/networku.xiidm +++ /dev/null @@ -1,42 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file From 5b63d1e66dbd766547b1b1c1484366062b710b85 Mon Sep 17 00:00:00 2001 From: Leclerc Clement Date: Tue, 3 Dec 2024 11:24:04 +0100 Subject: [PATCH 32/38] update danglingline xnodeCode generation Signed-off-by: Leclerc Clement --- .../com/powsybl/ucte/converter/CounterNamingStrategy.java | 4 ++-- .../com/powsybl/ucte/converter/CounterNamingStrategyTest.java | 2 ++ 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/ucte/ucte-converter/src/main/java/com/powsybl/ucte/converter/CounterNamingStrategy.java b/ucte/ucte-converter/src/main/java/com/powsybl/ucte/converter/CounterNamingStrategy.java index 812df6ccac8..f5ace8b0411 100644 --- a/ucte/ucte-converter/src/main/java/com/powsybl/ucte/converter/CounterNamingStrategy.java +++ b/ucte/ucte-converter/src/main/java/com/powsybl/ucte/converter/CounterNamingStrategy.java @@ -131,8 +131,8 @@ private UcteElementId generateUcteElementId(DanglingLine danglingLine) { code1 = getUcteNodeCode(danglingLine.getTerminal().getBusBreakerView().getBus()); if (danglingLine.getPairingKey() != null && UcteNodeCode.isUcteNodeId(danglingLine.getPairingKey())) { - UcteNodeCode pairingKeyId = UcteNodeCode.parseUcteNodeCode(danglingLine.getPairingKey()).orElseThrow(); - code2 = generateUcteNodeId(danglingLine.getPairingKey(), danglingLine.getTerminal().getVoltageLevel(), pairingKeyId.getBusbar()); + code2 = UcteNodeCode.parseUcteNodeCode(danglingLine.getPairingKey()).orElseThrow(); + ucteNodeIds.put(danglingLine.getPairingKey(), code2); } else { code2 = generateUcteNodeId(danglingLine.getId(), danglingLine.getTerminal().getVoltageLevel(), UcteConverterHelper.getOrderCode(0)); } diff --git a/ucte/ucte-converter/src/test/java/com/powsybl/ucte/converter/CounterNamingStrategyTest.java b/ucte/ucte-converter/src/test/java/com/powsybl/ucte/converter/CounterNamingStrategyTest.java index 36f75e43ec2..2ed518084ec 100644 --- a/ucte/ucte-converter/src/test/java/com/powsybl/ucte/converter/CounterNamingStrategyTest.java +++ b/ucte/ucte-converter/src/test/java/com/powsybl/ucte/converter/CounterNamingStrategyTest.java @@ -176,6 +176,8 @@ void testDanglingLineElementIds() { UcteElementId dlId2 = strategy.getUcteElementId(dl2); UcteElementId dlId3 = strategy.getUcteElementId(dl3); + System.out.println(dlId3); + assertAll( () -> assertTrue(UcteElementId.isUcteElementId(dlId1.toString())), () -> assertTrue(UcteElementId.isUcteElementId(dlId3.toString())), From 7c480e7e9e91edf223dcefb4dc348cfa582a3036 Mon Sep 17 00:00:00 2001 From: Leclerc Clement Date: Tue, 3 Dec 2024 11:38:27 +0100 Subject: [PATCH 33/38] update test Signed-off-by: Leclerc Clement --- .../powsybl/ucte/converter/CounterNamingStrategyTest.java | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/ucte/ucte-converter/src/test/java/com/powsybl/ucte/converter/CounterNamingStrategyTest.java b/ucte/ucte-converter/src/test/java/com/powsybl/ucte/converter/CounterNamingStrategyTest.java index 2ed518084ec..cfaee92234d 100644 --- a/ucte/ucte-converter/src/test/java/com/powsybl/ucte/converter/CounterNamingStrategyTest.java +++ b/ucte/ucte-converter/src/test/java/com/powsybl/ucte/converter/CounterNamingStrategyTest.java @@ -108,12 +108,11 @@ void testBranchElementIds() { Branch transformer1 = network.getBranch("NGEN_NHV1"); Branch transformer2 = network.getBranch("NHV2_NLOAD"); Branch line1 = network.getBranch("NHV1_NHV2_1"); - Branch line2 = network.getBranch("NHV1_NHV2_2"); UcteElementId transformerId1 = strategy.getUcteElementId(transformer1); UcteElementId transformerId2 = strategy.getUcteElementId(transformer2); UcteElementId lineId1 = strategy.getUcteElementId(line1); - UcteElementId lineId2 = strategy.getUcteElementId(line2); + UcteElementId lineId2 = strategy.getUcteElementId(line1); assertAll( () -> assertTrue(UcteElementId.isUcteElementId(transformerId1.toString())), @@ -176,8 +175,6 @@ void testDanglingLineElementIds() { UcteElementId dlId2 = strategy.getUcteElementId(dl2); UcteElementId dlId3 = strategy.getUcteElementId(dl3); - System.out.println(dlId3); - assertAll( () -> assertTrue(UcteElementId.isUcteElementId(dlId1.toString())), () -> assertTrue(UcteElementId.isUcteElementId(dlId3.toString())), From ed79865ed23813ab9dad3a6d9babbfd2283a209b Mon Sep 17 00:00:00 2001 From: Leclerc Clement Date: Tue, 3 Dec 2024 14:32:43 +0100 Subject: [PATCH 34/38] create fromVoltageLevel in UcteCountryCode Signed-off-by: Leclerc Clement --- .../ucte/converter/CounterNamingStrategy.java | 14 +------------- .../ucte/converter/util/UcteConstants.java | 2 -- .../ucte/converter/CounterNamingStrategyTest.java | 3 ++- ucte/ucte-network/pom.xml | 4 ++++ .../com/powsybl/ucte/network/UcteCountryCode.java | 15 +++++++++++++++ 5 files changed, 22 insertions(+), 16 deletions(-) diff --git a/ucte/ucte-converter/src/main/java/com/powsybl/ucte/converter/CounterNamingStrategy.java b/ucte/ucte-converter/src/main/java/com/powsybl/ucte/converter/CounterNamingStrategy.java index f5ace8b0411..d157ccf6776 100644 --- a/ucte/ucte-converter/src/main/java/com/powsybl/ucte/converter/CounterNamingStrategy.java +++ b/ucte/ucte-converter/src/main/java/com/powsybl/ucte/converter/CounterNamingStrategy.java @@ -70,7 +70,7 @@ private UcteNodeCode changeOrderCode(String busId, char orderCode) { private UcteNodeCode createNewUcteNodeId(String busId, VoltageLevel voltageLevel, char orderCode) { String newNodeId = String.format("%05d", voltageLevelCounter); - char countryCode = getCountryCode(voltageLevel).getUcteCode(); + char countryCode = UcteCountryCode.fromVoltagelevel(voltageLevel).getUcteCode(); char voltageLevelCode = UcteVoltageLevelCode.voltageLevelCodeFromVoltage(voltageLevel.getNominalV()); UcteNodeCode ucteNodeCode = new UcteNodeCode( @@ -83,18 +83,6 @@ private UcteNodeCode createNewUcteNodeId(String busId, VoltageLevel voltageLevel return ucteNodeCode; } - private UcteCountryCode getCountryCode(VoltageLevel voltageLevel) { - Country country = voltageLevel.getSubstation() - .flatMap(Substation::getCountry) - .orElseThrow(() -> new UcteException(UcteConstants.NO_COUNTRY_ERROR)); - - try { - return UcteCountryCode.valueOf(country.name()); - } catch (IllegalArgumentException e) { - throw new UcteException(String.format(UcteConstants.NO_UCTE_COUNTRY_ERROR, country.getName())); - } - } - private UcteElementId generateUcteElementId(String id, UcteNodeCode node1, UcteNodeCode node2) { if (ucteElementIds.containsKey(id)) { return ucteElementIds.get(id); diff --git a/ucte/ucte-converter/src/main/java/com/powsybl/ucte/converter/util/UcteConstants.java b/ucte/ucte-converter/src/main/java/com/powsybl/ucte/converter/util/UcteConstants.java index 1c2be494809..fec43a0ef92 100644 --- a/ucte/ucte-converter/src/main/java/com/powsybl/ucte/converter/util/UcteConstants.java +++ b/ucte/ucte-converter/src/main/java/com/powsybl/ucte/converter/util/UcteConstants.java @@ -28,9 +28,7 @@ private UcteConstants() { public static final String ORDER_CODE = "orderCode"; public static final String POWER_PLANT_TYPE_PROPERTY_KEY = "powerPlantType"; public static final int DEFAULT_POWER_LIMIT = 9999; - public static final String NO_COUNTRY_ERROR = "No country for this substation"; public static final String NO_UCTE_CODE_ERROR = "No UCTE code found for id: "; - public static final String NO_UCTE_COUNTRY_ERROR = "No UCTE country code for "; public static final List ORDER_CODES = List.of('1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'); } diff --git a/ucte/ucte-converter/src/test/java/com/powsybl/ucte/converter/CounterNamingStrategyTest.java b/ucte/ucte-converter/src/test/java/com/powsybl/ucte/converter/CounterNamingStrategyTest.java index cfaee92234d..36f75e43ec2 100644 --- a/ucte/ucte-converter/src/test/java/com/powsybl/ucte/converter/CounterNamingStrategyTest.java +++ b/ucte/ucte-converter/src/test/java/com/powsybl/ucte/converter/CounterNamingStrategyTest.java @@ -108,11 +108,12 @@ void testBranchElementIds() { Branch transformer1 = network.getBranch("NGEN_NHV1"); Branch transformer2 = network.getBranch("NHV2_NLOAD"); Branch line1 = network.getBranch("NHV1_NHV2_1"); + Branch line2 = network.getBranch("NHV1_NHV2_2"); UcteElementId transformerId1 = strategy.getUcteElementId(transformer1); UcteElementId transformerId2 = strategy.getUcteElementId(transformer2); UcteElementId lineId1 = strategy.getUcteElementId(line1); - UcteElementId lineId2 = strategy.getUcteElementId(line1); + UcteElementId lineId2 = strategy.getUcteElementId(line2); assertAll( () -> assertTrue(UcteElementId.isUcteElementId(transformerId1.toString())), diff --git a/ucte/ucte-network/pom.xml b/ucte/ucte-network/pom.xml index 36daacb7ed9..125c88eec65 100644 --- a/ucte/ucte-network/pom.xml +++ b/ucte/ucte-network/pom.xml @@ -85,6 +85,10 @@ ${project.version} test + + com.powsybl + powsybl-iidm-api + diff --git a/ucte/ucte-network/src/main/java/com/powsybl/ucte/network/UcteCountryCode.java b/ucte/ucte-network/src/main/java/com/powsybl/ucte/network/UcteCountryCode.java index 67557c04dcc..376eb1f81be 100644 --- a/ucte/ucte-network/src/main/java/com/powsybl/ucte/network/UcteCountryCode.java +++ b/ucte/ucte-network/src/main/java/com/powsybl/ucte/network/UcteCountryCode.java @@ -7,6 +7,10 @@ */ package com.powsybl.ucte.network; +import com.powsybl.iidm.network.Country; +import com.powsybl.iidm.network.Substation; +import com.powsybl.iidm.network.VoltageLevel; + import java.util.Objects; /** @@ -121,4 +125,15 @@ public static boolean isUcteCountryCode(char character) { } } + public static UcteCountryCode fromVoltagelevel(VoltageLevel voltageLevel) { + Country country = voltageLevel.getSubstation() + .flatMap(Substation::getCountry) + .orElseThrow(() -> new UcteException("No UCTE country found for substation")); + try { + return UcteCountryCode.valueOf(country.name()); + } catch (IllegalArgumentException e) { + throw new UcteException(String.format("No UCTE country found for " + country.name())); + } + } + } From 71046bb9be5c36a8b1a9b562614df88ebeb57b24 Mon Sep 17 00:00:00 2001 From: Leclerc Clement Date: Mon, 9 Dec 2024 10:17:23 +0100 Subject: [PATCH 35/38] update test Signed-off-by: Leclerc Clement --- .../converter/AbstractNamingStrategy.java | 8 +-- .../ucte/converter/CounterNamingStrategy.java | 11 ++-- .../ucte/converter/NamingStrategy.java | 2 +- .../powsybl/ucte/converter/UcteExporter.java | 4 +- .../powsybl/ucte/converter/UcteImporter.java | 2 +- ...tants.java => UcteConverterConstants.java} | 8 +-- .../converter/util/UcteConverterHelper.java | 11 +--- .../converter/CounterNamingStrategyTest.java | 56 +++++++++---------- .../ucte/converter/UcteImporterTest.java | 4 +- .../util/UcteConverterHelperTest.java | 12 ---- .../powsybl/ucte/network/UcteCountryCode.java | 2 +- .../powsybl/ucte/network/UcteElementId.java | 10 ++-- .../ucte/network/util/UcteNetworkUtil.java | 33 +++++++++++ .../network/util/UcteNetworkUtilTest.java | 32 +++++++++++ 14 files changed, 116 insertions(+), 79 deletions(-) rename ucte/ucte-converter/src/main/java/com/powsybl/ucte/converter/util/{UcteConstants.java => UcteConverterConstants.java} (79%) create mode 100644 ucte/ucte-network/src/main/java/com/powsybl/ucte/network/util/UcteNetworkUtil.java create mode 100644 ucte/ucte-network/src/test/java/com/powsybl/ucte/network/util/UcteNetworkUtilTest.java diff --git a/ucte/ucte-converter/src/main/java/com/powsybl/ucte/converter/AbstractNamingStrategy.java b/ucte/ucte-converter/src/main/java/com/powsybl/ucte/converter/AbstractNamingStrategy.java index 3052e72eab3..b146899fba4 100644 --- a/ucte/ucte-converter/src/main/java/com/powsybl/ucte/converter/AbstractNamingStrategy.java +++ b/ucte/ucte-converter/src/main/java/com/powsybl/ucte/converter/AbstractNamingStrategy.java @@ -8,7 +8,7 @@ package com.powsybl.ucte.converter; import com.powsybl.iidm.network.*; -import com.powsybl.ucte.converter.util.UcteConstants; +import com.powsybl.ucte.converter.util.UcteConverterConstants; import com.powsybl.ucte.network.UcteElementId; import com.powsybl.ucte.network.UcteNodeCode; @@ -24,14 +24,14 @@ public abstract class AbstractNamingStrategy implements NamingStrategy { protected final Map ucteElementIds = new HashMap<>(); @Override - public void initialiseNetwork(Network network) { + public void initializeNetwork(Network network) { //Empty implementation by default } @Override public UcteNodeCode getUcteNodeCode(String id) { return ucteNodeIds.computeIfAbsent(id, k -> UcteNodeCode.parseUcteNodeCode(k) - .orElseThrow(() -> new UcteException(UcteConstants.NO_UCTE_CODE_ERROR + k))); + .orElseThrow(() -> new UcteException(UcteConverterConstants.NO_UCTE_CODE_ERROR + k))); } @Override @@ -50,7 +50,7 @@ public UcteNodeCode getUcteNodeCode(DanglingLine danglingLine) { @Override public UcteElementId getUcteElementId(String id) { return ucteElementIds.computeIfAbsent(id, k -> UcteElementId.parseUcteElementId(k) - .orElseThrow(() -> new UcteException(UcteConstants.NO_UCTE_CODE_ERROR + k))); + .orElseThrow(() -> new UcteException(UcteConverterConstants.NO_UCTE_CODE_ERROR + k))); } @Override diff --git a/ucte/ucte-converter/src/main/java/com/powsybl/ucte/converter/CounterNamingStrategy.java b/ucte/ucte-converter/src/main/java/com/powsybl/ucte/converter/CounterNamingStrategy.java index d157ccf6776..318499e7749 100644 --- a/ucte/ucte-converter/src/main/java/com/powsybl/ucte/converter/CounterNamingStrategy.java +++ b/ucte/ucte-converter/src/main/java/com/powsybl/ucte/converter/CounterNamingStrategy.java @@ -9,12 +9,11 @@ import com.google.auto.service.AutoService; import com.powsybl.iidm.network.*; -import com.powsybl.ucte.converter.util.UcteConstants; -import com.powsybl.ucte.converter.util.UcteConverterHelper; import com.powsybl.ucte.network.UcteCountryCode; import com.powsybl.ucte.network.UcteElementId; import com.powsybl.ucte.network.UcteNodeCode; import com.powsybl.ucte.network.UcteVoltageLevelCode; +import com.powsybl.ucte.network.util.UcteNetworkUtil; import java.util.*; @@ -32,7 +31,7 @@ public String getName() { } @Override - public void initialiseNetwork(Network network) { + public void initializeNetwork(Network network) { voltageLevelCounter = 0; network.getVoltageLevelStream() .forEach(this::processVoltageLevel); @@ -45,7 +44,7 @@ private void processVoltageLevel(VoltageLevel voltageLevel) { Iterator buslist = voltageLevel.getBusBreakerView().getBuses().iterator(); for (int i = 0; buslist.hasNext(); i++) { Bus bus = buslist.next(); - char orderCode = UcteConverterHelper.getOrderCode(i); + char orderCode = UcteNetworkUtil.getOrderCode(i); generateUcteNodeId(bus.getId(), voltageLevel, orderCode); } @@ -88,7 +87,7 @@ private UcteElementId generateUcteElementId(String id, UcteNodeCode node1, UcteN return ucteElementIds.get(id); } - UcteElementId uniqueElementId = UcteConstants.ORDER_CODES.stream() + UcteElementId uniqueElementId = UcteNetworkUtil.ORDER_CODES.stream() .map(orderCode -> new UcteElementId(node1, node2, orderCode)) .filter(elementId -> !ucteElementIds.containsValue(elementId)) .findFirst() @@ -122,7 +121,7 @@ private UcteElementId generateUcteElementId(DanglingLine danglingLine) { code2 = UcteNodeCode.parseUcteNodeCode(danglingLine.getPairingKey()).orElseThrow(); ucteNodeIds.put(danglingLine.getPairingKey(), code2); } else { - code2 = generateUcteNodeId(danglingLine.getId(), danglingLine.getTerminal().getVoltageLevel(), UcteConverterHelper.getOrderCode(0)); + code2 = generateUcteNodeId(danglingLine.getId(), danglingLine.getTerminal().getVoltageLevel(), UcteNetworkUtil.getOrderCode(0)); } return generateUcteElementId(danglingLine.getId(), code1, code2); } diff --git a/ucte/ucte-converter/src/main/java/com/powsybl/ucte/converter/NamingStrategy.java b/ucte/ucte-converter/src/main/java/com/powsybl/ucte/converter/NamingStrategy.java index 3e2adcf5f9d..19fdca2291c 100644 --- a/ucte/ucte-converter/src/main/java/com/powsybl/ucte/converter/NamingStrategy.java +++ b/ucte/ucte-converter/src/main/java/com/powsybl/ucte/converter/NamingStrategy.java @@ -17,7 +17,7 @@ */ public interface NamingStrategy { - void initialiseNetwork(Network network); + void initializeNetwork(Network network); String getName(); diff --git a/ucte/ucte-converter/src/main/java/com/powsybl/ucte/converter/UcteExporter.java b/ucte/ucte-converter/src/main/java/com/powsybl/ucte/converter/UcteExporter.java index 55ca23aeb2b..33dbf02bed6 100644 --- a/ucte/ucte-converter/src/main/java/com/powsybl/ucte/converter/UcteExporter.java +++ b/ucte/ucte-converter/src/main/java/com/powsybl/ucte/converter/UcteExporter.java @@ -32,7 +32,7 @@ import java.util.*; import java.util.function.Supplier; -import static com.powsybl.ucte.converter.util.UcteConstants.*; +import static com.powsybl.ucte.converter.util.UcteConverterConstants.*; import static com.powsybl.ucte.converter.util.UcteConverterHelper.*; /** @@ -87,7 +87,7 @@ public void export(Network network, Properties parameters, DataSource dataSource String namingStrategyName = Parameter.readString(getFormat(), parameters, NAMING_STRATEGY_PARAMETER, defaultValueConfig); NamingStrategy namingStrategy = findNamingStrategy(namingStrategyName, NAMING_STRATEGY_SUPPLIERS.get()); - namingStrategy.initialiseNetwork(network); + namingStrategy.initializeNetwork(network); boolean combinePhaseAngleRegulation = Parameter.readBoolean(getFormat(), parameters, COMBINE_PHASE_ANGLE_REGULATION_PARAMETER, defaultValueConfig); UcteNetwork ucteNetwork = createUcteNetwork(network, namingStrategy, combinePhaseAngleRegulation); diff --git a/ucte/ucte-converter/src/main/java/com/powsybl/ucte/converter/UcteImporter.java b/ucte/ucte-converter/src/main/java/com/powsybl/ucte/converter/UcteImporter.java index d689a73b68d..8e577ab7d3c 100644 --- a/ucte/ucte-converter/src/main/java/com/powsybl/ucte/converter/UcteImporter.java +++ b/ucte/ucte-converter/src/main/java/com/powsybl/ucte/converter/UcteImporter.java @@ -38,7 +38,7 @@ import java.util.concurrent.TimeUnit; import java.util.stream.Collectors; -import static com.powsybl.ucte.converter.util.UcteConstants.*; +import static com.powsybl.ucte.converter.util.UcteConverterConstants.*; /** * @author Geoffroy Jamgotchian {@literal } diff --git a/ucte/ucte-converter/src/main/java/com/powsybl/ucte/converter/util/UcteConstants.java b/ucte/ucte-converter/src/main/java/com/powsybl/ucte/converter/util/UcteConverterConstants.java similarity index 79% rename from ucte/ucte-converter/src/main/java/com/powsybl/ucte/converter/util/UcteConstants.java rename to ucte/ucte-converter/src/main/java/com/powsybl/ucte/converter/util/UcteConverterConstants.java index fec43a0ef92..a73681a91bc 100644 --- a/ucte/ucte-converter/src/main/java/com/powsybl/ucte/converter/util/UcteConstants.java +++ b/ucte/ucte-converter/src/main/java/com/powsybl/ucte/converter/util/UcteConverterConstants.java @@ -7,14 +7,12 @@ */ package com.powsybl.ucte.converter.util; -import java.util.List; - /** * @author Sebastien Murgey {@literal } */ -public final class UcteConstants { +public final class UcteConverterConstants { - private UcteConstants() { + private UcteConverterConstants() { throw new IllegalStateException("Should not be constructed"); } @@ -29,6 +27,4 @@ private UcteConstants() { public static final String POWER_PLANT_TYPE_PROPERTY_KEY = "powerPlantType"; public static final int DEFAULT_POWER_LIMIT = 9999; public static final String NO_UCTE_CODE_ERROR = "No UCTE code found for id: "; - public static final List ORDER_CODES = List.of('1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', - 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'); } diff --git a/ucte/ucte-converter/src/main/java/com/powsybl/ucte/converter/util/UcteConverterHelper.java b/ucte/ucte-converter/src/main/java/com/powsybl/ucte/converter/util/UcteConverterHelper.java index 308d846ce3f..cc1b1f9c9e5 100644 --- a/ucte/ucte-converter/src/main/java/com/powsybl/ucte/converter/util/UcteConverterHelper.java +++ b/ucte/ucte-converter/src/main/java/com/powsybl/ucte/converter/util/UcteConverterHelper.java @@ -10,7 +10,6 @@ import com.powsybl.iidm.network.PhaseTapChanger; import com.powsybl.iidm.network.RatioTapChanger; import com.powsybl.iidm.network.TwoWindingsTransformer; -import com.powsybl.ucte.converter.UcteException; import com.powsybl.ucte.network.UcteAngleRegulation; import com.powsybl.ucte.network.UctePhaseRegulation; import org.apache.commons.math3.complex.Complex; @@ -121,14 +120,6 @@ public static Complex calculateAsymmAngleDuAndAngle(TwoWindingsTransformer twoWi } return ComplexUtils.polar2Complex(BigDecimal.valueOf(absDu).setScale(4, RoundingMode.HALF_UP).doubleValue(), - theta); + theta); } - - public static char getOrderCode(int index) { - if (index > UcteConstants.ORDER_CODES.size() || index < 0) { - throw new UcteException("Order code index out of bounds"); - } - return UcteConstants.ORDER_CODES.get(index); - } - } diff --git a/ucte/ucte-converter/src/test/java/com/powsybl/ucte/converter/CounterNamingStrategyTest.java b/ucte/ucte-converter/src/test/java/com/powsybl/ucte/converter/CounterNamingStrategyTest.java index 36f75e43ec2..d4b8a1ab4a5 100644 --- a/ucte/ucte-converter/src/test/java/com/powsybl/ucte/converter/CounterNamingStrategyTest.java +++ b/ucte/ucte-converter/src/test/java/com/powsybl/ucte/converter/CounterNamingStrategyTest.java @@ -43,7 +43,7 @@ void testInitialNetwork() { assertThrows(UcteException.class, () -> strategy.getUcteNodeCode("NGEN")); assertThrows(UcteException.class, () -> strategy.getUcteElementId("NHV1_NHV2_1")); - strategy.initialiseNetwork(network); + strategy.initializeNetwork(network); assertDoesNotThrow(() -> strategy.getUcteNodeCode("NGEN")); assertDoesNotThrow(() -> strategy.getUcteElementId("NHV1_NHV2_1")); @@ -51,7 +51,7 @@ void testInitialNetwork() { @Test void testVoltageLevelCounterNaming() { - strategy.initialiseNetwork(network); + strategy.initializeNetwork(network); UcteNodeCode firstBusCode = strategy.getUcteNodeCode(network.getBusBreakerView().getBus("NGEN")); UcteNodeCode secondBusCode = strategy.getUcteNodeCode(network.getBusBreakerView().getBus("NGEN2")); @@ -69,7 +69,7 @@ void testVoltageLevelCounterNaming() { @Test void testBasicNodeCodeGeneration() { - strategy.initialiseNetwork(network); + strategy.initializeNetwork(network); Bus genBus = network.getBusBreakerView().getBus("NGEN"); Bus genbus2 = network.getBusBreakerView().getBus("NGEN2"); Bus ucteBus = network.getBusBreakerView().getBus("F0000079"); @@ -93,17 +93,17 @@ void testBasicNodeCodeGeneration() { () -> assertNotEquals(genCode2, ucteCode), () -> assertNotEquals(ucteCode, loadCode), - () -> assertEquals("F0000071", genCode.toString()), - () -> assertEquals("F0000072", genCode2.toString()), - () -> assertEquals("F0000074", ucteCode.toString()), - () -> assertEquals("F0000331", loadCode.toString()) + () -> assertEquals("F0000070", genCode.toString()), + () -> assertEquals("F0000071", genCode2.toString()), + () -> assertEquals("F0000073", ucteCode.toString()), + () -> assertEquals("F0000330", loadCode.toString()) ); } @Test void testBranchElementIds() { - strategy.initialiseNetwork(network); + strategy.initializeNetwork(network); Branch transformer1 = network.getBranch("NGEN_NHV1"); Branch transformer2 = network.getBranch("NHV2_NLOAD"); @@ -127,17 +127,17 @@ void testBranchElementIds() { () -> assertEquals(transformerId1, strategy.getUcteElementId(transformer1)), () -> assertEquals(transformerId2, strategy.getUcteElementId(transformer2)), - () -> assertEquals("F0000071 F0000111 1", transformerId1.toString()), - () -> assertEquals("F0000211 F0000331 1", transformerId2.toString()), - () -> assertEquals("F0000111 F0000211 1", lineId1.toString()), - () -> assertEquals("F0000111 F0000211 2", lineId2.toString()) + () -> assertEquals("F0000070 F0000110 0", transformerId1.toString()), + () -> assertEquals("F0000210 F0000330 0", transformerId2.toString()), + () -> assertEquals("F0000110 F0000210 0", lineId1.toString()), + () -> assertEquals("F0000110 F0000210 1", lineId2.toString()) ); } @Test void testSwitchElementIds() { - strategy.initialiseNetwork(network); + strategy.initializeNetwork(network); Switch sw = network.getSwitch("NGEN-NGEN2"); Switch sw2 = network.getSwitch("NGEN-NGEN3"); Switch sw3 = network.getSwitch("NGEN-NGEN3"); @@ -159,16 +159,16 @@ void testSwitchElementIds() { () -> assertNotEquals(swId2, swId4), () -> assertEquals(swId3, swId2), - () -> assertEquals("F0000071 F0000072 1", swId.toString()), - () -> assertEquals("F0000071 F0000073 1", swId2.toString()), - () -> assertEquals("F0000071 F0000073 1", swId3.toString()), - () -> assertEquals("F0000071 F0000073 2", swId4.toString()) + () -> assertEquals("F0000070 F0000071 0", swId.toString()), + () -> assertEquals("F0000070 F0000072 0", swId2.toString()), + () -> assertEquals("F0000070 F0000072 0", swId3.toString()), + () -> assertEquals("F0000070 F0000072 1", swId4.toString()) ); } @Test void testDanglingLineElementIds() { - strategy.initialiseNetwork(network); + strategy.initializeNetwork(network); DanglingLine dl1 = network.getDanglingLine("DL1"); DanglingLine dl2 = network.getDanglingLine("DL2"); DanglingLine dl3 = network.getDanglingLine("DL3"); @@ -183,15 +183,15 @@ void testDanglingLineElementIds() { () -> assertNotEquals(dlId1, dlId3), () -> assertNotEquals(dlId2, dlId3), - () -> assertEquals("F0000072 F0000671 1", dlId1.toString()), - () -> assertEquals("F0000072 X0000011 1", dlId2.toString()), - () -> assertEquals("F0000072 F0000671 2", dlId3.toString()) + () -> assertEquals("F0000071 F0000670 0", dlId1.toString()), + () -> assertEquals("F0000071 X0000011 0", dlId2.toString()), + () -> assertEquals("F0000071 F0000670 1", dlId3.toString()) ); } @Test void testParallelLines() { - strategy.initialiseNetwork(network); + strategy.initializeNetwork(network); Branch line1 = network.getBranch("NHV1_NHV2_1"); Branch line2 = network.getBranch("NHV1_NHV2_2"); @@ -204,14 +204,14 @@ void testParallelLines() { () -> assertTrue(UcteElementId.isUcteElementId(id2.toString())), () -> assertNotEquals(id1, id2), - () -> assertEquals("F0000111 F0000211 1", id1.toString()), - () -> assertEquals("F0000111 F0000211 2", id2.toString()) + () -> assertEquals("F0000110 F0000210 0", id1.toString()), + () -> assertEquals("F0000110 F0000210 1", id2.toString()) ); } @Test void testExistingUcteNodeCodes() { - strategy.initialiseNetwork(network); + strategy.initializeNetwork(network); Bus bus = network.getBusBreakerView().getBus("NGEN"); UcteNodeCode firstCode = strategy.getUcteNodeCode(bus); @@ -229,7 +229,7 @@ void testExistingUcteNodeCodes() { @Test void testNullAndInvalidIds() { - strategy.initialiseNetwork(network); + strategy.initializeNetwork(network); assertAll( () -> assertThrows(PowsyblException.class, () -> strategy.getUcteNodeCode((String) null)), @@ -241,10 +241,10 @@ void testNullAndInvalidIds() { @Test void testCountryCode() { - strategy.initialiseNetwork(network); + strategy.initializeNetwork(network); Bus genBus = network.getBusBreakerView().getBus("NGEN"); UcteNodeCode code = strategy.getUcteNodeCode(genBus); - assertEquals('F', code.toString().charAt(0)); // France + assertEquals('F', code.toString().charAt(0)); } } diff --git a/ucte/ucte-converter/src/test/java/com/powsybl/ucte/converter/UcteImporterTest.java b/ucte/ucte-converter/src/test/java/com/powsybl/ucte/converter/UcteImporterTest.java index 079cf5ea9ea..add5d45cb37 100644 --- a/ucte/ucte-converter/src/test/java/com/powsybl/ucte/converter/UcteImporterTest.java +++ b/ucte/ucte-converter/src/test/java/com/powsybl/ucte/converter/UcteImporterTest.java @@ -18,7 +18,7 @@ import com.powsybl.entsoe.util.EntsoeGeographicalCode; import com.powsybl.iidm.network.*; import com.powsybl.iidm.network.impl.NetworkFactoryImpl; -import com.powsybl.ucte.converter.util.UcteConstants; +import com.powsybl.ucte.converter.util.UcteConverterConstants; import org.junit.jupiter.api.Test; import java.io.IOException; @@ -271,7 +271,7 @@ void emptyElementName() { Network network = new UcteImporter().importData(dataSource, new NetworkFactoryImpl(), null); Line l = network.getLine("F_SU1_12 F_SU1_11 1"); assertNotNull(l); - assertFalse(l.hasProperty(UcteConstants.ELEMENT_NAME_PROPERTY_KEY)); + assertFalse(l.hasProperty(UcteConverterConstants.ELEMENT_NAME_PROPERTY_KEY)); } @Test diff --git a/ucte/ucte-converter/src/test/java/com/powsybl/ucte/converter/util/UcteConverterHelperTest.java b/ucte/ucte-converter/src/test/java/com/powsybl/ucte/converter/util/UcteConverterHelperTest.java index 03925fea03b..85cbc0a3e67 100644 --- a/ucte/ucte-converter/src/test/java/com/powsybl/ucte/converter/util/UcteConverterHelperTest.java +++ b/ucte/ucte-converter/src/test/java/com/powsybl/ucte/converter/util/UcteConverterHelperTest.java @@ -12,7 +12,6 @@ import com.powsybl.commons.datasource.ResourceSet; import com.powsybl.iidm.network.Network; import com.powsybl.iidm.network.NetworkFactory; -import com.powsybl.ucte.converter.UcteException; import com.powsybl.ucte.converter.UcteImporter; import org.apache.commons.math3.complex.Complex; import org.junit.jupiter.api.BeforeAll; @@ -91,15 +90,4 @@ void calculatePhaseDuTest2() { assertEquals(90.00, Math.toDegrees(duRef6.getArgument()), 0.00001); // loss of one decimal with sign } - @Test - void getOrderCodeTest() { - assertEquals('1', getOrderCode(0)); - assertEquals('A', getOrderCode(9)); - assertThrows(UcteException.class, () -> { - getOrderCode(-1); - }); - assertThrows(UcteException.class, () -> { - getOrderCode(50); - }); - } } diff --git a/ucte/ucte-network/src/main/java/com/powsybl/ucte/network/UcteCountryCode.java b/ucte/ucte-network/src/main/java/com/powsybl/ucte/network/UcteCountryCode.java index 376eb1f81be..5700cfbe271 100644 --- a/ucte/ucte-network/src/main/java/com/powsybl/ucte/network/UcteCountryCode.java +++ b/ucte/ucte-network/src/main/java/com/powsybl/ucte/network/UcteCountryCode.java @@ -132,7 +132,7 @@ public static UcteCountryCode fromVoltagelevel(VoltageLevel voltageLevel) { try { return UcteCountryCode.valueOf(country.name()); } catch (IllegalArgumentException e) { - throw new UcteException(String.format("No UCTE country found for " + country.name())); + throw new UcteException(String.format("No UCTE country found for %s",country.name())); } } diff --git a/ucte/ucte-network/src/main/java/com/powsybl/ucte/network/UcteElementId.java b/ucte/ucte-network/src/main/java/com/powsybl/ucte/network/UcteElementId.java index a6b4c43d8aa..5cc8d13c480 100644 --- a/ucte/ucte-network/src/main/java/com/powsybl/ucte/network/UcteElementId.java +++ b/ucte/ucte-network/src/main/java/com/powsybl/ucte/network/UcteElementId.java @@ -7,6 +7,8 @@ */ package com.powsybl.ucte.network; +import com.powsybl.ucte.network.util.UcteNetworkUtil; + import java.util.*; /** @@ -15,9 +17,6 @@ */ public class UcteElementId implements Comparable { - private static final List ORDER_CODES = List.of('1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', - 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'); - private final UcteNodeCode nodeCode1; private final UcteNodeCode nodeCode2; private char orderCode; @@ -114,8 +113,7 @@ private static boolean isOrderCode(char orderCode) { /* Update to match modification on UCTE format The new update is available on the ENTSO-E website: - https://docstore.entsoe.eu/Documents/Publications/SOC/Continental_Europe/150420_quality_of_datasets_and_calculations_3rd_edition.pdf - */ - return ORDER_CODES.contains(orderCode); + https://eepublicdownloads.entsoe.eu/clean-documents/Publications/SOC/Continental_Europe/150420_quality_of_datasets_and_calculations_3rd_edition.pdf */ + return UcteNetworkUtil.ORDER_CODES.contains(orderCode); } } diff --git a/ucte/ucte-network/src/main/java/com/powsybl/ucte/network/util/UcteNetworkUtil.java b/ucte/ucte-network/src/main/java/com/powsybl/ucte/network/util/UcteNetworkUtil.java new file mode 100644 index 00000000000..85aad09fb35 --- /dev/null +++ b/ucte/ucte-network/src/main/java/com/powsybl/ucte/network/util/UcteNetworkUtil.java @@ -0,0 +1,33 @@ +/** + * Copyright (c) 2024, 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/. + * SPDX-License-Identifier: MPL-2.0 + */ + +package com.powsybl.ucte.network.util; + +import com.powsybl.ucte.network.UcteException; + +import java.util.List; + +/** + * @author Clément LECLERC {@literal } + */ +public final class UcteNetworkUtil { + + private UcteNetworkUtil() { + throw new IllegalStateException("Should not be constructed"); + } + + public static final List ORDER_CODES = List.of('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', + 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '_', '-', '.', ' '); + + public static char getOrderCode(int index) { + if (index > ORDER_CODES.size() || index < 0) { + throw new UcteException("Order code index out of bounds"); + } + return ORDER_CODES.get(index); + } +} diff --git a/ucte/ucte-network/src/test/java/com/powsybl/ucte/network/util/UcteNetworkUtilTest.java b/ucte/ucte-network/src/test/java/com/powsybl/ucte/network/util/UcteNetworkUtilTest.java new file mode 100644 index 00000000000..fe2b1aeea7f --- /dev/null +++ b/ucte/ucte-network/src/test/java/com/powsybl/ucte/network/util/UcteNetworkUtilTest.java @@ -0,0 +1,32 @@ +/** + * Copyright (c) 2024, 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/. + * SPDX-License-Identifier: MPL-2.0 + */ +package com.powsybl.ucte.network.util; + +import com.powsybl.ucte.network.UcteException; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; + +/** + * @author Clément LECLERC {@literal } + */ +class UcteNetworkUtilTest { + + @Test + void getOrderCodeTest() { + assertEquals('0', UcteNetworkUtil.getOrderCode(0)); + assertEquals('A', UcteNetworkUtil.getOrderCode(10)); + assertThrows(com.powsybl.ucte.network.UcteException.class, () -> { + UcteNetworkUtil.getOrderCode(-1); + }); + assertThrows(UcteException.class, () -> { + UcteNetworkUtil.getOrderCode(50); + }); + } +} From 444b3272355c61a6ce3a1be3de01b6263e576b4f Mon Sep 17 00:00:00 2001 From: Leclerc Clement Date: Mon, 9 Dec 2024 14:38:56 +0100 Subject: [PATCH 36/38] add Exception for null equipement Signed-off-by: Leclerc Clement --- .../ucte/converter/AbstractNamingStrategy.java | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/ucte/ucte-converter/src/main/java/com/powsybl/ucte/converter/AbstractNamingStrategy.java b/ucte/ucte-converter/src/main/java/com/powsybl/ucte/converter/AbstractNamingStrategy.java index b146899fba4..f7c9067ed9a 100644 --- a/ucte/ucte-converter/src/main/java/com/powsybl/ucte/converter/AbstractNamingStrategy.java +++ b/ucte/ucte-converter/src/main/java/com/powsybl/ucte/converter/AbstractNamingStrategy.java @@ -7,6 +7,7 @@ */ package com.powsybl.ucte.converter; +import com.powsybl.commons.PowsyblException; import com.powsybl.iidm.network.*; import com.powsybl.ucte.converter.util.UcteConverterConstants; import com.powsybl.ucte.network.UcteElementId; @@ -36,6 +37,9 @@ public UcteNodeCode getUcteNodeCode(String id) { @Override public UcteNodeCode getUcteNodeCode(Bus bus) { + if(bus == null) { + throw new PowsyblException("the bus is null"); + } return getUcteNodeCode(bus.getId()); } @@ -55,16 +59,25 @@ public UcteElementId getUcteElementId(String id) { @Override public UcteElementId getUcteElementId(Switch sw) { + if(sw == null) { + throw new PowsyblException("the bus is null"); + } return getUcteElementId(sw.getId()); } @Override public UcteElementId getUcteElementId(Branch branch) { + if(branch == null) { + throw new PowsyblException("the bus is null"); + } return getUcteElementId(branch.getId()); } @Override public UcteElementId getUcteElementId(DanglingLine danglingLine) { + if(danglingLine == null) { + throw new PowsyblException("the bus is null"); + } return getUcteElementId(danglingLine.getId()); } } From 1b686aebb5fc81ba6dd38eefad6d3eebf2a311ef Mon Sep 17 00:00:00 2001 From: Leclerc Clement Date: Mon, 9 Dec 2024 14:40:05 +0100 Subject: [PATCH 37/38] add Exception for null equipment Signed-off-by: Leclerc Clement --- .../powsybl/ucte/converter/AbstractNamingStrategy.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/ucte/ucte-converter/src/main/java/com/powsybl/ucte/converter/AbstractNamingStrategy.java b/ucte/ucte-converter/src/main/java/com/powsybl/ucte/converter/AbstractNamingStrategy.java index f7c9067ed9a..af851db9c3d 100644 --- a/ucte/ucte-converter/src/main/java/com/powsybl/ucte/converter/AbstractNamingStrategy.java +++ b/ucte/ucte-converter/src/main/java/com/powsybl/ucte/converter/AbstractNamingStrategy.java @@ -37,7 +37,7 @@ public UcteNodeCode getUcteNodeCode(String id) { @Override public UcteNodeCode getUcteNodeCode(Bus bus) { - if(bus == null) { + if (bus == null) { throw new PowsyblException("the bus is null"); } return getUcteNodeCode(bus.getId()); @@ -59,7 +59,7 @@ public UcteElementId getUcteElementId(String id) { @Override public UcteElementId getUcteElementId(Switch sw) { - if(sw == null) { + if (sw == null) { throw new PowsyblException("the bus is null"); } return getUcteElementId(sw.getId()); @@ -67,7 +67,7 @@ public UcteElementId getUcteElementId(Switch sw) { @Override public UcteElementId getUcteElementId(Branch branch) { - if(branch == null) { + if (branch == null) { throw new PowsyblException("the bus is null"); } return getUcteElementId(branch.getId()); @@ -75,7 +75,7 @@ public UcteElementId getUcteElementId(Branch branch) { @Override public UcteElementId getUcteElementId(DanglingLine danglingLine) { - if(danglingLine == null) { + if (danglingLine == null) { throw new PowsyblException("the bus is null"); } return getUcteElementId(danglingLine.getId()); From 930add0f7e5ac45765f906ac625bf2553968985f Mon Sep 17 00:00:00 2001 From: Leclerc Clement Date: Mon, 9 Dec 2024 15:39:47 +0100 Subject: [PATCH 38/38] fix checkstyle error Signed-off-by: Leclerc Clement --- .../powsybl/ucte/converter/AbstractNamingStrategy.java | 8 ++++---- .../java/com/powsybl/ucte/network/UcteCountryCode.java | 2 +- .../powsybl/ucte/network/util/UcteNetworkUtilTest.java | 8 ++------ 3 files changed, 7 insertions(+), 11 deletions(-) diff --git a/ucte/ucte-converter/src/main/java/com/powsybl/ucte/converter/AbstractNamingStrategy.java b/ucte/ucte-converter/src/main/java/com/powsybl/ucte/converter/AbstractNamingStrategy.java index af851db9c3d..42e70197d5a 100644 --- a/ucte/ucte-converter/src/main/java/com/powsybl/ucte/converter/AbstractNamingStrategy.java +++ b/ucte/ucte-converter/src/main/java/com/powsybl/ucte/converter/AbstractNamingStrategy.java @@ -38,7 +38,7 @@ public UcteNodeCode getUcteNodeCode(String id) { @Override public UcteNodeCode getUcteNodeCode(Bus bus) { if (bus == null) { - throw new PowsyblException("the bus is null"); + throw new PowsyblException("the bus is null"); } return getUcteNodeCode(bus.getId()); } @@ -60,7 +60,7 @@ public UcteElementId getUcteElementId(String id) { @Override public UcteElementId getUcteElementId(Switch sw) { if (sw == null) { - throw new PowsyblException("the bus is null"); + throw new PowsyblException("the switch is null"); } return getUcteElementId(sw.getId()); } @@ -68,7 +68,7 @@ public UcteElementId getUcteElementId(Switch sw) { @Override public UcteElementId getUcteElementId(Branch branch) { if (branch == null) { - throw new PowsyblException("the bus is null"); + throw new PowsyblException("the branch is null"); } return getUcteElementId(branch.getId()); } @@ -76,7 +76,7 @@ public UcteElementId getUcteElementId(Branch branch) { @Override public UcteElementId getUcteElementId(DanglingLine danglingLine) { if (danglingLine == null) { - throw new PowsyblException("the bus is null"); + throw new PowsyblException("the danglingLine is null"); } return getUcteElementId(danglingLine.getId()); } diff --git a/ucte/ucte-network/src/main/java/com/powsybl/ucte/network/UcteCountryCode.java b/ucte/ucte-network/src/main/java/com/powsybl/ucte/network/UcteCountryCode.java index 5700cfbe271..19ce759d720 100644 --- a/ucte/ucte-network/src/main/java/com/powsybl/ucte/network/UcteCountryCode.java +++ b/ucte/ucte-network/src/main/java/com/powsybl/ucte/network/UcteCountryCode.java @@ -132,7 +132,7 @@ public static UcteCountryCode fromVoltagelevel(VoltageLevel voltageLevel) { try { return UcteCountryCode.valueOf(country.name()); } catch (IllegalArgumentException e) { - throw new UcteException(String.format("No UCTE country found for %s",country.name())); + throw new UcteException(String.format("No UCTE country found for %s", country.name())); } } diff --git a/ucte/ucte-network/src/test/java/com/powsybl/ucte/network/util/UcteNetworkUtilTest.java b/ucte/ucte-network/src/test/java/com/powsybl/ucte/network/util/UcteNetworkUtilTest.java index fe2b1aeea7f..148b756b9a1 100644 --- a/ucte/ucte-network/src/test/java/com/powsybl/ucte/network/util/UcteNetworkUtilTest.java +++ b/ucte/ucte-network/src/test/java/com/powsybl/ucte/network/util/UcteNetworkUtilTest.java @@ -22,11 +22,7 @@ class UcteNetworkUtilTest { void getOrderCodeTest() { assertEquals('0', UcteNetworkUtil.getOrderCode(0)); assertEquals('A', UcteNetworkUtil.getOrderCode(10)); - assertThrows(com.powsybl.ucte.network.UcteException.class, () -> { - UcteNetworkUtil.getOrderCode(-1); - }); - assertThrows(UcteException.class, () -> { - UcteNetworkUtil.getOrderCode(50); - }); + assertThrows(UcteException.class, () -> UcteNetworkUtil.getOrderCode(-1)); + assertThrows(UcteException.class, () -> UcteNetworkUtil.getOrderCode(50)); } }