-
Notifications
You must be signed in to change notification settings - Fork 43
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
CGMES : Implement a pre-processor that completes the CGMES model with missing containers before starting the conversion #2463
Merged
annetill
merged 30 commits into
main
from
cgmes_pre_processor_define_missing_containers
Jun 15, 2023
Merged
Changes from 29 commits
Commits
Show all changes
30 commits
Select commit
Hold shift + click to select a range
c187c89
CGMES : Implement a pre-processor that completes the CGMES model with…
zamarrenolm 6bd2386
Merge branch 'main' into cgmes_pre_processor_define_missing_containers
zamarrenolm 8c99a09
separate module for CGMES completion post-processors
zamarrenolm d503a19
update comment
zamarrenolm 00ec877
Merge branch 'main' into cgmes_pre_processor_define_missing_containers
zamarrenolm 1685aeb
Merge branch 'main' into cgmes_pre_processor_define_missing_containers
zamarrenolm d0946f5
Merge branch 'main' into cgmes_pre_processor_define_missing_containers
zamarrenolm 3e9da15
accept parameter for output folder, where files with fixes will be wr…
zamarrenolm 98ce590
Merge branch 'main' into cgmes_pre_processor_define_missing_containers
zamarrenolm 6827a2a
write EQ instance file with missing data
zamarrenolm 11f2bb4
comment about fictitious substation from equipment export
zamarrenolm 378ff5c
fix comment
zamarrenolm 9c9ce21
Merge branch 'main' into cgmes_pre_processor_define_missing_containers
zamarrenolm 0f938b0
create temp folder in user.dir to fix Sonar security risk java:S5443
zamarrenolm 40862eb
fix sonar code smell
zamarrenolm 248b56c
add cgmes completion module to distribution
zamarrenolm 71c41ce
Merge branch 'main' into cgmes_pre_processor_define_missing_containers
zamarrenolm 02af044
changes after review
zamarrenolm 049104f
Merge branch 'main' into cgmes_pre_processor_define_missing_containers
zamarrenolm 349b00e
Merge branch 'main' into cgmes_pre_processor_define_missing_containers
zamarrenolm 870bba6
Merge branch 'main' into cgmes_pre_processor_define_missing_containers
zamarrenolm 80e1484
update pom.xml
zamarrenolm cd2c56f
Merge branch 'main' into cgmes_pre_processor_define_missing_containers
zamarrenolm 8689908
Merge branch 'main' into cgmes_pre_processor_define_missing_containers
zamarrenolm d0f3ff3
Merge branch 'main' into cgmes_pre_processor_define_missing_containers
zamarrenolm f602a1b
changes after review
zamarrenolm e3bcf96
test output temporal files to Jimfs
zamarrenolm ee8b339
Merge branch 'main' into cgmes_pre_processor_define_missing_containers
zamarrenolm 0c0dac6
Merge branch 'main' into cgmes_pre_processor_define_missing_containers
zamarrenolm 927908e
add javadoc
zamarrenolm File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<!-- | ||
|
||
Copyright (c) 2023, 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/. | ||
SPDX-License-Identifier: MPL-2.0 | ||
|
||
--> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<parent> | ||
<groupId>com.powsybl</groupId> | ||
<artifactId>powsybl-cgmes</artifactId> | ||
<version>5.4.0-SNAPSHOT</version> | ||
</parent> | ||
|
||
<artifactId>powsybl-cgmes-completion</artifactId> | ||
<name>CGMES network model completion</name> | ||
<description>CGMES (Common Grid Model Exchange Specification). Complete missing data in input instance files | ||
</description> | ||
|
||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-jar-plugin</artifactId> | ||
<configuration> | ||
<archive> | ||
<manifestEntries> | ||
<Automatic-Module-Name>com.powsybl.cgmes.completion</Automatic-Module-Name> | ||
</manifestEntries> | ||
</archive> | ||
</configuration> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
|
||
<dependencies> | ||
<!-- Compilation dependencies --> | ||
<dependency> | ||
<groupId>org.slf4j</groupId> | ||
<artifactId>slf4j-api</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>${project.groupId}</groupId> | ||
<artifactId>powsybl-cgmes-conversion</artifactId> | ||
<version>${project.version}</version> | ||
</dependency> | ||
|
||
<!-- Test dependencies --> | ||
<dependency> | ||
<groupId>ch.qos.logback</groupId> | ||
<artifactId>logback-classic</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.google.jimfs</groupId> | ||
<artifactId>jimfs</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.junit.jupiter</groupId> | ||
<artifactId>junit-jupiter</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.slf4j</groupId> | ||
<artifactId>log4j-over-slf4j</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>${project.groupId}</groupId> | ||
<artifactId>powsybl-config-test</artifactId> | ||
<version>${project.version}</version> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>${project.groupId}</groupId> | ||
<artifactId>powsybl-cgmes-conformity</artifactId> | ||
<version>${project.version}</version> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>${project.groupId}</groupId> | ||
<artifactId>powsybl-iidm-impl</artifactId> | ||
<version>${project.version}</version> | ||
<scope>test</scope> | ||
</dependency> | ||
|
||
<!-- Test dependencies include all considered triple store engines --> | ||
<dependency> | ||
<groupId>${project.groupId}</groupId> | ||
<artifactId>powsybl-triple-store-impl-rdf4j</artifactId> | ||
<version>${project.version}</version> | ||
<scope>test</scope> | ||
</dependency> | ||
</dependencies> | ||
</project> |
245 changes: 245 additions & 0 deletions
245
...etion/src/main/java/com/powsybl/cgmes/completion/CreateMissingContainersPreProcessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,245 @@ | ||
/** | ||
* Copyright (c) 2023, 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/. | ||
* SPDX-License-Identifier: MPL-2.0 | ||
*/ | ||
package com.powsybl.cgmes.completion; | ||
|
||
import com.google.auto.service.AutoService; | ||
import com.powsybl.cgmes.conversion.CgmesImportPreProcessor; | ||
import com.powsybl.cgmes.conversion.export.CgmesExportContext; | ||
import com.powsybl.cgmes.conversion.export.CgmesExportUtil; | ||
import com.powsybl.cgmes.conversion.export.elements.*; | ||
import com.powsybl.cgmes.extensions.CgmesTopologyKind; | ||
import com.powsybl.cgmes.extensions.CimCharacteristicsAdder; | ||
import com.powsybl.cgmes.model.CgmesModel; | ||
import com.powsybl.cgmes.model.CgmesNames; | ||
import com.powsybl.cgmes.model.triplestore.CgmesModelTripleStore; | ||
import com.powsybl.commons.PowsyblException; | ||
import com.powsybl.commons.config.PlatformConfig; | ||
import com.powsybl.commons.datasource.ZipFileDataSource; | ||
import com.powsybl.commons.parameters.Parameter; | ||
import com.powsybl.commons.parameters.ParameterDefaultValueConfig; | ||
import com.powsybl.commons.parameters.ParameterType; | ||
import com.powsybl.commons.reporter.Reporter; | ||
import com.powsybl.commons.xml.XmlUtil; | ||
import com.powsybl.iidm.network.Network; | ||
import com.powsybl.iidm.network.NetworkFactory; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import javax.xml.stream.XMLStreamException; | ||
import javax.xml.stream.XMLStreamWriter; | ||
import java.io.IOException; | ||
import java.net.URI; | ||
import java.net.URISyntaxException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
import java.util.Objects; | ||
import java.util.Optional; | ||
import java.util.Set; | ||
import java.util.stream.Collectors; | ||
import java.util.zip.ZipEntry; | ||
import java.util.zip.ZipOutputStream; | ||
|
||
/** | ||
* @author Luma Zamarreño <zamarrenolm at aia.es> | ||
*/ | ||
@AutoService(CgmesImportPreProcessor.class) | ||
public class CreateMissingContainersPreProcessor implements CgmesImportPreProcessor { | ||
|
||
public static final String NAME = "createMissingContainers"; | ||
public static final String FIXES_FOLDER_NAME = "iidm.import.cgmes.fixes-for-missing-containers-folder"; | ||
public static final double DEFAULT_NOMINAL_VALUE_FOR_MISSING_VOLTAGE_LEVELS = 100.0; | ||
|
||
private static final Logger LOG = LoggerFactory.getLogger(CreateMissingContainersPreProcessor.class); | ||
private static final Parameter FIXES_FOLDER_NAME_PARAMETER = new Parameter(FIXES_FOLDER_NAME, | ||
ParameterType.STRING, | ||
"Folder where zip files containing fixes will be created: one zip for each imported network missing data", | ||
null); | ||
|
||
private final PlatformConfig platformConfig; | ||
private final ParameterDefaultValueConfig defaultValueConfig; | ||
|
||
public CreateMissingContainersPreProcessor(PlatformConfig platformConfig) { | ||
Objects.requireNonNull(platformConfig); | ||
this.platformConfig = platformConfig; | ||
defaultValueConfig = new ParameterDefaultValueConfig(platformConfig); | ||
} | ||
|
||
public CreateMissingContainersPreProcessor() { | ||
this(PlatformConfig.defaultConfig()); | ||
} | ||
|
||
private static String nameFor(CgmesModel cgmes) { | ||
try { | ||
return new URI(cgmes.getBasename()).getAuthority(); | ||
} catch (URISyntaxException e) { | ||
return cgmes.modelId(); | ||
} | ||
} | ||
|
||
private static void prepareAndReadFixesUsingFolder(CgmesModel cgmes, String basename, Path fixesFolder) { | ||
if (!Files.isDirectory(fixesFolder)) { | ||
LOG.error("Output folder is not a directory {}. Skipping post processor.", fixesFolder); | ||
return; | ||
} | ||
Path fixesFile = fixesFolder.resolve(basename + ".zip"); | ||
// Check the file will be writable | ||
try { | ||
Files.deleteIfExists(fixesFile); | ||
Files.createFile(fixesFile); | ||
} catch (IOException e) { | ||
LOG.error("Output file {} is not writable. Skipping post processor.", fixesFile); | ||
return; | ||
} | ||
if (LOG.isInfoEnabled()) { | ||
LOG.info("Execute {} pre processor on CGMES model {}. Output to file {}", NAME, cgmes.modelId(), fixesFile); | ||
} | ||
prepareAndReadFixesUsingZipFile(cgmes, basename, fixesFile); | ||
} | ||
|
||
private static void prepareAndReadFixesUsingZipFile(CgmesModel cgmes, String basename, Path fixesFile) { | ||
// Assume all containers missing are voltage levels and create proper objects for them (substation, regions, ...) | ||
Set<String> missingVoltageLevels = findMissingVoltageLevels(cgmes); | ||
LOG.info("Missing voltage levels: {}", missingVoltageLevels); | ||
if (!missingVoltageLevels.isEmpty()) { | ||
buildZipFileWithFixes(cgmes, missingVoltageLevels, fixesFile, basename); | ||
cgmes.read(new ZipFileDataSource(fixesFile), Reporter.NO_OP); | ||
} | ||
Set<String> missingVoltageLevelsAfterFix = findMissingVoltageLevels(cgmes); | ||
if (!missingVoltageLevelsAfterFix.isEmpty()) { | ||
throw new IllegalStateException("Missing voltage levels after fix: " + missingVoltageLevelsAfterFix); | ||
} | ||
// The only containers without voltage level must be of type line | ||
LOG.info("After the fixes have been applied, the only node containers without voltage level must be of type Line."); | ||
LOG.info("Containers without voltage level that are not Lines will be reported as errors."); | ||
cgmes.connectivityNodeContainers().stream() | ||
.filter(c -> c.getId(CgmesNames.VOLTAGE_LEVEL) == null) | ||
.filter(c -> !c.getLocal("connectivityNodeContainerType").equals("Line")) | ||
.forEach(c -> LOG.error(c.getId(CgmesNames.CONNECTIVITY_NODE_CONTAINER))); | ||
} | ||
|
||
private static Set<String> findMissingVoltageLevels(CgmesModel cgmes) { | ||
// check missing CN containers | ||
Set<String> defined = cgmes.connectivityNodeContainers().stream().map(c -> c.getId(CgmesNames.CONNECTIVITY_NODE_CONTAINER)).collect(Collectors.toSet()); | ||
Set<String> referred = cgmes.connectivityNodes().stream().map(c -> c.getId(CgmesNames.CONNECTIVITY_NODE_CONTAINER)).collect(Collectors.toSet()); | ||
return referred.stream().filter(c -> !defined.contains(c)).collect(Collectors.toSet()); | ||
} | ||
|
||
private static void buildZipFileWithFixes(CgmesModel cgmes, Set<String> missingVoltageLevels, Path fixesFile, String basename) { | ||
Network network = prepareEmptyNetworkForExport(cgmes); | ||
CgmesExportContext context = new CgmesExportContext(network); | ||
try (ZipOutputStream zout = new ZipOutputStream(Files.newOutputStream(fixesFile))) { | ||
zout.putNextEntry(new ZipEntry(basename + "_EQ.xml")); | ||
XMLStreamWriter writer = XmlUtil.initializeWriter(true, " ", zout); | ||
writeHeader(writer, context); | ||
RegionContainers regionContainers = writeRegionContainers(writer, context); | ||
for (String missingVoltageLevel : missingVoltageLevels) { | ||
writeMissingVoltageLevel(missingVoltageLevel, writer, context, regionContainers); | ||
} | ||
writer.writeEndDocument(); | ||
zout.closeEntry(); | ||
} catch (IOException | XMLStreamException x) { | ||
throw new PowsyblException("Building file containing fixes for missing data", x); | ||
} | ||
} | ||
|
||
private static Network prepareEmptyNetworkForExport(CgmesModel cgmes) { | ||
Network network = NetworkFactory.findDefault().createNetwork("empty", "CGMES"); | ||
// We ensure that the fixes are exported to CGMES files with the same version of the input files | ||
// To achieve it, we set the CIM characteristics of the empty Network created | ||
if (cgmes instanceof CgmesModelTripleStore) { | ||
network.newExtension(CimCharacteristicsAdder.class) | ||
.setTopologyKind(cgmes.isNodeBreaker() ? CgmesTopologyKind.NODE_BREAKER : CgmesTopologyKind.BUS_BRANCH) | ||
.setCimVersion(((CgmesModelTripleStore) cgmes).getCimVersion()) | ||
.add(); | ||
} | ||
return network; | ||
} | ||
|
||
private static RegionContainers writeRegionContainers(XMLStreamWriter writer, CgmesExportContext context) throws XMLStreamException { | ||
String cimNamespace = context.getCim().getNamespace(); | ||
|
||
// An alternative to replicate this code would be to make public the method | ||
// EquipmentExport::writeFictitiousSubstationFor and use it here. | ||
// We could group all missing voltage levels in the same (fictitious) substation | ||
RegionContainers regionContainers = new RegionContainers(); | ||
regionContainers.subGeographicalRegionId = CgmesExportUtil.getUniqueId(); | ||
String subGeographicalRegionName = "SGR fix for missing data"; | ||
regionContainers.geographicalRegionId = CgmesExportUtil.getUniqueId(); | ||
String geographicalRegionName = "GR fix for missing data"; | ||
SubGeographicalRegionEq.write(regionContainers.subGeographicalRegionId, subGeographicalRegionName, regionContainers.geographicalRegionId, cimNamespace, writer, context); | ||
GeographicalRegionEq.write(regionContainers.geographicalRegionId, geographicalRegionName, cimNamespace, writer, context); | ||
return regionContainers; | ||
} | ||
|
||
private static void writeMissingVoltageLevel(String voltageLevelId, XMLStreamWriter writer, CgmesExportContext context, RegionContainers regionContainers) throws XMLStreamException { | ||
String cimNamespace = context.getCim().getNamespace(); | ||
|
||
// In a first approach, | ||
// we do not have additional information about the voltage level, | ||
// we create a different substation and base voltage for every missing voltage level | ||
String voltageLevelName = voltageLevelId + " VL"; | ||
String substationId = CgmesExportUtil.getUniqueId(); | ||
String substationName = voltageLevelId + "SUB for missing VL " + voltageLevelId; | ||
String baseVoltageId = CgmesExportUtil.getUniqueId(); | ||
|
||
VoltageLevelEq.write(voltageLevelId, voltageLevelName, Double.NaN, Double.NaN, substationId, baseVoltageId, cimNamespace, writer, context); | ||
SubstationEq.write(substationId, substationName, regionContainers.subGeographicalRegionId, cimNamespace, writer, context); | ||
BaseVoltageEq.write(baseVoltageId, DEFAULT_NOMINAL_VALUE_FOR_MISSING_VOLTAGE_LEVELS, cimNamespace, writer, context); | ||
} | ||
|
||
private static void writeHeader(XMLStreamWriter writer, CgmesExportContext context) throws XMLStreamException { | ||
String cimNamespace = context.getCim().getNamespace(); | ||
String euNamespace = context.getCim().getEuNamespace(); | ||
CgmesExportUtil.writeRdfRoot(cimNamespace, context.getCim().getEuPrefix(), euNamespace, writer); | ||
if (context.getCimVersion() >= 16) { | ||
ModelDescriptionEq.write(writer, context.getEqModelDescription(), context); | ||
} | ||
} | ||
|
||
private Path getFixesFolder() { | ||
String fixesFolderName = Parameter.readString("CGMES", null, FIXES_FOLDER_NAME_PARAMETER, defaultValueConfig); | ||
if (fixesFolderName == null) { | ||
LOG.error("Executing {} pre processor. Missing the folder name for the output of files containing required fixes. Use the parameter {}.", NAME, FIXES_FOLDER_NAME_PARAMETER.getName()); | ||
return null; | ||
} | ||
Path fixesFolder = Paths.get(fixesFolderName); | ||
if (fixesFolder.isAbsolute()) { | ||
return fixesFolder; | ||
} else { | ||
Optional<Path> configDir = platformConfig.getConfigDir(); | ||
if (configDir.isPresent()) { | ||
return configDir.get().resolve(fixesFolderName); | ||
} else { | ||
LOG.error("Executing {} pre processor. The folder name for the output of files containing required fixes is a relative path ({}), but the platform config dir is empty.", NAME, fixesFolderName); | ||
return null; | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public String getName() { | ||
return NAME; | ||
} | ||
|
||
@Override | ||
public void process(CgmesModel cgmes) { | ||
Objects.requireNonNull(cgmes); | ||
String basename = nameFor(cgmes); | ||
|
||
Path fixesFolder = getFixesFolder(); | ||
if (fixesFolder != null) { | ||
prepareAndReadFixesUsingFolder(cgmes, basename, fixesFolder); | ||
} | ||
} | ||
|
||
private static class RegionContainers { | ||
String subGeographicalRegionId; | ||
String geographicalRegionId; | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe a (light) javadoc, just to explain what is done in this pre-processor?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
added a small javadoc