Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove direction from FeederInfo class #387

Merged
merged 3 commits into from
May 16, 2022
Merged
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/**
* Copyright (c) 2019, RTE (http://www.rte-france.com)
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
package com.powsybl.sld.svg;

import java.util.Objects;
import java.util.Optional;

/**
* Class used to describe an information element which is displayed below feeders, which contains one or more of the following:
* <ul>
* <li>a string on its right</li>
* <li>a string on its left</li>
* </ul>
* Each of these two element part is optional
*
* @author Giovanni Ferrari <giovanni.ferrari at techrain.eu>
* @author Thomas Adam <tadam at silicom.fr>
*/
public abstract class AbstractFeederInfo implements FeederInfo {

private final String componentType;
private final String leftLabel;
private final String rightLabel;
private final String userDefinedId;

AbstractFeederInfo(String componentType, String leftLabel, String rightLabel, String userDefinedId) {
this.componentType = Objects.requireNonNull(componentType);
this.leftLabel = leftLabel;
this.rightLabel = rightLabel;
this.userDefinedId = userDefinedId;
}

public String getUserDefinedId() {
return userDefinedId;
}

public String getComponentType() {
return componentType;
}

public Optional<String> getLeftLabel() {
return Optional.ofNullable(leftLabel);
}

public Optional<String> getRightLabel() {
return Optional.ofNullable(rightLabel);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -243,19 +243,19 @@ private LabelPosition getBusLabelPosition() {

private List<FeederInfo> buildFeederInfos(ThreeWindingsTransformer transformer, ThreeWindingsTransformer.Side side) {
return Arrays.asList(
new FeederInfo(ARROW_ACTIVE, transformer.getTerminal(side).getP()),
new FeederInfo(ARROW_REACTIVE, transformer.getTerminal(side).getQ()));
new DirectionalFeederInfo(ARROW_ACTIVE, transformer.getTerminal(side).getP()),
new DirectionalFeederInfo(ARROW_REACTIVE, transformer.getTerminal(side).getQ()));
}

private List<FeederInfo> buildFeederInfos(Injection injection) {
return Arrays.asList(
new FeederInfo(ARROW_ACTIVE, injection.getTerminal().getP()),
new FeederInfo(ARROW_REACTIVE, injection.getTerminal().getQ()));
new DirectionalFeederInfo(ARROW_ACTIVE, injection.getTerminal().getP()),
new DirectionalFeederInfo(ARROW_REACTIVE, injection.getTerminal().getQ()));
}

private List<FeederInfo> buildFeederInfos(Branch branch, Branch.Side side) {
return Arrays.asList(
new FeederInfo(ARROW_ACTIVE, branch.getTerminal(side).getP()),
new FeederInfo(ARROW_REACTIVE, branch.getTerminal(side).getQ()));
new DirectionalFeederInfo(ARROW_ACTIVE, branch.getTerminal(side).getP()),
new DirectionalFeederInfo(ARROW_REACTIVE, branch.getTerminal(side).getQ()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import com.powsybl.sld.model.graphs.*;
import com.powsybl.sld.model.nodes.Node;
import com.powsybl.sld.model.nodes.*;
import com.powsybl.sld.svg.DiagramLabelProvider.LabelDirection;
import com.powsybl.sld.svg.GraphMetadata.FeederInfoMetadata;
import com.powsybl.sld.util.DomUtil;
import org.apache.commons.io.IOUtils;
Expand Down Expand Up @@ -844,19 +843,18 @@ protected void insertFeederInfos(String prefixId,
VoltageLevelGraph graph,
FeederNode feederNode,
GraphMetadata metadata,
DiagramLabelProvider labelProvider) {
DiagramLabelProvider labelProvider,
DiagramStyleProvider styleProvider) {
if (points.isEmpty()) {
points.add(graph.getShiftedPoint(feederNode));
points.add(graph.getShiftedPoint(feederNode));
}

double shiftFeederInfo = 0;
for (FeederInfo feederInfo : labelProvider.getFeederInfos(feederNode)) {
if (!feederInfo.isEmpty()) {
drawFeederInfo(prefixId, feederNode, points, root, feederInfo, shiftFeederInfo, metadata);
addInfoComponentMetadata(metadata, feederInfo.getComponentType());
}
// Compute shifting even if not displayed to ensure aligned feeder info
drawFeederInfo(prefixId, feederNode, points, root, feederInfo, shiftFeederInfo, metadata, styleProvider);
addInfoComponentMetadata(metadata, feederInfo.getComponentType());

double height = componentLibrary.getSize(feederInfo.getComponentType()).getHeight();
shiftFeederInfo += layoutParameters.getFeederInfosIntraMargin() + height;
}
Expand All @@ -873,7 +871,8 @@ private void addInfoComponentMetadata(GraphMetadata metadata, String componentTy
}

private void drawFeederInfo(String prefixId, FeederNode feederNode, List<Point> points, Element root,
FeederInfo feederInfo, double shift, GraphMetadata metadata) {
FeederInfo feederInfo, double shift, GraphMetadata metadata,
DiagramStyleProvider styleProvider) {

Element g = root.getOwnerDocument().createElement(GROUP);
ComponentSize size = componentLibrary.getSize(feederInfo.getComponentType());
Expand All @@ -892,12 +891,10 @@ private void drawFeederInfo(String prefixId, FeederNode feederNode, List<Point>
String side = feederNode instanceof FeederWithSideNode ? ((FeederWithSideNode) feederNode).getSide().name() : null;
metadata.addFeederInfoMetadata(new FeederInfoMetadata(svgId, feederNode.getEquipmentId(), side, feederInfo.getUserDefinedId()));

// we draw the feeder info only if direction is present
feederInfo.getDirection().ifPresent(direction -> {
double rotationAngle = points.get(0).getY() > points.get(1).getY() ? 180 : 0;
insertFeederInfoSVGIntoDocumentSVG(feederInfo, prefixId, g, rotationAngle);
styles.add(direction == LabelDirection.OUT ? OUT_CLASS : IN_CLASS);
});
// we draw the feeder info
double rotationAngle = points.get(0).getY() > points.get(1).getY() ? 180 : 0;
insertFeederInfoSVGIntoDocumentSVG(feederInfo, prefixId, g, rotationAngle);
styleProvider.getFeederInfoStyle(feederInfo).ifPresent(styles::add);

// we draw the right label only if present
feederInfo.getRightLabel().ifPresent(s -> {
Expand Down Expand Up @@ -1020,11 +1017,11 @@ protected void drawEdges(String prefixId, Element root, VoltageLevelGraph graph,

if (edge.getNode1() instanceof FeederNode) {
if (!(edge.getNode2() instanceof FeederNode)) {
insertFeederInfos(prefixId, pol, root, graph, (FeederNode) edge.getNode1(), metadata, initProvider);
insertFeederInfos(prefixId, pol, root, graph, (FeederNode) edge.getNode1(), metadata, initProvider, styleProvider);
}
} else if (edge.getNode2() instanceof FeederNode) {
Collections.reverse(pol);
insertFeederInfos(prefixId, pol, root, graph, (FeederNode) edge.getNode2(), metadata, initProvider);
insertFeederInfos(prefixId, pol, root, graph, (FeederNode) edge.getNode2(), metadata, initProvider, styleProvider);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
import java.util.List;
import java.util.Optional;

import static com.powsybl.sld.svg.DiagramStyles.IN_CLASS;
import static com.powsybl.sld.svg.DiagramStyles.OUT_CLASS;

/**
* @author Giovanni Ferrari <giovanni.ferrari at techrain.eu>
* @author Franck Lecuyer <franck.lecuyer at rte-france.com>
Expand Down Expand Up @@ -44,4 +47,11 @@ public interface DiagramStyleProvider {
default Optional<String> getBusInfoStyle(BusInfo info) {
return Optional.empty();
}

default Optional<String> getFeederInfoStyle(FeederInfo info) {
if (info instanceof DirectionalFeederInfo) {
return Optional.of(((DirectionalFeederInfo) info).getDirection() == DiagramLabelProvider.LabelDirection.OUT ? OUT_CLASS : IN_CLASS);
}
return Optional.empty();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.powsybl.sld.svg;

import java.util.Objects;

/**
* Class used to specify a directional element
* (see FeederInfo & AbstractFeederInfo)
*
* @author Thomas Adam <tadam at silicom.fr>
*/
public class DirectionalFeederInfo extends AbstractFeederInfo {

private final DiagramLabelProvider.LabelDirection arrowDirection;

public DirectionalFeederInfo(String componentType, DiagramLabelProvider.LabelDirection arrowDirection, String leftLabel, String rightLabel) {
this(componentType, arrowDirection, leftLabel, rightLabel, null);
}

public DirectionalFeederInfo(String componentType, DiagramLabelProvider.LabelDirection arrowDirection, String leftLabel, String rightLabel, String userDefinedId) {
super(componentType, leftLabel, rightLabel, userDefinedId);
this.arrowDirection = Objects.requireNonNull(arrowDirection);
}

public DirectionalFeederInfo(String componentType, double value) {
this(componentType, value, null);
}

public DirectionalFeederInfo(String componentType, double value, String userDefinedId) {
this(componentType, value > 0 ? DiagramLabelProvider.LabelDirection.OUT : DiagramLabelProvider.LabelDirection.IN, null, String.valueOf(Math.round(value)), userDefinedId);
}

public DiagramLabelProvider.LabelDirection getDirection() {
return arrowDirection;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,72 +6,18 @@
*/
package com.powsybl.sld.svg;

import java.util.Objects;
import java.util.Optional;

import com.powsybl.sld.svg.DiagramLabelProvider.LabelDirection;

/**
* Class used to describe an information element which is displayed below feeders, which contains one or more of the following:
* <ul>
* <li>an arrow whose direction is specified</li>
* <li>a string on its right</li>
* <li>a string on its left</li>
* </ul>
* Each of these three element part is optional
*
* @author Giovanni Ferrari <giovanni.ferrari at techrain.eu>
* @author Thomas Adam <tadam at silicom.fr>
*/
public class FeederInfo {

private final String componentType;
private final LabelDirection arrowDirection;
private final String leftLabel;
private final String rightLabel;
private final String userDefinedId;

public FeederInfo(String componentType, LabelDirection arrowDirection, String leftLabel, String rightLabel) {
this(componentType, arrowDirection, leftLabel, rightLabel, null);
}

public FeederInfo(String componentType, LabelDirection arrowDirection, String leftLabel, String rightLabel, String userDefinedId) {
this.componentType = Objects.requireNonNull(componentType);
this.arrowDirection = arrowDirection;
this.leftLabel = leftLabel;
this.rightLabel = rightLabel;
this.userDefinedId = userDefinedId;
}

public FeederInfo(String componentType, double value) {
this(componentType, value, null);
}

public FeederInfo(String componentType, double value, String userDefinedId) {
this(componentType, value > 0 ? LabelDirection.OUT : LabelDirection.IN, null, String.valueOf(Math.round(value)), userDefinedId);
}

public boolean isEmpty() {
return getDirection().isEmpty() && getLeftLabel().isEmpty() || getRightLabel().isEmpty();
}

public String getUserDefinedId() {
return userDefinedId;
}
public interface FeederInfo {

public String getComponentType() {
return componentType;
}
String getUserDefinedId();

public Optional<LabelDirection> getDirection() {
return Optional.ofNullable(arrowDirection);
}
String getComponentType();

public Optional<String> getLeftLabel() {
return Optional.ofNullable(leftLabel);
}
Optional<String> getLeftLabel();

public Optional<String> getRightLabel() {
return Optional.ofNullable(rightLabel);
}
Optional<String> getRightLabel();
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,7 @@
import com.powsybl.sld.model.nodes.FeederNode;
import com.powsybl.sld.model.nodes.Node;
import com.powsybl.sld.model.graphs.VoltageLevelGraph;
import com.powsybl.sld.svg.BasicStyleProvider;
import com.powsybl.sld.svg.DefaultDiagramLabelProvider;
import com.powsybl.sld.svg.DiagramLabelProvider;
import com.powsybl.sld.svg.FeederInfo;
import com.powsybl.sld.svg.*;
import org.junit.Before;
import org.junit.Test;

Expand Down Expand Up @@ -67,11 +64,11 @@ public void setUp() {
@Override
public List<FeederInfo> getFeederInfos(FeederNode node) {
List<FeederInfo> feederInfos = Arrays.asList(
new FeederInfo(ARROW_ACTIVE, LabelDirection.OUT, null, "10", null),
new FeederInfo(ARROW_REACTIVE, LabelDirection.IN, null, "20", null),
new FeederInfo(ARROW_REACTIVE, LabelDirection.IN, null, "30", null),
new FeederInfo(ARROW_ACTIVE, null, null, "40", null), // Not displayed
new FeederInfo(ARROW_ACTIVE, LabelDirection.OUT, null, "50", null));
new DirectionalFeederInfo(ARROW_ACTIVE, LabelDirection.OUT, null, "10"),
new DirectionalFeederInfo(ARROW_REACTIVE, LabelDirection.IN, null, "20", null),
new DirectionalFeederInfo(ARROW_REACTIVE, LabelDirection.IN, null, "30", null),
new DirectionalFeederInfo(ARROW_ACTIVE, LabelDirection.OUT, null, "40", null),
new DirectionalFeederInfo(ARROW_ACTIVE, LabelDirection.OUT, null, "50", null));
boolean feederArrowSymmetry = node.getDirection() == Direction.TOP || layoutParameters.isFeederInfoSymmetry();
if (!feederArrowSymmetry) {
Collections.reverse(feederInfos);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -596,8 +596,8 @@ public void setUp() {
@Override
public List<FeederInfo> getFeederInfos(FeederNode node) {
List<FeederInfo> feederInfos = Arrays.asList(
new FeederInfo(ARROW_ACTIVE, LabelDirection.OUT, null, "10", null),
new FeederInfo(ARROW_REACTIVE, LabelDirection.IN, null, "20", null));
new DirectionalFeederInfo(ARROW_ACTIVE, LabelDirection.OUT, null, "10", null),
new DirectionalFeederInfo(ARROW_REACTIVE, LabelDirection.IN, null, "20", null));
boolean feederArrowSymmetry = node.getDirection() == TOP || layoutParameters.isFeederInfoSymmetry();
if (!feederArrowSymmetry) {
Collections.reverse(feederInfos);
Expand All @@ -616,9 +616,7 @@ public List<DiagramLabelProvider.NodeDecorator> getNodeDecorators(Node node, Dir
noFeederInfoProvider = new DefaultDiagramLabelProvider(Network.create("empty", ""), componentLibrary, layoutParameters) {
@Override
public List<FeederInfo> getFeederInfos(FeederNode node) {
return Arrays.asList(
new FeederInfo(ARROW_ACTIVE, null, null, null, null),
new FeederInfo(ARROW_REACTIVE, null, null, null, null));
return Collections.emptyList();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,7 @@
import com.powsybl.sld.model.graphs.Graph;
import com.powsybl.sld.model.nodes.FeederNode;
import com.powsybl.sld.model.nodes.Node;
import com.powsybl.sld.svg.BasicStyleProvider;
import com.powsybl.sld.svg.DiagramLabelProvider;
import com.powsybl.sld.svg.FeederInfo;
import com.powsybl.sld.svg.LabelPosition;
import com.powsybl.sld.svg.*;

import java.util.*;
import java.util.stream.Stream;
Expand Down Expand Up @@ -58,8 +55,8 @@ private Optional<String> getLabelOrNameOrId(Node node) {
@Override
public List<FeederInfo> getFeederInfos(FeederNode node) {
return Arrays.asList(
new FeederInfo(ARROW_ACTIVE, LabelDirection.OUT, "", "tata", null),
new FeederInfo(ARROW_REACTIVE, LabelDirection.IN, "", "tutu", null));
new DirectionalFeederInfo(ARROW_ACTIVE, LabelDirection.OUT, "", "tata", null),
new DirectionalFeederInfo(ARROW_REACTIVE, LabelDirection.IN, "", "tutu", null));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,6 @@ public void test() {
assertTrue(feederInfos1.get(1).getRightLabel().isPresent());
assertFalse(feederInfos1.get(0).getLeftLabel().isPresent());
assertFalse(feederInfos1.get(1).getLeftLabel().isPresent());
assertTrue(feederInfos1.get(0).getDirection().isPresent());
assertTrue(feederInfos1.get(1).getDirection().isPresent());

List<FeederInfo> feederInfos2 = labelProvider.getFeederInfos((FeederNode) g.getNode("vsc"));
assertEquals(2, feederInfos2.size());
Expand All @@ -130,8 +128,6 @@ public void test() {
assertTrue(feederInfos2.get(1).getRightLabel().isPresent());
assertFalse(feederInfos2.get(0).getLeftLabel().isPresent());
assertFalse(feederInfos2.get(1).getLeftLabel().isPresent());
assertTrue(feederInfos2.get(0).getDirection().isPresent());
assertTrue(feederInfos2.get(1).getDirection().isPresent());

List<FeederInfo> feederInfos3 = labelProvider.getFeederInfos((FeederNode) g.getNode("C1"));
assertEquals(2, feederInfos3.size());
Expand All @@ -141,8 +137,6 @@ public void test() {
assertTrue(feederInfos3.get(1).getRightLabel().isPresent());
assertFalse(feederInfos3.get(0).getLeftLabel().isPresent());
assertFalse(feederInfos3.get(1).getLeftLabel().isPresent());
assertTrue(feederInfos3.get(0).getDirection().isPresent());
assertTrue(feederInfos3.get(1).getDirection().isPresent());

List<FeederInfo> feederInfos4 = labelProvider.getFeederInfos((FeederNode) g.getNode("dl1"));
assertEquals(2, feederInfos4.size());
Expand All @@ -152,8 +146,6 @@ public void test() {
assertTrue(feederInfos4.get(1).getRightLabel().isPresent());
assertFalse(feederInfos4.get(0).getLeftLabel().isPresent());
assertFalse(feederInfos4.get(1).getLeftLabel().isPresent());
assertTrue(feederInfos4.get(0).getDirection().isPresent());
assertTrue(feederInfos4.get(1).getDirection().isPresent());

// Reverse order
layoutParameters.setFeederInfoSymmetry(false);
Expand Down
Loading