From cf84bb373a3a29b0fdcbb937d0231d23fce5f665 Mon Sep 17 00:00:00 2001 From: Thomas ADAM <52197093+tadam50@users.noreply.github.com> Date: Wed, 25 Sep 2019 15:37:20 +0200 Subject: [PATCH] Implementation of Identifiable::getNetwork() (#947) * Implementation for Identifiable::getNetwork() Signed-off-by: Thomas ADAM --- .../com/powsybl/iidm/network/Identifiable.java | 5 +++++ .../powsybl/iidm/network/impl/AbstractBus.java | 5 +++++ .../iidm/network/impl/AbstractConnectable.java | 3 ++- .../iidm/network/impl/AbstractIdentifiable.java | 3 +++ .../com/powsybl/iidm/network/impl/MergedBus.java | 5 +++++ .../powsybl/iidm/network/impl/NetworkImpl.java | 5 +++++ .../com/powsybl/iidm/network/impl/SwitchImpl.java | 15 ++++++++++----- .../com/powsybl/iidm/network/impl/BusTest.java | 1 + .../iidm/network/impl/CalculatedTopologyTest.java | 2 ++ .../powsybl/iidm/network/impl/NetworkTest.java | 1 + .../iidm/network/impl/VariantManagerImplTest.java | 5 +++++ 11 files changed, 44 insertions(+), 6 deletions(-) diff --git a/iidm/iidm-api/src/main/java/com/powsybl/iidm/network/Identifiable.java b/iidm/iidm-api/src/main/java/com/powsybl/iidm/network/Identifiable.java index 6c49faf8383..022e35c11fd 100644 --- a/iidm/iidm-api/src/main/java/com/powsybl/iidm/network/Identifiable.java +++ b/iidm/iidm-api/src/main/java/com/powsybl/iidm/network/Identifiable.java @@ -20,6 +20,11 @@ */ public interface Identifiable> extends Extendable { + /** + * Get the network associated to the object. + */ + Network getNetwork(); + /** * Get the unique identifier of the object. */ diff --git a/iidm/iidm-impl/src/main/java/com/powsybl/iidm/network/impl/AbstractBus.java b/iidm/iidm-impl/src/main/java/com/powsybl/iidm/network/impl/AbstractBus.java index e84f45c9a4e..912e949bd94 100644 --- a/iidm/iidm-impl/src/main/java/com/powsybl/iidm/network/impl/AbstractBus.java +++ b/iidm/iidm-impl/src/main/java/com/powsybl/iidm/network/impl/AbstractBus.java @@ -42,6 +42,11 @@ public boolean isInMainSynchronousComponent() { return sc != null && sc.getNum() == ComponentConstants.MAIN_NUM; } + @Override + public NetworkImpl getNetwork() { + return voltageLevel.getNetwork(); + } + @Override public VoltageLevel getVoltageLevel() { return voltageLevel; diff --git a/iidm/iidm-impl/src/main/java/com/powsybl/iidm/network/impl/AbstractConnectable.java b/iidm/iidm-impl/src/main/java/com/powsybl/iidm/network/impl/AbstractConnectable.java index 56c8f3e8ad7..7b28ef13104 100644 --- a/iidm/iidm-impl/src/main/java/com/powsybl/iidm/network/impl/AbstractConnectable.java +++ b/iidm/iidm-impl/src/main/java/com/powsybl/iidm/network/impl/AbstractConnectable.java @@ -42,7 +42,8 @@ public List getTerminals() { return terminals; } - protected NetworkImpl getNetwork() { + @Override + public NetworkImpl getNetwork() { if (terminals.isEmpty()) { throw new PowsyblException(id + " is not attached to a network"); } diff --git a/iidm/iidm-impl/src/main/java/com/powsybl/iidm/network/impl/AbstractIdentifiable.java b/iidm/iidm-impl/src/main/java/com/powsybl/iidm/network/impl/AbstractIdentifiable.java index 406ee194147..ea84d2b4d1b 100644 --- a/iidm/iidm-impl/src/main/java/com/powsybl/iidm/network/impl/AbstractIdentifiable.java +++ b/iidm/iidm-impl/src/main/java/com/powsybl/iidm/network/impl/AbstractIdentifiable.java @@ -38,6 +38,9 @@ public String getName() { return name != null ? name : id; } + @Override + public abstract NetworkImpl getNetwork(); + protected abstract String getTypeDescription(); @Override diff --git a/iidm/iidm-impl/src/main/java/com/powsybl/iidm/network/impl/MergedBus.java b/iidm/iidm-impl/src/main/java/com/powsybl/iidm/network/impl/MergedBus.java index 8720624e2c9..b15d02e4957 100644 --- a/iidm/iidm-impl/src/main/java/com/powsybl/iidm/network/impl/MergedBus.java +++ b/iidm/iidm-impl/src/main/java/com/powsybl/iidm/network/impl/MergedBus.java @@ -87,6 +87,11 @@ public void invalidate() { buses.clear(); } + @Override + public NetworkImpl getNetwork() { + return (NetworkImpl) getVoltageLevel().getNetwork(); + } + @Override public VoltageLevel getVoltageLevel() { checkValidity(); diff --git a/iidm/iidm-impl/src/main/java/com/powsybl/iidm/network/impl/NetworkImpl.java b/iidm/iidm-impl/src/main/java/com/powsybl/iidm/network/impl/NetworkImpl.java index 5a2e258ba39..81038f91276 100644 --- a/iidm/iidm-impl/src/main/java/com/powsybl/iidm/network/impl/NetworkImpl.java +++ b/iidm/iidm-impl/src/main/java/com/powsybl/iidm/network/impl/NetworkImpl.java @@ -174,6 +174,11 @@ public NetworkIndex getIndex() { return index; } + @Override + public NetworkImpl getNetwork() { + return this; + } + @Override public VariantManagerImpl getVariantManager() { return variantManager; diff --git a/iidm/iidm-impl/src/main/java/com/powsybl/iidm/network/impl/SwitchImpl.java b/iidm/iidm-impl/src/main/java/com/powsybl/iidm/network/impl/SwitchImpl.java index bdfb7116350..987005ab821 100644 --- a/iidm/iidm-impl/src/main/java/com/powsybl/iidm/network/impl/SwitchImpl.java +++ b/iidm/iidm-impl/src/main/java/com/powsybl/iidm/network/impl/SwitchImpl.java @@ -42,6 +42,11 @@ class SwitchImpl extends AbstractIdentifiable implements Switch, MultiVa } } + @Override + public NetworkImpl getNetwork() { + return voltageLevel.getNetwork(); + } + @Override public VoltageLevelExt getVoltageLevel() { return voltageLevel; @@ -54,12 +59,12 @@ public SwitchKind getKind() { @Override public boolean isOpen() { - return open.get(voltageLevel.getNetwork().getVariantIndex()); + return open.get(getNetwork().getVariantIndex()); } @Override public void setOpen(boolean open) { - NetworkImpl network = voltageLevel.getNetwork(); + NetworkImpl network = getNetwork(); int index = network.getVariantIndex(); boolean oldValue = this.open.get(index); if (oldValue != open) { @@ -72,7 +77,7 @@ public void setOpen(boolean open) { @Override public boolean isRetained() { - return retained.get(voltageLevel.getNetwork().getVariantIndex()); + return retained.get(getNetwork().getVariantIndex()); } @Override @@ -80,7 +85,7 @@ public void setRetained(boolean retained) { if (voltageLevel.getTopologyKind() != TopologyKind.NODE_BREAKER) { throw new ValidationException(this, "retain status is not modifiable in a non node/breaker voltage level"); } - NetworkImpl network = voltageLevel.getNetwork(); + NetworkImpl network = getNetwork(); int index = network.getVariantIndex(); boolean oldValue = this.retained.get(index); if (oldValue != retained) { @@ -102,7 +107,7 @@ public void setFictitious(boolean fictitious) { if (oldValue != fictitious) { this.fictitious = fictitious; voltageLevel.invalidateCache(); - NetworkImpl network = voltageLevel.getNetwork(); + NetworkImpl network = getNetwork(); network.getListeners().notifyUpdate(this, "fictitious", oldValue, fictitious); } } diff --git a/iidm/iidm-impl/src/test/java/com/powsybl/iidm/network/impl/BusTest.java b/iidm/iidm-impl/src/test/java/com/powsybl/iidm/network/impl/BusTest.java index ee84cf2cbfb..2773154995e 100644 --- a/iidm/iidm-impl/src/test/java/com/powsybl/iidm/network/impl/BusTest.java +++ b/iidm/iidm-impl/src/test/java/com/powsybl/iidm/network/impl/BusTest.java @@ -69,6 +69,7 @@ public void testSetterGetter() { vscConverterStation.getTerminal().setQ(q2); assertSame(voltageLevel, bus.getVoltageLevel()); + assertSame(network, bus.getNetwork()); try { bus.setV(-1.0); fail(); diff --git a/iidm/iidm-impl/src/test/java/com/powsybl/iidm/network/impl/CalculatedTopologyTest.java b/iidm/iidm-impl/src/test/java/com/powsybl/iidm/network/impl/CalculatedTopologyTest.java index 52999b889cc..84b32d3b8a0 100644 --- a/iidm/iidm-impl/src/test/java/com/powsybl/iidm/network/impl/CalculatedTopologyTest.java +++ b/iidm/iidm-impl/src/test/java/com/powsybl/iidm/network/impl/CalculatedTopologyTest.java @@ -11,6 +11,7 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertSame; /** * @author Luma ZamarreƱo @@ -33,6 +34,7 @@ private void checkSameMergedBus(Network n, String ida, String idb) { assertNotNull(ma); assertNotNull(mb); assertEquals(ma, mb); + assertSame(n, ma.getNetwork()); } private Network createBusBreaker() { diff --git a/iidm/iidm-impl/src/test/java/com/powsybl/iidm/network/impl/NetworkTest.java b/iidm/iidm-impl/src/test/java/com/powsybl/iidm/network/impl/NetworkTest.java index 0149d9830d1..63f31792113 100644 --- a/iidm/iidm-impl/src/test/java/com/powsybl/iidm/network/impl/NetworkTest.java +++ b/iidm/iidm-impl/src/test/java/com/powsybl/iidm/network/impl/NetworkTest.java @@ -41,6 +41,7 @@ public NetworkTest() { @Test public void testNetwork1() { Network network = NetworkTest1Factory.create(); + assertSame(network, network.getNetwork()); assertEquals(1, Iterables.size(network.getCountries())); assertEquals(1, network.getCountryCount()); Country country1 = network.getCountries().iterator().next(); diff --git a/iidm/iidm-impl/src/test/java/com/powsybl/iidm/network/impl/VariantManagerImplTest.java b/iidm/iidm-impl/src/test/java/com/powsybl/iidm/network/impl/VariantManagerImplTest.java index 50c8c1219ec..5884dfb80d5 100644 --- a/iidm/iidm-impl/src/test/java/com/powsybl/iidm/network/impl/VariantManagerImplTest.java +++ b/iidm/iidm-impl/src/test/java/com/powsybl/iidm/network/impl/VariantManagerImplTest.java @@ -51,6 +51,11 @@ public String getName() { return id; } + @Override + public Network getNetwork() { + return null; + } + @Override public boolean hasProperty() { return false;