Skip to content

Commit

Permalink
Change urls for regions and operators + add cnec operator (#932)
Browse files Browse the repository at this point in the history
* Change urls for regions and operators + add cnec operator

Signed-off-by: Thomas Bouquet <thomas.bouquet@rte-france.com>

* Compilation issue

Signed-off-by: Thomas Bouquet <thomas.bouquet@rte-france.com>

* Use getTsoNameFromUrl for RA name

Signed-off-by: Thomas Bouquet <thomas.bouquet@rte-france.com>

* Remove useless import

Signed-off-by: Thomas Bouquet <thomas.bouquet@rte-france.com>

---------

Signed-off-by: Thomas Bouquet <thomas.bouquet@rte-france.com>
  • Loading branch information
bqth29 authored Mar 25, 2024
1 parent 5746fe1 commit 3d7e0a8
Show file tree
Hide file tree
Showing 49 changed files with 181 additions and 149 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public static Map<String, Set<PropertyBag>> getMappedPropertyBagsSet(PropertyBag
}

public static String getUniqueName(String prefixUrl, String suffix) {
return TsoEICode.fromEICode(prefixUrl.substring(prefixUrl.lastIndexOf('/') + 1)).getDisplayName().concat("_").concat(suffix);
return getTsoNameFromUrl(prefixUrl).concat("_").concat(suffix);
}

public static Optional<String> createElementName(String nativeElementName, String tsoNameUrl) {
Expand Down Expand Up @@ -116,4 +116,14 @@ public static PropertyBags overrideData(PropertyBags propertyBags, Map<String, S
}
return propertyBags;
}

public static String getEicFromUrl(String url) {
Pattern eicPattern = Pattern.compile("http://energy.referencedata.eu/EIC/([A-Z0-9_+\\-]{16})");
Matcher matcher = eicPattern.matcher(url);
return matcher.find() ? matcher.group(1) : null;
}

public static String getTsoNameFromUrl(String url) {
return TsoEICode.fromEICode(getEicFromUrl(url)).getDisplayName();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
import java.util.Optional;
import java.util.Set;

import static com.powsybl.openrao.data.craccreation.creator.csaprofile.craccreator.CsaProfileCracUtils.getTsoNameFromUrl;

public abstract class AbstractCnecCreator {
protected final Crac crac;
protected final Network network;
Expand Down Expand Up @@ -84,22 +86,23 @@ protected String getCnecName(String instantId, Contingency contingency, int tatl

protected boolean addCnecBaseInformation(CnecAdder<?> cnecAdder, Contingency contingency, String instantId) {
String cnecName = getCnecName(instantId, contingency);

cnecAdder.withContingency(contingency == null ? null : contingency.getId())
.withId(cnecName)
.withName(cnecName)
.withInstant(instantId)
.withOptimized(aeSecuredForRegion)
.withMonitored(aeScannedForRegion);
initCnecAdder(cnecAdder, contingency, instantId, cnecName);
return true;
}

protected void addCnecBaseInformation(CnecAdder<?> cnecAdder, Contingency contingency, String instantId, int tatlDuration) {
String cnecName = getCnecName(instantId, contingency, tatlDuration);
initCnecAdder(cnecAdder, contingency, instantId, cnecName);
}

private void initCnecAdder(CnecAdder<?> cnecAdder, Contingency contingency, String instantId, String cnecName) {
cnecAdder.withContingency(contingency == null ? null : contingency.getId())
.withId(cnecName)
.withName(cnecName)
.withInstant(instantId);
.withInstant(instantId)
.withOperator(getTsoNameFromUrl(assessedElementOperator))
.withOptimized(aeSecuredForRegion)
.withMonitored(aeScannedForRegion);
}

protected void markCnecAsImportedAndHandleRejectedContingencies(String instantId, Contingency contingency) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@
import com.powsybl.triplestore.api.PropertyBags;

import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static com.powsybl.openrao.data.craccreation.creator.csaprofile.craccreator.CsaProfileCracUtils.getEicFromUrl;

/**
* @author Jean-Pierre Arnould {@literal <jean-pierre.arnould at rte-france.com>}
*/
Expand All @@ -38,7 +39,7 @@ public class CsaProfileCnecCreator {
private final CsaProfileCracCreationContext cracCreationContext;
private final Set<Side> defaultMonitoredSides;
private final String regionEic;
private static final String EIC_REGEX = "http://energy.referencedata.eu/energy/EIC/([A-Z0-9_+\\-]{16})";
private static final String EIC_REGEX = "http://energy.referencedata.eu/EIC/([A-Z0-9_+\\-]{16})";
private static final Pattern EIC_PATTERN = Pattern.compile(EIC_REGEX);

public CsaProfileCnecCreator(Crac crac, Network network, PropertyBags assessedElementsPropertyBags, PropertyBags assessedElementsWithContingenciesPropertyBags, PropertyBags currentLimitsPropertyBags, PropertyBags voltageLimitsPropertyBags, PropertyBags angleLimitsPropertyBags, CsaProfileCracCreationContext cracCreationContext, Set<Side> defaultMonitoredSides, String regionEic) {
Expand Down Expand Up @@ -150,11 +151,8 @@ private boolean isAeScannedForRegion(PropertyBag assessedElementPropertyBag) {

private boolean isAeSecuredOrScannedForRegion(PropertyBag assessedElementPropertyBag, String propertyName) {
String rawRegionId = assessedElementPropertyBag.get(propertyName);
if (rawRegionId == null) {
return false;
}
Matcher matcher = EIC_PATTERN.matcher(rawRegionId);
return matcher.find() && matcher.group(1).equals(regionEic);
String region = rawRegionId == null ? null : getEicFromUrl(assessedElementPropertyBag.get(propertyName));
return region != null && region.equals(regionEic);
}

private PropertyBag getOperationalLimitPropertyBag(Map<String, Set<PropertyBag>> operationalLimitPropertyBags, PropertyBag assessedElementPropertyBag) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
package com.powsybl.openrao.data.craccreation.creator.csaprofile.craccreator.remedialaction;

import com.powsybl.openrao.commons.OpenRaoException;
import com.powsybl.openrao.commons.TsoEICode;
import com.powsybl.openrao.data.cracapi.*;
import com.powsybl.openrao.data.cracapi.cnec.AngleCnec;
import com.powsybl.openrao.data.cracapi.cnec.Cnec;
Expand Down Expand Up @@ -77,7 +76,7 @@ private void createRemedialActions(boolean isAuto) {
Optional<Integer> speedOpt = getSpeedOpt(remedialActionType, parentRemedialActionPropertyBag.get(TIME_TO_IMPLEMENT), remedialActionId, isAuto);
targetRemedialActionNameOpt.ifPresent(remedialActionAdder::withName);
if (tsoName != null) {
remedialActionAdder.withOperator(TsoEICode.fromEICode(tsoName.substring(tsoName.lastIndexOf("/") + 1)).getDisplayName());
remedialActionAdder.withOperator(CsaProfileCracUtils.getTsoNameFromUrl(tsoName));
}
speedOpt.ifPresent(remedialActionAdder::withSpeed);
if (elementaryActionsHelper.getContingenciesByRemedialAction().containsKey(remedialActionId)) {
Expand Down Expand Up @@ -333,6 +332,7 @@ private Set<String> createRemedialActionGroups() {
List<ElementaryAction> injectionSetpoints = new ArrayList<>();
List<ElementaryAction> pstSetPoints = new ArrayList<>();
List<ElementaryAction> topologicalActions = new ArrayList<>();
Set<String> operators = new HashSet<>();

dependingEnabledRemedialActions.forEach(remedialActionDependency -> {
String remedialActionId = remedialActionDependency.getId(REQUEST_REMEDIAL_ACTION);
Expand All @@ -347,9 +347,13 @@ private Set<String> createRemedialActionGroups() {
injectionSetpoints.addAll(crac.getNetworkAction(remedialActionId).getElementaryActions().stream().filter(InjectionSetpoint.class::isInstance).toList());
pstSetPoints.addAll(crac.getNetworkAction(remedialActionId).getElementaryActions().stream().filter(PstSetpoint.class::isInstance).toList());
topologicalActions.addAll(crac.getNetworkAction(remedialActionId).getElementaryActions().stream().filter(TopologicalAction.class::isInstance).toList());
operators.add(crac.getNetworkAction(remedialActionId).getOperator());
});

NetworkActionAdder networkActionAdder = crac.newNetworkAction().withId(groupId).withName(groupName);
if (operators.size() == 1) {
networkActionAdder.withOperator(operators.iterator().next());
}
addUsageRulesToGroup(onAngleConstraintUsageRules, onFlowConstraintUsageRules, onVoltageConstraintUsageRules, onContingencyStateUsageRules, onInstantUsageRules, injectionSetpoints, pstSetPoints, topologicalActions, networkActionAdder);
addElementaryActionsToGroup(injectionSetpoints, pstSetPoints, topologicalActions, networkActionAdder);
networkActionAdder.add();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public static void assertCnecNotImported(CsaProfileCracCreationContext cracCreat
assertTrue(cracCreationContext.getCnecCreationContexts().stream().anyMatch(context -> !context.isImported() && assessedElementId.equals(context.getNativeId()) && importStatus.equals(context.getImportStatus()) && importStatusDetail.equals(context.getImportStatusDetail())));
}

public static void assertFlowCnecEquality(FlowCnec flowCnec, String expectedFlowCnecIdAndName, String expectedNetworkElementId, String expectedInstant, String expectedContingencyId, Double expectedThresholdMaxLeft, Double expectedThresholdMinLeft, Double expectedThresholdMaxRight, Double expectedThresholdMinRight, Set<Side> expectedThresholdSides) {
public static void assertFlowCnecEquality(FlowCnec flowCnec, String expectedFlowCnecIdAndName, String expectedNetworkElementId, String expectedInstant, String expectedContingencyId, Double expectedThresholdMaxLeft, Double expectedThresholdMinLeft, Double expectedThresholdMaxRight, Double expectedThresholdMinRight, Set<Side> expectedThresholdSides, String expectedOperator) {
assertEquals(expectedFlowCnecIdAndName, flowCnec.getId());
assertEquals(expectedFlowCnecIdAndName, flowCnec.getName());
assertEquals(expectedNetworkElementId, flowCnec.getNetworkElement().getId());
Expand All @@ -93,9 +93,10 @@ public static void assertFlowCnecEquality(FlowCnec flowCnec, String expectedFlow
}

assertEquals(expectedThresholdSides, flowCnec.getMonitoredSides());
assertEquals(expectedOperator, flowCnec.getOperator());
}

public static void assertAngleCnecEquality(AngleCnec angleCnec, String expectedFlowCnecIdAndName, String expectedImportingNetworkElementId, String expectedExportingNetworkElementId, String expectedInstant, String expectedContingencyId, Double expectedThresholdMax, Double expectedThresholdMin) {
public static void assertAngleCnecEquality(AngleCnec angleCnec, String expectedFlowCnecIdAndName, String expectedImportingNetworkElementId, String expectedExportingNetworkElementId, String expectedInstant, String expectedContingencyId, Double expectedThresholdMax, Double expectedThresholdMin, String expectedOperator) {
assertEquals(expectedFlowCnecIdAndName, angleCnec.getId());
assertEquals(expectedFlowCnecIdAndName, angleCnec.getName());
assertEquals(expectedImportingNetworkElementId, angleCnec.getImportingNetworkElement().getId());
Expand All @@ -110,9 +111,10 @@ public static void assertAngleCnecEquality(AngleCnec angleCnec, String expectedF
Threshold threshold = angleCnec.getThresholds().stream().toList().iterator().next();
assertEquals(expectedThresholdMax, threshold.max().orElse(null));
assertEquals(expectedThresholdMin, threshold.min().orElse(null));
assertEquals(expectedOperator, angleCnec.getOperator());
}

public static void assertVoltageCnecEquality(VoltageCnec voltageCnec, String expectedVoltageCnecIdAndName, String expectedNetworkElementId, String expectedInstant, String expectedContingencyId, Double expectedThresholdMax, Double expectedThresholdMin) {
public static void assertVoltageCnecEquality(VoltageCnec voltageCnec, String expectedVoltageCnecIdAndName, String expectedNetworkElementId, String expectedInstant, String expectedContingencyId, Double expectedThresholdMax, Double expectedThresholdMin, String expectedOperator) {
assertEquals(expectedVoltageCnecIdAndName, voltageCnec.getId());
assertEquals(expectedVoltageCnecIdAndName, voltageCnec.getName());
assertEquals(expectedNetworkElementId, voltageCnec.getNetworkElement().getId());
Expand All @@ -125,9 +127,10 @@ public static void assertVoltageCnecEquality(VoltageCnec voltageCnec, String exp
Threshold threshold = voltageCnec.getThresholds().stream().toList().iterator().next();
assertEquals(expectedThresholdMax, threshold.max().orElse(null));
assertEquals(expectedThresholdMin, threshold.min().orElse(null));
assertEquals(expectedOperator, voltageCnec.getOperator());
}

public static void assertPstRangeActionImported(CsaProfileCracCreationContext cracCreationContext, String id, String networkElement, boolean isAltered, int numberOfUsageRules) {
public static void assertPstRangeActionImported(CsaProfileCracCreationContext cracCreationContext, String id, String networkElement, boolean isAltered, int numberOfUsageRules, String expectedOperator) {
CsaProfileElementaryCreationContext csaProfileElementaryCreationContext = cracCreationContext.getRemedialActionCreationContext(id);
assertNotNull(csaProfileElementaryCreationContext);
assertTrue(csaProfileElementaryCreationContext.isImported());
Expand All @@ -136,9 +139,10 @@ public static void assertPstRangeActionImported(CsaProfileCracCreationContext cr
String actualNetworkElement = cracCreationContext.getCrac().getPstRangeAction(id).getNetworkElement().toString();
assertEquals(networkElement, actualNetworkElement);
assertEquals(numberOfUsageRules, cracCreationContext.getCrac().getPstRangeAction(id).getUsageRules().size());
assertEquals(expectedOperator, cracCreationContext.getCrac().getPstRangeAction(id).getOperator());
}

public static void assertNetworkActionImported(CsaProfileCracCreationContext cracCreationContext, String id, Set<String> networkElements, boolean isAltered, int numberOfUsageRules) {
public static void assertNetworkActionImported(CsaProfileCracCreationContext cracCreationContext, String id, Set<String> networkElements, boolean isAltered, int numberOfUsageRules, String expectedOperator) {
CsaProfileElementaryCreationContext csaProfileElementaryCreationContext = cracCreationContext.getRemedialActionCreationContext(id);
assertNotNull(csaProfileElementaryCreationContext);
assertTrue(csaProfileElementaryCreationContext.isImported());
Expand All @@ -147,6 +151,7 @@ public static void assertNetworkActionImported(CsaProfileCracCreationContext cra
Set<String> actualNetworkElements = cracCreationContext.getCrac().getNetworkAction(id).getNetworkElements().stream().map(NetworkElement::getId).collect(Collectors.toSet());
assertEquals(networkElements, actualNetworkElements);
assertEquals(numberOfUsageRules, cracCreationContext.getCrac().getNetworkAction(id).getUsageRules().size());
assertEquals(expectedOperator, cracCreationContext.getCrac().getNetworkAction(id).getOperator());
}

public static void assertHasOnInstantUsageRule(CsaProfileCracCreationContext cracCreationContext, String raId, Instant instant, UsageMethod usageMethod) {
Expand Down Expand Up @@ -205,17 +210,18 @@ public static void assertRaNotImported(CsaProfileCracCreationContext cracCreatio
assertEquals(importStatus, context.getImportStatus());
}

public static void assertSimpleTopologicalActionImported(NetworkAction networkAction, String raId, String raName, String switchId, ActionType actionType) {
public static void assertSimpleTopologicalActionImported(NetworkAction networkAction, String raId, String raName, String switchId, ActionType actionType, String expectedOperator) {
assertEquals(raId, networkAction.getId());
assertEquals(raName, networkAction.getName());
assertEquals(1, networkAction.getElementaryActions().size());
ElementaryAction elementaryAction = networkAction.getElementaryActions().iterator().next();
assertTrue(elementaryAction instanceof TopologicalAction);
assertEquals(switchId, ((TopologicalAction) elementaryAction).getNetworkElement().getId());
assertEquals(actionType, ((TopologicalAction) elementaryAction).getActionType());
assertEquals(expectedOperator, networkAction.getOperator());
}

public static void assertSimpleInjectionSetpointActionImported(NetworkAction networkAction, String raId, String raName, String networkElementId, double setpoint, Unit unit) {
public static void assertSimpleInjectionSetpointActionImported(NetworkAction networkAction, String raId, String raName, String networkElementId, double setpoint, Unit unit, String expectedOperator) {
assertEquals(raId, networkAction.getId());
assertEquals(raName, networkAction.getName());
assertEquals(1, networkAction.getElementaryActions().size());
Expand All @@ -224,12 +230,14 @@ public static void assertSimpleInjectionSetpointActionImported(NetworkAction net
assertEquals(networkElementId, ((InjectionSetpoint) elementaryAction).getNetworkElement().getId());
assertEquals(setpoint, ((InjectionSetpoint) elementaryAction).getSetpoint());
assertEquals(unit, ((InjectionSetpoint) elementaryAction).getUnit());
assertEquals(expectedOperator, networkAction.getOperator());
}

public static void assertPstRangeActionImported(PstRangeAction pstRangeAction, String expectedId, String expectedName, String expectedPstId, Integer expectedMinTap, Integer expectedMaxTap) {
public static void assertPstRangeActionImported(PstRangeAction pstRangeAction, String expectedId, String expectedName, String expectedPstId, Integer expectedMinTap, Integer expectedMaxTap, String expectedOperator) {
assertEquals(expectedId, pstRangeAction.getId());
assertEquals(expectedName, pstRangeAction.getName());
assertEquals(expectedPstId, pstRangeAction.getNetworkElement().getId());
assertEquals(expectedOperator, pstRangeAction.getOperator());
if (expectedMinTap == null && expectedMaxTap == null) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import java.util.*;

import static com.powsybl.openrao.data.craccreation.creator.csaprofile.craccreator.CsaProfileCracUtils.getEicFromUrl;
import static org.junit.jupiter.api.Assertions.*;

class CsaProfileCracUtilsTest {
Expand Down Expand Up @@ -142,4 +143,12 @@ void testInvalidDurationPattern() {
assertThrows(OpenRaoException.class, () -> CsaProfileCracUtils.convertDurationToSeconds("P5Y4M"));
assertThrows(OpenRaoException.class, () -> CsaProfileCracUtils.convertDurationToSeconds("P0Y5MT3S"));
}

@Test
void testEicFromUrl() {
assertEquals("10XES-REE------E", getEicFromUrl("http://energy.referencedata.eu/EIC/10XES-REE------E"));
assertEquals("10XPT-REN------9", getEicFromUrl("http://energy.referencedata.eu/EIC/10XPT-REN------9"));
assertEquals("10XFR-RTE------Q", getEicFromUrl("http://energy.referencedata.eu/EIC/10XFR-RTE------Q"));
assertNull(getEicFromUrl("Hello world!"));
}
}
Loading

0 comments on commit 3d7e0a8

Please sign in to comment.