Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add parameter in NetworkSerDe.copy to choose between formats (xml, binary, etc.) #3132

Merged
merged 7 commits into from
Sep 24, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -874,14 +874,45 @@ public static Network copy(Network network, NetworkFactory networkFactory) {
}

public static Network copy(Network network, NetworkFactory networkFactory, ExecutorService executor) {
return copy(network, networkFactory, executor, TreeDataFormat.XML);
}

/**
* Deep copy of the network using the specified converter
*
* @param network the network to copy
* @param format the converter to use to export/import the network
* @return the copy of the network
*/
public static Network copy(Network network, TreeDataFormat format) {
return copy(network, NetworkFactory.findDefault(), format);
}

/**
* Deep copy of the network using the specified converter
*
* @param network the network to copy
* @param networkFactory the network factory to use for the copy
* @param format the converter to use to export/import the network
* @return the copy of the network
*/
public static Network copy(Network network, NetworkFactory networkFactory, TreeDataFormat format) {
return copy(network, networkFactory, ForkJoinPool.commonPool(), format);
}

public static Network copy(Network network, NetworkFactory networkFactory, ExecutorService executor, TreeDataFormat format) {
Objects.requireNonNull(network);
Objects.requireNonNull(networkFactory);
Objects.requireNonNull(executor);
ExportOptions exportOptions = new ExportOptions();
exportOptions.setFormat(format);
ImportOptions importOptions = new ImportOptions();
importOptions.setFormat(format);
PipedOutputStream pos = new PipedOutputStream();
try (InputStream is = new PipedInputStream(pos)) {
executor.execute(() -> {
try {
write(network, pos);
write(network, exportOptions, pos);
} catch (Exception t) {
LOGGER.error(t.toString(), t);
} finally {
Expand All @@ -892,7 +923,7 @@ public static Network copy(Network network, NetworkFactory networkFactory, Execu
}
}
});
return read(is, new ImportOptions(), null, networkFactory, ReportNode.NO_OP);
return read(is, importOptions, null, networkFactory, ReportNode.NO_OP);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import com.powsybl.commons.extensions.ExtensionSerDe;
import com.powsybl.commons.io.DeserializerContext;
import com.powsybl.commons.io.SerializerContext;
import com.powsybl.commons.io.TreeDataFormat;
import com.powsybl.commons.report.ReportNode;
import com.powsybl.commons.test.TestUtil;
import com.powsybl.iidm.network.*;
Expand Down Expand Up @@ -116,6 +117,21 @@ void testGzipGunzip() throws IOException {
assertArrayEquals(Files.readAllBytes(file1), Files.readAllBytes(file2));
}

@Test
void testCopyFormat() throws IOException {
Network network = createEurostagTutorialExample1();
Path file1 = tmpDir.resolve("n.xml");
NetworkSerDe.write(network, file1);
Network network2 = NetworkSerDe.copy(network);
Path file2 = tmpDir.resolve("n2.xml");
NetworkSerDe.write(network2, file2);
assertArrayEquals(Files.readAllBytes(file1), Files.readAllBytes(file2));
Network network3 = NetworkSerDe.copy(network, TreeDataFormat.BIN);
Path file3 = tmpDir.resolve("n3.xml");
NetworkSerDe.write(network3, file3);
assertArrayEquals(Files.readAllBytes(file1), Files.readAllBytes(file3));
}

@AutoService(ExtensionSerDe.class)
public static class BusbarSectionExtSerDe extends AbstractExtensionSerDe<BusbarSection, BusbarSectionExt> {

Expand Down