Skip to content

Commit

Permalink
Add metadata for 3wt edges and nodes (#458)
Browse files Browse the repository at this point in the history
* Add metadata for 3wt edges and nodes
* Map of edges in graph should only contain branch edges
* Factorize code writing style classes: joining done in writeStyleClasses
* Factorize code writing id: getPrefixId done in writeId 

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

Co-authored-by: Florian Dupuy <florian.dupuy@rte-france.com>
  • Loading branch information
zamarrenolm and flo-dup authored Jan 3, 2023
1 parent bf51785 commit 9efaa54
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 84 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public class Graph {

private final Map<String, Node> nodes = new LinkedHashMap<>();
private final Map<String, BusNode> busNodes = new LinkedHashMap<>();
private final Map<String, Edge> edges = new LinkedHashMap<>();
private final Map<String, BranchEdge> branchEdges = new LinkedHashMap<>();
private double minX = 0;
private double minY = 0;
private double maxX = 0;
Expand Down Expand Up @@ -58,6 +58,7 @@ public void addTextNode(VoltageLevelNode vlNode) {

public void addEdge(VoltageLevelNode node1, BusNode busNode1,
VoltageLevelNode node2, BusNode busNode2, BranchEdge edge) {
branchEdges.put(edge.getEquipmentId(), edge);
addVoltageLevelsEdge(node1, node2, edge);
addBusesEdge(busNode1, busNode2, edge);
}
Expand All @@ -78,7 +79,6 @@ private void addVoltageLevelsEdge(Node node1, Node node2, Edge edge) {
Objects.requireNonNull(node1);
Objects.requireNonNull(node2);
Objects.requireNonNull(edge);
edges.put(edge.getEquipmentId(), edge);
voltageLevelGraph.addEdge(node1, node2, edge);
}

Expand Down Expand Up @@ -116,8 +116,12 @@ public Collection<Pair<VoltageLevelNode, TextNode>> getVoltageLevelTextPairs() {
return Collections.unmodifiableCollection(textEdges.values());
}

public Stream<Edge> getEdgesStream() {
return edges.values().stream();
public Stream<BranchEdge> getBranchEdgeStream() {
return branchEdges.values().stream();
}

public Collection<BranchEdge> getBranchEdges() {
return Collections.unmodifiableCollection(branchEdges.values());
}

public Collection<Edge> getEdges() {
Expand All @@ -142,16 +146,6 @@ public Collection<Edge> getBusEdges(BusNode busNode) {
return busGraph.edgesOf(busNode);
}

public Stream<BranchEdge> getBranchEdgeStream() {
return voltageLevelGraph.edgeSet().stream()
.filter(BranchEdge.class::isInstance)
.map(BranchEdge.class::cast);
}

public List<BranchEdge> getBranchEdges() {
return getBranchEdgeStream().collect(Collectors.toList());
}

public Stream<TextEdge> getTextEdgesStream() {
return textEdges.keySet().stream();
}
Expand Down Expand Up @@ -308,7 +302,7 @@ public Node getBusGraphNode2(Edge edge) {
}

public boolean containsEdge(String equipmentId) {
return edges.containsKey(equipmentId);
return branchEdges.containsKey(equipmentId);
}

public boolean containsNode(String equipmentId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,8 @@ private void drawBranchEdges(Graph graph, XMLStreamWriter writer) throws XMLStre
writer.writeAttribute(CLASS_ATTRIBUTE, StyleProvider.BRANCH_EDGES_CLASS);
for (BranchEdge edge : graph.getBranchEdges()) {
writer.writeStartElement(GROUP_ELEMENT_NAME);
writer.writeAttribute(ID_ATTRIBUTE, getPrefixedId(edge.getDiagramId()));
addStylesIfAny(writer, styleProvider.getEdgeStyleClasses(edge));
writeId(writer, edge);
writeStyleClasses(writer, styleProvider.getEdgeStyleClasses(edge));
insertName(writer, edge::getName);

drawHalfEdge(graph, writer, edge, BranchEdge.Side.ONE);
Expand Down Expand Up @@ -205,11 +205,11 @@ private void drawHalfEdge(Graph graph, XMLStreamWriter writer, BranchEdge edge,
}
writer.writeStartElement(GROUP_ELEMENT_NAME);
writer.writeAttribute(ID_ATTRIBUTE, getPrefixedId(edge.getDiagramId() + "." + side.getNum()));
addStylesIfAny(writer, styleProvider.getSideEdgeStyleClasses(edge, side));
writeStyleClasses(writer, styleProvider.getSideEdgeStyleClasses(edge, side));
if (edge.isVisible(side)) {
if (!graph.isLoop(edge)) {
writer.writeEmptyElement(POLYLINE_ELEMENT_NAME);
writer.writeAttribute(CLASS_ATTRIBUTE, String.join(" ", StyleProvider.EDGE_PATH_CLASS, StyleProvider.STRETCHABLE_CLASS));
writeStyleClasses(writer, StyleProvider.EDGE_PATH_CLASS, StyleProvider.STRETCHABLE_CLASS);
writer.writeAttribute(POINTS_ATTRIBUTE, getPolylinePointsString(edge, side));
drawBranchEdgeInfo(graph, writer, edge, side, labelProvider.getEdgeInfos(graph, edge, side));
} else {
Expand Down Expand Up @@ -246,13 +246,16 @@ private void drawThreeWtEdge(Graph graph, XMLStreamWriter writer, ThreeWtEdge ed
return;
}
writer.writeStartElement(GROUP_ELEMENT_NAME);
writer.writeAttribute(ID_ATTRIBUTE, getPrefixedId(edge.getDiagramId()));
addStylesIfAny(writer, styleProvider.getEdgeStyleClasses(edge));
writeId(writer, edge);
writeStyleClasses(writer, styleProvider.getEdgeStyleClasses(edge));
insertName(writer, edge::getName);

writer.writeEmptyElement(POLYLINE_ELEMENT_NAME);
writer.writeAttribute(CLASS_ATTRIBUTE, StyleProvider.EDGE_PATH_CLASS);
writeStyleClasses(writer, StyleProvider.EDGE_PATH_CLASS, StyleProvider.STRETCHABLE_CLASS);
writer.writeAttribute(POINTS_ATTRIBUTE, getPolylinePointsString(edge));

drawThreeWtEdgeInfo(graph, writer, edge, labelProvider.getEdgeInfos(graph, edge));

writer.writeEndElement();
}

Expand All @@ -266,7 +269,8 @@ private void drawThreeWtNodes(Graph graph, XMLStreamWriter writer) throws XMLStr
writer.writeAttribute(CLASS_ATTRIBUTE, StyleProvider.THREE_WT_NODES_CLASS);
for (ThreeWtNode threeWtNode : threeWtNodes) {
writer.writeStartElement(GROUP_ELEMENT_NAME);
addStylesIfAny(writer, styleProvider.getNodeStyleClasses(threeWtNode));
writeId(writer, threeWtNode);
writeStyleClasses(writer, styleProvider.getNodeStyleClasses(threeWtNode));
List<ThreeWtEdge> edges = graph.getThreeWtEdgeStream(threeWtNode).collect(Collectors.toList());
for (ThreeWtEdge edge : edges) {
draw3WtWinding(edge, threeWtNode, writer);
Expand All @@ -277,12 +281,10 @@ private void drawThreeWtNodes(Graph graph, XMLStreamWriter writer) throws XMLStr
}

private void draw3WtWinding(ThreeWtEdge edge, ThreeWtNode threeWtNode, XMLStreamWriter writer) throws XMLStreamException {
List<String> styles = styleProvider.getThreeWtNodeStyle(threeWtNode, edge.getSide());
styles.add(StyleProvider.WINDING_CLASS);
double radius = svgParameters.getTransformerCircleRadius();
Point circleCenter = edge.getPoints().get(1).atDistance(radius, threeWtNode.getPosition());
writer.writeEmptyElement(CIRCLE_ELEMENT_NAME);
addStylesIfAny(writer, styles);
writeStyleClasses(writer, styleProvider.getThreeWtNodeStyle(threeWtNode, edge.getSide()), StyleProvider.WINDING_CLASS);
writer.writeAttribute("cx", getFormattedValue(circleCenter.getX()));
writer.writeAttribute("cy", getFormattedValue(circleCenter.getY()));
writer.writeAttribute(CIRCLE_RADIUS_ATTRIBUTE, getFormattedValue(svgParameters.getTransformerCircleRadius()));
Expand All @@ -302,7 +304,8 @@ private void drawBranchEdgeInfo(Graph graph, XMLStreamWriter writer, BranchEdge
private void drawThreeWtEdgeInfo(Graph graph, XMLStreamWriter writer, ThreeWtEdge edge, List<EdgeInfo> edgeInfos) throws XMLStreamException {
VoltageLevelNode vlNode = graph.getVoltageLevelNode(edge);
BusNode busNode = graph.getBusGraphNode(edge);
drawEdgeInfo(writer, edgeInfos, getArrowCenter(vlNode, busNode, edge.getPoints()), edge.getEdgeAngle());
List<String> additionalStyles = List.of(StyleProvider.GLUED_CLASS + "-1");
drawEdgeInfo(writer, additionalStyles, edgeInfos, getArrowCenter(vlNode, busNode, edge.getPoints()), edge.getEdgeAngle());
}

private void drawEdgeInfo(XMLStreamWriter writer, List<EdgeInfo> edgeInfos, Point infoCenter, double edgeAngle) throws XMLStreamException {
Expand All @@ -311,13 +314,11 @@ private void drawEdgeInfo(XMLStreamWriter writer, List<EdgeInfo> edgeInfos, Poin

private void drawEdgeInfo(XMLStreamWriter writer, List<String> additionalStyles, List<EdgeInfo> edgeInfos, Point infoCenter, double edgeAngle) throws XMLStreamException {
writer.writeStartElement(GROUP_ELEMENT_NAME);
List<String> styles = new ArrayList<>(additionalStyles);
styles.add(StyleProvider.EDGE_INFOS_CLASS);
writer.writeAttribute(CLASS_ATTRIBUTE, String.join(" ", styles));
writeStyleClasses(writer, additionalStyles, StyleProvider.EDGE_INFOS_CLASS);
writer.writeAttribute(TRANSFORM_ATTRIBUTE, getTranslateString(infoCenter));
for (EdgeInfo info : edgeInfos) {
writer.writeStartElement(GROUP_ELEMENT_NAME);
addStylesIfAny(writer, styleProvider.getEdgeInfoStyles(info));
writeStyleClasses(writer, styleProvider.getEdgeInfoStyles(info));
drawInAndOutArrows(writer, edgeAngle);
Optional<String> externalLabel = info.getExternalLabel();
if (externalLabel.isPresent()) {
Expand Down Expand Up @@ -424,9 +425,7 @@ private Point getArrowCenter(VoltageLevelNode vlNode, BusNode busNode, List<Poin

private void draw2WtWinding(XMLStreamWriter writer, BranchEdge edge, BranchEdge.Side side) throws XMLStreamException {
writer.writeEmptyElement(CIRCLE_ELEMENT_NAME);
List<String> styles = new ArrayList<>(styleProvider.getSideEdgeStyleClasses(edge, side));
styles.add(StyleProvider.WINDING_CLASS);
addStylesIfAny(writer, styles);
writeStyleClasses(writer, styleProvider.getSideEdgeStyleClasses(edge, side), StyleProvider.WINDING_CLASS);
List<Point> halfPoints = edge.getPoints(side);
Point point1 = halfPoints.get(halfPoints.size() - 1); // point near 2wt
Point point2 = halfPoints.get(halfPoints.size() - 2); // point near voltage level, or control point for loops
Expand Down Expand Up @@ -507,7 +506,7 @@ private void writeTextNode(XMLStreamWriter writer, TextNode textNode, VoltageLev

private void writeDetailedTextNode(XMLStreamWriter writer, TextNode textNode, VoltageLevelNode vlNode, List<String> content) throws XMLStreamException {
writer.writeStartElement(FOREIGN_OBJECT_ELEMENT_NAME);
writer.writeAttribute(ID_ATTRIBUTE, getPrefixedId(textNode.getDiagramId()));
writeId(writer, textNode);
writer.writeAttribute(Y_ATTRIBUTE, getFormattedValue(textNode.getY() - svgParameters.getDetailedTextNodeYShift()));
writer.writeAttribute(X_ATTRIBUTE, getFormattedValue(textNode.getX()));

Expand Down Expand Up @@ -549,9 +548,7 @@ private void writeBusNodeLegend(XMLStreamWriter writer, VoltageLevelNode vlNode)
writer.writeStartElement(TABLE_ROW_ELEMENT_NAME);
writer.writeStartElement(TABLE_DATA_ELEMENT_NAME);
writer.writeEmptyElement(DIV_ELEMENT_NAME);
List<String> styles = styleProvider.getNodeStyleClasses(busNode);
styles.add(StyleProvider.LEGEND_SQUARE_CLASS);
writer.writeAttribute(CLASS_ATTRIBUTE, String.join(" ", styles));
writeStyleClasses(writer, styleProvider.getNodeStyleClasses(busNode), StyleProvider.LEGEND_SQUARE_CLASS);
writer.writeEndElement();
writer.writeStartElement(TABLE_DATA_ELEMENT_NAME);
writer.writeCharacters(labelProvider.getBusDescription(busNode));
Expand All @@ -563,7 +560,7 @@ private void writeBusNodeLegend(XMLStreamWriter writer, VoltageLevelNode vlNode)

private void writeSimpleTextNode(XMLStreamWriter writer, TextNode textNode, List<String> content) throws XMLStreamException {
writer.writeStartElement(TEXT_ELEMENT_NAME);
writer.writeAttribute(ID_ATTRIBUTE, getPrefixedId(textNode.getDiagramId()));
writeId(writer, textNode);
writer.writeAttribute(Y_ATTRIBUTE, getFormattedValue(textNode.getY()));
if (content.size() == 1) {
writer.writeAttribute(X_ATTRIBUTE, getFormattedValue(textNode.getX()));
Expand All @@ -584,15 +581,15 @@ private void writeSimpleTextNode(XMLStreamWriter writer, TextNode textNode, List
}

private void drawNode(Graph graph, XMLStreamWriter writer, VoltageLevelNode vlNode) throws XMLStreamException {
writer.writeAttribute(ID_ATTRIBUTE, getPrefixedId(vlNode.getDiagramId()));
addStylesIfAny(writer, styleProvider.getNodeStyleClasses(vlNode));
writeId(writer, vlNode);
writeStyleClasses(writer, styleProvider.getNodeStyleClasses(vlNode));
insertName(writer, vlNode::getName);

double nodeOuterRadius = getVoltageLevelCircleRadius(vlNode);

if (vlNode.hasUnknownBusNode()) {
writer.writeEmptyElement(CIRCLE_ELEMENT_NAME);
addStylesIfAny(writer, styleProvider.getNodeStyleClasses(BusNode.UNKNOWN));
writeStyleClasses(writer, styleProvider.getNodeStyleClasses(BusNode.UNKNOWN));
writer.writeAttribute(CIRCLE_RADIUS_ATTRIBUTE, getFormattedValue(nodeOuterRadius + svgParameters.getUnknownBusNodeExtraRadius()));
}

Expand All @@ -614,11 +611,8 @@ private void drawNode(Graph graph, XMLStreamWriter writer, VoltageLevelNode vlNo
writer.writeEmptyElement(PATH_ELEMENT_NAME);
writer.writeAttribute(PATH_D_ATTRIBUTE, getFragmentedAnnulusPath(busInnerRadius, busOuterRadius, traversingBusEdges, graph, vlNode, busNode));
}
writer.writeAttribute(ID_ATTRIBUTE, getPrefixedId(busNode.getDiagramId()));

List<String> nodeStyleClasses = new ArrayList<>(styleProvider.getNodeStyleClasses(busNode));
nodeStyleClasses.add(StyleProvider.BUSNODE_CLASS);
addStylesIfAny(writer, nodeStyleClasses);
writeId(writer, busNode);
writeStyleClasses(writer, styleProvider.getNodeStyleClasses(busNode), StyleProvider.BUSNODE_CLASS);

traversingBusEdges.addAll(graph.getBusEdges(busNode));
}
Expand Down Expand Up @@ -738,8 +732,8 @@ private void drawTextEdges(Graph graph, XMLStreamWriter writer) throws XMLStream

private void drawTextEdge(XMLStreamWriter writer, TextEdge edge, VoltageLevelNode vlNode) throws XMLStreamException {
writer.writeEmptyElement(POLYLINE_ELEMENT_NAME);
writer.writeAttribute(ID_ATTRIBUTE, getPrefixedId(edge.getDiagramId()));
addStylesIfAny(writer, styleProvider.getEdgeStyleClasses(edge));
writeId(writer, edge);
writeStyleClasses(writer, styleProvider.getEdgeStyleClasses(edge));
List<Point> points = edge.getPoints();
shiftEdgeStart(points, vlNode);
String lineFormatted1 = points.stream()
Expand All @@ -748,10 +742,21 @@ private void drawTextEdge(XMLStreamWriter writer, TextEdge edge, VoltageLevelNod
writer.writeAttribute(POINTS_ATTRIBUTE, lineFormatted1);
}

private void addStylesIfAny(XMLStreamWriter writer, List<String> edgeStyleClasses) throws XMLStreamException {
if (!edgeStyleClasses.isEmpty()) {
writer.writeAttribute(CLASS_ATTRIBUTE, String.join(" ", edgeStyleClasses));
private void writeStyleClasses(XMLStreamWriter writer, String... additionalClasses) throws XMLStreamException {
writeStyleClasses(writer, Collections.emptyList(), additionalClasses);
}

private void writeStyleClasses(XMLStreamWriter writer, List<String> edgeStyleClasses, String... additionalClasses) throws XMLStreamException {
if (edgeStyleClasses.isEmpty() && additionalClasses.length == 0) {
return;
}
List<String> allClasses = new ArrayList<>(edgeStyleClasses);
allClasses.addAll(Arrays.asList(additionalClasses));
writer.writeAttribute(CLASS_ATTRIBUTE, String.join(" ", allClasses));
}

private void writeId(XMLStreamWriter writer, Identifiable identifiable) throws XMLStreamException {
writer.writeAttribute(ID_ATTRIBUTE, getPrefixedId(identifiable.getDiagramId()));
}

private void shiftEdgeStart(List<Point> points, VoltageLevelNode vlNode) {
Expand Down Expand Up @@ -834,7 +839,10 @@ private void addMetadata(Graph graph, XMLStreamWriter writer) throws XMLStreamEx
graph.getBusNodesStream().forEach(busNode -> metadata.addBusNode(getPrefixedId(busNode.getDiagramId()), busNode.getEquipmentId()));
graph.getNodesStream().forEach(node -> metadata.addNode(getPrefixedId(node.getDiagramId()), node.getEquipmentId(),
getFormattedValue(node.getX()), getFormattedValue(node.getY())));
graph.getEdgesStream().forEach(edge -> metadata.addEdge(getPrefixedId(edge.getDiagramId()), edge.getEquipmentId(),
graph.getBranchEdgeStream().forEach(edge -> metadata.addEdge(getPrefixedId(edge.getDiagramId()), edge.getEquipmentId(),
getPrefixedId(graph.getNode1(edge).getDiagramId()),
getPrefixedId(graph.getNode2(edge).getDiagramId())));
graph.getThreeWtEdgesStream().forEach(edge -> metadata.addEdge(getPrefixedId(edge.getDiagramId()), edge.getEquipmentId(),
getPrefixedId(graph.getNode1(edge).getDiagramId()),
getPrefixedId(graph.getNode2(edge).getDiagramId())));

Expand Down
16 changes: 9 additions & 7 deletions network-area-diagram/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.
Loading

0 comments on commit 9efaa54

Please sign in to comment.