diff --git a/iidm/iidm-impl/src/main/java/com/powsybl/iidm/network/impl/BusBreakerVoltageLevel.java b/iidm/iidm-impl/src/main/java/com/powsybl/iidm/network/impl/BusBreakerVoltageLevel.java index 4132a1d8eb5..f02b140ff77 100644 --- a/iidm/iidm-impl/src/main/java/com/powsybl/iidm/network/impl/BusBreakerVoltageLevel.java +++ b/iidm/iidm-impl/src/main/java/com/powsybl/iidm/network/impl/BusBreakerVoltageLevel.java @@ -353,7 +353,7 @@ public void edgeRemoved(int e, SwitchImpl obj) { } @Override - public void allEdgesRemoved() { + public void allEdgesRemoved(Collection obj) { invalidateCache(); } }); diff --git a/iidm/iidm-impl/src/main/java/com/powsybl/iidm/network/impl/NodeBreakerVoltageLevel.java b/iidm/iidm-impl/src/main/java/com/powsybl/iidm/network/impl/NodeBreakerVoltageLevel.java index c900dc5381f..cd759b6056e 100644 --- a/iidm/iidm-impl/src/main/java/com/powsybl/iidm/network/impl/NodeBreakerVoltageLevel.java +++ b/iidm/iidm-impl/src/main/java/com/powsybl/iidm/network/impl/NodeBreakerVoltageLevel.java @@ -528,18 +528,14 @@ public void edgeRemoved(int e, SwitchImpl aSwitch) { } @Override - public void allEdgesRemoved() { - List removedSwitchesIds = new ArrayList<>(graph.getEdgeCount()); + public void allEdgesRemoved(Collection aSwitches) { NetworkImpl network = getNetwork(); - for (SwitchImpl s : graph.getEdgesObject()) { - if (s != null) { - network.getListeners().notifyBeforeRemoval(s); - removedSwitchesIds.add(s.getId()); - network.getIndex().remove(s); - } - } + aSwitches.forEach(ss -> { + network.getListeners().notifyBeforeRemoval(ss); + network.getIndex().remove(ss); + }); switches.clear(); - removedSwitchesIds.forEach(id -> network.getListeners().notifyAfterRemoval(id)); + aSwitches.forEach(ss -> network.getListeners().notifyAfterRemoval(ss.getId())); } }); } diff --git a/iidm/iidm-reducer/src/test/java/com/powsybl/iidm/reducer/DefaultNetworkReducerTest.java b/iidm/iidm-reducer/src/test/java/com/powsybl/iidm/reducer/DefaultNetworkReducerTest.java index 381c4fe1d98..d4bf8736dd7 100644 --- a/iidm/iidm-reducer/src/test/java/com/powsybl/iidm/reducer/DefaultNetworkReducerTest.java +++ b/iidm/iidm-reducer/src/test/java/com/powsybl/iidm/reducer/DefaultNetworkReducerTest.java @@ -8,6 +8,7 @@ import com.powsybl.iidm.network.*; import com.powsybl.iidm.network.test.EurostagTutorialExample1Factory; +import com.powsybl.iidm.network.test.FictitiousSwitchFactory; import com.powsybl.iidm.network.test.HvdcTestNetwork; import com.powsybl.iidm.network.test.ThreeWindingsTransformerNetworkFactory; import org.junit.Rule; @@ -305,4 +306,24 @@ public void test3WTFailure() { .build(); reducer.reduce(network); } + + @Test + public void testNodeBreaker() { + Network network = FictitiousSwitchFactory.create(); + + NetworkReducer reducer = NetworkReducer.builder() + .withNetworkPredicate(IdentifierNetworkPredicate.of("C")) + .build(); + reducer.reduce(network); + + assertEquals(1, network.getSubstationCount()); + assertEquals(1, network.getVoltageLevelCount()); + assertEquals(0, network.getTwoWindingsTransformerCount()); + assertEquals(0, network.getLineCount()); + assertEquals(0, network.getGeneratorCount()); + assertEquals(2, network.getLoadCount()); + assertEquals(0, network.getDanglingLineCount()); + assertEquals(4, network.getSwitchCount()); + assertEquals(1, network.getBusbarSectionCount()); + } } diff --git a/math/src/main/java/com/powsybl/math/graph/DefaultUndirectedGraphListener.java b/math/src/main/java/com/powsybl/math/graph/DefaultUndirectedGraphListener.java index 74511617cb4..2f21be5aacc 100644 --- a/math/src/main/java/com/powsybl/math/graph/DefaultUndirectedGraphListener.java +++ b/math/src/main/java/com/powsybl/math/graph/DefaultUndirectedGraphListener.java @@ -6,6 +6,8 @@ */ package com.powsybl.math.graph; +import java.util.Collection; + /** * * @author Geoffroy Jamgotchian @@ -43,7 +45,7 @@ public void edgeRemoved(int e, E obj) { } @Override - public void allEdgesRemoved() { + public void allEdgesRemoved(Collection obj) { // nothing to do } } diff --git a/math/src/main/java/com/powsybl/math/graph/UndirectedGraphImpl.java b/math/src/main/java/com/powsybl/math/graph/UndirectedGraphImpl.java index f867a8c1e3d..059490ea9a3 100644 --- a/math/src/main/java/com/powsybl/math/graph/UndirectedGraphImpl.java +++ b/math/src/main/java/com/powsybl/math/graph/UndirectedGraphImpl.java @@ -254,10 +254,11 @@ public E removeEdge(int e) { @Override public void removeAllEdges() { + Collection allEdges = edges.stream().map(Edge::getObject).filter(Objects::nonNull).collect(Collectors.toList()); edges.clear(); removedEdges.clear(); invalidateAdjacencyList(); - notifyAllEdgesRemoved(); + notifyAllEdgesRemoved(allEdges); } @Override @@ -630,9 +631,9 @@ private void notifyEdgeRemoved(int e, E obj) { } } - private void notifyAllEdgesRemoved() { + private void notifyAllEdgesRemoved(Collection obj) { for (UndirectedGraphListener l : listeners) { - l.allEdgesRemoved(); + l.allEdgesRemoved(obj); } } diff --git a/math/src/main/java/com/powsybl/math/graph/UndirectedGraphListener.java b/math/src/main/java/com/powsybl/math/graph/UndirectedGraphListener.java index ced89db8a20..2652e55924b 100644 --- a/math/src/main/java/com/powsybl/math/graph/UndirectedGraphListener.java +++ b/math/src/main/java/com/powsybl/math/graph/UndirectedGraphListener.java @@ -6,6 +6,8 @@ */ package com.powsybl.math.graph; +import java.util.Collection; + /** * * @author Geoffroy Jamgotchian @@ -24,5 +26,5 @@ public interface UndirectedGraphListener { void edgeRemoved(int e, E obj); - void allEdgesRemoved(); + void allEdgesRemoved(Collection obj); }