Skip to content

Commit

Permalink
CgmesDLImportPostProcessor fix (#617)
Browse files Browse the repository at this point in the history
* Remove context name restriction
* Clarify logs
* Remove now useless unit test
* Add empty DL unit test
* Fix checkstyle

---------

Signed-off-by: Florian Dupuy <florian.dupuy@rte-france.com>
  • Loading branch information
flo-dup authored Jul 3, 2024
1 parent ce1243f commit 82aa447
Show file tree
Hide file tree
Showing 3 changed files with 127 additions and 67 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

import com.google.auto.service.AutoService;
import com.powsybl.cgmes.conversion.CgmesImportPostProcessor;
import com.powsybl.cgmes.model.CgmesSubset;
import com.powsybl.iidm.network.Network;
import com.powsybl.triplestore.api.QueryCatalog;
import com.powsybl.triplestore.api.TripleStore;
Expand Down Expand Up @@ -42,19 +41,10 @@ public String getName() {
return NAME;
}

private boolean isDlProfileAvailable(Network network, TripleStore tripleStore) {
return tripleStore != null && tripleStore.contextNames().contains(ContextUtils.contextNameFor(CgmesSubset.DIAGRAM_LAYOUT, tripleStore, network.getId()));
}

@Override
public void process(Network network, TripleStore tripleStore) {
if (isDlProfileAvailable(network, tripleStore)) {
LOG.info("Execute {} CGMES import post processor on network {}", getName(), network.getId());

CgmesDLModel cgmesDLModel = new CgmesDLModel(tripleStore, queryCatalog);
new CgmesDLImporter(network, cgmesDLModel).importDLData();
} else {
LOG.info("DL profile not found for network {}", network.getId());
}
LOG.info("Execute {} CGMES import post processor on network {}", getName(), network.getId());
CgmesDLModel cgmesDLModel = new CgmesDLModel(tripleStore, queryCatalog);
new CgmesDLImporter(network, cgmesDLModel).importDLData();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,7 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.*;

/**
*
Expand All @@ -37,94 +34,158 @@ public CgmesDLImporter(Network network, CgmesDLModel cgmesDLModel) {
}

public void importDLData() {
importTerminalsDLData();
importBusesDLData();
importBusbarsDLData();
importLinesDLData();
importGeneratorsDLData();
importLoadsDLData();
importShuntsDLData();
importSwitchesDLData();
importTransformersDLData();
importHvdcLinesDLData();
importSvcsDLData();
importVoltageLevelDLData();
Boolean[] importDl = new Boolean[]{
importTerminalsDLData(),
importBusesDLData(),
importBusbarsDLData(),
importLinesDLData(),
importGeneratorsDLData(),
importLoadsDLData(),
importShuntsDLData(),
importSwitchesDLData(),
importTransformersDLData(),
importHvdcLinesDLData(),
importSvcsDLData(),
importVoltageLevelDLData()
};
if (Arrays.stream(importDl).noneMatch(imported -> imported)) {
LOG.info("No DL data found for network {}", network.getId());
}
}

private void importTerminalsDLData() {
private boolean importTerminalsDLData() {
PropertyBags terminalDiagramData = cgmesDLModel.getTerminalsDiagramData();
if (terminalDiagramData.isEmpty()) {
return false;
}
LOG.info("Importing Terminals DL Data");
cgmesDLModel.getTerminalsDiagramData().forEach(terminalDiagramData -> {
String terminalKey = terminalDiagramData.get("diagramName") + "_" + terminalDiagramData.getId("terminalEquipment") + "_" + terminalDiagramData.get("terminalSide");
terminalDiagramData.forEach(propertyBag -> {
String terminalKey = propertyBag.get("diagramName") + "_" + propertyBag.getId("terminalEquipment") + "_" + propertyBag.get("terminalSide");
PropertyBags equipmentTerminalsDiagramData = new PropertyBags();
if (terminalsDiagramData.containsKey(terminalKey)) {
equipmentTerminalsDiagramData = terminalsDiagramData.get(terminalKey);
}
equipmentTerminalsDiagramData.add(terminalDiagramData);
equipmentTerminalsDiagramData.add(propertyBag);
terminalsDiagramData.put(terminalKey, equipmentTerminalsDiagramData);
});
return true;
}

private void importBusesDLData() {
private boolean importBusesDLData() {
PropertyBags busesDiagramData = cgmesDLModel.getBusesDiagramData();
if (busesDiagramData.isEmpty()) {
return false;
}
LOG.info("Importing Buses DL Data");
BusDiagramDataImporter diagramDataImporter = new BusDiagramDataImporter(network);
cgmesDLModel.getBusesDiagramData().forEach(diagramDataImporter::importDiagramData);
busesDiagramData.forEach(diagramDataImporter::importDiagramData);
return true;
}

private void importBusbarsDLData() {
private boolean importBusbarsDLData() {
PropertyBags busbarsDiagramData = cgmesDLModel.getBusbarsDiagramData();
if (busbarsDiagramData.isEmpty()) {
return false;
}
LOG.info("Importing Busbars DL Data");
BusbarDiagramDataImporter diagramDataImporter = new BusbarDiagramDataImporter(network);
cgmesDLModel.getBusbarsDiagramData().forEach(diagramDataImporter::importDiagramData);
busbarsDiagramData.forEach(diagramDataImporter::importDiagramData);
return true;
}

private void importLinesDLData() {
private boolean importLinesDLData() {
PropertyBags linesDiagramData = cgmesDLModel.getLinesDiagramData();
if (linesDiagramData.isEmpty()) {
return false;
}
LOG.info("Importing Lines DL Data");
LineDiagramDataImporter diagramDataImporter = new LineDiagramDataImporter(network);
cgmesDLModel.getLinesDiagramData().forEach(diagramDataImporter::importDiagramData);
linesDiagramData.forEach(diagramDataImporter::importDiagramData);
return true;
}

private void importGeneratorsDLData() {
private boolean importGeneratorsDLData() {
PropertyBags generatorsDiagramData = cgmesDLModel.getGeneratorsDiagramData();
if (generatorsDiagramData.isEmpty()) {
return false;
}
LOG.info("Importing Generators DL Data");
GeneratorDiagramDataImporter diagramDataImporter = new GeneratorDiagramDataImporter(network, terminalsDiagramData);
cgmesDLModel.getGeneratorsDiagramData().forEach(diagramDataImporter::importDiagramData);
generatorsDiagramData.forEach(diagramDataImporter::importDiagramData);
return true;
}

private void importLoadsDLData() {
private boolean importLoadsDLData() {
PropertyBags loadsDiagramData = cgmesDLModel.getLoadsDiagramData();
if (loadsDiagramData.isEmpty()) {
return false;
}
LOG.info("Importing Loads DL Data");
LoadDiagramDataImporter diagramDataImporter = new LoadDiagramDataImporter(network, terminalsDiagramData);
cgmesDLModel.getLoadsDiagramData().forEach(diagramDataImporter::importDiagramData);
loadsDiagramData.forEach(diagramDataImporter::importDiagramData);
return true;
}

private void importShuntsDLData() {
private boolean importShuntsDLData() {
PropertyBags shuntsDiagramData = cgmesDLModel.getShuntsDiagramData();
if (shuntsDiagramData.isEmpty()) {
return false;
}
LOG.info("Importing Shunts DL Data");
ShuntDiagramDataImporter diagramDataImporter = new ShuntDiagramDataImporter(network, terminalsDiagramData);
cgmesDLModel.getShuntsDiagramData().forEach(diagramDataImporter::importDiagramData);
shuntsDiagramData.forEach(diagramDataImporter::importDiagramData);
return true;
}

private void importSwitchesDLData() {
private boolean importSwitchesDLData() {
PropertyBags switchesDiagramData = cgmesDLModel.getSwitchesDiagramData();
if (switchesDiagramData.isEmpty()) {
return false;
}
LOG.info("Importing Switches DL Data");
SwitchDiagramDataImporter diagramDataImporter = new SwitchDiagramDataImporter(network, terminalsDiagramData);
cgmesDLModel.getSwitchesDiagramData().forEach(diagramDataImporter::importDiagramData);
switchesDiagramData.forEach(diagramDataImporter::importDiagramData);
return true;
}

private void importTransformersDLData() {
private boolean importTransformersDLData() {
PropertyBags transformersDiagramData = cgmesDLModel.getTransformersDiagramData();
if (transformersDiagramData.isEmpty()) {
return false;
}
LOG.info("Importing Transformers DL Data");
TransformerDiagramDataImporter diagramDataImporter = new TransformerDiagramDataImporter(network, terminalsDiagramData);
cgmesDLModel.getTransformersDiagramData().forEach(diagramDataImporter::importDiagramData);
transformersDiagramData.forEach(diagramDataImporter::importDiagramData);
return true;
}

private void importHvdcLinesDLData() {
private boolean importHvdcLinesDLData() {
PropertyBags hvdcLinesDiagramData = cgmesDLModel.getHvdcLinesDiagramData();
if (hvdcLinesDiagramData.isEmpty()) {
return false;
}
LOG.info("Importing HVDC Lines DL Data");
HvdcLineDiagramDataImporter diagramDataImporter = new HvdcLineDiagramDataImporter(network);
cgmesDLModel.getHvdcLinesDiagramData().forEach(diagramDataImporter::importDiagramData);
hvdcLinesDiagramData.forEach(diagramDataImporter::importDiagramData);
return true;
}

private void importSvcsDLData() {
private boolean importSvcsDLData() {
PropertyBags svcsDiagramData = cgmesDLModel.getSvcsDiagramData();
if (svcsDiagramData.isEmpty()) {
return false;
}
LOG.info("Importing Svcs DL Data");
SvcDiagramDataImporter diagramDataImporter = new SvcDiagramDataImporter(network, terminalsDiagramData);
cgmesDLModel.getSvcsDiagramData().forEach(diagramDataImporter::importDiagramData);
svcsDiagramData.forEach(diagramDataImporter::importDiagramData);
return true;
}

private void importVoltageLevelDLData() {
private boolean importVoltageLevelDLData() {
PropertyBags voltageLevelDiagramData = cgmesDLModel.getVoltageLevelDiagramData();
if (voltageLevelDiagramData.isEmpty()) {
return false;
}
LOG.info("Importing VoltageLevel DL Data");
Map<String, Set<String>> nodeSwitches = cgmesDLModel.findCgmesConnectivityNodesSwitchesForks();
Map<String, Map<String, Integer>> mapCnodeInode = new HashMap<>();
Expand All @@ -143,7 +204,8 @@ private void importVoltageLevelDLData() {
}
});
VoltageLevelDiagramDataImporter diagramDataImporter = new VoltageLevelDiagramDataImporter(network, mapCnodeInode);
cgmesDLModel.getVoltageLevelDiagramData().forEach(diagramDataImporter::importDiagramData);
voltageLevelDiagramData.forEach(diagramDataImporter::importDiagramData);
return true;
}

public Network getNetworkWithDLData() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,15 @@
*/
package com.powsybl.sld.cgmes.dl.conversion;

import com.powsybl.diagram.test.Networks;
import com.powsybl.iidm.network.BusbarSection;
import com.powsybl.iidm.network.Network;
import com.powsybl.diagram.test.Networks;
import com.powsybl.sld.cgmes.dl.iidm.extensions.NodeDiagramData;

import com.powsybl.triplestore.api.QueryCatalog;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;

import java.util.Arrays;
import java.util.HashSet;

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

/**
Expand All @@ -25,16 +23,24 @@
*/
class CgmesDLImportPostProcessorTest extends CgmesDLModelTest {

private Network network;

@BeforeEach
@Override
public void setUp() {
super.setUp();
network = Networks.createNetworkWithBusbar();
}

@Test
void process() throws Exception {
Network network = Networks.createNetworkWithBusbar();
void process() {
new CgmesDLImportPostProcessor(queryCatalog).process(network, tripleStore);

BusbarSection busbar = network.getBusbarSection("Busbar");
NodeDiagramData<BusbarSection> busDiagramData = busbar.getExtension(NodeDiagramData.class);
assertNotNull(busDiagramData);
assertNotNull(busDiagramData.getData(DEFAULT_DIAGRAM_NAME));
NodeDiagramData.NodeDiagramDataDetails nodeDiagramDataDetails = busDiagramData.getData(DEFAULT_DIAGRAM_NAME);
NodeDiagramData<BusbarSection>.NodeDiagramDataDetails nodeDiagramDataDetails = busDiagramData.getData(DEFAULT_DIAGRAM_NAME);
assertEquals(1, nodeDiagramDataDetails.getPoint1().getSeq(), 0);
assertEquals(20, nodeDiagramDataDetails.getPoint1().getX(), 0);
assertEquals(5, nodeDiagramDataDetails.getPoint1().getY(), 0);
Expand All @@ -44,10 +50,12 @@ void process() throws Exception {
}

@Test
void processNoDlContext() {
Network network = Networks.createNetworkWithBusbar();
Mockito.when(tripleStore.contextNames()).thenReturn(new HashSet<>(Arrays.asList("Network_EQ.xml", "Network_SV.xml", "Network_TP.xml")));
void processEmpty() {
queryCatalog = Mockito.mock(QueryCatalog.class);
cgmesDLModel = new CgmesDLModel(tripleStore, queryCatalog);
new CgmesDLImportPostProcessor(queryCatalog).process(network, tripleStore);
assertNull(network.getBusbarSection("Busbar").getExtension(NodeDiagramData.class));

BusbarSection busbar = network.getBusbarSection("Busbar");
assertNull(busbar.getExtension(NodeDiagramData.class));
}
}

0 comments on commit 82aa447

Please sign in to comment.