7
7
package org .gridsuite .network .map .dto .definition .extension ;
8
8
9
9
import com .powsybl .iidm .network .*;
10
+ import com .powsybl .iidm .network .extensions .BusbarSectionPosition ;
10
11
11
12
import java .util .*;
12
13
@@ -20,15 +21,15 @@ private BusbarSectionFinderTraverser() {
20
21
throw new UnsupportedOperationException ();
21
22
}
22
23
23
- private record NodePath (int node , List <SwitchInfo > pathSwitches , SwitchInfo lastSwitch ) { }
24
+ private record NodePath (int startNode , List <SwitchInfo > traversedSwitches , SwitchInfo lastSwitch ) { }
24
25
25
26
public record SwitchInfo (String id , SwitchKind kind , boolean isOpen ) { }
26
27
27
- public record BusbarSectionResult (String busbarSectionId , int depth , SwitchInfo lastSwitch ) { }
28
+ public record BusbarSectionResult (String busbarSectionId , int depth , SwitchInfo lastSwitch , int busbarIndex , int sectionIndex ) { }
28
29
29
30
public static String findBusbarSectionId (Terminal terminal ) {
30
31
BusbarSectionResult result = getBusbarSectionResult (terminal );
31
- return result != null ? result .busbarSectionId () : null ;
32
+ return result != null ? result .busbarSectionId () : terminal . getVoltageLevel (). getNodeBreakerView (). getBusbarSections (). iterator (). next (). getId () ;
32
33
}
33
34
34
35
public static BusbarSectionResult getBusbarSectionResult (Terminal terminal ) {
@@ -44,67 +45,81 @@ public static BusbarSectionResult getBusbarSectionResult(Terminal terminal) {
44
45
private static BusbarSectionResult selectBestBusbar (List <BusbarSectionResult > results ) {
45
46
List <BusbarSectionResult > withoutSwitch = results .stream ().filter (r -> r .lastSwitch () == null ).toList ();
46
47
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 );
48
51
}
49
52
List <BusbarSectionResult > withClosedSwitch = results .stream ().filter (r -> r .lastSwitch () != null && !r .lastSwitch ().isOpen ()).toList ();
50
53
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 );
52
57
}
53
58
List <BusbarSectionResult > withOpenSwitch = results .stream ().filter (r -> r .lastSwitch () != null && r .lastSwitch ().isOpen ()).toList ();
54
59
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 );
56
63
}
57
64
return results .getFirst ();
58
65
}
59
66
60
67
private static List <BusbarSectionResult > searchAllBusbars (VoltageLevel .NodeBreakerView view , int startNode ) {
61
68
List <BusbarSectionResult > results = new ArrayList <>();
62
69
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 )) {
68
75
continue ;
69
76
}
70
- visited .add (currentNodePath .node ());
71
- Optional <BusbarSectionResult > busbarSectionResult = tryCreateBusbarResult (view , currentNodePath );
77
+ visited .add (currentNodePath .startNode ());
78
+ Optional <BusbarSectionResult > busbarSectionResult = findBusbarSectionAtNode (view , currentNodePath );
72
79
if (busbarSectionResult .isPresent ()) {
73
80
results .add (busbarSectionResult .get ());
74
81
} else {
75
- exploreAdjacentNodes (view , currentNodePath , visited , queue );
82
+ exploreAdjacentNodes (view , currentNodePath , visited , nodePathsToVisit );
76
83
}
77
84
}
78
85
return results ;
79
86
}
80
87
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 );
83
90
}
84
91
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 ());
87
94
if (nodeTerminal .isEmpty ()) {
88
95
return Optional .empty ();
89
96
}
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 ();
94
101
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 ));
96
111
}
97
112
return Optional .empty ();
98
113
}
99
114
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 ) {
101
116
view .getSwitchStream ().forEach (sw -> {
102
117
int node1 = view .getNode1 (sw .getId ());
103
118
int node2 = view .getNode2 (sw .getId ());
104
- Optional <Integer > nextNode = getNextNodeIfAdjacent (currentNodePath .node (), node1 , node2 );
119
+ Optional <Integer > nextNode = getNextNodeIfAdjacent (currentNodePath .startNode (), node1 , node2 );
105
120
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 );
108
123
}
109
124
});
110
125
}
@@ -120,7 +135,7 @@ private static Optional<Integer> getNextNodeIfAdjacent(int currentNode, int node
120
135
}
121
136
122
137
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 ());
124
139
SwitchInfo switchInfo = new SwitchInfo (sw .getId (), sw .getKind (), sw .isOpen ());
125
140
newPathSwitches .add (switchInfo );
126
141
return new NodePath (nextNode , newPathSwitches , switchInfo );
0 commit comments