Skip to content

Commit 9b933b6

Browse files
authored
Add flatten in export options (#3680)
Add flatten in export options (#3680) * Add flatten in export options * Avoid modifying the exported network --------- Signed-off-by: BOUHOURS Antoine <antoine.bouhours@rte-france.com>
1 parent 548df7b commit 9b933b6

File tree

7 files changed

+62
-4
lines changed

7 files changed

+62
-4
lines changed

docs/grid_exchange_formats/iidm/export.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@ Depending on object types the following sorting key has been chosen :
5858

5959
By default, the network components are not sorted.
6060

61+
**iidm.export.xml.flatten**<br>
62+
The `iidm.export.xml.flatten` property is an optional property that defines whether the XIIDM exporter will flatten the network by merging subnetworks into the main network during export. When set to `true`, all subnetworks are integrated into a single flat network structure in the generated XIIDM file. If the network contains no subnetworks, setting this property to `true` has no effect. Its default value is `false`.
63+
6164
**iidm.export.xml.version**<br>
6265
The `iidm.export.xml.version` property is an optional property that defines the XIIDM version to use for the exported file. If the chosen version is not compatible with the network to write, an error occurs. This is typically the case when an attribute appeared in a version more recent than the target one, and its value is not the default one (importing back the file will lead to a different network). By default, the export is done in the more recent version that is supported.
6366

iidm/iidm-serde/src/main/java/com/powsybl/iidm/serde/AbstractTreeDataExporter.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ public abstract class AbstractTreeDataExporter implements Exporter {
116116
public static final String EXTENSIONS_INCLUDED_LIST = "iidm.export.xml.included.extensions";
117117
public static final String EXTENSIONS_EXCLUDED_LIST = "iidm.export.xml.excluded.extensions";
118118
public static final String SORTED = "iidm.export.xml.sorted";
119+
public static final String FLATTEN = "iidm.export.xml.flatten";
119120
public static final String VERSION = "iidm.export.xml.version";
120121
public static final String WITH_AUTOMATION_SYSTEMS = "iidm.export.xml.with-automation-systems";
121122
public static final String VOLTAGE_LEVELS_NODE_BREAKER = "iidm.export.xml.topology-level.voltage-levels.node-breaker";
@@ -142,6 +143,7 @@ public abstract class AbstractTreeDataExporter implements Exporter {
142143
"The list of extensions that will be excluded during export", null,
143144
EXTENSIONS_SUPPLIER.get().getProviders().stream().map(ExtensionProvider::getExtensionName).collect(Collectors.toList()));
144145
private static final Parameter SORTED_PARAMETER = new Parameter(SORTED, ParameterType.BOOLEAN, "Sort export output file", Boolean.FALSE);
146+
private static final Parameter FLATTEN_PARAMETER = new Parameter(FLATTEN, ParameterType.BOOLEAN, "Flatten network containing subnetworks", Boolean.FALSE);
145147
private static final Parameter VERSION_PARAMETER = new Parameter(VERSION, ParameterType.STRING, "IIDM version in which files will be generated", IidmSerDeConstants.CURRENT_IIDM_VERSION.toString("."),
146148
Arrays.stream(IidmVersion.values()).map(v -> v.toString(".")).collect(Collectors.toList()));
147149
private static final Parameter WITH_AUTOMATION_SYSTEMS_PARAMETER = new Parameter(WITH_AUTOMATION_SYSTEMS, ParameterType.BOOLEAN,
@@ -156,7 +158,7 @@ public abstract class AbstractTreeDataExporter implements Exporter {
156158
ONLY_MAIN_CC_PARAMETER, ANONYMISED_PARAMETER, IIDM_VERSION_INCOMPATIBILITY_BEHAVIOR_PARAMETER,
157159
TOPOLOGY_LEVEL_PARAMETER, THROW_EXCEPTION_IF_EXTENSION_NOT_FOUND_PARAMETER, EXTENSIONS_INCLUDED_LIST_PARAMETER,
158160
EXTENSIONS_EXCLUDED_LIST_PARAMETER, SORTED_PARAMETER, VERSION_PARAMETER, WITH_AUTOMATION_SYSTEMS_PARAMETER,
159-
VOLTAGE_LEVELS_NODEBREAKER_PARAMETER, VOLTAGE_LEVELS_BUSBREAKER_PARAMETER, VOLTAGE_LEVELS_BUSBRANCH_PARAMETER);
161+
VOLTAGE_LEVELS_NODEBREAKER_PARAMETER, VOLTAGE_LEVELS_BUSBREAKER_PARAMETER, VOLTAGE_LEVELS_BUSBRANCH_PARAMETER, FLATTEN_PARAMETER);
160162
private final ParameterDefaultValueConfig defaultValueConfig;
161163

162164
protected AbstractTreeDataExporter(PlatformConfig platformConfig) {
@@ -249,6 +251,7 @@ private ExportOptions createExportOptions(Properties parameters) {
249251
.setTopologyLevel(TopologyLevel.valueOf(Parameter.readString(getFormat(), parameters, TOPOLOGY_LEVEL_PARAMETER, defaultValueConfig)))
250252
.setThrowExceptionIfExtensionNotFound(Parameter.readBoolean(getFormat(), parameters, THROW_EXCEPTION_IF_EXTENSION_NOT_FOUND_PARAMETER, defaultValueConfig))
251253
.setSorted(Parameter.readBoolean(getFormat(), parameters, SORTED_PARAMETER, defaultValueConfig))
254+
.setFlatten(Parameter.readBoolean(getFormat(), parameters, FLATTEN_PARAMETER, defaultValueConfig))
252255
.setVersion(Parameter.readString(getFormat(), parameters, VERSION_PARAMETER, defaultValueConfig))
253256
.setFormat(getTreeDataFormat())
254257
.setBusBranchVoltageLevelIncompatibilityBehavior(ExportOptions.BusBranchVoltageLevelIncompatibilityBehavior.valueOf(

iidm/iidm-serde/src/main/java/com/powsybl/iidm/serde/ExportOptions.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ public enum BusBranchVoltageLevelIncompatibilityBehavior {
4949

5050
private BusBranchVoltageLevelIncompatibilityBehavior busBranchVoltageLevelIncompatibilityBehavior = BusBranchVoltageLevelIncompatibilityBehavior.THROW_EXCEPTION;
5151

52+
private boolean flatten = false;
53+
5254
private String version;
5355

5456
private IidmVersionIncompatibilityBehavior iidmVersionIncompatibilityBehavior = IidmVersionIncompatibilityBehavior.THROW_EXCEPTION;
@@ -93,6 +95,12 @@ public ExportOptions(boolean withBranchSV, boolean indent, boolean onlyMainCc, T
9395

9496
public ExportOptions(boolean withBranchSV, boolean indent, boolean onlyMainCc, TopologyLevel topologyLevel, boolean throwExceptionIfExtensionNotFound, boolean sorted, String version,
9597
IidmVersionIncompatibilityBehavior iidmVersionIncompatibilityBehavior) {
98+
this(withBranchSV, indent, onlyMainCc, topologyLevel, throwExceptionIfExtensionNotFound, sorted, version, iidmVersionIncompatibilityBehavior, false);
99+
100+
}
101+
102+
public ExportOptions(boolean withBranchSV, boolean indent, boolean onlyMainCc, TopologyLevel topologyLevel, boolean throwExceptionIfExtensionNotFound, boolean sorted, String version,
103+
IidmVersionIncompatibilityBehavior iidmVersionIncompatibilityBehavior, boolean flatten) {
96104
this.withBranchSV = withBranchSV;
97105
this.indent = indent;
98106
this.onlyMainCc = onlyMainCc;
@@ -101,6 +109,7 @@ public ExportOptions(boolean withBranchSV, boolean indent, boolean onlyMainCc, T
101109
this.sorted = sorted;
102110
this.version = version;
103111
this.iidmVersionIncompatibilityBehavior = Objects.requireNonNull(iidmVersionIncompatibilityBehavior);
112+
this.flatten = flatten;
104113
}
105114

106115
public boolean isWithBranchSV() {
@@ -258,4 +267,13 @@ public ExportOptions setWithAutomationSystems(boolean withAutomationSystems) {
258267
this.withAutomationSystems = withAutomationSystems;
259268
return this;
260269
}
270+
271+
public boolean isFlatten() {
272+
return flatten;
273+
}
274+
275+
public ExportOptions setFlatten(boolean flatten) {
276+
this.flatten = flatten;
277+
return this;
278+
}
261279
}

iidm/iidm-serde/src/main/java/com/powsybl/iidm/serde/NetworkSerDe.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,9 @@ private static void writeBaseNetwork(Network n, NetworkSerializerContext context
369369
AliasesSerDe.write(n, NETWORK_ROOT_ELEMENT_NAME, context);
370370
PropertiesSerDe.write(n, context);
371371

372-
IidmSerDeUtil.runFromMinimumVersion(IidmVersion.V_1_11, context, () -> writeSubnetworks(n, context, extensionsSupplier));
372+
if (supportSubnetworksExport(context)) {
373+
writeSubnetworks(n, context, extensionsSupplier);
374+
}
373375

374376
writeDcDetailed(n, context);
375377
writeVoltageLevels(n, context);
@@ -569,7 +571,7 @@ private static boolean isElementWrittenInsideNetwork(Identifiable<?> element, Ne
569571
}
570572

571573
private static boolean supportSubnetworksExport(NetworkSerializerContext context) {
572-
return context.getVersion().compareTo(IidmVersion.V_1_11) >= 0;
574+
return context.getVersion().compareTo(IidmVersion.V_1_11) >= 0 && !context.getOptions().isFlatten();
573575
}
574576

575577
private static NetworkSerializerContext createContext(Network n, ExportOptions options, TreeDataWriter writer) {

iidm/iidm-serde/src/test/java/com/powsybl/iidm/serde/ExportOptionsTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ void defaultExportOptionsTest() {
105105
testDefaultExportOptions(new ExportOptions(Boolean.TRUE, Boolean.TRUE, Boolean.FALSE, TopologyLevel.NODE_BREAKER, Boolean.FALSE, null));
106106
testDefaultExportOptions(new ExportOptions(Boolean.TRUE, Boolean.TRUE, Boolean.FALSE, TopologyLevel.NODE_BREAKER, Boolean.FALSE, Boolean.FALSE, null));
107107
testDefaultExportOptions(new ExportOptions(Boolean.TRUE, Boolean.TRUE, Boolean.FALSE, TopologyLevel.NODE_BREAKER, Boolean.FALSE, Boolean.FALSE, null, THROW_EXCEPTION));
108+
testDefaultExportOptions(new ExportOptions(Boolean.TRUE, Boolean.TRUE, Boolean.FALSE, TopologyLevel.NODE_BREAKER, Boolean.FALSE, Boolean.FALSE, null, THROW_EXCEPTION, Boolean.FALSE));
108109
}
109110

110111
private void testDefaultExportOptions(ExportOptions options) {
@@ -119,6 +120,7 @@ private void testDefaultExportOptions(ExportOptions options) {
119120
assertEquals(THROW_EXCEPTION, options.getIidmVersionIncompatibilityBehavior());
120121
assertEquals(StandardCharsets.UTF_8, options.getCharset());
121122
assertEquals(Boolean.TRUE, options.isWithAutomationSystems());
123+
assertEquals(Boolean.FALSE, options.isFlatten());
122124
}
123125

124126
}

iidm/iidm-serde/src/test/java/com/powsybl/iidm/serde/NetworkSerDeTest.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,4 +358,34 @@ void emptySourceFormatTest() {
358358
assertEquals("", readNetwork.getSourceFormat());
359359
});
360360
}
361+
362+
@Test
363+
void testExportWithFlatten() {
364+
Network n1 = createNetwork(1);
365+
Network n2 = createNetwork(2);
366+
Path xmlFile = tmpDir.resolve("flattenedNetwork.xml");
367+
368+
Network merged = Network.merge("Merged", n1, n2);
369+
370+
ExportOptions options = new ExportOptions().setFlatten(true);
371+
NetworkSerDe.write(merged, options, xmlFile);
372+
Network readNetwork = NetworkSerDe.validateAndRead(xmlFile);
373+
assertEquals(2, merged.getSubnetworks().size());
374+
assertEquals(0, readNetwork.getSubnetworks().size());
375+
}
376+
377+
@Test
378+
void testExportWithoutFlatten() {
379+
Network n1 = createNetwork(1);
380+
Network n2 = createNetwork(2);
381+
Path xmlFile = tmpDir.resolve("networkWithSubnetworks.xml");
382+
383+
Network merged = Network.merge("Merged", n1, n2);
384+
385+
ExportOptions options = new ExportOptions();
386+
NetworkSerDe.write(merged, options, xmlFile);
387+
Network readNetwork = NetworkSerDe.validateAndRead(xmlFile);
388+
assertEquals(2, merged.getSubnetworks().size());
389+
assertEquals(2, readNetwork.getSubnetworks().size());
390+
}
361391
}

iidm/iidm-serde/src/test/java/com/powsybl/iidm/serde/XMLExporterTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ void exportXiidmWithAnotherPrefixTest() throws IOException {
9191
@Test
9292
void paramsTest() {
9393
var xmlExporter = new XMLExporter();
94-
assertEquals(15, xmlExporter.getParameters().size());
94+
assertEquals(16, xmlExporter.getParameters().size());
9595
assertEquals("IIDM XML v" + CURRENT_IIDM_VERSION.toString(".") + " exporter", xmlExporter.getComment());
9696
}
9797

0 commit comments

Comments
 (0)