Skip to content

Commit

Permalink
Fix loop to get unused positions + refactoring + min is 0, not Intege…
Browse files Browse the repository at this point in the history
…r.MIN_VALUE (#2333)

Signed-off-by: VEDELAGO MIORA <[email protected]>
  • Loading branch information
miovd committed Nov 17, 2022
1 parent 1f36be4 commit bd95332
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -379,9 +379,8 @@ public static Optional<Range<Integer>> getUnusedOrderPositionsBefore(BusbarSecti
int sectionIndex = busbarSectionPosition.getSectionIndex();
Optional<Integer> previousSliceMax = getMaxOrderUsedBefore(allOrders, sectionIndex);
Optional<Integer> sliceMin = allOrders.get(sectionIndex).stream().min(Comparator.naturalOrder());
int min = previousSliceMax.map(o -> o + 1).orElse(Integer.MIN_VALUE);
int max = sliceMin.map(o -> o - 1).orElse(
getMinOrderUsedAfter(allOrders, sectionIndex).map(o -> o - 1).orElse(Integer.MAX_VALUE));
int min = previousSliceMax.map(o -> o + 1).orElse(0);
int max = sliceMin.or(() -> getMinOrderUsedAfter(allOrders, sectionIndex)).map(o -> o - 1).orElse(Integer.MAX_VALUE);
return Optional.ofNullable(min <= max ? Range.between(min, max) : null);
}

Expand All @@ -403,8 +402,7 @@ public static Optional<Range<Integer>> getUnusedOrderPositionsAfter(BusbarSectio
int sectionIndex = busbarSectionPosition.getSectionIndex();
Optional<Integer> nextSliceMin = getMinOrderUsedAfter(allOrders, sectionIndex);
Optional<Integer> sliceMax = allOrders.get(sectionIndex).stream().max(Comparator.naturalOrder());
int min = sliceMax.map(o -> o + 1).orElse(
getMaxOrderUsedBefore(allOrders, sectionIndex).map(o -> o + 1).orElse(Integer.MIN_VALUE));
int min = sliceMax.or(() -> getMaxOrderUsedBefore(allOrders, sectionIndex)).map(o -> o + 1).orElse(0);
int max = nextSliceMin.map(o -> o - 1).orElse(Integer.MAX_VALUE);
return Optional.ofNullable(min <= max ? Range.between(min, max) : null);
}
Expand All @@ -415,10 +413,15 @@ public static Optional<Range<Integer>> getUnusedOrderPositionsAfter(BusbarSectio
* applied to BBS2 will return 3.
*/
private static Optional<Integer> getMaxOrderUsedBefore(NavigableMap<Integer, List<Integer>> allOrders, int section) {
int s = section;
Map.Entry<Integer, List<Integer>> lowerEntry;
do {
lowerEntry = allOrders.lowerEntry(section);
} while (lowerEntry != null && lowerEntry.getValue().isEmpty());
lowerEntry = allOrders.lowerEntry(s);
if (lowerEntry == null) {
break;
}
s = lowerEntry.getKey();
} while (lowerEntry.getValue().isEmpty());

return Optional.ofNullable(lowerEntry)
.flatMap(entry -> entry.getValue().stream().max(Comparator.naturalOrder()));
Expand All @@ -430,10 +433,15 @@ private static Optional<Integer> getMaxOrderUsedBefore(NavigableMap<Integer, Lis
* applied to BBS1 will return 7.
*/
private static Optional<Integer> getMinOrderUsedAfter(NavigableMap<Integer, List<Integer>> allOrders, int section) {
int s = section;
Map.Entry<Integer, List<Integer>> higherEntry;
do {
higherEntry = allOrders.higherEntry(section);
} while (higherEntry != null && higherEntry.getValue().isEmpty());
higherEntry = allOrders.higherEntry(s);
if (higherEntry == null) {
break;
}
s = higherEntry.getKey();
} while (higherEntry.getValue().isEmpty());

return Optional.ofNullable(higherEntry)
.flatMap(entry -> entry.getValue().stream().min(Comparator.naturalOrder()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,23 @@ public void testGetUnusedPositionsWithEmptyVoltageLevel() {

Optional<Range<Integer>> unusedOrderPositionsAfter = TopologyModificationUtils.getUnusedOrderPositionsAfter(bbs);
assertTrue(unusedOrderPositionsAfter.isPresent());
assertEquals(Integer.MIN_VALUE, (int) unusedOrderPositionsAfter.get().getMinimum());
assertEquals(0, (int) unusedOrderPositionsAfter.get().getMinimum());
assertEquals(Integer.MAX_VALUE, (int) unusedOrderPositionsAfter.get().getMaximum());

Optional<Range<Integer>> unusedOrderPositionsBefore = TopologyModificationUtils.getUnusedOrderPositionsAfter(bbs);
assertTrue(unusedOrderPositionsBefore.isPresent());
assertEquals(Integer.MIN_VALUE, (int) unusedOrderPositionsBefore.get().getMinimum());
assertEquals(0, (int) unusedOrderPositionsBefore.get().getMinimum());
assertEquals(Integer.MAX_VALUE, (int) unusedOrderPositionsBefore.get().getMaximum());
}

@Test
public void testNoConnectablePositionExt() {
Network network = Importers.loadNetwork("network-nb-no-connectable-position.xiidm", getClass().getResourceAsStream("/network-nb-no-connectable-position.xiidm"));
Optional<Range<Integer>> unusedOrderPositionsBefore = TopologyModificationUtils.getUnusedOrderPositionsBefore(network.getBusbarSection("vl_test_1_1"));
Optional<Range<Integer>> unusedOrderPositionsAfter = TopologyModificationUtils.getUnusedOrderPositionsAfter(network.getBusbarSection("vl_test_1_1"));
assertEquals(0, (int) unusedOrderPositionsBefore.map(Range::getMinimum).orElse(-1));
assertEquals(Integer.MAX_VALUE, (int) unusedOrderPositionsBefore.map(Range::getMaximum).orElse(-1));
assertEquals(0, (int) unusedOrderPositionsAfter.map(Range::getMinimum).orElse(-1));
assertEquals(Integer.MAX_VALUE, (int) unusedOrderPositionsAfter.map(Range::getMaximum).orElse(-1));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?xml version="1.0" encoding="UTF-8"?>
<iidm:network xmlns:iidm="http://www.powsybl.org/schema/iidm/1_8" xmlns:bbsp="http://www.itesla_project.eu/schema/iidm/ext/busbarsectionposition/1_0" id="test" caseDate="2018-11-08T00:00:00.000Z" forecastDistance="0" sourceFormat="test" minimumValidationLevel="STEADY_STATE_HYPOTHESIS">
<iidm:substation id="sub_test" country="FR">
<iidm:voltageLevel id="vl_test" nominalV="225.0" topologyKind="NODE_BREAKER">
<iidm:nodeBreakerTopology>
<iidm:busbarSection id="vl_test_1_1" name="1.1" node="1"/>
<iidm:busbarSection id="vl_test_1_2" name="1.2" node="2"/>
<iidm:switch id="disconnector_vl_test_vl_test_1_1_vl_test_1_2_1" kind="DISCONNECTOR" retained="false" open="false" node1="1" node2="2"/>
<iidm:switch id="disconnector_gen1" kind="DISCONNECTOR" retained="false" open="false" node1="1" node2="3"/>
<iidm:switch id="breaker_gen1" kind="BREAKER" retained="false" open="false" node1="3" node2="4"/>
<iidm:switch id="disconnector_gen2" kind="DISCONNECTOR" retained="false" open="false" node1="2" node2="5"/>
<iidm:switch id="breaker_gen2" kind="BREAKER" retained="false" open="false" node1="5" node2="6"/>
</iidm:nodeBreakerTopology>
<iidm:generator id="gen1" energySource="OTHER" minP="0.0" maxP="100.0" voltageRegulatorOn="false" targetP="10.0" targetQ="0.0" node="4">
<iidm:minMaxReactiveLimits minQ="-1.7976931348623157E308" maxQ="1.7976931348623157E308"/>
</iidm:generator>
<iidm:generator id="gen2" energySource="OTHER" minP="0.0" maxP="100.0" voltageRegulatorOn="false" targetP="20.0" targetQ="0.0" node="6">
<iidm:minMaxReactiveLimits minQ="-1.7976931348623157E308" maxQ="1.7976931348623157E308"/>
</iidm:generator>
</iidm:voltageLevel>
</iidm:substation>
<iidm:extension id="vl_test_1_1">
<bbsp:busbarSectionPosition busbarIndex="1" sectionIndex="1"/>
</iidm:extension>
<iidm:extension id="vl_test_1_2">
<bbsp:busbarSectionPosition busbarIndex="1" sectionIndex="2"/>
</iidm:extension>
</iidm:network>

0 comments on commit bd95332

Please sign in to comment.