From 1fd93ea5867ccb07cd65dd1b2821a05742dc9a30 Mon Sep 17 00:00:00 2001 From: Slimane AMAR Date: Wed, 2 Oct 2024 16:51:34 +0200 Subject: [PATCH 1/3] Add notification when move connectable Signed-off-by: Slimane AMAR --- .../store/iidm/impl/BusTopologyPointImpl.java | 59 +++++++++++++++++++ .../iidm/impl/NodeTopologyPointImpl.java | 50 ++++++++++++++++ .../iidm/impl/TerminalBusBreakerViewImpl.java | 17 +++--- .../network/store/iidm/impl/TerminalImpl.java | 22 +++++-- .../impl/TerminalNodeBreakerViewImpl.java | 14 ++++- .../impl/tck/MoveConnectableNotifTest.java | 9 --- 6 files changed, 149 insertions(+), 22 deletions(-) create mode 100644 network-store-iidm-impl/src/main/java/com/powsybl/network/store/iidm/impl/BusTopologyPointImpl.java create mode 100644 network-store-iidm-impl/src/main/java/com/powsybl/network/store/iidm/impl/NodeTopologyPointImpl.java diff --git a/network-store-iidm-impl/src/main/java/com/powsybl/network/store/iidm/impl/BusTopologyPointImpl.java b/network-store-iidm-impl/src/main/java/com/powsybl/network/store/iidm/impl/BusTopologyPointImpl.java new file mode 100644 index 000000000..aa20a713d --- /dev/null +++ b/network-store-iidm-impl/src/main/java/com/powsybl/network/store/iidm/impl/BusTopologyPointImpl.java @@ -0,0 +1,59 @@ +/** + * Copyright (c) 2024, RTE (http://www.rte-france.com) + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ +package com.powsybl.network.store.iidm.impl; + +import com.powsybl.iidm.network.BusTopologyPoint; +import com.powsybl.iidm.network.TopologyKind; + +import java.util.Objects; + +/** + * @author Slimane Amar + */ +public class BusTopologyPointImpl implements BusTopologyPoint { + + private final String voltageLevelId; + + private final String connectableBusId; + + private final boolean connected; + + public BusTopologyPointImpl(String voltageLevelId, String connectableBusId, boolean connected) { + this.voltageLevelId = Objects.requireNonNull(voltageLevelId); + this.connectableBusId = Objects.requireNonNull(connectableBusId); + this.connected = connected; + } + + @Override + public TopologyKind getTopologyKind() { + return TopologyKind.BUS_BREAKER; + } + + @Override + public String getVoltageLevelId() { + return voltageLevelId; + } + + @Override + public String getConnectableBusId() { + return connectableBusId; + } + + @Override + public boolean isConnected() { + return connected; + } + + @Override + public String toString() { + return "NodeTopologyPoint(" + + "voltageLevelId='" + voltageLevelId + '\'' + + ", connectableBusId='" + connectableBusId + '\'' + + ", connected=" + connected + + ')'; + } +} diff --git a/network-store-iidm-impl/src/main/java/com/powsybl/network/store/iidm/impl/NodeTopologyPointImpl.java b/network-store-iidm-impl/src/main/java/com/powsybl/network/store/iidm/impl/NodeTopologyPointImpl.java new file mode 100644 index 000000000..078998e3c --- /dev/null +++ b/network-store-iidm-impl/src/main/java/com/powsybl/network/store/iidm/impl/NodeTopologyPointImpl.java @@ -0,0 +1,50 @@ +/** + * Copyright (c) 2024, RTE (http://www.rte-france.com) + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ +package com.powsybl.network.store.iidm.impl; + +import com.powsybl.iidm.network.NodeTopologyPoint; +import com.powsybl.iidm.network.TopologyKind; + +import java.util.Objects; + +/** + * @author Slimane Amar + */ +public class NodeTopologyPointImpl implements NodeTopologyPoint { + + private final String voltageLevelId; + + private final int node; + + public NodeTopologyPointImpl(String voltageLevelId, int node) { + this.voltageLevelId = Objects.requireNonNull(voltageLevelId); + this.node = node; + } + + @Override + public TopologyKind getTopologyKind() { + return TopologyKind.NODE_BREAKER; + } + + @Override + public String getVoltageLevelId() { + return voltageLevelId; + } + + @Override + public int getNode() { + return node; + } + + @Override + public String toString() { + return "NodeTopologyPoint(" + + "voltageLevelId='" + voltageLevelId + '\'' + + ", node=" + node + + ')'; + } +} diff --git a/network-store-iidm-impl/src/main/java/com/powsybl/network/store/iidm/impl/TerminalBusBreakerViewImpl.java b/network-store-iidm-impl/src/main/java/com/powsybl/network/store/iidm/impl/TerminalBusBreakerViewImpl.java index 0608fbeda..2ea96a8cc 100644 --- a/network-store-iidm-impl/src/main/java/com/powsybl/network/store/iidm/impl/TerminalBusBreakerViewImpl.java +++ b/network-store-iidm-impl/src/main/java/com/powsybl/network/store/iidm/impl/TerminalBusBreakerViewImpl.java @@ -7,10 +7,7 @@ package com.powsybl.network.store.iidm.impl; import com.powsybl.commons.PowsyblException; -import com.powsybl.iidm.network.Bus; -import com.powsybl.iidm.network.Connectable; -import com.powsybl.iidm.network.Terminal; -import com.powsybl.iidm.network.TopologyKind; +import com.powsybl.iidm.network.*; import com.powsybl.network.store.model.IdentifiableAttributes; import com.powsybl.network.store.model.InjectionAttributes; import com.powsybl.network.store.model.Resource; @@ -52,8 +49,12 @@ private InjectionAttributes getAttributes() { return getAttributes(getAbstractIdentifiable().getResource()); } + private VoltageLevelImpl getVoltageLevel() { + return index.getVoltageLevel(getAttributes().getVoltageLevelId()).orElseThrow(IllegalStateException::new); + } + private Resource getVoltageLevelResource() { - return index.getVoltageLevel(getAttributes().getVoltageLevelId()).orElseThrow(IllegalStateException::new).getResource(); + return getVoltageLevel().getResource(); } private boolean isNodeBeakerTopologyKind() { @@ -127,7 +128,7 @@ public void setConnectableBus(String busId) { } }); - index.getVoltageLevel(getVoltageLevelResource().getId()).orElseThrow(AssertionError::new).invalidateCalculatedBuses(); + getVoltageLevel().invalidateCalculatedBuses(); } @Override @@ -146,13 +147,15 @@ public void moveConnectable(String busId, boolean connected) { throw new PowsyblException("Trying to move connectable " + attributes.getResource().getId() + " to bus " + busId + " of voltage level " + bus.getVoltageLevel().getId() + ", which is a node breaker voltage level"); } + VoltageLevelImpl oldVoltageLevel = getVoltageLevel(); getAbstractIdentifiable().updateResource(res -> { - InjectionAttributes attr = attributesGetter.apply(res); + InjectionAttributes attr = getAttributes(res); attr.setConnectableBus(busId); attr.setBus(connected ? busId : null); attr.setNode(null); attr.setVoltageLevelId(voltageLevel.getId()); }); + oldVoltageLevel.invalidateCalculatedBuses(); voltageLevel.invalidateCalculatedBuses(); } } diff --git a/network-store-iidm-impl/src/main/java/com/powsybl/network/store/iidm/impl/TerminalImpl.java b/network-store-iidm-impl/src/main/java/com/powsybl/network/store/iidm/impl/TerminalImpl.java index 565595f82..95b40ab52 100644 --- a/network-store-iidm-impl/src/main/java/com/powsybl/network/store/iidm/impl/TerminalImpl.java +++ b/network-store-iidm-impl/src/main/java/com/powsybl/network/store/iidm/impl/TerminalImpl.java @@ -42,8 +42,22 @@ public TerminalImpl(NetworkObjectIndex index, Connectable connectable, Functi this.index = index; this.connectable = connectable; this.attributesGetter = attributesGetter; - nodeBreakerView = new TerminalNodeBreakerViewImpl<>(index, connectable, attributesGetter); - busBreakerView = new TerminalBusBreakerViewImpl<>(index, connectable, attributesGetter); + nodeBreakerView = new TerminalNodeBreakerViewImpl<>(index, connectable, attributesGetter) { + @Override + public void moveConnectable(int node, String voltageLevelId) { + TopologyPoint oldTopologyPoint = TerminalImpl.this.getTopologyPoint(); + super.moveConnectable(node, voltageLevelId); + index.notifyUpdate(connectable, "moveConnectable", oldTopologyPoint, TerminalImpl.this.getTopologyPoint()); + } + }; + busBreakerView = new TerminalBusBreakerViewImpl<>(index, connectable, attributesGetter) { + @Override + public void moveConnectable(String busId, boolean connected) { + TopologyPoint oldTopologyPoint = TerminalImpl.this.getTopologyPoint(); + super.moveConnectable(busId, connected); + index.notifyUpdate(connectable, "moveConnectable", oldTopologyPoint, TerminalImpl.this.getTopologyPoint()); + } + }; busView = new TerminalBusViewImpl<>(index, connectable, attributesGetter); } @@ -55,8 +69,8 @@ protected boolean isNodeBeakerTopologyKind() { return getTopologyKind() == TopologyKind.NODE_BREAKER; } - private boolean isBusBeakerTopologyKind() { - return getTopologyKind() == TopologyKind.BUS_BREAKER; + private TopologyPoint getTopologyPoint() { + return isNodeBeakerTopologyKind() ? new NodeTopologyPointImpl(getAttributes().getVoltageLevelId(), getNodeBreakerView().getNode()) : new BusTopologyPointImpl(getAttributes().getVoltageLevelId(), getBusBreakerView().getConnectableBus().getId(), isConnected()); } @Override diff --git a/network-store-iidm-impl/src/main/java/com/powsybl/network/store/iidm/impl/TerminalNodeBreakerViewImpl.java b/network-store-iidm-impl/src/main/java/com/powsybl/network/store/iidm/impl/TerminalNodeBreakerViewImpl.java index 08cb6325e..286606fbc 100644 --- a/network-store-iidm-impl/src/main/java/com/powsybl/network/store/iidm/impl/TerminalNodeBreakerViewImpl.java +++ b/network-store-iidm-impl/src/main/java/com/powsybl/network/store/iidm/impl/TerminalNodeBreakerViewImpl.java @@ -40,7 +40,15 @@ private AbstractIdentifiableImpl getAbstractIdentifiable() { } private InjectionAttributes getAttributes() { - return attributesGetter.apply(getAbstractIdentifiable().getResource()); + return getAttributes(getAbstractIdentifiable().getResource()); + } + + private InjectionAttributes getAttributes(Resource resource) { + return attributesGetter.apply(resource); + } + + private VoltageLevelImpl getVoltageLevel() { + return index.getVoltageLevel(getAttributes().getVoltageLevelId()).orElseThrow(IllegalStateException::new); } @Override @@ -70,13 +78,15 @@ public void moveConnectable(int node, String voltageLevelId) { throw new ValidationException(attributes.getResource(), "an equipment (" + terminal.getConnectable().getId() + ") is already connected to node " + node + " of voltage level " + voltageLevelId); } + VoltageLevelImpl oldVoltageLevel = getVoltageLevel(); getAbstractIdentifiable().updateResource(res -> { - InjectionAttributes attr = attributesGetter.apply(res); + InjectionAttributes attr = getAttributes(res); attr.setConnectableBus(null); attr.setBus(null); attr.setNode(node); attr.setVoltageLevelId(voltageLevelId); }); + oldVoltageLevel.invalidateCalculatedBuses(); voltageLevel.invalidateCalculatedBuses(); } } diff --git a/network-store-iidm-impl/src/test/java/com/powsybl/network/store/iidm/impl/tck/MoveConnectableNotifTest.java b/network-store-iidm-impl/src/test/java/com/powsybl/network/store/iidm/impl/tck/MoveConnectableNotifTest.java index 16ef9cdd7..22b09fcb4 100644 --- a/network-store-iidm-impl/src/test/java/com/powsybl/network/store/iidm/impl/tck/MoveConnectableNotifTest.java +++ b/network-store-iidm-impl/src/test/java/com/powsybl/network/store/iidm/impl/tck/MoveConnectableNotifTest.java @@ -12,13 +12,4 @@ * @author Franck Lecuyer */ public class MoveConnectableNotifTest extends AbstractMoveConnectableNotifTest { - @Override - public void nodeBreakerTest() { - // FIXME remove this test when Terminal.moveConnectable sends a notification (notifyUpdate) - } - - @Override - public void busBreakerTest() { - // FIXME remove this test when Terminal.moveConnectable sends a notification (notifyUpdate) - } } From 2016ca3b944c0c9e65f3b2af57062be1cd32f104 Mon Sep 17 00:00:00 2001 From: Slimane AMAR Date: Thu, 3 Oct 2024 17:27:39 +0200 Subject: [PATCH 2/3] Review remarks Signed-off-by: Slimane AMAR --- .../network/store/iidm/impl/BusTopologyPointImpl.java | 2 +- .../powsybl/network/store/iidm/impl/TerminalImpl.java | 11 +++++++---- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/network-store-iidm-impl/src/main/java/com/powsybl/network/store/iidm/impl/BusTopologyPointImpl.java b/network-store-iidm-impl/src/main/java/com/powsybl/network/store/iidm/impl/BusTopologyPointImpl.java index aa20a713d..2a1fd934d 100644 --- a/network-store-iidm-impl/src/main/java/com/powsybl/network/store/iidm/impl/BusTopologyPointImpl.java +++ b/network-store-iidm-impl/src/main/java/com/powsybl/network/store/iidm/impl/BusTopologyPointImpl.java @@ -50,7 +50,7 @@ public boolean isConnected() { @Override public String toString() { - return "NodeTopologyPoint(" + + return "BusTopologyPoint(" + "voltageLevelId='" + voltageLevelId + '\'' + ", connectableBusId='" + connectableBusId + '\'' + ", connected=" + connected + diff --git a/network-store-iidm-impl/src/main/java/com/powsybl/network/store/iidm/impl/TerminalImpl.java b/network-store-iidm-impl/src/main/java/com/powsybl/network/store/iidm/impl/TerminalImpl.java index 95b40ab52..afdbaed34 100644 --- a/network-store-iidm-impl/src/main/java/com/powsybl/network/store/iidm/impl/TerminalImpl.java +++ b/network-store-iidm-impl/src/main/java/com/powsybl/network/store/iidm/impl/TerminalImpl.java @@ -47,7 +47,7 @@ public TerminalImpl(NetworkObjectIndex index, Connectable connectable, Functi public void moveConnectable(int node, String voltageLevelId) { TopologyPoint oldTopologyPoint = TerminalImpl.this.getTopologyPoint(); super.moveConnectable(node, voltageLevelId); - index.notifyUpdate(connectable, "moveConnectable", oldTopologyPoint, TerminalImpl.this.getTopologyPoint()); + index.notifyUpdate(connectable, "terminal" + getSide().getNum(), oldTopologyPoint, TerminalImpl.this.getTopologyPoint()); } }; busBreakerView = new TerminalBusBreakerViewImpl<>(index, connectable, attributesGetter) { @@ -55,7 +55,7 @@ public void moveConnectable(int node, String voltageLevelId) { public void moveConnectable(String busId, boolean connected) { TopologyPoint oldTopologyPoint = TerminalImpl.this.getTopologyPoint(); super.moveConnectable(busId, connected); - index.notifyUpdate(connectable, "moveConnectable", oldTopologyPoint, TerminalImpl.this.getTopologyPoint()); + index.notifyUpdate(connectable, "terminal" + getSide().getNum(), oldTopologyPoint, TerminalImpl.this.getTopologyPoint()); } }; busView = new TerminalBusViewImpl<>(index, connectable, attributesGetter); @@ -584,8 +584,11 @@ public String getVoltageLevelId() { @Override public ThreeSides getSide() { - // TODO Auto-generated method stub - throw new UnsupportedOperationException("Unimplemented method 'getSide'"); + int index = connectable.getTerminals().indexOf(this); + if (index < 0) { + throw new IllegalStateException(); + } + return ThreeSides.valueOf(index + 1); } public void addNewRegulatingPoint(RegulatingPoint regulatingPoint) { From 847ef2b46ee5277d5fb0266385f7f8e7ceab986b Mon Sep 17 00:00:00 2001 From: Slimane AMAR Date: Fri, 4 Oct 2024 11:01:24 +0200 Subject: [PATCH 3/3] Review remarks Signed-off-by: Slimane AMAR --- .../com/powsybl/network/store/iidm/impl/TerminalImpl.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/network-store-iidm-impl/src/main/java/com/powsybl/network/store/iidm/impl/TerminalImpl.java b/network-store-iidm-impl/src/main/java/com/powsybl/network/store/iidm/impl/TerminalImpl.java index afdbaed34..53a230e19 100644 --- a/network-store-iidm-impl/src/main/java/com/powsybl/network/store/iidm/impl/TerminalImpl.java +++ b/network-store-iidm-impl/src/main/java/com/powsybl/network/store/iidm/impl/TerminalImpl.java @@ -584,11 +584,11 @@ public String getVoltageLevelId() { @Override public ThreeSides getSide() { - int index = connectable.getTerminals().indexOf(this); - if (index < 0) { + int terminalIndex = connectable.getTerminals().indexOf(this); + if (terminalIndex < 0) { throw new IllegalStateException(); } - return ThreeSides.valueOf(index + 1); + return ThreeSides.valueOf(terminalIndex + 1); } public void addNewRegulatingPoint(RegulatingPoint regulatingPoint) {