Skip to content

Commit 4b31798

Browse files
authored
Unify onConstraint usage rules in API (#1025)
* Unify onConstraint usage rules in API Signed-off-by: Thomas Bouquet <thomas.bouquet@rte-france.com>
1 parent b2e61a8 commit 4b31798

76 files changed

Lines changed: 2030 additions & 2322 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

data/crac-creation/crac-creator-cim/src/main/java/com/powsybl/openrao/data/craccreation/creator/cim/craccreator/remedialaction/RemedialActionSeriesCreator.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -471,16 +471,16 @@ private static void addOnFlowConstraintUsageRule(RemedialActionAdder<?> adder, F
471471
if (flowCnec.getState().getInstant().comesBefore(instant)) {
472472
return;
473473
}
474-
adder.newOnFlowConstraintUsageRule()
475-
.withFlowCnec(flowCnec.getId())
474+
adder.newOnConstraintUsageRule()
475+
.withCnec(flowCnec.getId())
476476
.withUsageMethod(instant.isAuto() ? UsageMethod.FORCED : UsageMethod.AVAILABLE)
477477
.withInstant(instant.getId())
478478
.add();
479479
}
480480

481481
private static void addOnAngleConstraintUsageRule(RemedialActionAdder<?> adder, AngleCnec angleCnec, Instant instant) {
482-
adder.newOnAngleConstraintUsageRule()
483-
.withAngleCnec(angleCnec.getId())
482+
adder.newOnConstraintUsageRule()
483+
.withCnec(angleCnec.getId())
484484
.withUsageMethod(instant.isAuto() ? UsageMethod.FORCED : UsageMethod.AVAILABLE)
485485
.withInstant(instant.getId())
486486
.add();

data/crac-creation/crac-creator-cim/src/test/java/com/powsybl/openrao/data/craccreation/creator/cim/craccreator/CimCracCreatorTest.java

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import com.powsybl.contingency.ContingencyElement;
1111
import com.powsybl.openrao.commons.Unit;
1212
import com.powsybl.openrao.data.cracapi.*;
13+
import com.powsybl.openrao.data.cracapi.cnec.AngleCnec;
1314
import com.powsybl.openrao.data.cracapi.cnec.FlowCnec;
1415
import com.powsybl.openrao.data.cracapi.cnec.Side;
1516
import com.powsybl.openrao.data.cracapi.networkaction.*;
@@ -280,24 +281,27 @@ private void assertNetworkActionImported(String id, Set<String> networkElements,
280281
private void assertHasOnFlowConstraintUsageRule(RemedialAction<?> ra, Instant instant, String flowCnecId) {
281282
assertTrue(
282283
ra.getUsageRules().stream()
283-
.filter(OnFlowConstraint.class::isInstance)
284-
.map(OnFlowConstraint.class::cast)
284+
.filter(OnConstraint.class::isInstance)
285+
.map(OnConstraint.class::cast)
285286
.anyMatch(
286287
ur -> ur.getInstant().equals(instant)
287-
&& ur.getFlowCnec().getId().equals(flowCnecId)
288+
&& ur.getCnec() instanceof FlowCnec
289+
&& ur.getCnec().getId().equals(flowCnecId)
288290
&& ur.getUsageMethod().equals(instant.isAuto() ? UsageMethod.FORCED : UsageMethod.AVAILABLE)
289291
));
290292
}
291293

292294
private void assertHasOnAngleUsageRule(String raId, String angleCnecId) {
293-
RemedialAction ra = importedCrac.getRemedialAction(raId);
295+
RemedialAction<?> ra = importedCrac.getRemedialAction(raId);
294296
assertTrue(
295297
ra.getUsageRules().stream()
296-
.filter(OnAngleConstraint.class::isInstance)
298+
.filter(OnConstraint.class::isInstance)
299+
.map(OnConstraint.class::cast)
297300
.anyMatch(
298-
ur -> ((OnAngleConstraint) ur).getInstant().isCurative()
299-
&& ((OnAngleConstraint) ur).getAngleCnec().getId().equals(angleCnecId)
300-
&& ((OnAngleConstraint) ur).getUsageMethod().equals(UsageMethod.AVAILABLE)
301+
ur -> ur.getInstant().isCurative()
302+
&& ur.getCnec() instanceof AngleCnec
303+
&& ur.getCnec().getId().equals(angleCnecId)
304+
&& ur.getUsageMethod().equals(UsageMethod.AVAILABLE)
301305
));
302306
}
303307

data/crac-creation/crac-creator-csa-profiles/src/main/java/com/powsybl/openrao/data/craccreation/creator/csaprofile/craccreator/remedialaction/CsaProfileRemedialActionsCreator.java

Lines changed: 14 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88

99
import com.powsybl.openrao.data.cracapi.*;
1010
import com.powsybl.openrao.data.cracapi.cnec.Cnec;
11-
import com.powsybl.openrao.data.cracapi.cnec.FlowCnec;
12-
import com.powsybl.openrao.data.cracapi.cnec.VoltageCnec;
1311
import com.powsybl.openrao.data.cracapi.networkaction.*;
1412
import com.powsybl.openrao.data.cracapi.usagerule.*;
1513
import com.powsybl.openrao.data.craccreation.creator.api.ImportStatus;
@@ -117,25 +115,11 @@ private boolean addOnConstraintUsageRules(String remedialActionId, Set<AssessedE
117115
Cnec<?> cnec = crac.getCnec(cnecId);
118116
UsageMethod usageMethod = getUsageMethod(cnecStatus.elementCombinationConstraintKind(), isSchemeRemedialAction, instant, remedialActionType);
119117
if (isOnConstraintInstantCoherent(cnec.getState().getInstant(), instant)) {
120-
if (cnec instanceof FlowCnec) {
121-
remedialActionAdder.newOnFlowConstraintUsageRule()
122-
.withInstant(instant.getId())
123-
.withFlowCnec(cnecId)
124-
.withUsageMethod(usageMethod)
125-
.add();
126-
} else if (cnec instanceof VoltageCnec) {
127-
remedialActionAdder.newOnVoltageConstraintUsageRule()
128-
.withInstant(instant.getId())
129-
.withVoltageCnec(cnecId)
130-
.withUsageMethod(usageMethod)
131-
.add();
132-
} else {
133-
remedialActionAdder.newOnAngleConstraintUsageRule()
134-
.withInstant(instant.getId())
135-
.withAngleCnec(cnecId)
136-
.withUsageMethod(usageMethod)
137-
.add();
138-
}
118+
remedialActionAdder.newOnConstraintUsageRule()
119+
.withInstant(instant.getId())
120+
.withCnec(cnecId)
121+
.withUsageMethod(usageMethod)
122+
.add();
139123
}
140124
} else {
141125
alterations.add(cnecStatus.statusDetails());
@@ -259,9 +243,7 @@ private Set<String> createRemedialActionGroups() {
259243
standaloneRasImplicatedIntoAGroup.addAll(dependingEnabledRemedialActions.stream().map(RemedialActionDependency::remedialAction).collect(Collectors.toSet()));
260244
throw new OpenRaoImportException(ImportStatus.INCONSISTENCY_IN_DATA, "Remedial action group " + remedialActionGroup.mrid() + " will not be imported because the remedial action " + refRemedialActionDependency.remedialAction() + " does not exist or not imported. All RA's depending in that group will be ignored: " + printRaIds(dependingEnabledRemedialActions));
261245
}
262-
List<UsageRule> onAngleConstraintUsageRules = refNetworkAction.getUsageRules().stream().filter(OnAngleConstraint.class::isInstance).toList();
263-
List<UsageRule> onFlowConstraintUsageRules = refNetworkAction.getUsageRules().stream().filter(OnFlowConstraint.class::isInstance).toList();
264-
List<UsageRule> onVoltageConstraintUsageRules = refNetworkAction.getUsageRules().stream().filter(OnVoltageConstraint.class::isInstance).toList();
246+
List<UsageRule> onConstraintUsageRules = refNetworkAction.getUsageRules().stream().filter(OnConstraint.class::isInstance).toList();
265247
List<UsageRule> onContingencyStateUsageRules = refNetworkAction.getUsageRules().stream().filter(OnContingencyState.class::isInstance).toList();
266248
List<UsageRule> onInstantUsageRules = refNetworkAction.getUsageRules().stream().filter(OnInstant.class::isInstance).toList();
267249

@@ -289,7 +271,7 @@ private Set<String> createRemedialActionGroups() {
289271
if (operators.size() == 1) {
290272
networkActionAdder.withOperator(operators.iterator().next());
291273
}
292-
addUsageRulesToGroup(onAngleConstraintUsageRules, onFlowConstraintUsageRules, onVoltageConstraintUsageRules, onContingencyStateUsageRules, onInstantUsageRules, injectionSetpoints, pstSetPoints, topologicalActions, networkActionAdder);
274+
addUsageRulesToGroup(onConstraintUsageRules, onContingencyStateUsageRules, onInstantUsageRules, injectionSetpoints, pstSetPoints, topologicalActions, networkActionAdder);
293275
addElementaryActionsToGroup(injectionSetpoints, pstSetPoints, topologicalActions, networkActionAdder);
294276
networkActionAdder.add();
295277
contextByRaId.put(remedialActionGroup.mrid(), CsaProfileElementaryCreationContext.imported(remedialActionGroup.mrid(), remedialActionGroup.mrid(), groupName, "The RemedialActionGroup with mRID " + remedialActionGroup.mrid() + " was turned into a remedial action from the following remedial actions: " + printRaIds(dependingEnabledRemedialActions), true));
@@ -328,29 +310,13 @@ private static void addElementaryActionsToGroup(List<ElementaryAction> injection
328310
});
329311
}
330312

331-
private static void addUsageRulesToGroup(List<UsageRule> onAngleConstraintUsageRules, List<UsageRule> onFlowConstraintUsageRules, List<UsageRule> onVoltageConstraintUsageRules, List<UsageRule> onContingencyStateUsageRules, List<UsageRule> onInstantUsageRules, List<ElementaryAction> injectionSetpoints, List<ElementaryAction> pstSetPoints, List<ElementaryAction> topologicalActions, NetworkActionAdder networkActionAdder) {
332-
onAngleConstraintUsageRules.forEach(ur -> {
333-
OnAngleConstraint onAngleConstraintUsageRule = (OnAngleConstraint) ur;
334-
networkActionAdder.newOnAngleConstraintUsageRule()
335-
.withInstant(onAngleConstraintUsageRule.getInstant().getId())
336-
.withUsageMethod(onAngleConstraintUsageRule.getUsageMethod())
337-
.withAngleCnec(onAngleConstraintUsageRule.getAngleCnec().getId())
338-
.add();
339-
});
340-
onFlowConstraintUsageRules.forEach(ur -> {
341-
OnFlowConstraint onFlowConstraintUsageRule = (OnFlowConstraint) ur;
342-
networkActionAdder.newOnFlowConstraintUsageRule()
343-
.withInstant(onFlowConstraintUsageRule.getInstant().getId())
344-
.withUsageMethod(onFlowConstraintUsageRule.getUsageMethod())
345-
.withFlowCnec(onFlowConstraintUsageRule.getFlowCnec().getId())
346-
.add();
347-
});
348-
onVoltageConstraintUsageRules.forEach(ur -> {
349-
OnVoltageConstraint onVoltageConstraintUsageRule = (OnVoltageConstraint) ur;
350-
networkActionAdder.newOnVoltageConstraintUsageRule()
351-
.withInstant(onVoltageConstraintUsageRule.getInstant().getId())
352-
.withUsageMethod(onVoltageConstraintUsageRule.getUsageMethod())
353-
.withVoltageCnec(onVoltageConstraintUsageRule.getVoltageCnec().getId())
313+
private static void addUsageRulesToGroup(List<UsageRule> onConstraintUsageRules, List<UsageRule> onContingencyStateUsageRules, List<UsageRule> onInstantUsageRules, List<ElementaryAction> injectionSetpoints, List<ElementaryAction> pstSetPoints, List<ElementaryAction> topologicalActions, NetworkActionAdder networkActionAdder) {
314+
onConstraintUsageRules.forEach(ur -> {
315+
OnConstraint<?> onConstraintUsageRule = (OnConstraint<?>) ur;
316+
networkActionAdder.newOnConstraintUsageRule()
317+
.withInstant(onConstraintUsageRule.getInstant().getId())
318+
.withUsageMethod(onConstraintUsageRule.getUsageMethod())
319+
.withCnec(onConstraintUsageRule.getCnec().getId())
354320
.add();
355321
});
356322
onContingencyStateUsageRules.forEach(ur -> {

data/crac-creation/crac-creator-csa-profiles/src/test/java/com/powsybl/openrao/data/craccreation/creator/csaprofile/craccreator/CsaProfileCracCreationTestUtil.java

Lines changed: 7 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import com.powsybl.openrao.data.cracapi.Instant;
1010
import com.powsybl.openrao.data.cracapi.NetworkElement;
1111
import com.powsybl.openrao.data.cracapi.cnec.AngleCnec;
12+
import com.powsybl.openrao.data.cracapi.cnec.Cnec;
1213
import com.powsybl.openrao.data.cracapi.cnec.FlowCnec;
1314
import com.powsybl.openrao.data.cracapi.cnec.Side;
1415
import com.powsybl.openrao.data.cracapi.cnec.VoltageCnec;
@@ -180,32 +181,16 @@ public static void assertHasOnContingencyStateUsageRule(CsaProfileCracCreationCo
180181
assertHasOnContingencyStateUsageRule(cracCreationContext, raId, contingencyId, cracCreationContext.getCrac().getInstant(instant), usageMethod);
181182
}
182183

183-
public static void assertHasOnFlowConstraintUsageRule(CsaProfileCracCreationContext cracCreationContext, String raId, String flowCnecId, Instant instant, UsageMethod usageMethod) {
184+
public static void assertHasOnConstraintUsageRule(CsaProfileCracCreationContext cracCreationContext, String raId, String cnecId, Instant instant, UsageMethod usageMethod, Class<? extends Cnec<?>> cnecType) {
184185
assertTrue(
185-
cracCreationContext.getCrac().getRemedialAction(raId).getUsageRules().stream().filter(OnFlowConstraint.class::isInstance)
186-
.map(OnFlowConstraint.class::cast)
187-
.anyMatch(ur -> ur.getFlowCnec().getId().equals(flowCnecId) && ur.getInstant().equals(instant) && ur.getUsageMethod().equals(usageMethod))
186+
cracCreationContext.getCrac().getRemedialAction(raId).getUsageRules().stream().filter(OnConstraint.class::isInstance)
187+
.map(OnConstraint.class::cast)
188+
.anyMatch(ur -> ur.getCnec().getId().equals(cnecId) && cnecType.isInstance(ur.getCnec()) && ur.getInstant().equals(instant) && ur.getUsageMethod().equals(usageMethod))
188189
);
189190
}
190191

191-
public static void assertHasOnFlowConstraintUsageRule(CsaProfileCracCreationContext cracCreationContext, String raId, String flowCnecId, String instant, UsageMethod usageMethod) {
192-
assertHasOnFlowConstraintUsageRule(cracCreationContext, raId, flowCnecId, cracCreationContext.getCrac().getInstant(instant), usageMethod);
193-
}
194-
195-
public static void assertHasOnAngleConstraintUsageRule(CsaProfileCracCreationContext cracCreationContext, String raId, String angleCnecId, Instant instant, UsageMethod usageMethod) {
196-
assertTrue(
197-
cracCreationContext.getCrac().getRemedialAction(raId).getUsageRules().stream().filter(OnAngleConstraint.class::isInstance)
198-
.map(OnAngleConstraint.class::cast)
199-
.anyMatch(ur -> ur.getAngleCnec().getId().equals(angleCnecId) && ur.getInstant().equals(instant) && ur.getUsageMethod().equals(usageMethod))
200-
);
201-
}
202-
203-
public static void assertHasOnVoltageConstraintUsageRule(CsaProfileCracCreationContext cracCreationContext, String raId, String voltageCnecId, Instant instant, UsageMethod usageMethod) {
204-
assertTrue(
205-
cracCreationContext.getCrac().getRemedialAction(raId).getUsageRules().stream().filter(OnVoltageConstraint.class::isInstance)
206-
.map(OnVoltageConstraint.class::cast)
207-
.anyMatch(ur -> ur.getVoltageCnec().getId().equals(voltageCnecId) && ur.getInstant().equals(instant) && ur.getUsageMethod().equals(usageMethod))
208-
);
192+
public static void assertHasOnConstraintUsageRule(CsaProfileCracCreationContext cracCreationContext, String raId, String flowCnecId, String instant, UsageMethod usageMethod, Class<? extends Cnec<?>> cnecType) {
193+
assertHasOnConstraintUsageRule(cracCreationContext, raId, flowCnecId, cracCreationContext.getCrac().getInstant(instant), usageMethod, cnecType);
209194
}
210195

211196
public static void assertRaNotImported(CsaProfileCracCreationContext cracCreationContext, String raId, ImportStatus importStatus, String importStatusDetail) {

0 commit comments

Comments
 (0)