Skip to content

Commit

Permalink
[CGMES export] UUID of fullModel on each file should be unique (#2888)
Browse files Browse the repository at this point in the history
* CGMES post-processor to remove grounds (#2877)

Signed-off-by: Luma <zamarrenolm@aia.es>
Signed-off-by: Coline PILOQUET <coline.piloquet@rte-france.com>

* UUID of fullModel should be unique

Signed-off-by: Coline PILOQUET <coline.piloquet@rte-france.com>

* Revert "UUID of fullModel should be unique"

This reverts commit 7cbce1d.

Signed-off-by: Coline PILOQUET <coline.piloquet@rte-france.com>

* Add scenario time and version in CgmesObjectReference used to generate FullModel UUID

Signed-off-by: Coline PILOQUET <coline.piloquet@rte-france.com>

* [Shortcircuit API] Fix javadoc (#2887)

Signed-off-by: Coline PILOQUET <coline.piloquet@rte-france.com>

* Fix checkstyle

Signed-off-by: Coline PILOQUET <coline.piloquet@rte-france.com>

* unit test

Signed-off-by: Luma <zamarrenolm@aia.es>

* fix sonar issue

Signed-off-by: Luma <zamarrenolm@aia.es>

---------

Signed-off-by: Luma <zamarrenolm@aia.es>
Signed-off-by: Coline PILOQUET <coline.piloquet@rte-france.com>
Co-authored-by: Luma <zamarrenolm@aia.es>
(cherry picked from commit a22b33e)
  • Loading branch information
colinepiloquet authored and olperr1 committed Feb 15, 2024
1 parent a2bce56 commit c55ad6c
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,14 @@ public static void writeRdfRoot(String cimNamespace, String euPrefix, String euN

public static void writeModelDescription(Network network, CgmesSubset subset, XMLStreamWriter writer, ModelDescription modelDescription, CgmesExportContext context) throws XMLStreamException {
// The ref to build a unique model id must contain:
// the network, the subset (EQ, SSH, SV, ...), and the FULL_MODEL part
// the network, the subset (EQ, SSH, SV, ...), the time of the scenario, the version and the FULL_MODEL part
// If we use name-based UUIDs this ensures that the UUID for the model will be specific enough
CgmesObjectReference[] modelRef = {refTyped(network), ref(subset), Part.FULL_MODEL};
CgmesObjectReference[] modelRef = {
refTyped(network),
ref(subset),
ref(DATE_TIME_FORMATTER.format(context.getScenarioTime())),
ref(format(modelDescription.getVersion())),
Part.FULL_MODEL};
String modelId = "urn:uuid:" + context.getNamingStrategy().getCgmesId(modelRef);
modelDescription.setIds(modelId);
context.updateDependencies();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/**
* Copyright (c) 2024, 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.conversion.test.export.issues;

import com.powsybl.cgmes.conversion.CgmesExport;
import com.powsybl.cgmes.extensions.CgmesSshMetadataAdder;
import com.powsybl.commons.PowsyblException;
import com.powsybl.commons.test.AbstractSerDeTest;
import com.powsybl.iidm.network.Network;
import com.powsybl.iidm.network.test.NetworkTest1Factory;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.nio.file.Files;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.HashSet;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static org.junit.jupiter.api.Assertions.*;

/**
* @author Luma Zamarreño {@literal <zamarrenolm at aia.es>}
*/
class ModelIdTest extends AbstractSerDeTest {

private static final Pattern REGEX_FULL_MODEL_ID = Pattern.compile("FullModel rdf:about=\"(.*?)\"");

@Test
void testModelIds() throws IOException {
Network network = NetworkTest1Factory.create("minimal-network");

// We will export the case for two different times: t0 and t1
ZonedDateTime t0 = ZonedDateTime.of(2020, 1, 1, 0, 0, 30, 0, ZoneId.systemDefault());
ZonedDateTime t1 = ZonedDateTime.of(2020, 1, 1, 1, 0, 30, 0, ZoneId.systemDefault());
Properties exportParams = new Properties();
exportParams.put(CgmesExport.NAMING_STRATEGY, "cgmes");

// Check ids of the different export parts (EQ, SSH, SV, TP) are different
network.setCaseDate(t0);
Map<String, String> ids0 = exportedIds(network, exportParams);
Set<String> uniqueIds = new HashSet<>(ids0.values());
assertEquals(ids0.size(), uniqueIds.size());

// Check we get different ids when scenarioTime changes
network.setCaseDate(t1);
Map<String, String> ids1 = exportedIds(network, exportParams);
assertNotEquals(ids0.get("SSH"), ids1.get("SSH"));
assertNotEquals(ids0.get("SV"), ids1.get("SV"));
assertNotEquals(ids0.get("TP"), ids1.get("TP"));

// Check that we get different ids when version changes
// The way to force a different version number in the output
// is to give a previous version number inside metadata extension
// Export will increase the version number available in the metadata
network.newExtension(CgmesSshMetadataAdder.class)
.setId("not-relevant")
.setDescription("not-relevant")
.setSshVersion(42)
.setModelingAuthoritySet("not-relevant")
.addDependency("not-relevant")
.add();
Map<String, String> ids1v = exportedIds(network, exportParams);
// We only have added previous version for SSH data, so only SSH id must have changed
assertNotEquals(ids1.get("SSH"), ids1v.get("SSH"));
assertEquals(ids1.get("SV"), ids1v.get("SV"));
}

String modelId(String xml) {
Matcher matcher = REGEX_FULL_MODEL_ID.matcher(xml);
if (matcher.find()) {
return matcher.group(1);
} else {
throw new PowsyblException("missing model id");
}
}

private String read(String basename, String profile) throws IOException {
String instanceFile = String.format("%s_%s.xml", basename, profile);
return Files.readString(tmpDir.resolve(instanceFile));
}

private Map<String, String> exportedIds(Network network, Properties exportParams) throws IOException {
String basename = network.getNameOrId();
network.write("CGMES", exportParams, tmpDir.resolve(basename));
return Map.of("EQ", modelId(read(basename, "EQ")),
"SSH", modelId(read(basename, "SSH")),
"TP", modelId(read(basename, "TP")),
"SV", modelId(read(basename, "SV")));
}
}

0 comments on commit c55ad6c

Please sign in to comment.