From c58bb3793cc4ef893271aec5e0215149a25af215 Mon Sep 17 00:00:00 2001 From: Bertrand Rix Date: Mon, 23 Jan 2023 10:40:01 +0100 Subject: [PATCH 1/7] WIP, Add emf merge tests. Signed-off-by: Bertrand Rix --- emf/pom.xml | 85 ++++++ .../java/com/powsybl/emf/IGMmergeTests.java | 243 ++++++++++++++++++ pom.xml | 1 + 3 files changed, 329 insertions(+) create mode 100644 emf/pom.xml create mode 100644 emf/src/test/java/com/powsybl/emf/IGMmergeTests.java diff --git a/emf/pom.xml b/emf/pom.xml new file mode 100644 index 00000000..39b6d649 --- /dev/null +++ b/emf/pom.xml @@ -0,0 +1,85 @@ + + + + powsybl-entsoe + com.powsybl + 2.2.0-SNAPSHOT + + 4.0.0 + + powsybl-emf + European merging function algorithm + Implementation of european merging function algorithm + + + + com.powsybl + powsybl-commons + + + com.powsybl + powsybl-iidm-api + + + com.powsybl + powsybl-cgmes-extensions + + + com.powsybl + powsybl-cgmes-conversion + + + com.powsybl + powsybl-iidm-mergingview + + + com.powsybl + powsybl-cgmes-conformity + + + com.powsybl + powsybl-cgmes-model + + + ch.qos.logback + logback-classic + runtime + + + com.google.jimfs + jimfs + test + + + com.powsybl + powsybl-config-test + test + + + org.junit.jupiter + junit-jupiter + ${junit-jupiter.version} + test + + + + + com.powsybl + powsybl-iidm-impl + runtime + + + com.powsybl + powsybl-open-loadflow + runtime + + + com.powsybl + powsybl-triple-store-impl-rdf4j + runtime + + + + diff --git a/emf/src/test/java/com/powsybl/emf/IGMmergeTests.java b/emf/src/test/java/com/powsybl/emf/IGMmergeTests.java new file mode 100644 index 00000000..1e1afe3b --- /dev/null +++ b/emf/src/test/java/com/powsybl/emf/IGMmergeTests.java @@ -0,0 +1,243 @@ +/* + * Copyright (c) 2022, RTE (http://www.rte-france.com) + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ +package com.powsybl.emf; + +import com.google.common.jimfs.Configuration; +import com.google.common.jimfs.Jimfs; +import com.powsybl.cgmes.conformity.CgmesConformity1Catalog; +import com.powsybl.cgmes.conversion.export.*; +import com.powsybl.cgmes.model.test.TestGridModelResources; +import com.powsybl.commons.datasource.GenericReadOnlyDataSource; +import com.powsybl.commons.datasource.ResourceSet; +import com.powsybl.commons.xml.XmlUtil; +import com.powsybl.iidm.mergingview.MergingView; +import com.powsybl.iidm.network.Network; +import org.junit.jupiter.api.Test; + +import javax.xml.stream.XMLStreamException; +import javax.xml.stream.XMLStreamWriter; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.net.URI; +import java.net.URISyntaxException; +import java.nio.file.FileSystem; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.*; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertTrue; + +class IGMmergeTests { + + @Test + void igmsDestructiveMerge() throws IOException { + + FileSystem fs = Jimfs.newFileSystem(Configuration.unix()); + + Set branchIds = new HashSet<>(); + Set generatorsId = new HashSet<>(); + Set voltageLevelIds = new HashSet<>(); + + //Load two IGMs BE and NL + Map validNetworks = new HashMap<>(); + TestGridModelResources resBE = CgmesConformity1Catalog.microGridBaseCaseBE(); + Network igmBE = Network.read(resBE.dataSource()); + validNetworks.put("BE", igmBE); + igmBE.getBranches().forEach(b -> branchIds.add(b.getId())); + igmBE.getGenerators().forEach(g -> generatorsId.add(g.getId())); + igmBE.getVoltageLevels().forEach(v -> voltageLevelIds.add(v.getId())); + + TestGridModelResources resNL = CgmesConformity1Catalog.microGridBaseCaseNL(); + Network igmNL = Network.read(resNL.dataSource()); + validNetworks.put("NL", igmNL); + igmNL.getBranches().forEach(b -> branchIds.add(b.getId())); + igmNL.getGenerators().forEach(g -> generatorsId.add(g.getId())); + igmNL.getVoltageLevels().forEach(v -> voltageLevelIds.add(v.getId())); + + //Merge, Serialize and Deserialize the network + igmBE.merge(igmNL); + validNetworks.put("Merged", igmBE); + + Path destructiveMergeDir = Files.createDirectory(fs.getPath("/destructiveMerge")); + exportNetwork(igmBE, destructiveMergeDir, "BE_NL", validNetworks, Set.of("EQ", "TP", "SSH", "SV")); + + //Copy the boundary set explicitly it is not serialized and is needed for reimport + ResourceSet boundaries = CgmesConformity1Catalog.microGridBaseCaseBoundaries(); + for (String bFile : boundaries.getFileNames()) { + Files.copy(boundaries.newInputStream(bFile), destructiveMergeDir.resolve("BE_NL" + bFile)); + } + + //Reimport and check + Network serializedMergedNetwork = Network.read(new GenericReadOnlyDataSource(destructiveMergeDir, "BE_NL"), null); + validate(serializedMergedNetwork, branchIds, generatorsId, voltageLevelIds); + } + + @Test + void igmsMergeWithMergingView() throws IOException { + + FileSystem fs = Jimfs.newFileSystem(Configuration.unix()); + + Set branchIds = new HashSet<>(); + Set generatorsId = new HashSet<>(); + Set voltageLevelIds = new HashSet<>(); + + Map validNetworks = new HashMap<>(); + TestGridModelResources resBE = CgmesConformity1Catalog.microGridBaseCaseBE(); + Network igmBE = Network.read(resBE.dataSource()); + validNetworks.put("BE", igmBE); + igmBE.getBranches().forEach(b -> branchIds.add(b.getId())); + igmBE.getGenerators().forEach(g -> generatorsId.add(g.getId())); + igmBE.getVoltageLevels().forEach(v -> voltageLevelIds.add(v.getId())); + + TestGridModelResources resNL = CgmesConformity1Catalog.microGridBaseCaseNL(); + Network igmNL = Network.read(resNL.dataSource()); + + MergingView mergingView = MergingView.create("merged", "validation"); + mergingView.merge(igmBE, igmNL); + validNetworks.put("NL", igmNL); + igmNL.getBranches().forEach(b -> branchIds.add(b.getId())); + igmNL.getGenerators().forEach(g -> generatorsId.add(g.getId())); + igmNL.getVoltageLevels().forEach(v -> voltageLevelIds.add(v.getId())); + validNetworks.put("Merged", mergingView); + + Path mergingViewMergeDir = Files.createDirectory(fs.getPath("/mergingViewMerge")); + //Export to CGMES only state variable of the merged network, the rest is exported separately for each igms + exportNetwork(mergingView, mergingViewMergeDir, "BE_NL", validNetworks, Set.of("SV")); + exportNetwork(igmBE, mergingViewMergeDir, "BE_NL_BE", Map.of("BE", igmBE), Set.of("EQ", "TP", "SSH")); + exportNetwork(igmNL, mergingViewMergeDir, "BE_NL_NL", Map.of("NL", igmNL), Set.of("EQ", "TP", "SSH")); + + //Copy the boundary set explicitly it is not serialized and is needed for reimport + ResourceSet boundaries = CgmesConformity1Catalog.microGridBaseCaseBoundaries(); + for (String bFile : boundaries.getFileNames()) { + Files.copy(boundaries.newInputStream(bFile), mergingViewMergeDir.resolve("BE_NL" + bFile)); + } + + Network serializedMergedNetwork = Network.read(new GenericReadOnlyDataSource(mergingViewMergeDir, "BE_NL"), null); + validate(serializedMergedNetwork, branchIds, generatorsId, voltageLevelIds); + } + + @Test + void cgmToCGMES() throws IOException { + //Read resources for BE and NL, merge the resources themselves and read a network from this set of resources + TestGridModelResources mergedResourcesBENL = new TestGridModelResources( + "MicroGrid-BaseCase-BE_NL_MergedResources", + null, + new ResourceSet("/conformity/cas-1.1.3-data-4.0.3/MicroGrid/BaseCase/CGMES_v2.4.15_MicroGridTestConfiguration_BC_BE_v2/", + "MicroGridTestConfiguration_BC_BE_DL_V2.xml", + "MicroGridTestConfiguration_BC_BE_DY_V2.xml", + "MicroGridTestConfiguration_BC_BE_EQ_V2.xml", + "MicroGridTestConfiguration_BC_BE_GL_V2.xml", + "MicroGridTestConfiguration_BC_BE_SSH_V2.xml", + "MicroGridTestConfiguration_BC_BE_SV_V2.xml", + "MicroGridTestConfiguration_BC_BE_TP_V2.xml"), + new ResourceSet("/conformity/cas-1.1.3-data-4.0.3/MicroGrid/BaseCase/CGMES_v2.4.15_MicroGridTestConfiguration_BC_NL_v2/", + "MicroGridTestConfiguration_BC_NL_DL_V2.xml", + "MicroGridTestConfiguration_BC_NL_DY_V2.xml", + "MicroGridTestConfiguration_BC_NL_EQ_V2.xml", + "MicroGridTestConfiguration_BC_NL_GL_V2.xml", + "MicroGridTestConfiguration_BC_NL_SSH_V2.xml", + "MicroGridTestConfiguration_BC_NL_SV_V2.xml", + "MicroGridTestConfiguration_BC_NL_TP_V2.xml"), + CgmesConformity1Catalog.microGridBaseCaseBoundaries()); + Network networkBENL = Network.read(mergedResourcesBENL.dataSource()); + + FileSystem fs = Jimfs.newFileSystem(Configuration.unix()); + + Set branchIds = new HashSet<>(); + Set generatorsId = new HashSet<>(); + Set voltageLevelIds = new HashSet<>(); + + networkBENL.getBranches().forEach(b -> branchIds.add(b.getId())); + networkBENL.getGenerators().forEach(g -> generatorsId.add(g.getId())); + networkBENL.getVoltageLevels().forEach(v -> voltageLevelIds.add(v.getId())); + + Path mergedResourcesDir = Files.createDirectory(fs.getPath("/mergedResourcesExport")); + exportNetwork(networkBENL, mergedResourcesDir, "BE_NL", Map.of("BENL", networkBENL), Set.of("EQ", "TP", "SSH", "SV")); + + + //Copy the boundary set explicitly it is not serialized and is needed for reimport + ResourceSet boundaries = CgmesConformity1Catalog.microGridBaseCaseBoundaries(); + for (String bFile : boundaries.getFileNames()) { + Files.copy(boundaries.newInputStream(bFile), mergedResourcesDir.resolve("BE_NL" + bFile)); + } + Network serializedMergedNetwork = Network.read(new GenericReadOnlyDataSource(mergedResourcesDir, "BE_NL"), null); + validate(serializedMergedNetwork, branchIds, generatorsId, voltageLevelIds); + } + + public void validate(Network n, Set branchIds, Set generatorsId, Set voltageLevelIds) { + branchIds.forEach(b -> assertNotNull(n.getBranch(b))); + generatorsId.forEach(g -> assertNotNull(n.getGenerator(g))); + voltageLevelIds.forEach(v -> assertNotNull(n.getVoltageLevel(v))); + } + + public void exportNetwork(Network network, Path outputDir, String baseName, Map validNetworks, Set profilesToExport) { + Objects.requireNonNull(network); + Path filenameEq = outputDir.resolve(baseName + "_EQ.xml"); + Path filenameTp = outputDir.resolve( baseName + "_TP.xml"); + Path filenameSsh = outputDir.resolve( baseName + "_SSH.xml"); + Path filenameSv = outputDir.resolve( baseName + "_SV.xml"); + CgmesExportContext context = new CgmesExportContext(); + context.setScenarioTime(network.getCaseDate()); + validNetworks.forEach((name, n) -> { + context.addIidmMappings(n); + }); + + if (profilesToExport.contains("EQ")) { + exportEquipment(network, context, filenameEq); + } + if (profilesToExport.contains("TP")) { + exportTopology(network, context, filenameTp); + } + if (profilesToExport.contains("SSH")) { + exportSteadyStateHypothesis(network, context, filenameSsh); + } + if (profilesToExport.contains("SV")) { + exportStateVariable(network, context, filenameSv); + } + } + + public void exportEquipment(Network network, CgmesExportContext context, Path file) { + try (OutputStream out = Files.newOutputStream(file)) { + XMLStreamWriter writer = XmlUtil.initializeWriter(true, " ", out); + EquipmentExport.write(network, writer, context); + } catch (IOException | XMLStreamException e) { + throw new RuntimeException(e); + } + } + + public void exportTopology(Network network, CgmesExportContext context, Path file) { + try (OutputStream out = Files.newOutputStream(file)) { + XMLStreamWriter writer = XmlUtil.initializeWriter(true, " ", out); + TopologyExport.write(network, writer, context); + } catch (IOException | XMLStreamException e) { + throw new RuntimeException(e); + } + } + + public void exportSteadyStateHypothesis(Network network, CgmesExportContext context, Path file) { + try (OutputStream out = Files.newOutputStream(file)) { + XMLStreamWriter writer = XmlUtil.initializeWriter(true, " ", out); + SteadyStateHypothesisExport.write(network, writer, context); + } catch (XMLStreamException | IOException e) { + throw new RuntimeException(e); + } + } + + public void exportStateVariable(Network network, CgmesExportContext context, Path file) { + try (OutputStream out = Files.newOutputStream(file)) { + XMLStreamWriter writer = XmlUtil.initializeWriter(true, " ", out); + StateVariablesExport.write(network, writer, context); + } catch (IOException | XMLStreamException e) { + throw new RuntimeException(e); + } + } +} diff --git a/pom.xml b/pom.xml index 5c4c50ec..c4f770d0 100644 --- a/pom.xml +++ b/pom.xml @@ -77,6 +77,7 @@ distribution-entsoe flow-decomposition glsk + emf From bfbf3ddc54a5beaa3c2375c64764bc5db1094250 Mon Sep 17 00:00:00 2001 From: Bertrand Rix Date: Mon, 23 Jan 2023 10:54:44 +0100 Subject: [PATCH 2/7] Fix style. Signed-off-by: Bertrand Rix --- .../test/java/com/powsybl/emf/IGMmergeTests.java | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) diff --git a/emf/src/test/java/com/powsybl/emf/IGMmergeTests.java b/emf/src/test/java/com/powsybl/emf/IGMmergeTests.java index 1e1afe3b..fb5643d0 100644 --- a/emf/src/test/java/com/powsybl/emf/IGMmergeTests.java +++ b/emf/src/test/java/com/powsybl/emf/IGMmergeTests.java @@ -21,20 +21,13 @@ import javax.xml.stream.XMLStreamException; import javax.xml.stream.XMLStreamWriter; import java.io.IOException; -import java.io.InputStream; import java.io.OutputStream; -import java.net.URI; -import java.net.URISyntaxException; import java.nio.file.FileSystem; import java.nio.file.Files; import java.nio.file.Path; -import java.nio.file.Paths; import java.util.*; -import java.util.stream.Collectors; -import java.util.stream.Stream; import static org.junit.jupiter.api.Assertions.assertNotNull; -import static org.junit.jupiter.api.Assertions.assertTrue; class IGMmergeTests { @@ -126,7 +119,7 @@ void igmsMergeWithMergingView() throws IOException { } @Test - void cgmToCGMES() throws IOException { + void cgmToCgmes() throws IOException { //Read resources for BE and NL, merge the resources themselves and read a network from this set of resources TestGridModelResources mergedResourcesBENL = new TestGridModelResources( "MicroGrid-BaseCase-BE_NL_MergedResources", @@ -163,7 +156,6 @@ void cgmToCGMES() throws IOException { Path mergedResourcesDir = Files.createDirectory(fs.getPath("/mergedResourcesExport")); exportNetwork(networkBENL, mergedResourcesDir, "BE_NL", Map.of("BENL", networkBENL), Set.of("EQ", "TP", "SSH", "SV")); - //Copy the boundary set explicitly it is not serialized and is needed for reimport ResourceSet boundaries = CgmesConformity1Catalog.microGridBaseCaseBoundaries(); for (String bFile : boundaries.getFileNames()) { @@ -182,9 +174,9 @@ public void validate(Network n, Set branchIds, Set generatorsId, public void exportNetwork(Network network, Path outputDir, String baseName, Map validNetworks, Set profilesToExport) { Objects.requireNonNull(network); Path filenameEq = outputDir.resolve(baseName + "_EQ.xml"); - Path filenameTp = outputDir.resolve( baseName + "_TP.xml"); - Path filenameSsh = outputDir.resolve( baseName + "_SSH.xml"); - Path filenameSv = outputDir.resolve( baseName + "_SV.xml"); + Path filenameTp = outputDir.resolve(baseName + "_TP.xml"); + Path filenameSsh = outputDir.resolve(baseName + "_SSH.xml"); + Path filenameSv = outputDir.resolve(baseName + "_SV.xml"); CgmesExportContext context = new CgmesExportContext(); context.setScenarioTime(network.getCaseDate()); validNetworks.forEach((name, n) -> { From f6bea46df2218ca443fc26338514c20a8fafc575 Mon Sep 17 00:00:00 2001 From: Bertrand Rix Date: Wed, 25 Jan 2023 15:55:49 +0100 Subject: [PATCH 3/7] Some adjustments after review. Signed-off-by: Bertrand Rix --- emf/pom.xml | 7 +++-- .../java/com/powsybl/emf/IGMmergeTests.java | 26 ++++++++++--------- 2 files changed, 17 insertions(+), 16 deletions(-) diff --git a/emf/pom.xml b/emf/pom.xml index 39b6d649..55d7c661 100644 --- a/emf/pom.xml +++ b/emf/pom.xml @@ -64,21 +64,20 @@ test - com.powsybl powsybl-iidm-impl - runtime + test com.powsybl powsybl-open-loadflow - runtime + test com.powsybl powsybl-triple-store-impl-rdf4j - runtime + test diff --git a/emf/src/test/java/com/powsybl/emf/IGMmergeTests.java b/emf/src/test/java/com/powsybl/emf/IGMmergeTests.java index fb5643d0..0f84129b 100644 --- a/emf/src/test/java/com/powsybl/emf/IGMmergeTests.java +++ b/emf/src/test/java/com/powsybl/emf/IGMmergeTests.java @@ -16,6 +16,7 @@ import com.powsybl.commons.xml.XmlUtil; import com.powsybl.iidm.mergingview.MergingView; import com.powsybl.iidm.network.Network; +import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; import javax.xml.stream.XMLStreamException; @@ -31,11 +32,16 @@ class IGMmergeTests { + private static FileSystem fs; + + @BeforeAll + static void setUp() { + fs = Jimfs.newFileSystem(Configuration.unix()); + } + @Test void igmsDestructiveMerge() throws IOException { - FileSystem fs = Jimfs.newFileSystem(Configuration.unix()); - Set branchIds = new HashSet<>(); Set generatorsId = new HashSet<>(); Set voltageLevelIds = new HashSet<>(); @@ -77,8 +83,6 @@ void igmsDestructiveMerge() throws IOException { @Test void igmsMergeWithMergingView() throws IOException { - FileSystem fs = Jimfs.newFileSystem(Configuration.unix()); - Set branchIds = new HashSet<>(); Set generatorsId = new HashSet<>(); Set voltageLevelIds = new HashSet<>(); @@ -143,8 +147,6 @@ void cgmToCgmes() throws IOException { CgmesConformity1Catalog.microGridBaseCaseBoundaries()); Network networkBENL = Network.read(mergedResourcesBENL.dataSource()); - FileSystem fs = Jimfs.newFileSystem(Configuration.unix()); - Set branchIds = new HashSet<>(); Set generatorsId = new HashSet<>(); Set voltageLevelIds = new HashSet<>(); @@ -165,13 +167,13 @@ void cgmToCgmes() throws IOException { validate(serializedMergedNetwork, branchIds, generatorsId, voltageLevelIds); } - public void validate(Network n, Set branchIds, Set generatorsId, Set voltageLevelIds) { + private static void validate(Network n, Set branchIds, Set generatorsId, Set voltageLevelIds) { branchIds.forEach(b -> assertNotNull(n.getBranch(b))); generatorsId.forEach(g -> assertNotNull(n.getGenerator(g))); voltageLevelIds.forEach(v -> assertNotNull(n.getVoltageLevel(v))); } - public void exportNetwork(Network network, Path outputDir, String baseName, Map validNetworks, Set profilesToExport) { + private static void exportNetwork(Network network, Path outputDir, String baseName, Map validNetworks, Set profilesToExport) { Objects.requireNonNull(network); Path filenameEq = outputDir.resolve(baseName + "_EQ.xml"); Path filenameTp = outputDir.resolve(baseName + "_TP.xml"); @@ -197,7 +199,7 @@ public void exportNetwork(Network network, Path outputDir, String baseName, Map< } } - public void exportEquipment(Network network, CgmesExportContext context, Path file) { + private static void exportEquipment(Network network, CgmesExportContext context, Path file) { try (OutputStream out = Files.newOutputStream(file)) { XMLStreamWriter writer = XmlUtil.initializeWriter(true, " ", out); EquipmentExport.write(network, writer, context); @@ -206,7 +208,7 @@ public void exportEquipment(Network network, CgmesExportContext context, Path fi } } - public void exportTopology(Network network, CgmesExportContext context, Path file) { + private static void exportTopology(Network network, CgmesExportContext context, Path file) { try (OutputStream out = Files.newOutputStream(file)) { XMLStreamWriter writer = XmlUtil.initializeWriter(true, " ", out); TopologyExport.write(network, writer, context); @@ -215,7 +217,7 @@ public void exportTopology(Network network, CgmesExportContext context, Path fil } } - public void exportSteadyStateHypothesis(Network network, CgmesExportContext context, Path file) { + private static void exportSteadyStateHypothesis(Network network, CgmesExportContext context, Path file) { try (OutputStream out = Files.newOutputStream(file)) { XMLStreamWriter writer = XmlUtil.initializeWriter(true, " ", out); SteadyStateHypothesisExport.write(network, writer, context); @@ -224,7 +226,7 @@ public void exportSteadyStateHypothesis(Network network, CgmesExportContext cont } } - public void exportStateVariable(Network network, CgmesExportContext context, Path file) { + private static void exportStateVariable(Network network, CgmesExportContext context, Path file) { try (OutputStream out = Files.newOutputStream(file)) { XMLStreamWriter writer = XmlUtil.initializeWriter(true, " ", out); StateVariablesExport.write(network, writer, context); From 998badbcbfd368a49758e39eb47be71c8b38c2a7 Mon Sep 17 00:00:00 2001 From: Bertrand Rix Date: Mon, 30 Jan 2023 10:42:02 +0100 Subject: [PATCH 4/7] Some refactoring. Signed-off-by: Bertrand Rix --- .../java/com/powsybl/emf/IGMmergeTests.java | 57 +++++++------------ 1 file changed, 19 insertions(+), 38 deletions(-) diff --git a/emf/src/test/java/com/powsybl/emf/IGMmergeTests.java b/emf/src/test/java/com/powsybl/emf/IGMmergeTests.java index 0f84129b..c465aee1 100644 --- a/emf/src/test/java/com/powsybl/emf/IGMmergeTests.java +++ b/emf/src/test/java/com/powsybl/emf/IGMmergeTests.java @@ -16,7 +16,8 @@ import com.powsybl.commons.xml.XmlUtil; import com.powsybl.iidm.mergingview.MergingView; import com.powsybl.iidm.network.Network; -import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import javax.xml.stream.XMLStreamException; @@ -27,18 +28,24 @@ import java.nio.file.Files; import java.nio.file.Path; import java.util.*; +import java.util.function.Consumer; import static org.junit.jupiter.api.Assertions.assertNotNull; class IGMmergeTests { - private static FileSystem fs; + private FileSystem fs; - @BeforeAll - static void setUp() { + @BeforeEach + public void setUp() { fs = Jimfs.newFileSystem(Configuration.unix()); } + @AfterEach + public void tearDown() throws IOException { + fs.close(); + } + @Test void igmsDestructiveMerge() throws IOException { @@ -151,7 +158,8 @@ void cgmToCgmes() throws IOException { Set generatorsId = new HashSet<>(); Set voltageLevelIds = new HashSet<>(); - networkBENL.getBranches().forEach(b -> branchIds.add(b.getId())); + //networkBENL.getBranches().forEach(b -> branchIds.add(b.getId())); + networkBENL.getBranches().forEach(b -> branchIds.add(b.getId().replace(" ", "%20"))); // FIXME workaround before fixing CGMES export/import networkBENL.getGenerators().forEach(g -> generatorsId.add(g.getId())); networkBENL.getVoltageLevels().forEach(v -> voltageLevelIds.add(v.getId())); @@ -186,50 +194,23 @@ private static void exportNetwork(Network network, Path outputDir, String baseNa }); if (profilesToExport.contains("EQ")) { - exportEquipment(network, context, filenameEq); + export(filenameEq, (writer) -> {EquipmentExport.write(network, writer, context);}); } if (profilesToExport.contains("TP")) { - exportTopology(network, context, filenameTp); + export(filenameTp, (writer) -> {TopologyExport.write(network, writer, context);}); } if (profilesToExport.contains("SSH")) { - exportSteadyStateHypothesis(network, context, filenameSsh); + export(filenameSsh, (writer) -> {SteadyStateHypothesisExport.write(network, writer, context);}); } if (profilesToExport.contains("SV")) { - exportStateVariable(network, context, filenameSv); - } - } - - private static void exportEquipment(Network network, CgmesExportContext context, Path file) { - try (OutputStream out = Files.newOutputStream(file)) { - XMLStreamWriter writer = XmlUtil.initializeWriter(true, " ", out); - EquipmentExport.write(network, writer, context); - } catch (IOException | XMLStreamException e) { - throw new RuntimeException(e); - } - } - - private static void exportTopology(Network network, CgmesExportContext context, Path file) { - try (OutputStream out = Files.newOutputStream(file)) { - XMLStreamWriter writer = XmlUtil.initializeWriter(true, " ", out); - TopologyExport.write(network, writer, context); - } catch (IOException | XMLStreamException e) { - throw new RuntimeException(e); - } - } - - private static void exportSteadyStateHypothesis(Network network, CgmesExportContext context, Path file) { - try (OutputStream out = Files.newOutputStream(file)) { - XMLStreamWriter writer = XmlUtil.initializeWriter(true, " ", out); - SteadyStateHypothesisExport.write(network, writer, context); - } catch (XMLStreamException | IOException e) { - throw new RuntimeException(e); + export(filenameSv, (writer) -> {StateVariablesExport.write(network, writer, context);}); } } - private static void exportStateVariable(Network network, CgmesExportContext context, Path file) { + private static void export(Path file, Consumer outConsumer) { try (OutputStream out = Files.newOutputStream(file)) { XMLStreamWriter writer = XmlUtil.initializeWriter(true, " ", out); - StateVariablesExport.write(network, writer, context); + outConsumer.accept(writer); } catch (IOException | XMLStreamException e) { throw new RuntimeException(e); } From 84f10a6ad162e2adc2d3b8a2a040d529a4161956 Mon Sep 17 00:00:00 2001 From: Bertrand Rix Date: Mon, 30 Jan 2023 10:47:11 +0100 Subject: [PATCH 5/7] Clean lambdas. Signed-off-by: Bertrand Rix --- emf/src/test/java/com/powsybl/emf/IGMmergeTests.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/emf/src/test/java/com/powsybl/emf/IGMmergeTests.java b/emf/src/test/java/com/powsybl/emf/IGMmergeTests.java index c465aee1..3a4d9dec 100644 --- a/emf/src/test/java/com/powsybl/emf/IGMmergeTests.java +++ b/emf/src/test/java/com/powsybl/emf/IGMmergeTests.java @@ -194,16 +194,16 @@ private static void exportNetwork(Network network, Path outputDir, String baseNa }); if (profilesToExport.contains("EQ")) { - export(filenameEq, (writer) -> {EquipmentExport.write(network, writer, context);}); + export(filenameEq, writer -> EquipmentExport.write(network, writer, context)); } if (profilesToExport.contains("TP")) { - export(filenameTp, (writer) -> {TopologyExport.write(network, writer, context);}); + export(filenameTp, writer -> TopologyExport.write(network, writer, context)); } if (profilesToExport.contains("SSH")) { - export(filenameSsh, (writer) -> {SteadyStateHypothesisExport.write(network, writer, context);}); + export(filenameSsh, writer -> SteadyStateHypothesisExport.write(network, writer, context)); } if (profilesToExport.contains("SV")) { - export(filenameSv, (writer) -> {StateVariablesExport.write(network, writer, context);}); + export(filenameSv, writer -> StateVariablesExport.write(network, writer, context)); } } From b817a9260bd661889973f18f05badcc4b7791984 Mon Sep 17 00:00:00 2001 From: Bertrand Rix Date: Mon, 30 Jan 2023 11:55:47 +0100 Subject: [PATCH 6/7] Fix headers. Signed-off-by: Bertrand Rix --- emf/src/test/java/com/powsybl/emf/IGMmergeTests.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/emf/src/test/java/com/powsybl/emf/IGMmergeTests.java b/emf/src/test/java/com/powsybl/emf/IGMmergeTests.java index 3a4d9dec..5086bec7 100644 --- a/emf/src/test/java/com/powsybl/emf/IGMmergeTests.java +++ b/emf/src/test/java/com/powsybl/emf/IGMmergeTests.java @@ -3,6 +3,7 @@ * 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.emf; @@ -32,6 +33,9 @@ import static org.junit.jupiter.api.Assertions.assertNotNull; +/** + * @author Bertrand Rix + */ class IGMmergeTests { private FileSystem fs; From fe467d8ae93f5513bfc76a08881127830f2a7973 Mon Sep 17 00:00:00 2001 From: Bertrand Rix Date: Mon, 30 Jan 2023 12:05:06 +0100 Subject: [PATCH 7/7] 2023. Signed-off-by: Bertrand Rix --- emf/src/test/java/com/powsybl/emf/IGMmergeTests.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/emf/src/test/java/com/powsybl/emf/IGMmergeTests.java b/emf/src/test/java/com/powsybl/emf/IGMmergeTests.java index 5086bec7..13e3c17b 100644 --- a/emf/src/test/java/com/powsybl/emf/IGMmergeTests.java +++ b/emf/src/test/java/com/powsybl/emf/IGMmergeTests.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022, RTE (http://www.rte-france.com) + * Copyright (c) 2023, 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/.