Skip to content

Commit 7a975c9

Browse files
committed
Save paths throw one traverse
Signed-off-by: Etienne Homer <[email protected]>
1 parent 7cd598c commit 7a975c9

File tree

2 files changed

+12
-84
lines changed

2 files changed

+12
-84
lines changed

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

Lines changed: 11 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,9 @@ private BusbarSectionFinderTraverser() {
2323
throw new UnsupportedOperationException();
2424
}
2525

26-
private record NodePath(int startNode, List<SwitchInfo> traversedSwitches, SwitchInfo lastSwitch) { }
27-
2826
public record SwitchInfo(String id, boolean isOpen) { }
2927

30-
public record BusbarSectionResult(String busbarSectionId, int depth, SwitchInfo lastSwitch) { }
28+
public record BusbarSectionResult(String busbarSectionId, int depth, SwitchInfo lastSwitch, boolean allSwitchesClosed) { }
3129

3230
public static String findBusbarSectionId(Terminal terminal) {
3331
BusbarSectionResult result = getBusbarSectionResult(terminal);
@@ -37,7 +35,7 @@ public static String findBusbarSectionId(Terminal terminal) {
3735
public static BusbarSectionResult getBusbarSectionResult(Terminal terminal) {
3836
VoltageLevel.NodeBreakerView view = terminal.getVoltageLevel().getNodeBreakerView();
3937
int startNode = terminal.getNodeBreakerView().getNode();
40-
List<BusbarSectionResult> allResults = searchAllBusbars(view, startNode);
38+
List<BusbarSectionResult> allResults = searchAllBusbars(terminal.getVoltageLevel(), startNode);
4139
if (allResults.isEmpty()) {
4240
return null;
4341
}
@@ -63,106 +61,35 @@ private static BusbarSectionResult selectBestBusbar(List<BusbarSectionResult> re
6361
return results.getFirst();
6462
}
6563

66-
private static List<BusbarSectionResult> searchAllBusbars(VoltageLevel.NodeBreakerView view, int startNode) {
64+
private static List<BusbarSectionResult> searchAllBusbars(VoltageLevel voltageLevel, int startNode) {
6765
List<BusbarSectionResult> results = new ArrayList<>();
68-
// Set<Integer> visited = new HashSet<>();
69-
// Queue<NodePath> nodePathsToVisit = new LinkedList<>();
70-
// nodePathsToVisit.offer(new NodePath(startNode, new ArrayList<>(), null));
71-
7266

73-
view.getTerminal(startNode).traverse(new Terminal.TopologyTraverser() {
67+
voltageLevel.getNodeBreakerView().getTerminal(startNode).traverse(new Terminal.TopologyTraverser() {
7468
int currentDepth = 0;
69+
boolean allSwitchesClosed = true;
7570
SwitchInfo lastSwitch = null;
7671
@Override
7772
public TraverseResult traverse(Terminal terminal, boolean connected) {
78-
//if (terminal.getVoltageLevel() != view.
73+
if (terminal.getVoltageLevel() != voltageLevel)
74+
return TraverseResult.TERMINATE_PATH;
7975

8076
if (terminal.getConnectable() instanceof BusbarSection busbarSection) {
81-
// add busbar section to the path
82-
results.add(new BusbarSectionResult(busbarSection.getId(), currentDepth, lastSwitch));
77+
results.add(new BusbarSectionResult(busbarSection.getId(), currentDepth, lastSwitch,allSwitchesClosed));
8378
return TraverseResult.TERMINATE_PATH;
8479
}
85-
// currentDepth++;
8680
return TraverseResult.CONTINUE;
8781
}
8882

8983
@Override
9084
public TraverseResult traverse(Switch aSwitch) {
9185
currentDepth++;
9286
lastSwitch = new SwitchInfo(aSwitch.getId(), aSwitch.isOpen());
87+
if (!aSwitch.isOpen()) {
88+
allSwitchesClosed = false;
89+
}
9390
return TraverseResult.CONTINUE;
9491
}
9592
}, TraversalType.BREADTH_FIRST);
96-
97-
// while (!nodePathsToVisit.isEmpty()) {
98-
// NodePath currentNodePath = nodePathsToVisit.poll();
99-
// if (hasBeenVisited(currentNodePath.startNode(), visited)) {
100-
// continue;
101-
// }
102-
// visited.add(currentNodePath.startNode());
103-
// Optional<BusbarSectionResult> busbarSectionResult = findBusbarSectionAtNode(view, currentNodePath);
104-
// if (busbarSectionResult.isPresent()) {
105-
// results.add(busbarSectionResult.get());
106-
// } else {
107-
//// exploreAdjacentNodes(view, currentNodePath, visited, nodePathsToVisit);
108-
// }
109-
// }
11093
return results;
11194
}
112-
113-
private static boolean hasBeenVisited(int node, Set<Integer> visited) {
114-
return visited.contains(node);
115-
}
116-
117-
private static Optional<BusbarSectionResult> findBusbarSectionAtNode(VoltageLevel.NodeBreakerView view, NodePath currentNodePath) {
118-
Optional<Terminal> nodeTerminal = view.getOptionalTerminal(currentNodePath.startNode());
119-
if (nodeTerminal.isEmpty()) {
120-
return Optional.empty();
121-
}
122-
Terminal terminal = nodeTerminal.get();
123-
if (terminal.getConnectable().getType() == IdentifiableType.BUSBAR_SECTION) {
124-
String busbarSectionId = terminal.getConnectable().getId();
125-
int depth = currentNodePath.traversedSwitches().size();
126-
SwitchInfo lastSwitch = currentNodePath.lastSwitch();
127-
BusbarSection busbarSection = (BusbarSection) terminal.getConnectable();
128-
int busbarIndex = 1;
129-
int sectionIndex = 1;
130-
var busbarSectionPosition = busbarSection.getExtension(BusbarSectionPosition.class);
131-
if (busbarSectionPosition != null) {
132-
busbarIndex = busbarSectionPosition.getBusbarIndex();
133-
sectionIndex = busbarSectionPosition.getSectionIndex();
134-
}
135-
return Optional.of(new BusbarSectionResult(busbarSectionId, depth, lastSwitch));
136-
}
137-
return Optional.empty();
138-
}
139-
140-
private static void exploreAdjacentNodes(VoltageLevel.NodeBreakerView view, NodePath currentNodePath, Set<Integer> visited, Queue<NodePath> nodePathsToVisit) {
141-
view.getSwitchStream().forEach(sw -> {
142-
int node1 = view.getNode1(sw.getId());
143-
int node2 = view.getNode2(sw.getId());
144-
Optional<Integer> nextNode = getNextNodeIfAdjacent(currentNodePath.startNode(), node1, node2);
145-
if (nextNode.isPresent() && !visited.contains(nextNode.get())) {
146-
NodePath newNodePath = createNodePath(currentNodePath, sw, nextNode.get());
147-
nodePathsToVisit.offer(newNodePath);
148-
}
149-
});
150-
}
151-
152-
private static Optional<Integer> getNextNodeIfAdjacent(int currentNode, int node1, int node2) {
153-
if (node1 == currentNode) {
154-
return Optional.of(node2);
155-
}
156-
if (node2 == currentNode) {
157-
return Optional.of(node1);
158-
}
159-
return Optional.empty();
160-
}
161-
162-
private static NodePath createNodePath(NodePath currentNodePath, Switch sw, int nextNode) {
163-
List<SwitchInfo> newPathSwitches = new ArrayList<>(currentNodePath.traversedSwitches());
164-
SwitchInfo switchInfo = new SwitchInfo(sw.getId(), sw.isOpen());
165-
newPathSwitches.add(switchInfo);
166-
return new NodePath(nextNode, newPathSwitches, switchInfo);
167-
}
16895
}

src/test/java/org/gridsuite/network/map/mapper/BusbarSectionFinderTraverserTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.powsybl.iidm.network.*;
44
import com.powsybl.iidm.network.extensions.*;
55
import com.powsybl.iidm.network.test.EurostagTutorialExample1Factory;
6+
import com.powsybl.iidm.network.util.Networks;
67
import com.powsybl.network.store.iidm.impl.NetworkFactoryImpl;
78
import org.gridsuite.network.map.dto.definition.extension.BusbarSectionFinderTraverser;
89
import org.junit.jupiter.api.BeforeEach;

0 commit comments

Comments
 (0)