Skip to content
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
16 changes: 14 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@
<liquibase-hibernate-package>com.powsybl.sld.server</liquibase-hibernate-package>
<powsybl-ws-dependencies.version>2.29.0</powsybl-ws-dependencies.version>
<supercsv.version>2.4.0</supercsv.version>
<!-- FIXME to remove at next upgrade of powsybl-ws-dependencies -->

Check warning on line 55 in pom.xml

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Take the required action to fix the issue indicated by this "FIXME" comment.

See more on https://sonarcloud.io/project/issues?id=com.powsybl%3Apowsybl-single-line-diagram-server&issues=AZsDuC3Kf5DgXOM5l5qy&open=AZsDuC3Kf5DgXOM5l5qy&pullRequest=188
<powsybl-diagram.version>5.1.0</powsybl-diagram.version>
</properties>

<build>
Expand Down Expand Up @@ -123,14 +125,24 @@
<dependency>
<groupId>com.powsybl</groupId>
<artifactId>powsybl-network-area-diagram</artifactId>
<!-- FIXME to remove at next upgrade of powsybl-ws-dependencies -->

Check warning on line 128 in pom.xml

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Take the required action to fix the issue indicated by this "FIXME" comment.

See more on https://sonarcloud.io/project/issues?id=com.powsybl%3Apowsybl-single-line-diagram-server&issues=AZsDuC3Kf5DgXOM5l5qz&open=AZsDuC3Kf5DgXOM5l5qz&pullRequest=188
<version>${powsybl-diagram.version}</version>
</dependency>
<dependency>
<groupId>com.powsybl</groupId>
<artifactId>powsybl-network-store-client</artifactId>
<artifactId>powsybl-single-line-diagram-core</artifactId>
<!-- FIXME to remove at next upgrade of powsybl-ws-dependencies -->

Check warning on line 134 in pom.xml

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Take the required action to fix the issue indicated by this "FIXME" comment.

See more on https://sonarcloud.io/project/issues?id=com.powsybl%3Apowsybl-single-line-diagram-server&issues=AZsDuC3Kf5DgXOM5l5q0&open=AZsDuC3Kf5DgXOM5l5q0&pullRequest=188
<version>${powsybl-diagram.version}</version>
</dependency>
<!-- FIXME to remove at next upgrade of powsybl-ws-dependencies -->

Check warning on line 137 in pom.xml

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Take the required action to fix the issue indicated by this "FIXME" comment.

See more on https://sonarcloud.io/project/issues?id=com.powsybl%3Apowsybl-single-line-diagram-server&issues=AZsDuC3Kf5DgXOM5l5q1&open=AZsDuC3Kf5DgXOM5l5q1&pullRequest=188
<dependency>
<groupId>com.powsybl</groupId>
<artifactId>powsybl-single-line-diagram-core</artifactId>
<artifactId>powsybl-diagram-util</artifactId>
<version>${powsybl-diagram.version}</version>
</dependency>
<dependency>
<groupId>com.powsybl</groupId>
<artifactId>powsybl-network-store-client</artifactId>
</dependency>
<dependency>
<groupId>com.powsybl</groupId>
Expand Down
84 changes: 9 additions & 75 deletions src/main/java/com/powsybl/sld/server/CommonLabelProvider.java
Original file line number Diff line number Diff line change
@@ -1,71 +1,30 @@
/**
* Copyright (c) 2025, 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.server;

import com.powsybl.iidm.network.*;
import com.powsybl.iidm.network.extensions.OperatingStatus;
import com.powsybl.sld.layout.LayoutParameters;
import com.powsybl.sld.library.SldComponentLibrary;
import com.powsybl.sld.model.coordinate.Direction;
import com.powsybl.sld.model.graphs.VoltageLevelGraph;
import com.powsybl.sld.model.nodes.*;
import com.powsybl.sld.svg.BusLegendInfo;
import com.powsybl.sld.svg.DefaultLabelProvider;
import com.powsybl.sld.svg.LabelProviderFactory;
import com.powsybl.sld.svg.SvgParameters;

import java.util.*;
import java.util.stream.DoubleStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

public class CommonLabelProvider extends DefaultLabelProvider {
private static final String UNIT_MW = "MW";
private static final String UNIT_KV = "kV";
private static final String UNIT_KA = "kA";

private static final String PREFIX_VOLTAGE = "U = ";
private static final String PREFIX_ANGLE = "θ = ";
private static final String PREFIX_PRODUCTION = "P = ";
private static final String PREFIX_CONSUMPTION = "C = ";
private static final String PREFIX_ICC = "ICC = ";

private static final String KEY_BUS_ID = "busId";
public static final String KEY_VOLTAGE = "v";
public static final String KEY_ANGLE = "angle";
public static final String KEY_CONSUMPTION = "consumption-sum";
public static final String KEY_PRODUCTION = "production-sum";
public static final String KEY_ICC = "icc";

private static final String PLANNED_OUTAGE_BRANCH_NODE_DECORATOR = "LOCK";
private static final String FORCED_OUTAGE_BRANCH_NODE_DECORATOR = "FLASH";

private final Map<String, Double> busIdToIccMap;

public CommonLabelProvider(Network network, SldComponentLibrary componentLibrary, LayoutParameters layoutParameters, SvgParameters svgParameters) {
super(network, componentLibrary, layoutParameters, svgParameters);
this.busIdToIccMap = null;
}

public CommonLabelProvider(Network network, SldComponentLibrary componentLibrary, LayoutParameters layoutParameters, SvgParameters svgParameters, Map<String, Double> busIdToIccMap) {
super(network, componentLibrary, layoutParameters, svgParameters);
this.busIdToIccMap = busIdToIccMap;
}

public static LabelProviderFactory newCommonLabelProviderFactory(Map<String, Double> busIdToIccMap) {
return (network, compLibrary, layoutParameters, svgParameters) -> new CommonLabelProvider(network, compLibrary, layoutParameters, svgParameters, busIdToIccMap);
}

@Override
public List<BusLegendInfo> getBusLegendInfos(VoltageLevelGraph graph) {
VoltageLevel vl = network.getVoltageLevel(graph.getVoltageLevelInfos().getId());
return vl.getBusView().getBusStream()
.map(b ->
new BusLegendInfo(b.getId(), List.of(
new BusLegendInfo.Caption(b.getId(), KEY_BUS_ID),
new BusLegendInfo.Caption(PREFIX_VOLTAGE + valueFormatter.formatVoltage(b.getV(), UNIT_KV), KEY_VOLTAGE),
new BusLegendInfo.Caption(PREFIX_ANGLE + valueFormatter.formatAngleInDegrees(b.getAngle()), KEY_ANGLE),
new BusLegendInfo.Caption(PREFIX_PRODUCTION + formatPowerSum(b.getGeneratorStream().mapToDouble(g -> g.getTerminal().getP())), KEY_CONSUMPTION),
new BusLegendInfo.Caption(PREFIX_CONSUMPTION + formatPowerSum(b.getLoadStream().mapToDouble(l -> l.getTerminal().getP())), KEY_PRODUCTION),
new BusLegendInfo.Caption(PREFIX_ICC + getFormattedBusIcc(b.getId()), KEY_ICC)
))
).toList();
}

@Override
Expand Down Expand Up @@ -161,29 +120,4 @@ private NodeDecorator addOperatingStatusDecorator(Node node, Direction direction
case null, default -> new NodeDecorator(decoratorType, getGenericDecoratorPosition());
};
}

private String getFormattedBusIcc(String busId) {
Double iccInA = busIdToIccMap == null ? null : busIdToIccMap.get(busId);

String value = iccInA != null
? String.format(Locale.US, "%.1f", iccInA / 1000.0)
: svgParameters.getUndefinedValueSymbol();

return value + " " + UNIT_KA;
}

private String formatPowerSum(DoubleStream stream) {
OptionalDouble sum = sumDoubleStream(stream);
String value = sum.isPresent()
? String.valueOf(Math.round(Math.abs(sum.getAsDouble())))
: svgParameters.getUndefinedValueSymbol();
return value + " " + UNIT_MW;
}

private static OptionalDouble sumDoubleStream(DoubleStream stream) {
DoubleSummaryStatistics stats = stream.summaryStatistics();
return stats.getCount() == 0
? OptionalDouble.empty()
: OptionalDouble.of(stats.getSum());
}
}
86 changes: 86 additions & 0 deletions src/main/java/com/powsybl/sld/server/CommonLegendWriter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/**
* Copyright (c) 2025, 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.server;

import com.powsybl.iidm.network.Network;
import com.powsybl.iidm.network.VoltageLevel;
import com.powsybl.sld.model.graphs.VoltageLevelGraph;
import com.powsybl.sld.svg.*;

import java.util.*;
import java.util.stream.DoubleStream;

public class CommonLegendWriter extends DefaultSVGLegendWriter {
private static final String UNIT_MW = "MW";
private static final String UNIT_KV = "kV";
private static final String UNIT_KA = "kA";

private static final String PREFIX_VOLTAGE = "U = ";
private static final String PREFIX_ANGLE = "θ = ";
private static final String PREFIX_PRODUCTION = "P = ";
private static final String PREFIX_CONSUMPTION = "C = ";
private static final String PREFIX_ICC = "ICC = ";

private static final String KEY_BUS_ID = "busId";
public static final String KEY_VOLTAGE = "v";
public static final String KEY_ANGLE = "angle";
public static final String KEY_CONSUMPTION = "consumption-sum";
public static final String KEY_PRODUCTION = "production-sum";
public static final String KEY_ICC = "icc";

private final Map<String, Double> iccByBusId;

public static LegendWriterFactory createFactory(Map<String, Double> iccByBusId) {
return (network, svgParameters) -> new CommonLegendWriter(network, svgParameters, iccByBusId);
}

public CommonLegendWriter(Network network, SvgParameters svgParameters, Map<String, Double> iccByBusId) {
super(network, svgParameters);
this.iccByBusId = iccByBusId;
}

@Override
protected List<BusLegendInfo> getBusLegendInfos(VoltageLevelGraph graph) {
VoltageLevel vl = network.getVoltageLevel(graph.getVoltageLevelInfos().getId());
return vl.getBusView().getBusStream()
.map(b ->
new BusLegendInfo(b.getId(), List.of(
new BusLegendInfo.Caption(b.getId(), KEY_BUS_ID),
new BusLegendInfo.Caption(PREFIX_VOLTAGE + valueFormatter.formatVoltage(b.getV(), UNIT_KV), KEY_VOLTAGE),
new BusLegendInfo.Caption(PREFIX_ANGLE + valueFormatter.formatAngleInDegrees(b.getAngle()), KEY_ANGLE),
new BusLegendInfo.Caption(PREFIX_PRODUCTION + formatPowerSum(b.getGeneratorStream().mapToDouble(g -> g.getTerminal().getP())), KEY_CONSUMPTION),
new BusLegendInfo.Caption(PREFIX_CONSUMPTION + formatPowerSum(b.getLoadStream().mapToDouble(l -> l.getTerminal().getP())), KEY_PRODUCTION),
new BusLegendInfo.Caption(PREFIX_ICC + getFormattedBusIcc(b.getId()), KEY_ICC)
))
).toList();
}

private String getFormattedBusIcc(String busId) {
Double iccInA = iccByBusId == null ? null : iccByBusId.get(busId);

String value = iccInA != null
? String.format(Locale.US, "%.1f", iccInA / 1000.0)
: svgParameters.getUndefinedValueSymbol();

return value + " " + UNIT_KA;
}

private String formatPowerSum(DoubleStream stream) {
OptionalDouble sum = sumDoubleStream(stream);
String value = sum.isPresent()
? String.valueOf(Math.round(Math.abs(sum.getAsDouble())))
: svgParameters.getUndefinedValueSymbol();
return value + " " + UNIT_MW;
}

private static OptionalDouble sumDoubleStream(DoubleStream stream) {
DoubleSummaryStatistics stats = stream.summaryStatistics();
return stats.getCount() == 0
? OptionalDouble.empty()
: OptionalDouble.of(stats.getSum());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,8 @@ SvgAndMetadata generateSvgAndMetadata(UUID networkUuid, String variantId, String
break;
case SldDisplayMode.STATE_VARIABLE:
svgParameters.setBusesLegendAdded(true);
sldParameters.setLabelProviderFactory(CommonLabelProvider.newCommonLabelProviderFactory(sldRequestInfos.getBusIdToIccValues()));
sldParameters.setLabelProviderFactory(CommonLabelProvider::new);
sldParameters.setLegendWriterFactory(CommonLegendWriter.createFactory(sldRequestInfos.getBusIdToIccValues()));
break;
default:
throw new PowsyblException(String.format("Given sld display mode %s doesn't exist", sldRequestInfos.getSldDisplayMode()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import java.util.Collections;
import java.util.List;

import static com.powsybl.sld.server.CommonLabelProvider.*;
import static com.powsybl.sld.server.CommonLegendWriter.*;

/**
* @author Kevin Le Saulnier <kevin.lesaulnier at rte-france.com>
Expand Down