Skip to content

Commit

Permalink
Add parameter in NetworkSerDe.copy to choose between formats (xml, bi…
Browse files Browse the repository at this point in the history
…nary, etc.) (#3132)

* use binary format in NetworkSerDe.copy
* revert default format to XML + add format as new copy parameter
* inline Export/Import parameters + add and use assertTxtEquals(Path expected, Path actual)
* make JSON the default format for copy

Signed-off-by: Nicolas Rol <nicolas.rol@rte-france.com>
  • Loading branch information
rolnico authored Sep 24, 2024
1 parent d7bdbb4 commit 465eab1
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;

import static org.junit.jupiter.api.Assertions.*;
Expand All @@ -38,6 +40,15 @@ public static void assertXmlEquals(InputStream expected, InputStream actual) {
assertFalse(hasDiff);
}

public static void assertTxtEquals(Path expected, Path actual) {
try (InputStream expectedStream = Files.newInputStream(expected);
InputStream actualStream = Files.newInputStream(actual)) {
assertTxtEquals(expectedStream, actualStream);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}

public static void assertTxtEquals(InputStream expected, InputStream actual) {
try {
assertTxtEquals(expected, new String(ByteStreams.toByteArray(actual), StandardCharsets.UTF_8));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -874,14 +874,41 @@ public static Network copy(Network network, NetworkFactory networkFactory) {
}

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

/**
* 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);
PipedOutputStream pos = new PipedOutputStream();
try (InputStream is = new PipedInputStream(pos)) {
executor.execute(() -> {
try {
write(network, pos);
write(network, new ExportOptions().setFormat(format), pos);
} catch (Exception t) {
LOGGER.error(t.toString(), t);
} finally {
Expand All @@ -892,7 +919,7 @@ public static Network copy(Network network, NetworkFactory networkFactory, Execu
}
}
});
return read(is, new ImportOptions(), null, networkFactory, ReportNode.NO_OP);
return read(is, new ImportOptions().setFormat(format), 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 All @@ -28,6 +29,7 @@
import java.nio.file.Path;
import java.time.ZonedDateTime;

import static com.powsybl.commons.test.ComparisonUtils.assertTxtEquals;
import static com.powsybl.iidm.serde.IidmSerDeConstants.CURRENT_IIDM_VERSION;
import static org.junit.jupiter.api.Assertions.*;

Expand Down Expand Up @@ -116,6 +118,21 @@ void testGzipGunzip() throws IOException {
assertArrayEquals(Files.readAllBytes(file1), Files.readAllBytes(file2));
}

@Test
void testCopyFormat() {
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);
assertTxtEquals(file1, file2);
Network network3 = NetworkSerDe.copy(network, TreeDataFormat.BIN);
Path file3 = tmpDir.resolve("n3.xml");
NetworkSerDe.write(network3, file3);
assertTxtEquals(file1, file3);
}

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

Expand Down

0 comments on commit 465eab1

Please sign in to comment.