Skip to content

Commit

Permalink
add implementation to modify connectablePosition attributes (#437)
Browse files Browse the repository at this point in the history
---------

Signed-off-by: Rehili Ghazwa <[email protected]>
  • Loading branch information
ghazwarhili authored Sep 6, 2024
1 parent b8da07a commit f34c329
Show file tree
Hide file tree
Showing 2 changed files with 288 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
*/
package com.powsybl.network.store.iidm.impl;

import com.powsybl.commons.PowsyblException;
import com.powsybl.commons.extensions.AbstractExtension;
import com.powsybl.iidm.network.Connectable;
import com.powsybl.iidm.network.ThreeSides;
import com.powsybl.iidm.network.extensions.ConnectablePosition;
import com.powsybl.network.store.model.ConnectablePositionAttributes;

Expand All @@ -23,10 +25,21 @@ public class ConnectablePositionImpl<C extends Connectable<C>> extends AbstractE

public class FeederImpl implements Feeder {

private static final String LABEL = "label";
private static final String ORDER = "order";
private static final String DIRECTION = "direction";
private static final String NOT_SUPPORTED = "Not supported side";
private static final String UNEXPECTED_EXTENDABLE = "Unexpected extendable instance: ";
private final ThreeSides side;
private final Function<Connectable<C>, ConnectablePositionAttributes> getter;

public FeederImpl(Function<Connectable<C>, ConnectablePositionAttributes> getter) {
public FeederImpl(Function<Connectable<C>, ConnectablePositionAttributes> getter, ThreeSides side) {
this.getter = Objects.requireNonNull(getter);
this.side = side;
}

private ThreeSides getSide() {
return side;
}

private ConnectablePositionAttributes getAttributes() {
Expand All @@ -40,7 +53,21 @@ public Optional<String> getName() {

@Override
public Feeder setName(String name) {
getAttributes().setLabel(Objects.requireNonNull(name));
String oldValue = getAttributes().getLabel();
if (!Objects.equals(oldValue, name)) {
if (getExtendable() instanceof AbstractInjectionImpl<?, ?> injection) {
updateInjectionLabelResource(injection, name);
notifyUpdate(injection, LABEL, oldValue, name);
} else if (getExtendable() instanceof AbstractBranchImpl<?, ?> branch) {
updateBranchLabelResource(branch, name);
notifyUpdate(branch, LABEL, oldValue, name);
} else if (getExtendable() instanceof ThreeWindingsTransformerImpl windingsTransformer) {
updateTransformerLabelResource(windingsTransformer, name);
notifyUpdate(windingsTransformer, LABEL, oldValue, name);
} else {
throw new AssertionError(UNEXPECTED_EXTENDABLE + getExtendable().getClass());
}
}
return this;
}

Expand All @@ -57,7 +84,22 @@ public Feeder removeOrder() {

@Override
public Feeder setOrder(int order) {
getAttributes().setOrder(order);
Integer oldValue = getAttributes().getOrder();
if (!Objects.equals(oldValue, order)) {
if (getExtendable() instanceof AbstractInjectionImpl<?, ?> injection) {
updateInjectionOrderResource(injection, order);
notifyUpdate(injection, ORDER, oldValue, order);

} else if (getExtendable() instanceof AbstractBranchImpl<?, ?> branch) {
updateBranchOrderResource(branch, order);
notifyUpdate(branch, ORDER, oldValue, order);
} else if (getExtendable() instanceof ThreeWindingsTransformerImpl windingsTransformer) {
updateTransformerOrderResource(windingsTransformer, order);
notifyUpdate(windingsTransformer, ORDER, oldValue, order);
} else {
throw new AssertionError(UNEXPECTED_EXTENDABLE + getExtendable().getClass());
}
}
return this;
}

Expand All @@ -68,9 +110,129 @@ public Direction getDirection() {

@Override
public Feeder setDirection(Direction direction) {
getAttributes().setDirection(Direction.valueOf(Objects.requireNonNull(direction).name()));
Direction oldValue = getAttributes().getDirection();
if (!Objects.equals(oldValue, direction)) {
if (getExtendable() instanceof AbstractInjectionImpl<?, ?> injection) {
updateInjectionDirectionResource(injection, direction);
notifyUpdate(injection, DIRECTION, oldValue, direction);
} else if (getExtendable() instanceof AbstractBranchImpl<?, ?> branch) {
updateBranchDirectionResource(branch, direction);
notifyUpdate(branch, DIRECTION, oldValue, direction);
} else if (getExtendable() instanceof ThreeWindingsTransformerImpl windingsTransformer) {
updateTransformerDirectionResource(windingsTransformer, direction);
notifyUpdate(windingsTransformer, DIRECTION, oldValue, direction);
} else {
throw new AssertionError(UNEXPECTED_EXTENDABLE + getExtendable().getClass());
}
}
return this;
}

private void updateInjectionLabelResource(AbstractInjectionImpl<?, ?> injection, String name) {
injection.updateResource(res -> res.getAttributes().getPosition().setLabel(Objects.requireNonNull(name)));
}

private void updateBranchLabelResource(AbstractBranchImpl<?, ?> branch, String name) {
Objects.requireNonNull(name);
ThreeSides sides = Objects.requireNonNull(getSide());
branch.updateResource(res -> {
if (sides == ThreeSides.ONE) {
res.getAttributes().getPosition1().setLabel(name);
} else if (sides == ThreeSides.TWO) {
res.getAttributes().getPosition2().setLabel(name);
} else {
throw new PowsyblException(NOT_SUPPORTED);
}
});
}

private void updateTransformerLabelResource(ThreeWindingsTransformerImpl windingsTransformer, String name) {
Objects.requireNonNull(name);
ThreeSides sides = Objects.requireNonNull(getSide());
windingsTransformer.updateResource(res -> {
if (sides == ThreeSides.ONE) {
res.getAttributes().getPosition1().setLabel(name);
} else if (sides == ThreeSides.TWO) {
res.getAttributes().getPosition2().setLabel(name);
} else if (sides == ThreeSides.THREE) {
res.getAttributes().getPosition3().setLabel(name);
} else {
throw new PowsyblException(NOT_SUPPORTED);
}
});
}

private void updateInjectionOrderResource(AbstractInjectionImpl<?, ?> injection, int order) {
injection.updateResource(res -> res.getAttributes().getPosition().setOrder(order));
}

private void updateBranchOrderResource(AbstractBranchImpl<?, ?> branch, int order) {
ThreeSides sides = Objects.requireNonNull(getSide());
branch.updateResource(res -> {
if (sides == ThreeSides.ONE) {
res.getAttributes().getPosition1().setOrder(order);
} else if (sides == ThreeSides.TWO) {
res.getAttributes().getPosition2().setOrder(order);
} else {
throw new PowsyblException(NOT_SUPPORTED);
}
});
}

private void updateTransformerOrderResource(ThreeWindingsTransformerImpl windingsTransformer, int order) {
ThreeSides sides = Objects.requireNonNull(getSide());
windingsTransformer.updateResource(res -> {
if (sides == ThreeSides.ONE) {
res.getAttributes().getPosition1().setOrder(order);
} else if (sides == ThreeSides.TWO) {
res.getAttributes().getPosition2().setOrder(order);
} else if (sides == ThreeSides.THREE) {
res.getAttributes().getPosition3().setOrder(order);
} else {
throw new PowsyblException(NOT_SUPPORTED);
}
});
}

private void updateInjectionDirectionResource(AbstractInjectionImpl<?, ?> injection, Direction direction) {
injection.updateResource(res -> res.getAttributes().getPosition()
.setDirection(Direction.valueOf(Objects.requireNonNull(direction).name())));
}

private void updateBranchDirectionResource(AbstractBranchImpl<?, ?> branch, Direction direction) {
Objects.requireNonNull(direction);
ThreeSides sides = Objects.requireNonNull(getSide());
branch.updateResource(res -> {
if (sides == ThreeSides.ONE) {
res.getAttributes().getPosition1().setDirection(direction);
} else if (sides == ThreeSides.TWO) {
res.getAttributes().getPosition2().setDirection(direction);
} else {
throw new PowsyblException(NOT_SUPPORTED);
}
});
}

private void updateTransformerDirectionResource(ThreeWindingsTransformerImpl windingsTransformer, Direction direction) {
Objects.requireNonNull(direction);
ThreeSides sides = Objects.requireNonNull(getSide());
windingsTransformer.updateResource(res -> {
if (sides == ThreeSides.ONE) {
res.getAttributes().getPosition1().setDirection(direction);
} else if (sides == ThreeSides.TWO) {
res.getAttributes().getPosition2().setDirection(direction);
} else if (sides == ThreeSides.THREE) {
res.getAttributes().getPosition3().setDirection(direction);
} else {
throw new PowsyblException(NOT_SUPPORTED);
}
});
}

private void notifyUpdate(AbstractConnectableImpl<?, ?> connectable, String attribute, Object oldValue, Object newValue) {
String variantId = connectable.getNetwork().getVariantManager().getWorkingVariantId();
connectable.getNetwork().getIndex().notifyUpdate(getExtendable(), attribute, variantId, oldValue, newValue);
}
}

private final Function<Connectable<C>, ConnectablePositionAttributes> positionAttributesGetter;
Expand All @@ -90,28 +252,28 @@ public ConnectablePositionImpl(C connectable,
this.positionAttributesGetter3 = positionAttributesGetter3;
}

private FeederImpl getFeeder(Function<Connectable<C>, ConnectablePositionAttributes> positionAttributesGetter) {
private FeederImpl getFeeder(Function<Connectable<C>, ConnectablePositionAttributes> positionAttributesGetter, ThreeSides side) {
return (positionAttributesGetter != null && positionAttributesGetter.apply(getExtendable()) != null) ?
new FeederImpl(positionAttributesGetter) : null;
new FeederImpl(positionAttributesGetter, side) : null;
}

@Override
public FeederImpl getFeeder() {
return getFeeder(positionAttributesGetter);
return getFeeder(positionAttributesGetter, null);
}

@Override
public FeederImpl getFeeder1() {
return getFeeder(positionAttributesGetter1);
return getFeeder(positionAttributesGetter1, ThreeSides.ONE);
}

@Override
public FeederImpl getFeeder2() {
return getFeeder(positionAttributesGetter2);
return getFeeder(positionAttributesGetter2, ThreeSides.TWO);
}

@Override
public FeederImpl getFeeder3() {
return getFeeder(positionAttributesGetter3);
return getFeeder(positionAttributesGetter3, ThreeSides.THREE);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
/**
* 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.*;
import com.powsybl.iidm.network.extensions.ConnectablePosition;
import com.powsybl.iidm.network.extensions.ConnectablePositionAdder;
import org.junit.Test;

import static com.powsybl.network.store.iidm.impl.CreateNetworksUtil.createNodeBreakerNetworkWithLine;
import static org.junit.jupiter.api.Assertions.*;

/**
* @author Ghazwa Rehili <ghazwa.rehili at rte-france.com>
*/

public class ConnectablePositionExtensionTest {
@Test
public void testModifyConnectablePositionExtensionOfBranch() {
Network network = CreateNetworksUtil.createNodeBreakerNetworkWithLine();
Line l1 = network.getLine("L1");

l1.newExtension(ConnectablePositionAdder.class)
.newFeeder1()
.withName("l1")
.withOrder(1)
.withDirection(ConnectablePosition.Direction.TOP)
.add()
.newFeeder2()
.withName("l2")
.withOrder(2)
.withDirection(ConnectablePosition.Direction.TOP)
.add()
.add();

assertNotNull(l1.getExtension(ConnectablePosition.class).getFeeder1());
assertNotNull(l1.getExtension(ConnectablePosition.class).getFeeder2());
l1.getExtension(ConnectablePosition.class).getFeeder1().setName("l1Modified").setOrder(10).setDirection(ConnectablePosition.Direction.BOTTOM);
l1.getExtension(ConnectablePosition.class).getFeeder2().setName("l2Modified").setOrder(20).setDirection(ConnectablePosition.Direction.BOTTOM);
assertEquals("l1Modified", l1.getExtension(ConnectablePosition.class).getFeeder1().getName().orElseThrow());
assertEquals("l2Modified", l1.getExtension(ConnectablePosition.class).getFeeder2().getName().orElseThrow());
assertEquals(10, (int) l1.getExtension(ConnectablePosition.class).getFeeder1().getOrder().orElseThrow());
assertEquals(20, (int) l1.getExtension(ConnectablePosition.class).getFeeder2().getOrder().orElseThrow());
assertEquals(ConnectablePosition.Direction.BOTTOM, l1.getExtension(ConnectablePosition.class).getFeeder1().getDirection());
assertEquals(ConnectablePosition.Direction.BOTTOM, l1.getExtension(ConnectablePosition.class).getFeeder2().getDirection());
}

@Test
public void testModifyConnectablePositionExtensionOfInjection() {
Network network = createNodeBreakerNetworkWithLine();
Load load = network.getLoad("LD");

load.newExtension(ConnectablePositionAdder.class)
.newFeeder()
.withName("ld")
.withOrder(20)
.withDirection(ConnectablePosition.Direction.TOP)
.add()
.add();

assertNull(load.getExtension(ConnectablePosition.class).getFeeder1());
assertNull(load.getExtension(ConnectablePosition.class).getFeeder2());
assertNull(load.getExtension(ConnectablePosition.class).getFeeder3());
load.getExtension(ConnectablePosition.class).getFeeder().setName("ldModfied").setOrder(200)
.setDirection(ConnectablePosition.Direction.BOTTOM);
assertEquals("ldModfied", load.getExtension(ConnectablePosition.class).getFeeder().getName().orElseThrow());
assertEquals(200, load.getExtension(ConnectablePosition.class).getFeeder().getOrder().orElseThrow());
assertEquals(ConnectablePosition.Direction.BOTTOM, load.getExtension(ConnectablePosition.class).getFeeder().getDirection());
}

@Test
public void testModifyConnectablePositionExtensionOfThreeWindingsTransformer() {
Network network = CreateNetworksUtil.createNodeBreakerNetwokWithMultipleEquipments();

ThreeWindingsTransformer twt1 = network.getThreeWindingsTransformer("TWT1");
assertNotNull(twt1);

twt1.newExtension(ConnectablePositionAdder.class)
.newFeeder1()
.withName("twt1")
.withOrder(10)
.withDirection(ConnectablePosition.Direction.TOP)
.add()
.newFeeder2()
.withName("twt2")
.withOrder(20)
.withDirection(ConnectablePosition.Direction.BOTTOM)
.add()
.newFeeder3()
.withName("twt3")
.withOrder(30)
.withDirection(ConnectablePosition.Direction.BOTTOM)
.add()
.add();

assertNotNull(twt1.getExtension(ConnectablePosition.class).getFeeder1());
assertNotNull(twt1.getExtension(ConnectablePosition.class).getFeeder2());
assertNotNull(twt1.getExtension(ConnectablePosition.class).getFeeder3());
twt1.getExtension(ConnectablePosition.class).getFeeder1().setName("twt1Modfied").setOrder(100).setDirection(ConnectablePosition.Direction.BOTTOM);
twt1.getExtension(ConnectablePosition.class).getFeeder2().setName("twt2Modfied").setOrder(200).setDirection(ConnectablePosition.Direction.TOP);
twt1.getExtension(ConnectablePosition.class).getFeeder3().setName("twt3Modfied").setOrder(300).setDirection(ConnectablePosition.Direction.TOP);
assertEquals("twt1Modfied", twt1.getExtension(ConnectablePosition.class).getFeeder1().getName().orElseThrow());
assertEquals("twt2Modfied", twt1.getExtension(ConnectablePosition.class).getFeeder2().getName().orElseThrow());
assertEquals("twt3Modfied", twt1.getExtension(ConnectablePosition.class).getFeeder3().getName().orElseThrow());
assertEquals(100, twt1.getExtension(ConnectablePosition.class).getFeeder1().getOrder().orElseThrow());
assertEquals(200, twt1.getExtension(ConnectablePosition.class).getFeeder2().getOrder().orElseThrow());
assertEquals(300, twt1.getExtension(ConnectablePosition.class).getFeeder3().getOrder().orElseThrow());
assertEquals(ConnectablePosition.Direction.BOTTOM, twt1.getExtension(ConnectablePosition.class).getFeeder1().getDirection());
assertEquals(ConnectablePosition.Direction.TOP, twt1.getExtension(ConnectablePosition.class).getFeeder2().getDirection());
assertEquals(ConnectablePosition.Direction.TOP, twt1.getExtension(ConnectablePosition.class).getFeeder3().getDirection());
}
}

0 comments on commit f34c329

Please sign in to comment.