Skip to content

Commit

Permalink
TerminalRefXml with writer param (#815)
Browse files Browse the repository at this point in the history
Signed-off-by: RALAMBOTIANA MIORA <miora.ralambotiana@rte-france.com>
Signed-off-by: Mathieu BAGUE <mathieu.bague@rte-france.com>
  • Loading branch information
miovd authored and Mathieu BAGUE committed Jun 19, 2019
1 parent f5ed001 commit 204e081
Show file tree
Hide file tree
Showing 13 changed files with 298 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -384,4 +384,15 @@ public static Network createWithMultipleConnectedComponents() {
return network;
}

public static Network createWithTerminalMockExt() {
Network network = create();
network.setCaseDate(DateTime.parse("2013-01-15T18:45:00.000+01:00"));

Load load = network.getLoad("LOAD");
TerminalMockExt terminalMockExt = new TerminalMockExt(load);
load.addExtension(TerminalMockExt.class, terminalMockExt);

return network;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/**
* 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/.
*/
package com.powsybl.iidm.network.test;

import com.powsybl.commons.extensions.AbstractExtension;
import com.powsybl.iidm.network.Load;
import com.powsybl.iidm.network.Terminal;

import java.util.Objects;

/**
* @author Miora Ralambotiana <miora.ralambotiana at rte-france.com>
*/
public class TerminalMockExt extends AbstractExtension<Load> {

private Terminal terminal;

public TerminalMockExt(Load load) {
super(load);
terminal = load.getTerminal();
}

@Override
public String getName() {
return "terminalMock";
}

public Terminal getTerminal() {
return terminal;
}

public TerminalMockExt setTerminal(Terminal terminal) {
this.terminal = Objects.requireNonNull(terminal);
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import com.powsybl.iidm.network.*;

import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamWriter;

import static com.powsybl.iidm.xml.IidmXmlConstants.IIDM_URI;

Expand All @@ -24,21 +25,25 @@ public static void writeTerminalRef(Terminal t, NetworkXmlWriterContext context,
}

public static void writeTerminalRef(Terminal t, NetworkXmlWriterContext context, String namespace, String elementName) throws XMLStreamException {
writeTerminalRef(t, context, namespace, elementName, context.getWriter());
}

public static void writeTerminalRef(Terminal t, NetworkXmlWriterContext context, String namespace, String elementName, XMLStreamWriter writer) throws XMLStreamException {
Connectable c = t.getConnectable();
if (!context.getFilter().test(c)) {
throw new PowsyblException("Oups, terminal ref point to a filtered equipment " + c.getId());
}
context.getWriter().writeEmptyElement(namespace, elementName);
context.getWriter().writeAttribute("id", context.getAnonymizer().anonymizeString(c.getId()));
writer.writeEmptyElement(namespace, elementName);
writer.writeAttribute("id", context.getAnonymizer().anonymizeString(c.getId()));
if (c.getTerminals().size() > 1) {
if (c instanceof Injection) {
// nothing to do
} else if (c instanceof Branch) {
Branch branch = (Branch) c;
context.getWriter().writeAttribute("side", branch.getSide(t).name());
writer.writeAttribute("side", branch.getSide(t).name());
} else if (c instanceof ThreeWindingsTransformer) {
ThreeWindingsTransformer twt = (ThreeWindingsTransformer) c;
context.getWriter().writeAttribute("side", twt.getSide(t).name());
writer.writeAttribute("side", twt.getSide(t).name());
} else {
throw new AssertionError("Unexpected Connectable instance: " + c.getClass());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,7 @@

import javax.xml.stream.XMLStreamException;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.*;

/**
* @author Geoffroy Jamgotchian <geoffroy.jamgotchian at rte-france.com>
Expand Down Expand Up @@ -199,4 +197,16 @@ public void testNetworkSourceExtension() throws IOException {
assertEquals(sourceData, source2.getSourceData());
}
}

@Test
public void testTerminalExtension() throws IOException {
Network network2 = roundTripXmlTest(EurostagTutorialExample1Factory.createWithTerminalMockExt(),
NetworkXml::writeAndValidate,
NetworkXml::read,
"/eurostag-tutorial-example1-with-terminalMock-ext.xml");
Load loadXml = network2.getLoad("LOAD");
TerminalMockExt terminalMockExtXml = loadXml.getExtension(TerminalMockExt.class);
assertNotNull(terminalMockExtXml);
assertSame(loadXml.getTerminal(), terminalMockExtXml.getTerminal());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,13 @@ public String getNamespacePrefix() {

@Override
public void write(LoadZipModel zipModel, XmlWriterContext context) throws XMLStreamException {
XmlUtil.writeDouble("a1", zipModel.getA1(), context.getWriter());
XmlUtil.writeDouble("a2", zipModel.getA2(), context.getWriter());
XmlUtil.writeDouble("a3", zipModel.getA3(), context.getWriter());
XmlUtil.writeDouble("a4", zipModel.getA4(), context.getWriter());
XmlUtil.writeDouble("a5", zipModel.getA5(), context.getWriter());
XmlUtil.writeDouble("a6", zipModel.getA6(), context.getWriter());
XmlUtil.writeDouble("v0", zipModel.getV0(), context.getWriter());
XmlUtil.writeDouble("a1", zipModel.getA1(), context.getExtensionsWriter());
XmlUtil.writeDouble("a2", zipModel.getA2(), context.getExtensionsWriter());
XmlUtil.writeDouble("a3", zipModel.getA3(), context.getExtensionsWriter());
XmlUtil.writeDouble("a4", zipModel.getA4(), context.getExtensionsWriter());
XmlUtil.writeDouble("a5", zipModel.getA5(), context.getExtensionsWriter());
XmlUtil.writeDouble("a6", zipModel.getA6(), context.getExtensionsWriter());
XmlUtil.writeDouble("v0", zipModel.getV0(), context.getExtensionsWriter());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/**
* 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/.
*/
package com.powsybl.iidm.xml;

import com.google.auto.service.AutoService;
import com.powsybl.commons.extensions.ExtensionXmlSerializer;
import com.powsybl.commons.xml.XmlReaderContext;
import com.powsybl.commons.xml.XmlUtil;
import com.powsybl.commons.xml.XmlWriterContext;
import com.powsybl.iidm.network.Load;
import com.powsybl.iidm.network.Network;
import com.powsybl.iidm.network.test.TerminalMockExt;

import javax.xml.stream.XMLStreamException;
import java.io.InputStream;

/**
* @author Miora Ralambotiana <miora.ralambotiana at rte-france.com>
*/
@AutoService(ExtensionXmlSerializer.class)
public class TerminalMockXmlSerializer implements ExtensionXmlSerializer<Load, TerminalMockExt> {

@Override
public boolean hasSubElements() {
return true;
}

@Override
public InputStream getXsdAsStream() {
return getClass().getResourceAsStream("/xsd/terminalMock.xsd");
}

@Override
public String getNamespaceUri() {
return "http://www.itesla_project.eu/schema/iidm/ext/terminal_mock/1_0";
}

@Override
public String getNamespacePrefix() {
return "mock";
}

@Override
public void write(TerminalMockExt extension, XmlWriterContext context) throws XMLStreamException {
NetworkXmlWriterContext networkContext = (NetworkXmlWriterContext) context;
TerminalRefXml.writeTerminalRef(extension.getTerminal(), networkContext, getNamespaceUri(), "terminal", context.getExtensionsWriter());
}

@Override
public TerminalMockExt read(Load extendable, XmlReaderContext context) throws XMLStreamException {
NetworkXmlReaderContext networkContext = (NetworkXmlReaderContext) context;
TerminalMockExt terminalMockExt = new TerminalMockExt(extendable);
XmlUtil.readUntilEndElement(getExtensionName(), networkContext.getReader(), () -> {
if (networkContext.getReader().getLocalName().equals("terminal")) {
String id = networkContext.getAnonymizer().deanonymizeString(networkContext.getReader().getAttributeValue(null, "id"));
String side = networkContext.getReader().getAttributeValue(null, "side");
networkContext.getEndTasks().add(() -> {
Network network = extendable.getTerminal().getVoltageLevel().getSubstation().getNetwork();
terminalMockExt.setTerminal(TerminalRefXml.readTerminalRef(network, id, side));
});
} else {
throw new AssertionError("Unexpected element: " + networkContext.getReader().getLocalName());
}
});
return terminalMockExt;
}

@Override
public String getExtensionName() {
return "terminalMock";
}

@Override
public String getCategoryName() {
return "network";
}

@Override
public Class<? super TerminalMockExt> getExtensionClass() {
return TerminalMockExt.class;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,9 @@
/**
* @author Chamseddine BENHAMED <chamseddine.benhamed at rte-france.com>
*/
public class XMLImporterExporterBaseEXtensionsTest extends AbstractConverterTest {

public void importExport(String xiidmBaseRef, String xiidmExtRef) throws IOException {
public class XMLImporterExporterBaseExtensionsTest extends AbstractConverterTest {

private void importExport(String xiidmBaseRef, String xiidmExtRef) throws IOException {
Properties exportProperties = new Properties();
exportProperties.put(XMLExporter.EXPORT_MODE, String.valueOf(IidmImportExportMode.EXTENSIONS_IN_ONE_SEPARATED_FILE));
List<String> exportExtensionsList = Arrays.asList("loadFoo", "loadBar");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@

public class XmlExporterBaseExtensionsTest extends AbstractConverterTest {

public void exporterTestBaseExtensions(Network network, String xiidmBaseRef, String xiidmExtRef) throws IOException {
private void exporterTestBaseExtensions(Network network, String xiidmBaseRef, String xiidmExtRef) throws IOException {
Properties exportProperties = new Properties();
exportProperties.put(XMLExporter.ANONYMISED, "false");
exportProperties.put(XMLExporter.EXPORT_MODE, String.valueOf(IidmImportExportMode.EXTENSIONS_IN_ONE_SEPARATED_FILE));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import com.powsybl.commons.datasource.MemDataSource;
import com.powsybl.iidm.IidmImportExportMode;
import com.powsybl.iidm.network.Network;
import com.powsybl.iidm.network.test.EurostagTutorialExample1Factory;
import com.powsybl.iidm.network.test.MultipleExtensionsTestNetworkFactory;
import org.junit.Test;

Expand All @@ -26,18 +27,23 @@

public class XmlExporterBaseOneExtensionPerFileTest extends AbstractConverterTest {

public void exporterOneFilePerExtensionType(Network network, String xiidmBaseRef, List<String> extensionsList) throws IOException {

private MemDataSource export(Network network, List<String> extensionsList) {
Properties properties = new Properties();
properties.put(XMLExporter.EXPORT_MODE, String.valueOf(IidmImportExportMode.ONE_SEPARATED_FILE_PER_EXTENSION_TYPE));
properties.put(XMLExporter.EXTENSIONS_LIST, extensionsList);

MemDataSource dataSource = new MemDataSource();
new XMLExporter(platformConfig).export(network, properties, dataSource);
return dataSource;
}

private void exporterOneFilePerExtensionType(Network network, List<String> extensionsList) throws IOException {
MemDataSource dataSource = export(network, extensionsList);

// check the base exported file and compare it to iidmBaseRef reference file
try (InputStream is = new ByteArrayInputStream(dataSource.getData("", "xiidm"))) {
assertNotNull(is);
compareXml(getClass().getResourceAsStream(xiidmBaseRef), is);
compareXml(getClass().getResourceAsStream("/multiple-extensions.xiidm"), is);
}

try (InputStream is = new ByteArrayInputStream(dataSource.getData("-loadBar.xiidm"))) {
Expand All @@ -53,8 +59,8 @@ public void exporterOneFilePerExtensionType(Network network, String xiidmBaseRef

@Test(expected = NullPointerException.class)
public void exportOneExtensionTypeTest() throws IOException {
List<String> extensionsList = Arrays.asList("loadBar");
exporterOneFilePerExtensionType(MultipleExtensionsTestNetworkFactory.create(), "/multiple-extensions.xiidm", extensionsList);
List<String> extensionsList = Collections.singletonList("loadBar");
exporterOneFilePerExtensionType(MultipleExtensionsTestNetworkFactory.create(), extensionsList);
}

@Test
Expand All @@ -68,6 +74,21 @@ public void getIdentifiablesPerExtensionTypeTest() {
@Test
public void exportAllExtensionsTest() throws IOException {
List<String> extensionsList = Arrays.asList("loadFoo", "loadBar");
exporterOneFilePerExtensionType(MultipleExtensionsTestNetworkFactory.create(), "/multiple-extensions.xiidm", extensionsList);
exporterOneFilePerExtensionType(MultipleExtensionsTestNetworkFactory.create(), extensionsList);
}

@Test
public void exportTerminalExtTest() throws IOException {
MemDataSource dataSource = export(EurostagTutorialExample1Factory.createWithTerminalMockExt(), Collections.singletonList("terminalMock"));

try (InputStream is = new ByteArrayInputStream(dataSource.getData("", "xiidm"))) {
assertNotNull(is);
compareXml(getClass().getResourceAsStream("/eurostag-tutorial-example1.xml"), is);
}

try (InputStream is = new ByteArrayInputStream(dataSource.getData("-terminalMock.xiidm"))) {
assertNotNull(is);
compareXml(getClass().getResourceAsStream("/eurostag-tutorial-example1-terminalMock.xml"), is);
}
}
}
Loading

0 comments on commit 204e081

Please sign in to comment.