diff --git a/data/crac/crac-impl/src/main/java/com/powsybl/openrao/data/crac/impl/NetworkActionImpl.java b/data/crac/crac-impl/src/main/java/com/powsybl/openrao/data/crac/impl/NetworkActionImpl.java index f5c43a59b6..4c8f51bc66 100644 --- a/data/crac/crac-impl/src/main/java/com/powsybl/openrao/data/crac/impl/NetworkActionImpl.java +++ b/data/crac/crac-impl/src/main/java/com/powsybl/openrao/data/crac/impl/NetworkActionImpl.java @@ -9,12 +9,12 @@ import com.powsybl.action.*; import com.powsybl.commons.report.ReportNode; +import com.powsybl.iidm.modification.NetworkModificationImpact; import com.powsybl.iidm.network.*; import com.powsybl.openrao.data.crac.api.networkaction.NetworkAction; import com.powsybl.openrao.data.crac.api.networkaction.SwitchPair; import com.powsybl.openrao.data.crac.api.NetworkElement; import com.powsybl.openrao.data.crac.api.usagerule.UsageRule; -import org.apache.commons.lang3.NotImplementedException; import java.util.HashSet; import java.util.Set; @@ -46,41 +46,10 @@ public Set getElementaryActions() { @Override public boolean hasImpactOnNetwork(Network network) { return elementaryActions.stream().anyMatch(elementaryAction -> { - if (elementaryAction instanceof GeneratorAction generatorAction) { - Generator generator = network.getGenerator(generatorAction.getGeneratorId()); - return Math.abs(generator.getTargetP() - generatorAction.getActivePowerValue().getAsDouble()) >= EPSILON; - } else if (elementaryAction instanceof LoadAction loadAction) { - Load load = network.getLoad(loadAction.getLoadId()); - return Math.abs(load.getP0() - loadAction.getActivePowerValue().getAsDouble()) >= EPSILON; - } else if (elementaryAction instanceof DanglingLineAction danglingLineAction) { - DanglingLine danglingLine = network.getDanglingLine(danglingLineAction.getDanglingLineId()); - return Math.abs(danglingLine.getP0() - danglingLineAction.getActivePowerValue().getAsDouble()) >= EPSILON; - } else if (elementaryAction instanceof ShuntCompensatorPositionAction shuntCompensatorPositionAction) { - ShuntCompensator shuntCompensator = network.getShuntCompensator(shuntCompensatorPositionAction.getShuntCompensatorId()); - return Math.abs(shuntCompensator.getSectionCount() - shuntCompensatorPositionAction.getSectionCount()) > 0; - } else if (elementaryAction instanceof PhaseTapChangerTapPositionAction phaseTapChangerTapPositionAction) { - PhaseTapChanger phaseTapChanger = network.getTwoWindingsTransformer(phaseTapChangerTapPositionAction.getTransformerId()).getPhaseTapChanger(); - return phaseTapChangerTapPositionAction.getTapPosition() != phaseTapChanger.getTapPosition(); - } else if (elementaryAction instanceof SwitchPair switchPair) { + if (elementaryAction instanceof SwitchPair switchPair) { return !network.getSwitch(switchPair.getSwitchToOpen().getId()).isOpen() || network.getSwitch(switchPair.getSwitchToClose().getId()).isOpen(); - } else if (elementaryAction instanceof TerminalsConnectionAction terminalsConnectionAction) { - Identifiable element = network.getIdentifiable(terminalsConnectionAction.getElementId()); - if (element instanceof Branch branch) { - if (terminalsConnectionAction.isOpen()) { - // Line is considered closed if both terminal are connected - return branch.getTerminal1().isConnected() && branch.getTerminal2().isConnected(); - } else { - // Line is already considered opened if one of the terminals is disconnected - return !branch.getTerminal1().isConnected() || !branch.getTerminal2().isConnected(); - } - } else { - throw new NotImplementedException("TerminalsConnectionAction are only on branches for now"); - } - } else if (elementaryAction instanceof SwitchAction switchAction) { - Switch aSwitch = network.getSwitch(switchAction.getSwitchId()); - return aSwitch.isOpen() != switchAction.isOpen(); } else { - throw new NotImplementedException(); + return elementaryAction.toModification().hasImpactOnNetwork(network) == NetworkModificationImpact.HAS_IMPACT_ON_NETWORK; } }); } @@ -97,25 +66,12 @@ public boolean apply(Network network) { @Override public boolean canBeApplied(Network network) { - // TODO: To implement on powsybl-core Action return elementaryActions.stream().allMatch(elementaryAction -> { - if (elementaryAction instanceof ShuntCompensatorPositionAction shuntCompensatorPositionAction) { - ShuntCompensator shuntCompensator = network.getShuntCompensator(shuntCompensatorPositionAction.getShuntCompensatorId()); - return shuntCompensatorPositionAction.getSectionCount() <= shuntCompensator.getMaximumSectionCount(); - } else if (elementaryAction instanceof GeneratorAction || elementaryAction instanceof LoadAction || elementaryAction instanceof DanglingLineAction) { - return true; - } else if (elementaryAction instanceof PhaseTapChangerTapPositionAction phaseTapChangerTapPositionAction) { - // hypothesis: transformer is a two windings transformer - PhaseTapChanger phaseTapChanger = network.getTwoWindingsTransformer(phaseTapChangerTapPositionAction.getTransformerId()).getPhaseTapChanger(); - int tapPosition = phaseTapChangerTapPositionAction.getTapPosition(); - return tapPosition >= phaseTapChanger.getLowTapPosition() && tapPosition <= phaseTapChanger.getHighTapPosition(); - } else if (elementaryAction instanceof SwitchPair switchPair) { + if (elementaryAction instanceof SwitchPair switchPair) { // It is only applicable if, initially, one switch was closed and the other was open. return network.getSwitch(switchPair.getSwitchToOpen().getId()).isOpen() != network.getSwitch(switchPair.getSwitchToClose().getId()).isOpen(); - } else if (elementaryAction instanceof TerminalsConnectionAction || elementaryAction instanceof SwitchAction) { - return true; } else { - throw new NotImplementedException(); + return elementaryAction.toModification().hasImpactOnNetwork(network) != NetworkModificationImpact.CANNOT_BE_APPLIED; } }); } diff --git a/data/crac/crac-impl/src/test/java/com/powsybl/openrao/data/crac/impl/DanglingLineActionImplTest.java b/data/crac/crac-impl/src/test/java/com/powsybl/openrao/data/crac/impl/DanglingLineActionImplTest.java index f92b4e33ed..cdd9c96935 100644 --- a/data/crac/crac-impl/src/test/java/com/powsybl/openrao/data/crac/impl/DanglingLineActionImplTest.java +++ b/data/crac/crac-impl/src/test/java/com/powsybl/openrao/data/crac/impl/DanglingLineActionImplTest.java @@ -114,7 +114,8 @@ void hasImpactOnNetworkThrow() { .withActivePowerValue(100) .add() .add(); - assertThrows(NullPointerException.class, () -> dummy.hasImpactOnNetwork(network)); + assertFalse(dummy.canBeApplied(network)); + assertFalse(dummy.hasImpactOnNetwork(network)); } @Test diff --git a/data/crac/crac-impl/src/test/java/com/powsybl/openrao/data/crac/impl/GeneratorActionImplTest.java b/data/crac/crac-impl/src/test/java/com/powsybl/openrao/data/crac/impl/GeneratorActionImplTest.java index a34a21179a..b971bd03cb 100644 --- a/data/crac/crac-impl/src/test/java/com/powsybl/openrao/data/crac/impl/GeneratorActionImplTest.java +++ b/data/crac/crac-impl/src/test/java/com/powsybl/openrao/data/crac/impl/GeneratorActionImplTest.java @@ -109,7 +109,8 @@ void hasImpactOnNetworkThrow() { .withActivePowerValue(100) .add() .add(); - assertThrows(NullPointerException.class, () -> dummy.hasImpactOnNetwork(network)); + assertFalse(dummy.canBeApplied(network)); + assertFalse(dummy.hasImpactOnNetwork(network)); } @Test diff --git a/data/crac/crac-impl/src/test/java/com/powsybl/openrao/data/crac/impl/LoadActionImplTest.java b/data/crac/crac-impl/src/test/java/com/powsybl/openrao/data/crac/impl/LoadActionImplTest.java index 93a8c1e986..a439d48154 100644 --- a/data/crac/crac-impl/src/test/java/com/powsybl/openrao/data/crac/impl/LoadActionImplTest.java +++ b/data/crac/crac-impl/src/test/java/com/powsybl/openrao/data/crac/impl/LoadActionImplTest.java @@ -109,7 +109,8 @@ void hasImpactOnNetworkThrow() { .withActivePowerValue(100) .add() .add(); - assertThrows(NullPointerException.class, () -> dummy.hasImpactOnNetwork(network)); + assertFalse(dummy.canBeApplied(network)); + assertFalse(dummy.hasImpactOnNetwork(network)); } @Test diff --git a/data/crac/crac-impl/src/test/java/com/powsybl/openrao/data/crac/impl/ShuntCompensatorPositionActionImplTest.java b/data/crac/crac-impl/src/test/java/com/powsybl/openrao/data/crac/impl/ShuntCompensatorPositionActionImplTest.java index a43895a997..df7fcbcd48 100644 --- a/data/crac/crac-impl/src/test/java/com/powsybl/openrao/data/crac/impl/ShuntCompensatorPositionActionImplTest.java +++ b/data/crac/crac-impl/src/test/java/com/powsybl/openrao/data/crac/impl/ShuntCompensatorPositionActionImplTest.java @@ -146,7 +146,8 @@ void hasImpactOnNetworkThrow() { .withSectionCount(3) .add() .add(); - assertThrows(NullPointerException.class, () -> dummy.hasImpactOnNetwork(network)); + assertFalse(dummy.canBeApplied(network)); + assertFalse(dummy.hasImpactOnNetwork(network)); } @Test