Skip to content
This repository has been archived by the owner on Nov 18, 2022. It is now read-only.

Add metadata to enable moving nodes #87

Closed
wants to merge 3 commits into from
Closed
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
87 changes: 64 additions & 23 deletions src/main/java/com/powsybl/nad/svg/GraphMetadata.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -32,18 +29,22 @@ 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<String, String> busNodeIdByDiagramId = new LinkedHashMap<>();

private final Map<String, String> nodeIdByDiagramId = new LinkedHashMap<>();

private final Map<String, String> edgeIdByDiagramId = new LinkedHashMap<>();

private final Map<String, List<String>> edgeDiagramIdsByVoltageLevelNodeDiagramId = new LinkedHashMap<>();

public static GraphMetadata parseXml(InputStream inputStream) throws XMLStreamException {
return parseXml(XMLInputFactory.newDefaultFactory().createXMLStreamReader(inputStream));
}
Expand All @@ -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
Expand All @@ -82,16 +74,37 @@ public static GraphMetadata parseXml(XMLStreamReader reader) throws XMLStreamExc
return metadata;
}

private static void parseId(Map<String, String> 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<String, String> 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<String, List<String>> 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
Expand All @@ -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<String, List<String>> entry : edgeDiagramIdsByVoltageLevelNodeDiagramId.entrySet()) {
List<String> 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<String, String> ids, XMLStreamWriter writer) throws XMLStreamException {
if (ids.entrySet().isEmpty()) {
writer.writeEmptyElement(METADATA_PREFIX, rootElementName, METADATA_NAMESPACE_URI);
Expand All @@ -116,6 +153,10 @@ private void writeIdMapping(String rootElementName, String tagElementName, Map<S
}
}

public void addVoltageLevelNode(VoltageLevelNode node, Edge edge, UnaryOperator<String> diagramIdToSvgId) {
edgeDiagramIdsByVoltageLevelNodeDiagramId.computeIfAbsent(diagramIdToSvgId.apply(node.getDiagramId()), v -> new ArrayList<>()).add(diagramIdToSvgId.apply(edge.getDiagramId()));
}

public void addBusNode(BusNode node, UnaryOperator<String> diagramIdToSvgId) {
addIdentifiable(busNodeIdByDiagramId, node, diagramIdToSvgId);
}
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/com/powsybl/nad/svg/SvgWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -659,9 +659,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);
}
Expand Down
1 change: 1 addition & 0 deletions src/test/java/com/powsybl/nad/svg/GraphMetadataTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ void testInvalid() throws XMLStreamException {
actual = actual.substring(actual.indexOf(METADATA_START_TOKEN));
// Keep only metadata from svg file
String expected = "<metadata xmlns:nad=\"http://www.powsybl.org/schema/nad-metadata/1_0\">\n" +
" <nad:voltageLevelNodes/>\n" +
" <nad:busNodes/>\n" +
" <nad:nodes/>\n" +
" <nad:edges/>\n" +
Expand Down
1 change: 1 addition & 0 deletions src/test/resources/3wt.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions src/test/resources/3wt_disconnected.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions src/test/resources/3wt_partial.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading