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

Add prefix on svg id #76

Merged
merged 6 commits into from
Jun 15, 2022
Merged
Show file tree
Hide file tree
Changes from 5 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
8 changes: 1 addition & 7 deletions src/main/java/com/powsybl/nad/build/iidm/IntIdProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,9 @@
* @author Florian Dupuy <florian.dupuy at rte-france.com>
*/
public class IntIdProvider implements IdProvider {
private final String prefix;
private int count;

public IntIdProvider() {
this("");
}

public IntIdProvider(String prefix) {
this.prefix = prefix;
count = 0;
}

Expand All @@ -36,6 +30,6 @@ public String createId(ThreeWindingsTransformer.Leg leg) {
}

private String nextId() {
return prefix + (count++);
return String.valueOf(count++);
}
}
30 changes: 22 additions & 8 deletions src/main/java/com/powsybl/nad/svg/GraphMetadata.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import java.io.InputStream;
import java.util.*;
import java.util.function.UnaryOperator;
import java.util.stream.Collectors;
import java.util.stream.Stream;

Expand All @@ -35,22 +36,27 @@ public class GraphMetadata {
private static final String DIAGRAM_ID_ATTRIBUTE = "diagramId";
private static final String EQUIPMENT_ID_ATTRIBUTE = "equipmentId";

private final Map<String, String> nodeIdByDiagramId = new TreeMap<>(Comparator.comparingInt(Integer::valueOf));
private final Map<String, String> nodeIdByDiagramId = new LinkedHashMap<>();

private final Map<String, String> edgeIdByDiagramId = new TreeMap<>(Comparator.comparingInt(Integer::valueOf));
private final Map<String, String> edgeIdByDiagramId = new LinkedHashMap<>();

private final UnaryOperator<String> diagramIdFunction;

public GraphMetadata() {
this(Collections.emptyList(), Collections.emptyList());
this(Collections.emptyList(), Collections.emptyList(), null);
}

public GraphMetadata(Stream<Node> nodes,
Stream<Edge> edges) {
this(nodes.collect(Collectors.toUnmodifiableList()), edges.collect(Collectors.toUnmodifiableList()));
Stream<Edge> edges,
UnaryOperator<String> diagramIdFunction) {
this(nodes.collect(Collectors.toUnmodifiableList()), edges.collect(Collectors.toUnmodifiableList()), diagramIdFunction);
}

public GraphMetadata(List<Node> nodes,
List<Edge> edges) {
List<Edge> edges,
UnaryOperator<String> diagramIdFunction) {

this.diagramIdFunction = diagramIdFunction;
nodes.forEach(this::addNode);
edges.forEach(this::addEdge);
}
Expand Down Expand Up @@ -120,11 +126,19 @@ private void writeIdMapping(String rootElementName, String tagElementName, Map<S

public void addNode(Node node) {
Objects.requireNonNull(node);
nodeIdByDiagramId.put(node.getDiagramId(), node.getEquipmentId());
String svgId = node.getDiagramId();
if (diagramIdFunction != null) {
svgId = diagramIdFunction.apply(svgId);
}
nodeIdByDiagramId.put(svgId, node.getEquipmentId());
}

public void addEdge(Edge edge) {
Objects.requireNonNull(edge);
edgeIdByDiagramId.put(edge.getDiagramId(), edge.getEquipmentId());
String svgId = edge.getDiagramId();
if (diagramIdFunction != null) {
svgId = diagramIdFunction.apply(svgId);
}
edgeIdByDiagramId.put(svgId, edge.getEquipmentId());
}
}
11 changes: 11 additions & 0 deletions src/main/java/com/powsybl/nad/svg/SvgParameters.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public class SvgParameters {
private double loopControlDistance = 0.4;
private boolean textNodeBackground = true;
private boolean edgeInfoAlongEdge = true;
private String svgPrefix = "";

public enum CssLocation {
INSERTED_IN_SVG, EXTERNAL_IMPORTED, EXTERNAL_NO_IMPORT
Expand Down Expand Up @@ -72,6 +73,7 @@ public SvgParameters(SvgParameters other) {
this.loopControlDistance = other.loopControlDistance;
this.textNodeBackground = other.textNodeBackground;
this.edgeInfoAlongEdge = other.edgeInfoAlongEdge;
this.svgPrefix = other.svgPrefix;
}

public Padding getDiagramPadding() {
Expand Down Expand Up @@ -282,4 +284,13 @@ public SvgParameters setEdgeInfoAlongEdge(boolean edgeInfoAlongEdge) {
this.edgeInfoAlongEdge = edgeInfoAlongEdge;
return this;
}

public String getSvgPrefix() {
return svgPrefix;
}

public SvgParameters setSvgPrefix(String svgPrefix) {
this.svgPrefix = svgPrefix;
return this;
}
}
16 changes: 10 additions & 6 deletions src/main/java/com/powsybl/nad/svg/SvgWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ 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, edge.getDiagramId());
writer.writeAttribute(ID_ATTRIBUTE, getPrefixedId(edge.getDiagramId()));
addStylesIfAny(writer, styleProvider.getEdgeStyleClasses(edge));
insertName(writer, edge::getName);

Expand Down Expand Up @@ -221,7 +221,7 @@ private void drawThreeWtEdge(Graph graph, XMLStreamWriter writer, ThreeWtEdge ed
return;
}
writer.writeStartElement(GROUP_ELEMENT_NAME);
writer.writeAttribute(ID_ATTRIBUTE, edge.getDiagramId());
writer.writeAttribute(ID_ATTRIBUTE, getPrefixedId(edge.getDiagramId()));
addStylesIfAny(writer, styleProvider.getEdgeStyleClasses(edge));
insertName(writer, edge::getName);
writer.writeEmptyElement(POLYLINE_ELEMENT_NAME);
Expand Down Expand Up @@ -433,7 +433,7 @@ private void writeTextNode(XMLStreamWriter writer, TextNode textNode, List<Strin
}

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

Expand All @@ -458,7 +458,7 @@ 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, busNode.getDiagramId());
writer.writeAttribute(ID_ATTRIBUTE, getPrefixedId(busNode.getDiagramId()));
addStylesIfAny(writer, styleProvider.getNodeStyleClasses(busNode));
busInnerRadius = busOuterRadius;
traversingBusEdges.addAll(graph.getBusEdges(busNode));
Expand Down Expand Up @@ -572,7 +572,7 @@ 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, edge.getDiagramId());
writer.writeAttribute(ID_ATTRIBUTE, getPrefixedId(edge.getDiagramId()));
addStylesIfAny(writer, styleProvider.getEdgeStyleClasses(edge));
List<Point> points = edge.getPoints();
shiftEdgeStart(points, vlNode);
Expand Down Expand Up @@ -660,7 +660,7 @@ private void addStyle(XMLStreamWriter writer) throws XMLStreamException {
}

private void addMetadata(Graph graph, XMLStreamWriter writer) throws XMLStreamException {
GraphMetadata metadata = new GraphMetadata(graph.getNodesStream(), graph.getEdgesStream());
GraphMetadata metadata = new GraphMetadata(graph.getNodesStream(), graph.getEdgesStream(), this::getPrefixedId);
metadata.writeXml(writer);
}

Expand Down Expand Up @@ -704,4 +704,8 @@ public static double getBusAnnulusOuterRadius(BusNode node, VoltageLevelNode vlN
double unitaryRadius = SvgWriter.getVoltageLevelCircleRadius(vlNode, svgParameters) / (nbNeighbours + 1);
return (node.getIndex() + 1) * unitaryRadius;
}

public String getPrefixedId(String id) {
return svgParameters.getSvgPrefix() + id;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,13 @@ void testIEEE14ForceLayoutWithTextNodes() {
assertEquals(toString("/IEEE_14_bus_text_nodes.svg"), generateSvgString(network, "/IEEE_14_bus_text_nodes.svg"));
}

@Test
void testIEEE14FWithSvgPrefix() {
Network network = IeeeCdfNetworkFactory.create14();
getSvgParameters().setSvgPrefix("test_");
assertEquals(toString("/IEEE_14_id_prefixed.svg"), generateSvgString(network, "/IEEE_14_id_prefixed.svg"));
}

@Test
void testDisconnection() {
Network network = IeeeCdfNetworkFactory.create14();
Expand Down
4 changes: 3 additions & 1 deletion src/test/java/com/powsybl/nad/svg/SvgParametersTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ void test() {
.setLoopEdgesAperture(10.)
.setLoopControlDistance(1.)
.setTextNodeBackground(false)
.setEdgeInfoAlongEdge(false);
.setEdgeInfoAlongEdge(false)
.setSvgPrefix("TestPrefix");

SvgParameters svgParameters1 = new SvgParameters(svgParameters0);

Expand Down Expand Up @@ -70,5 +71,6 @@ void test() {
assertEquals(svgParameters0.getLoopControlDistance(), svgParameters1.getLoopControlDistance());
assertEquals(svgParameters0.isTextNodeBackground(), svgParameters1.isTextNodeBackground());
assertEquals(svgParameters0.isEdgeInfoAlongEdge(), svgParameters1.isEdgeInfoAlongEdge());
assertEquals(svgParameters0.getSvgPrefix(), svgParameters1.getSvgPrefix());
}
}
Loading