Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix getUsageMethod bug introduced by #979 #989

Merged
merged 4 commits into from
May 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -88,13 +88,19 @@ private void computeUsageMethodPerStateAndInstant() {
updateMapWithValue(usageMethodPerInstant, usageRule.getInstant(), usageRule.getUsageMethod());
} else if (usageRule instanceof OnFlowConstraint ofc) {
State state = ofc.getFlowCnec().getState();
updateMapWithValue(usageMethodPerState, state, usageRule.getUsageMethod());
if (usageRule.getInstant().equals(state.getInstant())) {
updateMapWithValue(usageMethodPerState, state, usageRule.getUsageMethod());
}
} else if (usageRule instanceof OnAngleConstraint oac) {
State state = oac.getAngleCnec().getState();
updateMapWithValue(usageMethodPerState, state, usageRule.getUsageMethod());
if (usageRule.getInstant().equals(state.getInstant())) {
updateMapWithValue(usageMethodPerState, state, usageRule.getUsageMethod());
}
} else if (usageRule instanceof OnVoltageConstraint ovc) {
State state = ovc.getVoltageCnec().getState();
updateMapWithValue(usageMethodPerState, state, usageRule.getUsageMethod());
if (usageRule.getInstant().equals(state.getInstant())) {
updateMapWithValue(usageMethodPerState, state, usageRule.getUsageMethod());
}
} else if (usageRule instanceof OnContingencyState ocs) {
State state = ocs.getState();
updateMapWithValue(usageMethodPerState, state, usageRule.getUsageMethod());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
import com.powsybl.iidm.network.Country;
import com.powsybl.openrao.data.cracapi.Instant;
import com.powsybl.openrao.data.cracapi.State;
import com.powsybl.openrao.data.cracapi.cnec.AngleCnec;
import com.powsybl.openrao.data.cracapi.cnec.FlowCnec;
import com.powsybl.openrao.data.cracapi.cnec.VoltageCnec;
import com.powsybl.openrao.data.cracapi.usagerule.UsageMethod;
import com.powsybl.openrao.data.cracapi.usagerule.UsageRule;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -96,4 +98,85 @@ void testStrongestStateAndInstantUsageRule() {
AbstractRemedialAction<?> ra = new NetworkActionImpl("id", "name", "operator", usageRules, Collections.emptySet(), 0);
assertEquals(UsageMethod.FORCED, ra.getUsageMethod(state));
}

@Test
void testDifferentInstantsBetweenOnFlowConstraintUsageRuleAndCnec() {
Instant autoInstant = Mockito.mock(Instant.class);
Mockito.when(autoInstant.isPreventive()).thenReturn(false);
Instant curativeInstant = Mockito.mock(Instant.class);
Mockito.when(curativeInstant.isPreventive()).thenReturn(false);

State autoState = Mockito.mock(State.class);
Mockito.when(autoState.getInstant()).thenReturn(autoInstant);
State curativeState = Mockito.mock(State.class);
Mockito.when(curativeState.getInstant()).thenReturn(curativeInstant);

FlowCnec autoFlowCnec = Mockito.mock(FlowCnec.class);
Mockito.when(autoFlowCnec.getState()).thenReturn(autoState);
FlowCnec curativeFlowCnec = Mockito.mock(FlowCnec.class);
Mockito.when(curativeFlowCnec.getState()).thenReturn(curativeState);

Set<UsageRule> usageRules = Set.of(
new OnFlowConstraintImpl(UsageMethod.FORCED, autoInstant, autoFlowCnec),
new OnFlowConstraintImpl(UsageMethod.FORCED, autoInstant, curativeFlowCnec)
);

AbstractRemedialAction<?> ra = new NetworkActionImpl("id", "name", "operator", usageRules, Collections.emptySet(), 0);
assertEquals(UsageMethod.FORCED, ra.getUsageMethod(autoState));
assertEquals(UsageMethod.UNDEFINED, ra.getUsageMethod(curativeState));
}

@Test
void testDifferentInstantsBetweenOnAngleConstraintUsageRuleAndCnec() {
Instant autoInstant = Mockito.mock(Instant.class);
Mockito.when(autoInstant.isPreventive()).thenReturn(false);
Instant curativeInstant = Mockito.mock(Instant.class);
Mockito.when(curativeInstant.isPreventive()).thenReturn(false);

State autoState = Mockito.mock(State.class);
Mockito.when(autoState.getInstant()).thenReturn(autoInstant);
State curativeState = Mockito.mock(State.class);
Mockito.when(curativeState.getInstant()).thenReturn(curativeInstant);

AngleCnec autoAngleCnec = Mockito.mock(AngleCnec.class);
Mockito.when(autoAngleCnec.getState()).thenReturn(autoState);
AngleCnec curativeAngleCnec = Mockito.mock(AngleCnec.class);
Mockito.when(curativeAngleCnec.getState()).thenReturn(curativeState);

Set<UsageRule> usageRules = Set.of(
new OnAngleConstraintImpl(UsageMethod.FORCED, autoInstant, autoAngleCnec),
new OnAngleConstraintImpl(UsageMethod.FORCED, autoInstant, curativeAngleCnec)
);

AbstractRemedialAction<?> ra = new NetworkActionImpl("id", "name", "operator", usageRules, Collections.emptySet(), 0);
assertEquals(UsageMethod.FORCED, ra.getUsageMethod(autoState));
assertEquals(UsageMethod.UNDEFINED, ra.getUsageMethod(curativeState));
}

@Test
void testDifferentInstantsBetweenOnVoltageConstraintUsageRuleAndCnec() {
Instant autoInstant = Mockito.mock(Instant.class);
Mockito.when(autoInstant.isPreventive()).thenReturn(false);
Instant curativeInstant = Mockito.mock(Instant.class);
Mockito.when(curativeInstant.isPreventive()).thenReturn(false);

State autoState = Mockito.mock(State.class);
Mockito.when(autoState.getInstant()).thenReturn(autoInstant);
State curativeState = Mockito.mock(State.class);
Mockito.when(curativeState.getInstant()).thenReturn(curativeInstant);

VoltageCnec autoVoltageCnec = Mockito.mock(VoltageCnec.class);
Mockito.when(autoVoltageCnec.getState()).thenReturn(autoState);
VoltageCnec curativeVoltageCnec = Mockito.mock(VoltageCnec.class);
Mockito.when(curativeVoltageCnec.getState()).thenReturn(curativeState);

Set<UsageRule> usageRules = Set.of(
new OnVoltageConstraintImpl(UsageMethod.FORCED, autoInstant, autoVoltageCnec),
new OnVoltageConstraintImpl(UsageMethod.FORCED, autoInstant, curativeVoltageCnec)
);

AbstractRemedialAction<?> ra = new NetworkActionImpl("id", "name", "operator", usageRules, Collections.emptySet(), 0);
assertEquals(UsageMethod.FORCED, ra.getUsageMethod(autoState));
assertEquals(UsageMethod.UNDEFINED, ra.getUsageMethod(curativeState));
}
}
Loading