Skip to content

Commit 999f53c

Browse files
committed
etienneH code review remarks part 2
1 parent 8bf25d4 commit 999f53c

File tree

4 files changed

+46
-31
lines changed

4 files changed

+46
-31
lines changed

src/main/java/org/gridsuite/network/map/dto/definition/extension/BusbarSectionFinderTraverser.java

Lines changed: 43 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
package org.gridsuite.network.map.dto.definition.extension;
88

99
import com.powsybl.iidm.network.*;
10+
import com.powsybl.iidm.network.extensions.BusbarSectionPosition;
1011

1112
import java.util.*;
1213

@@ -20,15 +21,15 @@ private BusbarSectionFinderTraverser() {
2021
throw new UnsupportedOperationException();
2122
}
2223

23-
private record NodePath(int node, List<SwitchInfo> pathSwitches, SwitchInfo lastSwitch) { }
24+
private record NodePath(int startNode, List<SwitchInfo> traversedSwitches, SwitchInfo lastSwitch) { }
2425

2526
public record SwitchInfo(String id, SwitchKind kind, boolean isOpen) { }
2627

27-
public record BusbarSectionResult(String busbarSectionId, int depth, SwitchInfo lastSwitch) { }
28+
public record BusbarSectionResult(String busbarSectionId, int depth, SwitchInfo lastSwitch, int busbarIndex, int sectionIndex) { }
2829

2930
public static String findBusbarSectionId(Terminal terminal) {
3031
BusbarSectionResult result = getBusbarSectionResult(terminal);
31-
return result != null ? result.busbarSectionId() : null;
32+
return result != null ? result.busbarSectionId() : terminal.getVoltageLevel().getNodeBreakerView().getBusbarSections().iterator().next().getId();
3233
}
3334

3435
public static BusbarSectionResult getBusbarSectionResult(Terminal terminal) {
@@ -44,67 +45,81 @@ public static BusbarSectionResult getBusbarSectionResult(Terminal terminal) {
4445
private static BusbarSectionResult selectBestBusbar(List<BusbarSectionResult> results) {
4546
List<BusbarSectionResult> withoutSwitch = results.stream().filter(r -> r.lastSwitch() == null).toList();
4647
if (!withoutSwitch.isEmpty()) {
47-
return withoutSwitch.stream().min(Comparator.comparingInt(BusbarSectionResult::depth)).orElse(null);
48+
return withoutSwitch.stream().min(Comparator.comparingInt(BusbarSectionResult::depth)
49+
.thenComparingInt(BusbarSectionResult::busbarIndex)
50+
.thenComparingInt(BusbarSectionResult::sectionIndex)).orElse(null);
4851
}
4952
List<BusbarSectionResult> withClosedSwitch = results.stream().filter(r -> r.lastSwitch() != null && !r.lastSwitch().isOpen()).toList();
5053
if (!withClosedSwitch.isEmpty()) {
51-
return withClosedSwitch.stream().min(Comparator.comparingInt(BusbarSectionResult::depth)).orElse(null);
54+
return withClosedSwitch.stream().min(Comparator.comparingInt(BusbarSectionResult::depth)
55+
.thenComparingInt(BusbarSectionResult::busbarIndex)
56+
.thenComparingInt(BusbarSectionResult::sectionIndex)).orElse(null);
5257
}
5358
List<BusbarSectionResult> withOpenSwitch = results.stream().filter(r -> r.lastSwitch() != null && r.lastSwitch().isOpen()).toList();
5459
if (!withOpenSwitch.isEmpty()) {
55-
return withOpenSwitch.stream().min(Comparator.comparingInt(BusbarSectionResult::depth)).orElse(null);
60+
return withOpenSwitch.stream().min(Comparator.comparingInt(BusbarSectionResult::depth)
61+
.thenComparingInt(BusbarSectionResult::busbarIndex)
62+
.thenComparingInt(BusbarSectionResult::sectionIndex)).orElse(null);
5663
}
5764
return results.getFirst();
5865
}
5966

6067
private static List<BusbarSectionResult> searchAllBusbars(VoltageLevel.NodeBreakerView view, int startNode) {
6168
List<BusbarSectionResult> results = new ArrayList<>();
6269
Set<Integer> visited = new HashSet<>();
63-
Queue<NodePath> queue = new LinkedList<>();
64-
queue.offer(new NodePath(startNode, new ArrayList<>(), null));
65-
while (!queue.isEmpty()) {
66-
NodePath currentNodePath = queue.poll();
67-
if (!hasNotBeenVisited(currentNodePath.node(), visited)) {
70+
Queue<NodePath> nodePathsToVisit = new LinkedList<>();
71+
nodePathsToVisit.offer(new NodePath(startNode, new ArrayList<>(), null));
72+
while (!nodePathsToVisit.isEmpty()) {
73+
NodePath currentNodePath = nodePathsToVisit.poll();
74+
if (hasBeenVisited(currentNodePath.startNode(), visited)) {
6875
continue;
6976
}
70-
visited.add(currentNodePath.node());
71-
Optional<BusbarSectionResult> busbarSectionResult = tryCreateBusbarResult(view, currentNodePath);
77+
visited.add(currentNodePath.startNode());
78+
Optional<BusbarSectionResult> busbarSectionResult = findBusbarSectionAtNode(view, currentNodePath);
7279
if (busbarSectionResult.isPresent()) {
7380
results.add(busbarSectionResult.get());
7481
} else {
75-
exploreAdjacentNodes(view, currentNodePath, visited, queue);
82+
exploreAdjacentNodes(view, currentNodePath, visited, nodePathsToVisit);
7683
}
7784
}
7885
return results;
7986
}
8087

81-
private static boolean hasNotBeenVisited(int node, Set<Integer> visited) {
82-
return !visited.contains(node);
88+
private static boolean hasBeenVisited(int node, Set<Integer> visited) {
89+
return visited.contains(node);
8390
}
8491

85-
private static Optional<BusbarSectionResult> tryCreateBusbarResult(VoltageLevel.NodeBreakerView view, NodePath currentNodePath) {
86-
Optional<Terminal> nodeTerminal = view.getOptionalTerminal(currentNodePath.node());
92+
private static Optional<BusbarSectionResult> findBusbarSectionAtNode(VoltageLevel.NodeBreakerView view, NodePath currentNodePath) {
93+
Optional<Terminal> nodeTerminal = view.getOptionalTerminal(currentNodePath.startNode());
8794
if (nodeTerminal.isEmpty()) {
8895
return Optional.empty();
8996
}
90-
Terminal term = nodeTerminal.get();
91-
if (term.getConnectable().getType() == IdentifiableType.BUSBAR_SECTION) {
92-
String busbarSectionId = term.getConnectable().getId();
93-
int depth = currentNodePath.pathSwitches().size();
97+
Terminal terminal = nodeTerminal.get();
98+
if (terminal.getConnectable().getType() == IdentifiableType.BUSBAR_SECTION) {
99+
String busbarSectionId = terminal.getConnectable().getId();
100+
int depth = currentNodePath.traversedSwitches().size();
94101
SwitchInfo lastSwitch = currentNodePath.lastSwitch();
95-
return Optional.of(new BusbarSectionResult(busbarSectionId, depth, lastSwitch));
102+
BusbarSection busbarSection = (BusbarSection) terminal.getConnectable();
103+
int busbarIndex = 1;
104+
int sectionIndex = 1;
105+
var busbarSectionPosition = busbarSection.getExtension(BusbarSectionPosition.class);
106+
if (busbarSectionPosition != null) {
107+
busbarIndex = busbarSectionPosition.getBusbarIndex();
108+
sectionIndex = busbarSectionPosition.getSectionIndex();
109+
}
110+
return Optional.of(new BusbarSectionResult(busbarSectionId, depth, lastSwitch, busbarIndex, sectionIndex));
96111
}
97112
return Optional.empty();
98113
}
99114

100-
private static void exploreAdjacentNodes(VoltageLevel.NodeBreakerView view, NodePath currentNodePath, Set<Integer> visited, Queue<NodePath> queue) {
115+
private static void exploreAdjacentNodes(VoltageLevel.NodeBreakerView view, NodePath currentNodePath, Set<Integer> visited, Queue<NodePath> nodePathsToVisit) {
101116
view.getSwitchStream().forEach(sw -> {
102117
int node1 = view.getNode1(sw.getId());
103118
int node2 = view.getNode2(sw.getId());
104-
Optional<Integer> nextNode = getNextNodeIfAdjacent(currentNodePath.node(), node1, node2);
119+
Optional<Integer> nextNode = getNextNodeIfAdjacent(currentNodePath.startNode(), node1, node2);
105120
if (nextNode.isPresent() && !visited.contains(nextNode.get())) {
106-
NodePath newPath = createNodePath(currentNodePath, sw, nextNode.get());
107-
queue.offer(newPath);
121+
NodePath newNodePath = createNodePath(currentNodePath, sw, nextNode.get());
122+
nodePathsToVisit.offer(newNodePath);
108123
}
109124
});
110125
}
@@ -120,7 +135,7 @@ private static Optional<Integer> getNextNodeIfAdjacent(int currentNode, int node
120135
}
121136

122137
private static NodePath createNodePath(NodePath currentNodePath, Switch sw, int nextNode) {
123-
List<SwitchInfo> newPathSwitches = new ArrayList<>(currentNodePath.pathSwitches());
138+
List<SwitchInfo> newPathSwitches = new ArrayList<>(currentNodePath.traversedSwitches());
124139
SwitchInfo switchInfo = new SwitchInfo(sw.getId(), sw.getKind(), sw.isOpen());
125140
newPathSwitches.add(switchInfo);
126141
return new NodePath(nextNode, newPathSwitches, switchInfo);

src/test/resources/substations-form-data.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@
129129
],
130130
"SHUNT_NON_LINEAR": [
131131
{
132-
"busbarSectionId": null,
132+
"busbarSectionId": "NGEN4",
133133
"connectablePositionInfos": {
134134
"connectionDirection": null
135135
},

src/test/resources/voltage-level-form-data.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
],
3535
"SHUNT_NON_LINEAR": [
3636
{
37-
"busbarSectionId": null,
37+
"busbarSectionId": "NGEN4",
3838
"connectablePositionInfos": {
3939
"connectionDirection": null
4040
},

src/test/resources/voltage-levels-form-data.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@
9595
],
9696
"SHUNT_NON_LINEAR": [
9797
{
98-
"busbarSectionId": null,
98+
"busbarSectionId": "NGEN4",
9999
"connectablePositionInfos": {
100100
"connectionDirection": null
101101
},

0 commit comments

Comments
 (0)