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

Subnetworks XIIDM export #2678

Merged
merged 15 commits into from
Sep 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,12 @@ protected <T> Path writeTest(T data, BiConsumer<T, Path> out, BiConsumer<InputSt
}

protected <T> T roundTripTest(T data, BiConsumer<T, Path> out, Function<Path, T> in, BiConsumer<InputStream, InputStream> compare, String ref) throws IOException {
// Export the data and check the result with the reference
Path xmlFile = writeTest(data, out, compare, ref);
try (InputStream is1 = Files.newInputStream(xmlFile)) {
compare.accept(getClass().getResourceAsStream(ref), is1);
}
// Read the exported data, export the retrieved data and check the result with the reference
T data2 = in.apply(xmlFile);
Path xmlFile2 = tmpDir.resolve("data2");
out.accept(data2, xmlFile2);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,7 @@
import com.powsybl.commons.PowsyblException;
import com.powsybl.commons.util.ServiceLoaderCache;

import java.util.Collection;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.*;
import java.util.stream.Collectors;

/**
Expand Down Expand Up @@ -48,7 +45,8 @@ private ExtensionProviders(Class<T> clazz, String categoryName, Set<String> exte
Objects.requireNonNull(clazz);
Objects.requireNonNull(categoryName);

providers = new ServiceLoaderCache<>(clazz).getServices().stream()
List<T> services = new ServiceLoaderCache<>(clazz).getServices();
providers = services.stream()
.filter(s -> s.getCategoryName().equals(categoryName) && (extensionNames == null || extensionNames.contains(s.getExtensionName())))
.collect(Collectors.toMap(T::getExtensionName, e -> e));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ protected String getTypeDescription() {
* @param to the destination network
*/
protected static void transferExtensions(Network from, Network to) {
// Only well-defined extensions (with an interface) are transferred
new ArrayList<>(from.getExtensions())
.forEach(e -> Arrays.stream(e.getClass().getInterfaces())
.filter(c -> Objects.nonNull(from.getExtension(c)))
Expand Down

Large diffs are not rendered by default.

66 changes: 33 additions & 33 deletions iidm/iidm-xml-converter/src/main/resources/xsd/iidm_V1_11.xsd
Original file line number Diff line number Diff line change
Expand Up @@ -17,39 +17,39 @@
<xs:minLength value='1'/>
</xs:restriction>
</xs:simpleType>
<xs:element name="network">
<xs:complexType>
<xs:sequence>
<xs:element name="alias" type="iidm:Alias" minOccurs="0" maxOccurs="unbounded" />
<xs:element name="property" type="iidm:Property" minOccurs="0" maxOccurs="unbounded" />
<xs:element name="voltageLevel" type="iidm:VoltageLevel" minOccurs="0" maxOccurs="unbounded"/>
<xs:element name="substation" type="iidm:Substation" minOccurs="0" maxOccurs="unbounded"/>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="twoWindingsTransformer" type="iidm:TwoWindingsTransformer"/>
<xs:element name="threeWindingsTransformer" type="iidm:ThreeWindingsTransformer"/>
</xs:choice>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="line" type="iidm:Line"/>
<xs:element name="tieLine" type="iidm:TieLine"/>
<xs:element name="hvdcLine" type="iidm:HvdcLine"/>
</xs:choice>
<xs:element name="voltageAngleLimit" minOccurs="0" maxOccurs="unbounded" type="iidm:VoltageAngleLimit"/>
<xs:element name="extension" minOccurs="0" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:any minOccurs="1" maxOccurs="unbounded"/>
</xs:sequence>
<xs:attribute name="id" use="required" type="xs:string"/>
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="id" use="required" type="iidm:nonEmptyString"/>
<xs:attribute name="caseDate" use="required" type="xs:dateTime"/>
<xs:attribute name="forecastDistance" use="required" type="xs:int"/>
<xs:attribute name="sourceFormat" use="required" type="iidm:nonEmptyString"/>
<xs:attribute name="minimumValidationLevel" use="required" type="iidm:nonEmptyString"/>
</xs:complexType>
</xs:element>
<xs:element name="network" type="iidm:Network"/>
<xs:complexType name="Network">
<xs:sequence>
<xs:element name="alias" type="iidm:Alias" minOccurs="0" maxOccurs="unbounded" />
<xs:element name="property" type="iidm:Property" minOccurs="0" maxOccurs="unbounded" />
<xs:element name="network" type="iidm:Network" minOccurs="0" maxOccurs="unbounded" />
<xs:element name="voltageLevel" type="iidm:VoltageLevel" minOccurs="0" maxOccurs="unbounded"/>
<xs:element name="substation" type="iidm:Substation" minOccurs="0" maxOccurs="unbounded"/>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="twoWindingsTransformer" type="iidm:TwoWindingsTransformer"/>
<xs:element name="threeWindingsTransformer" type="iidm:ThreeWindingsTransformer"/>
</xs:choice>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="line" type="iidm:Line"/>
<xs:element name="tieLine" type="iidm:TieLine"/>
<xs:element name="hvdcLine" type="iidm:HvdcLine"/>
</xs:choice>
<xs:element name="voltageAngleLimit" minOccurs="0" maxOccurs="unbounded" type="iidm:VoltageAngleLimit"/>
<xs:element name="extension" minOccurs="0" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:any minOccurs="1" maxOccurs="unbounded"/>
</xs:sequence>
<xs:attribute name="id" use="required" type="xs:string"/>
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="id" use="required" type="iidm:nonEmptyString"/>
<xs:attribute name="caseDate" use="required" type="xs:dateTime"/>
<xs:attribute name="forecastDistance" use="required" type="xs:int"/>
<xs:attribute name="sourceFormat" use="required" type="iidm:nonEmptyString"/>
<xs:attribute name="minimumValidationLevel" use="required" type="iidm:nonEmptyString"/>
</xs:complexType>
<xs:complexType name="Identifiable">
<xs:sequence>
<xs:element name="alias" type="iidm:Alias" minOccurs="0" maxOccurs="unbounded" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,38 +17,38 @@
<xs:minLength value='1'/>
</xs:restriction>
</xs:simpleType>
<xs:element name="network">
<xs:complexType>
<xs:sequence>
<xs:element name="alias" type="iidm:Alias" minOccurs="0" maxOccurs="unbounded" />
<xs:element name="property" type="iidm:Property" minOccurs="0" maxOccurs="unbounded" />
<xs:element name="voltageLevel" type="iidm:VoltageLevel" minOccurs="0" maxOccurs="unbounded"/>
<xs:element name="substation" type="iidm:Substation" minOccurs="0" maxOccurs="unbounded"/>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="twoWindingsTransformer" type="iidm:TwoWindingsTransformer"/>
<xs:element name="threeWindingsTransformer" type="iidm:ThreeWindingsTransformer"/>
</xs:choice>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="line" type="iidm:Line"/>
<xs:element name="tieLine" type="iidm:TieLine"/>
<xs:element name="hvdcLine" type="iidm:HvdcLine"/>
</xs:choice>
<xs:element name="extension" minOccurs="0" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:any minOccurs="1" maxOccurs="unbounded"/>
</xs:sequence>
<xs:attribute name="id" use="required" type="xs:string"/>
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="id" use="required" type="iidm:nonEmptyString"/>
<xs:attribute name="caseDate" use="required" type="xs:dateTime"/>
<xs:attribute name="forecastDistance" use="required" type="xs:int"/>
<xs:attribute name="sourceFormat" use="required" type="iidm:nonEmptyString"/>
<xs:attribute name="minimumValidationLevel" use="required" type="iidm:nonEmptyString"/>
</xs:complexType>
</xs:element>
<xs:element name="network" type="iidm:Network"/>
<xs:complexType name="Network">
<xs:sequence>
<xs:element name="alias" type="iidm:Alias" minOccurs="0" maxOccurs="unbounded" />
<xs:element name="property" type="iidm:Property" minOccurs="0" maxOccurs="unbounded" />
<xs:element name="network" type="iidm:Network" minOccurs="0" maxOccurs="unbounded" />
<xs:element name="voltageLevel" type="iidm:VoltageLevel" minOccurs="0" maxOccurs="unbounded"/>
<xs:element name="substation" type="iidm:Substation" minOccurs="0" maxOccurs="unbounded"/>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="twoWindingsTransformer" type="iidm:TwoWindingsTransformer"/>
<xs:element name="threeWindingsTransformer" type="iidm:ThreeWindingsTransformer"/>
</xs:choice>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="line" type="iidm:Line"/>
<xs:element name="tieLine" type="iidm:TieLine"/>
<xs:element name="hvdcLine" type="iidm:HvdcLine"/>
</xs:choice>
<xs:element name="extension" minOccurs="0" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:any minOccurs="1" maxOccurs="unbounded"/>
</xs:sequence>
<xs:attribute name="id" use="required" type="xs:string"/>
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="id" use="required" type="iidm:nonEmptyString"/>
<xs:attribute name="caseDate" use="required" type="xs:dateTime"/>
<xs:attribute name="forecastDistance" use="required" type="xs:int"/>
<xs:attribute name="sourceFormat" use="required" type="iidm:nonEmptyString"/>
<xs:attribute name="minimumValidationLevel" use="required" type="iidm:nonEmptyString"/>
</xs:complexType>
<xs:complexType name="Identifiable">
<xs:sequence>
<xs:element name="alias" type="iidm:Alias" minOccurs="0" maxOccurs="unbounded" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,19 @@
*/
package com.powsybl.iidm.xml;

import com.google.auto.service.AutoService;
import com.powsybl.commons.PowsyblException;
import com.powsybl.commons.datasource.MemDataSource;
import com.powsybl.commons.extensions.AbstractExtension;
import com.powsybl.commons.extensions.AbstractExtensionXmlSerializer;
import com.powsybl.commons.extensions.ExtensionXmlSerializer;
import com.powsybl.commons.xml.XmlReaderContext;
import com.powsybl.commons.xml.XmlWriterContext;
import com.powsybl.iidm.network.Load;
import com.powsybl.iidm.network.Network;
import com.powsybl.iidm.network.test.EurostagTutorialExample1Factory;
import com.powsybl.iidm.network.test.LoadZipModel;
import com.powsybl.iidm.network.test.MultipleExtensionsTestNetworkFactory;
import com.powsybl.iidm.network.test.TerminalMockExt;
import com.powsybl.iidm.xml.extensions.util.NetworkSourceExtension;
import com.powsybl.iidm.xml.extensions.util.NetworkSourceExtensionImpl;
import org.junit.jupiter.api.Test;

import javax.xml.stream.XMLStreamException;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
Expand Down Expand Up @@ -115,50 +111,11 @@ void testExtensionWithoutSerializerDoNotThrowException() throws IOException {
}
}

// Define a network extension with XML serializer
static class NetworkSourceExtension extends AbstractExtension<Network> {

NetworkSourceExtension(String sourceData) {
this.sourceData = sourceData;
}

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

String getSourceData() {
return sourceData;
}

private final String sourceData;
}

@AutoService(ExtensionXmlSerializer.class)
public static class NetworkSourceExtensionXmlSerializer extends AbstractExtensionXmlSerializer<Network, NetworkSourceExtension> {

public NetworkSourceExtensionXmlSerializer() {
super("networkSource", "network", NetworkSourceExtension.class, false, "networkSource.xsd",
"http://www.itesla_project.eu/schema/iidm/ext/networksource/1_0", "extNetworkSource");
}

@Override
public void write(NetworkSourceExtension networkSource, XmlWriterContext context) throws XMLStreamException {
context.getWriter().writeAttribute("sourceData", networkSource.getSourceData());
}

@Override
public NetworkSourceExtension read(Network network, XmlReaderContext context) {
String sourceData = context.getReader().getAttributeValue(null, "sourceData");
return new NetworkSourceExtension(sourceData);
}
}

@Test
void testNetworkSourceExtension() throws IOException {
Network network = EurostagTutorialExample1Factory.create();
String sourceData = "eurostag-tutorial-example1-created-from-IIDM-API";
NetworkSourceExtension source = new NetworkSourceExtension(sourceData);
NetworkSourceExtension source = new NetworkSourceExtensionImpl(sourceData);
network.addExtension(NetworkSourceExtension.class, source);
byte[] buffer;
try (ByteArrayOutputStream os = new ByteArrayOutputStream()) {
Expand Down
Loading