From 87dd93b5d511b3b4f576bd36b92faf90689ec9c0 Mon Sep 17 00:00:00 2001 From: Thomas ADAM Date: Wed, 13 Jul 2022 15:52:46 +0200 Subject: [PATCH 1/2] Add Edge diagramId mapping with voltagelevel nodes Signed-off-by: Thomas ADAM --- .../com/powsybl/nad/svg/GraphMetadata.java | 87 ++++++++++++++----- .../java/com/powsybl/nad/svg/SvgWriter.java | 5 +- .../powsybl/nad/svg/GraphMetadataTest.java | 1 + 3 files changed, 68 insertions(+), 25 deletions(-) diff --git a/src/main/java/com/powsybl/nad/svg/GraphMetadata.java b/src/main/java/com/powsybl/nad/svg/GraphMetadata.java index 92c36fb5..ff98f624 100644 --- a/src/main/java/com/powsybl/nad/svg/GraphMetadata.java +++ b/src/main/java/com/powsybl/nad/svg/GraphMetadata.java @@ -7,10 +7,7 @@ package com.powsybl.nad.svg; import com.powsybl.commons.xml.XmlUtil; -import com.powsybl.nad.model.BusNode; -import com.powsybl.nad.model.Edge; -import com.powsybl.nad.model.Identifiable; -import com.powsybl.nad.model.Node; +import com.powsybl.nad.model.*; import javax.xml.stream.XMLInputFactory; import javax.xml.stream.XMLStreamException; @@ -32,11 +29,13 @@ public class GraphMetadata { private static final String METADATA_BUS_NODES_ELEMENT_NAME = "busNodes"; private static final String METADATA_NODES_ELEMENT_NAME = "nodes"; private static final String METADATA_EDGES_ELEMENT_NAME = "edges"; + private static final String METADATA_VOLTAGE_NODES_ELEMENT_NAME = "voltageLevelNodes"; private static final String METADATA_BUS_NODE_ELEMENT_NAME = "busNode"; private static final String METADATA_NODE_ELEMENT_NAME = "node"; private static final String METADATA_EDGE_ELEMENT_NAME = "edge"; private static final String DIAGRAM_ID_ATTRIBUTE = "diagramId"; private static final String EQUIPMENT_ID_ATTRIBUTE = "equipmentId"; + private static final String METADATA_VOLTAGE_NODE_ELEMENT_NAME = "voltageLevelNode"; private final Map busNodeIdByDiagramId = new LinkedHashMap<>(); @@ -44,6 +43,8 @@ public class GraphMetadata { private final Map edgeIdByDiagramId = new LinkedHashMap<>(); + private final Map> edgeDiagramIdsByVoltageLevelNodeDiagramId = new LinkedHashMap<>(); + public static GraphMetadata parseXml(InputStream inputStream) throws XMLStreamException { return parseXml(XMLInputFactory.newDefaultFactory().createXMLStreamReader(inputStream)); } @@ -55,25 +56,16 @@ public static GraphMetadata parseXml(XMLStreamReader reader) throws XMLStreamExc String token = reader.getLocalName(); switch (token) { case METADATA_BUS_NODES_ELEMENT_NAME: - XmlUtil.readUntilEndElement(token, reader, () -> { - if (reader.getLocalName().equals(METADATA_BUS_NODE_ELEMENT_NAME)) { - parseId(metadata.busNodeIdByDiagramId, reader); - } - }); + parseMappingId(metadata.busNodeIdByDiagramId, token, METADATA_BUS_NODE_ELEMENT_NAME, reader); break; case METADATA_NODES_ELEMENT_NAME: - XmlUtil.readUntilEndElement(token, reader, () -> { - if (reader.getLocalName().equals(METADATA_NODE_ELEMENT_NAME)) { - parseId(metadata.nodeIdByDiagramId, reader); - } - }); + parseMappingId(metadata.nodeIdByDiagramId, token, METADATA_NODE_ELEMENT_NAME, reader); break; case METADATA_EDGES_ELEMENT_NAME: - XmlUtil.readUntilEndElement(token, reader, () -> { - if (reader.getLocalName().equals(METADATA_EDGE_ELEMENT_NAME)) { - parseId(metadata.edgeIdByDiagramId, reader); - } - }); + parseMappingId(metadata.edgeIdByDiagramId, token, METADATA_EDGE_ELEMENT_NAME, reader); + break; + case METADATA_VOLTAGE_NODES_ELEMENT_NAME: + parseVoltageLevelNodes(metadata.edgeDiagramIdsByVoltageLevelNodeDiagramId, token, reader); break; default: // Not managed @@ -82,16 +74,37 @@ public static GraphMetadata parseXml(XMLStreamReader reader) throws XMLStreamExc return metadata; } - private static void parseId(Map ids, XMLStreamReader reader) { - String diagramId = reader.getAttributeValue(null, DIAGRAM_ID_ATTRIBUTE); - String equipmentId = reader.getAttributeValue(null, EQUIPMENT_ID_ATTRIBUTE); - ids.put(diagramId, equipmentId); + private static void parseMappingId(Map ids, String parentToken, String token, XMLStreamReader reader) throws XMLStreamException { + XmlUtil.readUntilEndElement(parentToken, reader, () -> { + if (reader.getLocalName().equals(token)) { + String diagramId = reader.getAttributeValue(null, DIAGRAM_ID_ATTRIBUTE); + String equipmentId = reader.getAttributeValue(null, EQUIPMENT_ID_ATTRIBUTE); + ids.put(diagramId, equipmentId); + } + }); + } + + private static void parseVoltageLevelNodes(Map> ids, String parentToken, XMLStreamReader reader) throws XMLStreamException { + XmlUtil.readUntilEndElement(parentToken, reader, () -> { + if (reader.getLocalName().equals(METADATA_VOLTAGE_NODE_ELEMENT_NAME)) { + String diagramId = reader.getAttributeValue(null, DIAGRAM_ID_ATTRIBUTE); + + XmlUtil.readUntilEndElement(reader.getLocalName(), reader, () -> { + if (reader.getLocalName().equals(METADATA_EDGE_ELEMENT_NAME)) { + String edgeDiagramId = reader.getAttributeValue(null, DIAGRAM_ID_ATTRIBUTE); + ids.computeIfAbsent(diagramId, v -> new ArrayList<>()).add(edgeDiagramId); + } + }); + } + }); } public void writeXml(XMLStreamWriter writer) throws XMLStreamException { // Root element writer.writeStartElement(METADATA_ELEMENT_NAME); writer.writeNamespace(METADATA_PREFIX, METADATA_NAMESPACE_URI); + // VoltageLevelNodes + writeVoltageLevelNodes(writer); // BusNodes writeIdMapping(METADATA_BUS_NODES_ELEMENT_NAME, METADATA_BUS_NODE_ELEMENT_NAME, busNodeIdByDiagramId, writer); // Nodes @@ -102,6 +115,30 @@ public void writeXml(XMLStreamWriter writer) throws XMLStreamException { writer.writeEndElement(); } + private void writeVoltageLevelNodes(XMLStreamWriter writer) throws XMLStreamException { + if (edgeDiagramIdsByVoltageLevelNodeDiagramId.entrySet().isEmpty()) { + writer.writeEmptyElement(METADATA_PREFIX, METADATA_VOLTAGE_NODES_ELEMENT_NAME, METADATA_NAMESPACE_URI); + } else { + writer.writeStartElement(METADATA_PREFIX, METADATA_VOLTAGE_NODES_ELEMENT_NAME, METADATA_NAMESPACE_URI); + for (Map.Entry> entry : edgeDiagramIdsByVoltageLevelNodeDiagramId.entrySet()) { + List edgeDiagramIds = entry.getValue(); + if (Objects.isNull(edgeDiagramIds)) { + writer.writeEmptyElement(METADATA_PREFIX, METADATA_VOLTAGE_NODE_ELEMENT_NAME, METADATA_NAMESPACE_URI); + writer.writeAttribute(DIAGRAM_ID_ATTRIBUTE, entry.getKey()); + } else { + writer.writeStartElement(METADATA_PREFIX, METADATA_VOLTAGE_NODE_ELEMENT_NAME, METADATA_NAMESPACE_URI); + writer.writeAttribute(DIAGRAM_ID_ATTRIBUTE, entry.getKey()); + for (String edgeDiagramId : edgeDiagramIds) { + writer.writeEmptyElement(METADATA_PREFIX, METADATA_EDGE_ELEMENT_NAME, METADATA_NAMESPACE_URI); + writer.writeAttribute(DIAGRAM_ID_ATTRIBUTE, edgeDiagramId); + } + writer.writeEndElement(); + } + } + writer.writeEndElement(); + } + } + private void writeIdMapping(String rootElementName, String tagElementName, Map ids, XMLStreamWriter writer) throws XMLStreamException { if (ids.entrySet().isEmpty()) { writer.writeEmptyElement(METADATA_PREFIX, rootElementName, METADATA_NAMESPACE_URI); @@ -116,6 +153,10 @@ private void writeIdMapping(String rootElementName, String tagElementName, Map diagramIdToSvgId) { + edgeDiagramIdsByVoltageLevelNodeDiagramId.computeIfAbsent(diagramIdToSvgId.apply(node.getDiagramId()), v -> new ArrayList<>()).add(diagramIdToSvgId.apply(edge.getDiagramId())); + } + public void addBusNode(BusNode node, UnaryOperator diagramIdToSvgId) { addIdentifiable(busNodeIdByDiagramId, node, diagramIdToSvgId); } diff --git a/src/main/java/com/powsybl/nad/svg/SvgWriter.java b/src/main/java/com/powsybl/nad/svg/SvgWriter.java index 6299b6d5..b62da34e 100644 --- a/src/main/java/com/powsybl/nad/svg/SvgWriter.java +++ b/src/main/java/com/powsybl/nad/svg/SvgWriter.java @@ -660,9 +660,10 @@ private void addStyle(XMLStreamWriter writer) throws XMLStreamException { private void addMetadata(Graph graph, XMLStreamWriter writer) throws XMLStreamException { GraphMetadata metadata = new GraphMetadata(); + graph.getBranchEdgeStream().forEach(be -> metadata.addVoltageLevelNode(graph.getVoltageLevelNode1(be), be, this::getPrefixedId)); graph.getBusNodesStream().forEach(bn -> metadata.addBusNode(bn, this::getPrefixedId)); - graph.getNodesStream().forEach(bn -> metadata.addNode(bn, this::getPrefixedId)); - graph.getEdgesStream().forEach(bn -> metadata.addEdge(bn, this::getPrefixedId)); + graph.getNodesStream().forEach(n -> metadata.addNode(n, this::getPrefixedId)); + graph.getEdgesStream().forEach(e -> metadata.addEdge(e, this::getPrefixedId)); metadata.writeXml(writer); } diff --git a/src/test/java/com/powsybl/nad/svg/GraphMetadataTest.java b/src/test/java/com/powsybl/nad/svg/GraphMetadataTest.java index 25d1c49b..1bd9d6a0 100644 --- a/src/test/java/com/powsybl/nad/svg/GraphMetadataTest.java +++ b/src/test/java/com/powsybl/nad/svg/GraphMetadataTest.java @@ -116,6 +116,7 @@ void testInvalid() throws XMLStreamException { actual = actual.substring(actual.indexOf(METADATA_START_TOKEN)); // Keep only metadata from svg file String expected = "\n" + + " \n" + " \n" + " \n" + " \n" + From 548b8c0b3202acb01715a5df19dae38c154d7f54 Mon Sep 17 00:00:00 2001 From: Thomas ADAM Date: Wed, 13 Jul 2022 15:54:42 +0200 Subject: [PATCH 2/2] Update test references svg files Signed-off-by: Thomas ADAM --- src/test/resources/3wt.svg | 1 + src/test/resources/3wt_disconnected.svg | 1 + src/test/resources/3wt_partial.svg | 1 + src/test/resources/IEEE_118_bus.svg | 388 ++++++++++++++++++ src/test/resources/IEEE_118_bus_partial.svg | 102 +++++ .../IEEE_118_bus_partial_non_connected.svg | 69 ++++ src/test/resources/IEEE_14_bus.svg | 44 ++ .../resources/IEEE_14_bus_disconnection.svg | 44 ++ src/test/resources/IEEE_14_bus_fictitious.svg | 44 ++ src/test/resources/IEEE_14_bus_text_nodes.svg | 44 ++ src/test/resources/IEEE_14_id_prefixed.svg | 44 ++ src/test/resources/IEEE_24_bus.svg | 82 ++++ src/test/resources/IEEE_30_bus.svg | 89 ++++ src/test/resources/IEEE_57_bus.svg | 178 ++++++++ src/test/resources/current_limits.svg | 5 + .../resources/edge_info_double_labels.svg | 1 + .../resources/edge_info_missing_label.svg | 5 + .../edge_info_perpendicular_label.svg | 5 + src/test/resources/edge_info_shift.svg | 10 + src/test/resources/hvdc.svg | 15 + src/test/resources/parallel_transformers.svg | 6 + src/test/resources/simple-eu-loop100.svg | 34 ++ src/test/resources/simple-eu-loop80.svg | 34 ++ src/test/resources/simple-eu.svg | 34 ++ src/test/resources/voltage_limits.svg | 6 + 25 files changed, 1286 insertions(+) diff --git a/src/test/resources/3wt.svg b/src/test/resources/3wt.svg index d7390d0f..fa6408a8 100644 --- a/src/test/resources/3wt.svg +++ b/src/test/resources/3wt.svg @@ -46,6 +46,7 @@ } ]]> + diff --git a/src/test/resources/3wt_disconnected.svg b/src/test/resources/3wt_disconnected.svg index 94bf6685..5b9ad746 100644 --- a/src/test/resources/3wt_disconnected.svg +++ b/src/test/resources/3wt_disconnected.svg @@ -46,6 +46,7 @@ } ]]> + diff --git a/src/test/resources/3wt_partial.svg b/src/test/resources/3wt_partial.svg index c08bfbd5..5767d24a 100644 --- a/src/test/resources/3wt_partial.svg +++ b/src/test/resources/3wt_partial.svg @@ -46,6 +46,7 @@ } ]]> + diff --git a/src/test/resources/IEEE_118_bus.svg b/src/test/resources/IEEE_118_bus.svg index d4203260..93018800 100644 --- a/src/test/resources/IEEE_118_bus.svg +++ b/src/test/resources/IEEE_118_bus.svg @@ -110,6 +110,394 @@ } ]]> + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/test/resources/IEEE_118_bus_partial.svg b/src/test/resources/IEEE_118_bus_partial.svg index a9cddc50..d0faef0d 100644 --- a/src/test/resources/IEEE_118_bus_partial.svg +++ b/src/test/resources/IEEE_118_bus_partial.svg @@ -110,6 +110,108 @@ } ]]> + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/test/resources/IEEE_118_bus_partial_non_connected.svg b/src/test/resources/IEEE_118_bus_partial_non_connected.svg index 2ce005c2..84a4fe21 100644 --- a/src/test/resources/IEEE_118_bus_partial_non_connected.svg +++ b/src/test/resources/IEEE_118_bus_partial_non_connected.svg @@ -110,6 +110,75 @@ } ]]> + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/test/resources/IEEE_14_bus.svg b/src/test/resources/IEEE_14_bus.svg index 65f3c7cb..b77fb67c 100644 --- a/src/test/resources/IEEE_14_bus.svg +++ b/src/test/resources/IEEE_14_bus.svg @@ -46,6 +46,50 @@ } ]]> + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/test/resources/IEEE_14_bus_disconnection.svg b/src/test/resources/IEEE_14_bus_disconnection.svg index 64164425..50944219 100644 --- a/src/test/resources/IEEE_14_bus_disconnection.svg +++ b/src/test/resources/IEEE_14_bus_disconnection.svg @@ -46,6 +46,50 @@ } ]]> + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/test/resources/IEEE_14_bus_fictitious.svg b/src/test/resources/IEEE_14_bus_fictitious.svg index e577fbca..ecef8f06 100644 --- a/src/test/resources/IEEE_14_bus_fictitious.svg +++ b/src/test/resources/IEEE_14_bus_fictitious.svg @@ -46,6 +46,50 @@ } ]]> + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/test/resources/IEEE_14_bus_text_nodes.svg b/src/test/resources/IEEE_14_bus_text_nodes.svg index e1f9342d..9c081a8e 100644 --- a/src/test/resources/IEEE_14_bus_text_nodes.svg +++ b/src/test/resources/IEEE_14_bus_text_nodes.svg @@ -46,6 +46,50 @@ } ]]> + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/test/resources/IEEE_14_id_prefixed.svg b/src/test/resources/IEEE_14_id_prefixed.svg index f4faf114..f9e2ba56 100644 --- a/src/test/resources/IEEE_14_id_prefixed.svg +++ b/src/test/resources/IEEE_14_id_prefixed.svg @@ -46,6 +46,50 @@ } ]]> + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/test/resources/IEEE_24_bus.svg b/src/test/resources/IEEE_24_bus.svg index 15f61cb2..eed654b2 100644 --- a/src/test/resources/IEEE_24_bus.svg +++ b/src/test/resources/IEEE_24_bus.svg @@ -46,6 +46,88 @@ } ]]> + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/test/resources/IEEE_30_bus.svg b/src/test/resources/IEEE_30_bus.svg index e592310d..6578c2f8 100644 --- a/src/test/resources/IEEE_30_bus.svg +++ b/src/test/resources/IEEE_30_bus.svg @@ -46,6 +46,95 @@ } ]]> + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/test/resources/IEEE_57_bus.svg b/src/test/resources/IEEE_57_bus.svg index d6455870..5ff0daa7 100644 --- a/src/test/resources/IEEE_57_bus.svg +++ b/src/test/resources/IEEE_57_bus.svg @@ -110,6 +110,184 @@ } ]]> + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/test/resources/current_limits.svg b/src/test/resources/current_limits.svg index bb50fe63..06b896cf 100644 --- a/src/test/resources/current_limits.svg +++ b/src/test/resources/current_limits.svg @@ -46,6 +46,11 @@ } ]]> + + + + + diff --git a/src/test/resources/edge_info_double_labels.svg b/src/test/resources/edge_info_double_labels.svg index b67b48b1..9f24b702 100644 --- a/src/test/resources/edge_info_double_labels.svg +++ b/src/test/resources/edge_info_double_labels.svg @@ -46,6 +46,7 @@ } ]]> + diff --git a/src/test/resources/edge_info_missing_label.svg b/src/test/resources/edge_info_missing_label.svg index 10dd6c3d..707f248b 100644 --- a/src/test/resources/edge_info_missing_label.svg +++ b/src/test/resources/edge_info_missing_label.svg @@ -46,6 +46,11 @@ } ]]> + + + + + diff --git a/src/test/resources/edge_info_perpendicular_label.svg b/src/test/resources/edge_info_perpendicular_label.svg index 57e00d32..bbb9387b 100644 --- a/src/test/resources/edge_info_perpendicular_label.svg +++ b/src/test/resources/edge_info_perpendicular_label.svg @@ -46,6 +46,11 @@ } ]]> + + + + + diff --git a/src/test/resources/edge_info_shift.svg b/src/test/resources/edge_info_shift.svg index e9698f48..a76980a5 100644 --- a/src/test/resources/edge_info_shift.svg +++ b/src/test/resources/edge_info_shift.svg @@ -46,6 +46,16 @@ } ]]> + + + + + + + + + + diff --git a/src/test/resources/hvdc.svg b/src/test/resources/hvdc.svg index 00f10bb7..07cc43ef 100644 --- a/src/test/resources/hvdc.svg +++ b/src/test/resources/hvdc.svg @@ -46,6 +46,21 @@ } ]]> + + + + + + + + + + + + + + + diff --git a/src/test/resources/parallel_transformers.svg b/src/test/resources/parallel_transformers.svg index 1c34f6a8..4caa3121 100644 --- a/src/test/resources/parallel_transformers.svg +++ b/src/test/resources/parallel_transformers.svg @@ -46,6 +46,12 @@ } ]]> + + + + + + diff --git a/src/test/resources/simple-eu-loop100.svg b/src/test/resources/simple-eu-loop100.svg index 3e3a9531..af72f334 100644 --- a/src/test/resources/simple-eu-loop100.svg +++ b/src/test/resources/simple-eu-loop100.svg @@ -46,6 +46,40 @@ } ]]> + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/test/resources/simple-eu-loop80.svg b/src/test/resources/simple-eu-loop80.svg index 53509b37..5ec24bdc 100644 --- a/src/test/resources/simple-eu-loop80.svg +++ b/src/test/resources/simple-eu-loop80.svg @@ -46,6 +46,40 @@ } ]]> + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/test/resources/simple-eu.svg b/src/test/resources/simple-eu.svg index e530c237..aee447c0 100644 --- a/src/test/resources/simple-eu.svg +++ b/src/test/resources/simple-eu.svg @@ -46,6 +46,40 @@ } ]]> + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/test/resources/voltage_limits.svg b/src/test/resources/voltage_limits.svg index bb13a329..f68b0c27 100644 --- a/src/test/resources/voltage_limits.svg +++ b/src/test/resources/voltage_limits.svg @@ -46,6 +46,12 @@ } ]]> + + + + + +