From 049ebae8de1f30887d4d1361bc636c35c336e464 Mon Sep 17 00:00:00 2001 From: Ghazoua Rehili Date: Thu, 3 Oct 2024 09:36:32 +0200 Subject: [PATCH] handle connectablePosition extension in modification network (#539) Signed-off-by: REHILI Ghazwa --- .../modifications/ModificationUtils.java | 160 ++++++++++++------ .../modifications/LineModificationTest.java | 22 +++ .../server/utils/NetworkCreation.java | 2 +- .../server/utils/NetworkUtil.java | 20 ++- 4 files changed, 152 insertions(+), 52 deletions(-) diff --git a/src/main/java/org/gridsuite/modification/server/modifications/ModificationUtils.java b/src/main/java/org/gridsuite/modification/server/modifications/ModificationUtils.java index c2b94e850..b68ecc296 100644 --- a/src/main/java/org/gridsuite/modification/server/modifications/ModificationUtils.java +++ b/src/main/java/org/gridsuite/modification/server/modifications/ModificationUtils.java @@ -609,7 +609,7 @@ public ReportNode modifyInjectionConnectivityAttributes(ConnectablePosition c if (isVoltageOrBusbarIdMissing(modificationInfos.getVoltageLevelId(), modificationInfos.getBusOrBusbarSectionId(), modificationInfos.getEquipmentId(), reports)) { return reports.get(0); } - processConnectivityPosition(connectablePosition, connectablePositionAdder, modificationInfos, reports, false); + processConnectivityPosition(connectablePosition, connectablePositionAdder, modificationInfos, injection.getNetwork(), reports, false); modifyConnection(modificationInfos.getTerminalConnected(), injection, injection.getTerminal(), reports); return reportModifications(connectivityReports, reports, "ConnectivityModified", CONNECTIVITY); @@ -626,7 +626,7 @@ public ReportNode modifyBranchConnectivityAttributes(ConnectablePosition conn return reports.get(0); } - processConnectivityPosition(connectablePosition, connectablePositionAdder, modificationInfos, reports, true); + processConnectivityPosition(connectablePosition, connectablePositionAdder, modificationInfos, branch.getNetwork(), reports, true); modifyConnection(modificationInfos.getTerminal1Connected(), branch, branch.getTerminal1(), reports); modifyConnection(modificationInfos.getTerminal2Connected(), branch, branch.getTerminal2(), reports); @@ -648,53 +648,54 @@ private boolean isVoltageOrBusbarIdMissing(AttributeModification voltage private void processConnectivityPosition(ConnectablePosition connectablePosition, ConnectablePositionAdder connectablePositionAdder, - Object modificationInfos, + BasicEquipmentModificationInfos modificationInfos, + Network network, List reports, boolean isBranch) { if (connectablePosition != null) { modifyExistingConnectivityPosition(connectablePosition, modificationInfos, reports, isBranch); } else { - createNewConnectivityPosition(connectablePositionAdder, modificationInfos, reports, isBranch); + createNewConnectivityPosition(connectablePositionAdder, modificationInfos, network, reports, isBranch); } } private void modifyExistingConnectivityPosition(ConnectablePosition connectablePosition, - Object modificationInfos, + BasicEquipmentModificationInfos modificationInfos, List reports, boolean isBranch) { if (isBranch) { - modifyFeeder(connectablePosition.getFeeder1(), modificationInfos, reports, 1); - modifyFeeder(connectablePosition.getFeeder2(), modificationInfos, reports, 2); + modifyConnectablePosition(connectablePosition.getFeeder1(), modificationInfos, reports, 1); + modifyConnectablePosition(connectablePosition.getFeeder2(), modificationInfos, reports, 2); } else { - modifyFeeder(connectablePosition.getFeeder(), modificationInfos, reports, 0); + modifyConnectablePosition(connectablePosition.getFeeder(), modificationInfos, reports, 0); } } private void createNewConnectivityPosition(ConnectablePositionAdder adder, - Object modificationInfos, + BasicEquipmentModificationInfos modificationInfos, + Network network, List reports, boolean isBranch) { if (isBranch) { - ConnectablePositionAdder.FeederAdder feeder1 = adder.newFeeder1(); - ConnectablePositionAdder.FeederAdder feeder2 = adder.newFeeder2(); - modifyFeederAdder(feeder1, modificationInfos, reports, 1); - modifyFeederAdder(feeder2, modificationInfos, reports, 2); + ConnectablePositionAdder.FeederAdder feederAdder1 = adder.newFeeder1(); + ConnectablePositionAdder.FeederAdder feederAdder2 = adder.newFeeder2(); + addConnectablePosition(adder, feederAdder1, modificationInfos, network, reports, 1); + addConnectablePosition(adder, feederAdder2, modificationInfos, network, reports, 2); } else { - ConnectablePositionAdder.FeederAdder feeder = adder.newFeeder(); - modifyFeederAdder(feeder, modificationInfos, reports, 0); + ConnectablePositionAdder.FeederAdder feederAdder = adder.newFeeder(); + addConnectablePosition(adder, feederAdder, modificationInfos, network, reports, 0); } - adder.add(); } - private void modifyFeeder(ConnectablePosition.Feeder feeder, - Object modificationInfos, - List reports, - int feederNumber) { + private void modifyConnectablePosition(ConnectablePosition.Feeder feeder, + BasicEquipmentModificationInfos modificationInfos, + List reports, + int feederNumber) { applyModifications(feeder, modificationInfos, reports, feederNumber); } private void applyModifications(ConnectablePosition.Feeder feeder, - Object modificationInfos, + BasicEquipmentModificationInfos modificationInfos, List reports, int feederNumber) { ReportNode connectionNameReport = applyElementaryModificationsAndReturnReport(feeder::setName, @@ -720,37 +721,68 @@ private void applyModifications(ConnectablePosition.Feeder feeder, } } - private void modifyFeederAdder(ConnectablePositionAdder.FeederAdder feeder, - Object modificationInfos, - List reports, - int feederNumber) { - ReportNode connectionNameReport = applyElementaryModificationsAndReturnReport(feeder::withName, - () -> null, - getConnectionName(modificationInfos, feederNumber), - getConnectionNameField(feederNumber)); - if (connectionNameReport != null) { - reports.add(connectionNameReport); - } - ReportNode connectionDirectionReport = applyElementaryModificationsAndReturnReport(feeder::withDirection, - () -> null, - getConnectionDirection(modificationInfos, feederNumber), - getConnectionDirectionField(feederNumber)); - if (connectionDirectionReport != null) { - reports.add(connectionDirectionReport); - } - ReportNode connectionPositionReport = applyElementaryModificationsAndReturnReport(feeder::withOrder, - () -> null, - getConnectionPosition(modificationInfos, feederNumber), - getConnectionPositionField(feederNumber)); - if (connectionPositionReport != null) { - reports.add(connectionPositionReport); - } + private void addConnectablePosition(ConnectablePositionAdder adder, + ConnectablePositionAdder.FeederAdder feeder, + BasicEquipmentModificationInfos modificationInfos, + Network network, + List reports, + int feederNumber) { + AttributeModification connectionName = getConnectionName(modificationInfos, feederNumber); + AttributeModification connectionDirection = getConnectionDirection(modificationInfos, feederNumber); + AttributeModification connectionPosition = getConnectionPosition(modificationInfos, feederNumber); + AttributeModification equipmentId = getEquipmentId(modificationInfos); + AttributeModification voltageLevelId = getVoltageLevelId(modificationInfos, feederNumber); + AttributeModification busOrBusbarSectionId = getBusOrBusbarSectionId(modificationInfos, feederNumber); + int position = getPosition(connectionPosition, busOrBusbarSectionId, network, voltageLevelId); + + ReportNode connectionNameReport = applyConnectablePositionAttribute( + feeder::withName, connectionName, equipmentId, reports, + getConnectionNameField(feederNumber), connectionDirection, connectionPosition + ); + + ReportNode connectionDirectionReport = applyConnectablePositionAttribute( + feeder::withDirection, connectionDirection, + new AttributeModification<>(ConnectablePosition.Direction.UNDEFINED, OperationType.SET), + reports, getConnectionDirectionField(feederNumber), + connectionName, connectionPosition + ); + + ReportNode connectionPositionReport = applyConnectablePositionAttribute( + feeder::withOrder, connectionPosition, + new AttributeModification<>(position, OperationType.SET), + reports, getConnectionPositionField(feederNumber), + connectionName, connectionDirection + ); + if (connectionNameReport != null || connectionDirectionReport != null || connectionPositionReport != null) { feeder.add(); + adder.add(); } } - private T getConnectionDetail(Object modificationInfos, int feederNumber, + private ReportNode applyConnectablePositionAttribute(Consumer setter, + AttributeModification newValue, + AttributeModification defaultValue, + List reports, + String fieldName, + AttributeModification... dependentAttributes) { + + AttributeModification finalModification = (newValue == null && isAnyAttributesNonNull(dependentAttributes)) + ? defaultValue + : newValue; + + ReportNode report = applyElementaryModificationsAndReturnReport(setter, () -> null, finalModification, fieldName); + if (report != null) { + reports.add(report); + } + return report; + } + + private boolean isAnyAttributesNonNull(AttributeModification... attributes) { + return Arrays.stream(attributes).anyMatch(Objects::nonNull); + } + + private T getConnectionDetail(BasicEquipmentModificationInfos modificationInfos, int feederNumber, Function branchFunc1, Function branchFunc2, Function injectionFunc) { @@ -771,7 +803,23 @@ private String getConnectionFieldName(int feederNumber, String baseFieldName) { }; } - private AttributeModification getConnectionName(Object modificationInfos, int feederNumber) { + private AttributeModification getVoltageLevelId(BasicEquipmentModificationInfos modificationInfos, int feederNumber) { + return getConnectionDetail(modificationInfos, feederNumber, + BranchModificationInfos::getVoltageLevelId1, BranchModificationInfos::getVoltageLevelId2, + InjectionModificationInfos::getVoltageLevelId); + } + + private AttributeModification getBusOrBusbarSectionId(BasicEquipmentModificationInfos modificationInfos, int feederNumber) { + return getConnectionDetail(modificationInfos, feederNumber, + BranchModificationInfos::getBusOrBusbarSectionId1, BranchModificationInfos::getBusOrBusbarSectionId2, + InjectionModificationInfos::getBusOrBusbarSectionId); + } + + private AttributeModification getEquipmentId(BasicEquipmentModificationInfos modificationInfos) { + return AttributeModification.toAttributeModification(modificationInfos.getEquipmentId(), OperationType.SET); + } + + private AttributeModification getConnectionName(BasicEquipmentModificationInfos modificationInfos, int feederNumber) { return getConnectionDetail(modificationInfos, feederNumber, BranchModificationInfos::getConnectionName1, BranchModificationInfos::getConnectionName2, InjectionModificationInfos::getConnectionName); @@ -789,18 +837,30 @@ private String getConnectionPositionField(int feederNumber) { return getConnectionFieldName(feederNumber, CONNECTION_POSITION_FIELD_NAME); } - private AttributeModification getConnectionDirection(Object modificationInfos, int feederNumber) { + private AttributeModification getConnectionDirection(BasicEquipmentModificationInfos modificationInfos, int feederNumber) { return getConnectionDetail(modificationInfos, feederNumber, BranchModificationInfos::getConnectionDirection1, BranchModificationInfos::getConnectionDirection2, InjectionModificationInfos::getConnectionDirection); } - private AttributeModification getConnectionPosition(Object modificationInfos, int feederNumber) { + private AttributeModification getConnectionPosition(BasicEquipmentModificationInfos modificationInfos, int feederNumber) { return getConnectionDetail(modificationInfos, feederNumber, BranchModificationInfos::getConnectionPosition1, BranchModificationInfos::getConnectionPosition2, InjectionModificationInfos::getConnectionPosition); } + private int getPosition(AttributeModification connectionPosition, + AttributeModification busOrBusbarSectionId, + Network network, + AttributeModification voltageLevelId) { + return getPosition( + connectionPosition == null ? null : connectionPosition.getValue(), + busOrBusbarSectionId == null ? null : busOrBusbarSectionId.getValue(), + network, + getVoltageLevel(network, voltageLevelId == null ? null : voltageLevelId.getValue()) + ); + } + private void modifyConnection(AttributeModification terminalConnected, Identifiable equipment, Terminal terminal, List reports) { if (terminalConnected == null || equipment == null) { return; diff --git a/src/test/java/org/gridsuite/modification/server/modifications/LineModificationTest.java b/src/test/java/org/gridsuite/modification/server/modifications/LineModificationTest.java index 69af786b8..109581464 100644 --- a/src/test/java/org/gridsuite/modification/server/modifications/LineModificationTest.java +++ b/src/test/java/org/gridsuite/modification/server/modifications/LineModificationTest.java @@ -423,4 +423,26 @@ private void changeLineConnectionState(Line existingEquipment, boolean expectedS assertThat(existingEquipment.getTerminal1().isConnected()).isEqualTo(expectedState); assertThat(existingEquipment.getTerminal2().isConnected()).isEqualTo(expectedState); } + + @Test + public void changeLineConnectablePosition() throws Exception { + LineModificationInfos lineModificationInfos = LineModificationInfos.builder() + .stashed(false) + .equipmentId("line3") + .equipmentName(new AttributeModification<>("LineModified", OperationType.SET)) + .voltageLevelId1(new AttributeModification<>("v1", OperationType.SET)) + .voltageLevelId2(new AttributeModification<>("v3", OperationType.SET)) + .busOrBusbarSectionId1(new AttributeModification<>("1B", OperationType.SET)) + .busOrBusbarSectionId2(new AttributeModification<>("2B", OperationType.SET)) + .connectionPosition1(new AttributeModification<>(1, OperationType.SET)) + .connectionPosition2(new AttributeModification<>(1, OperationType.SET)) + .build(); + String modificationInfosJson = mapper.writeValueAsString(lineModificationInfos); + mockMvc.perform(post(getNetworkModificationUri()).content(modificationInfosJson).contentType(MediaType.APPLICATION_JSON)) + .andExpect(status().isOk()); + LineModificationInfos createdModification = (LineModificationInfos) modificationRepository.getModifications(getGroupId(), false, true).get(0); + assertEquals(1, createdModification.getConnectionPosition1().getValue()); + assertEquals(1, createdModification.getConnectionPosition2().getValue()); + + } } diff --git a/src/test/java/org/gridsuite/modification/server/utils/NetworkCreation.java b/src/test/java/org/gridsuite/modification/server/utils/NetworkCreation.java index a5c228386..9c90abbb3 100644 --- a/src/test/java/org/gridsuite/modification/server/utils/NetworkCreation.java +++ b/src/test/java/org/gridsuite/modification/server/utils/NetworkCreation.java @@ -288,7 +288,7 @@ public static Network create(UUID uuid, boolean createHvdcLine, NetworkFactory n createSwitch(v3, "v3dl2", "v3dl2", SwitchKind.DISCONNECTOR, true, false, false, 0, 30); createSwitch(v3, "v3bl2", "v3bl2", SwitchKind.BREAKER, true, false, false, 30, 31); - createLine(network, "line3", "line3", "v1", "v3", 10, 12, 12.0, 7.0, 5.5, 7.5, 6.5, 8.5, "cn1line3", 3, ConnectablePosition.Direction.TOP, "cn2line3", 3, ConnectablePosition.Direction.TOP); + createLineWithoutConnectivity(network, "line3", "line3", "v1", "v3", 10, 12, 12.0, 7.0, 5.5, 7.5, 6.5, 8.5); createSwitch(v1, "v1dl3", "v1dl3", SwitchKind.DISCONNECTOR, true, false, false, 0, 9); createSwitch(v1, "v1bl3", "v1bl3", SwitchKind.BREAKER, true, false, true, 9, 10); createSwitch(v3, "v3dl3", "v3dl3", SwitchKind.DISCONNECTOR, true, false, false, 0, 11); diff --git a/src/test/java/org/gridsuite/modification/server/utils/NetworkUtil.java b/src/test/java/org/gridsuite/modification/server/utils/NetworkUtil.java index 9507ab3f5..bd038d7b7 100644 --- a/src/test/java/org/gridsuite/modification/server/utils/NetworkUtil.java +++ b/src/test/java/org/gridsuite/modification/server/utils/NetworkUtil.java @@ -106,6 +106,24 @@ public static void createLine(Network network, String id, String name, String vo .add(); } + public static void createLineWithoutConnectivity(Network network, String id, String name, String voltageLevel1, String voltageLevel2, int node1, int node2, + double r, double x, double g1, double g2, double b1, double b2) { + network.newLine() + .setId(id) + .setName(name) + .setR(r) + .setX(x) + .setG1(g1) + .setG2(g2) + .setB1(b1) + .setB2(b2) + .setVoltageLevel1(voltageLevel1) + .setVoltageLevel2(voltageLevel2) + .setNode1(node1) + .setNode2(node2) + .add(); + } + @SuppressWarnings("SameParameterValue") public static void createLoad(VoltageLevel vl, String id, String name, int node, double p0, double q0, String feederName, int feederOrder, ConnectablePosition.Direction direction) { @@ -160,7 +178,7 @@ public static void createGenerator(VoltageLevel vl, String id, int node, double } public static void createGeneratorOnBus(VoltageLevel vl, String id, String busId, double targetP, double targetQ) { - var g = vl.newGenerator() + vl.newGenerator() .setId(id) .setName(id) .setTargetP(targetP)