Skip to content

Commit

Permalink
overriding fields with SSI and SSH (#849)
Browse files Browse the repository at this point in the history
* overriding fields with SSI and SSH

* update unit tests

* suppress unused method

* replace removePrefix with getId from propertyBag

---------

Co-authored-by: OpenSuze <[email protected]>
  • Loading branch information
jipea and OpenSuze authored Dec 15, 2023
1 parent 07e0834 commit dc780f3
Show file tree
Hide file tree
Showing 10 changed files with 520 additions and 69 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,15 @@

import com.farao_community.farao.commons.logs.FaraoLoggerProvider;
import com.farao_community.farao.data.crac_creation.creator.csa_profile.crac_creator.CsaProfileConstants;
import com.farao_community.farao.data.crac_creation.creator.csa_profile.crac_creator.CsaProfileCracUtils;
import com.farao_community.farao.data.native_crac_api.NativeCrac;
import com.powsybl.triplestore.api.PropertyBag;
import com.powsybl.triplestore.api.PropertyBags;
import com.powsybl.triplestore.api.QueryCatalog;
import com.powsybl.triplestore.api.TripleStore;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.time.OffsetDateTime;
import java.util.*;

/**
* @author Jean-Pierre Arnould {@literal <jean-pierre.arnould at rte-france.com>}
Expand Down Expand Up @@ -74,15 +71,15 @@ public Map<String, PropertyBags> getHeaders() {
}

public PropertyBags getPropertyBags(String csaProfileConstant, String keyword) {
Set<String> namesToRequest = getContextNamesToRequest(keyword);
Set<String> namesToRequest = getContextNamesToRequest(keyword);
if (namesToRequest.isEmpty()) {
return new PropertyBags();
}
return this.queryTripleStore(csaProfileConstant, namesToRequest);
}

public PropertyBags getPropertyBags(List<String> csaProfileConstants, String keyword) {
Set<String> namesToRequest = getContextNamesToRequest(keyword);
Set<String> namesToRequest = getContextNamesToRequest(keyword);
if (namesToRequest.isEmpty()) {
return new PropertyBags();
}
Expand Down Expand Up @@ -181,6 +178,45 @@ public PropertyBags getSchemeRemedialActions() {
return this.queryTripleStore(CsaProfileConstants.SCHEME_REMEDIAL_ACTION, new HashSet<>());
}

public PropertyBags getRemedialActionsSchedule() {
return this.queryTripleStore(CsaProfileConstants.REMEDIAL_ACTION_SCHEDULE, tripleStoreCsaProfileCrac.contextNames());
}

public Map<String, String> getOverridingCracData(OffsetDateTime importTimestamp) {
Map<String, String> overridingData = new HashMap<>();
for (CsaProfileConstants.OverridingObjectsFields overridingObject : CsaProfileConstants.OverridingObjectsFields.values()) {
addDataFromTripleStoreToMap(overridingData, overridingObject.getRequestName(), overridingObject.getObjectName(), overridingObject.getOverridedFieldName(), overridingObject.getHeaderType(), importTimestamp);
}
return overridingData;
}

private void addDataFromTripleStoreToMap(Map<String, String> dataMap, String queryName, String queryObjectName, String queryFieldName, CsaProfileConstants.HeaderType headerType, OffsetDateTime importTimestamp) {
PropertyBags propertyBagsResult = queryTripleStore(queryName, tripleStoreCsaProfileCrac.contextNames());
for (PropertyBag propertyBag : propertyBagsResult) {
if (CsaProfileConstants.HeaderType.START_END_DATE.equals(headerType)) {
if (CsaProfileCracUtils.checkProfileKeyword(propertyBag, CsaProfileConstants.CsaProfileKeywords.SSI) && CsaProfileCracUtils.checkProfileValidityInterval(propertyBag, importTimestamp)) {
String id = propertyBag.getId(queryObjectName);
String overridedValue = propertyBag.get(queryFieldName);
dataMap.put(id, overridedValue);
}
} else {
if (CsaProfileCracUtils.checkProfileKeyword(propertyBag, CsaProfileConstants.CsaProfileKeywords.SSH)) {
OffsetDateTime scenarioTime = OffsetDateTime.parse(propertyBag.get(CsaProfileConstants.SCENARIO_TIME));
if (importTimestamp.isEqual(scenarioTime)) {
String id = propertyBag.getId(queryObjectName);
String overridedValue = propertyBag.get(queryFieldName);
dataMap.put(id, overridedValue);
}
}
}

}
}

private PropertyBags queryTripleStore(String queryKey) {
return this.queryTripleStore(queryKey, new HashSet<>());
}

private PropertyBags queryTripleStore(List<String> queryKeys, Set<String> contexts) {
PropertyBags mergedPropertyBags = new PropertyBags();
for (String queryKey : queryKeys) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ public enum CsaProfileKeywords {
ASSESSED_ELEMENT("AE"),
CONTINGENCY("CO"),
EQUIPMENT_RELIABILITY("ER"),
REMEDIAL_ACTION("RA");
REMEDIAL_ACTION("RA"),
REMEDIAL_ACTION_SCHEDULE("RAS"),
SSI("SSI"),
SSH("SSH");

private final String keyword;

Expand Down Expand Up @@ -318,4 +321,66 @@ public String toString() {
return this.type;
}
}

public enum HeaderValidity {
OK,
INVALID_KEYWORD,
INVALID_INTERVAL;
}

public enum HeaderType {
START_END_DATE,
SCENARIO_TIME;
}

public enum OverridingObjectsFields {
CONTINGENCY("contingencyOverriding", "contingency", "normalMustStudy", "mustStudy", HeaderType.START_END_DATE),
ASSESSED_ELEMENT("assessedElementOverriding", "assessedElement", "normalEnabled", "enabled", HeaderType.START_END_DATE),
ASSESSED_ELEMENT_WITH_CONTINGENCY("assessedElementWithContingencyOverriding", "assessedElementWithContingency", "normalEnabled", "enabled", HeaderType.START_END_DATE),
ASSESSED_ELEMENT_WITH_REMEDIAL_ACTION("assessedElementWithRemedialActionOverriding", "assessedElementWithRemedialAction", "normalEnabled", "enabled", HeaderType.START_END_DATE),
CONTINGENCY_WITH_REMEDIAL_ACTION("contingencyWithRemedialActionOverriding", "contingencyWithRemedialAction", "normalEnabled", "enabled", HeaderType.START_END_DATE),
REMEDIAL_ACTION("remedialActionOverriding", "remedialAction", "normalAvailable", "available", HeaderType.START_END_DATE),
GRID_STATE_ALTERATION("gridStateAlterationOverriding", "gridStateAlteration", "normalEnabled", "enabled", HeaderType.START_END_DATE),
RANGE_CONSTRAINT("rangeConstraintOverriding", "rangeConstraint", "normalValue", "value", HeaderType.START_END_DATE),
REMEDIAL_ACTION_SCHEME("remedialActionSchemeOverriding", "remedialActionScheme", "normalArmed", "armed", HeaderType.START_END_DATE),
VOLTAGE_ANGLE_LIMIT("voltageAngleLimitOverriding", "voltageAngleLimit", "normalValue", "value", HeaderType.START_END_DATE),
CURRENT_LIMIT("currentLimitOverriding", "currentLimit", "normalValue", "value", HeaderType.SCENARIO_TIME),
VOLTAGE_LIMIT("voltageLimitOverriding", "voltageLimit", "normalValue", "value", HeaderType.SCENARIO_TIME);

String requestName;
String objectName;
String initialFieldName;
String overridedFieldName;
HeaderType headerType;

OverridingObjectsFields(String requestName, String objectName, String initialFieldName, String overridedFieldName, HeaderType headerType) {
this.requestName = requestName;
this.objectName = objectName;
this.initialFieldName = initialFieldName;
this.overridedFieldName = overridedFieldName;
this.headerType = headerType;
}

public String getRequestName() {
return this.requestName;
}

public String getObjectName() {
return this.objectName;
}

public String getInitialFieldName() {
return this.initialFieldName;
}

public String getOverridedFieldName() {
return this.overridedFieldName;
}

public HeaderType getHeaderType() {
return this.headerType;
}
}

public static final String SCENARIO_TIME = "scenarioTime";
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.powsybl.triplestore.api.PropertyBags;

import java.time.OffsetDateTime;
import java.util.Map;
import java.util.Set;

import static com.farao_community.farao.data.crac_creation.creator.csa_profile.crac_creator.CsaProfileCracUtils.isValidInterval;
Expand All @@ -46,13 +47,27 @@ public CsaProfileCracCreationContext createCrac(CsaProfileCrac nativeCrac, Netwo
this.crac = cracCreationParameters.getCracFactory().create(nativeCrac.toString());
this.network = network;
this.creationContext = new CsaProfileCracCreationContext(crac, offsetDateTime, network.getNameOrId());

clearNativeCracContextsAndMap(nativeCrac, offsetDateTime);
createContingencies(nativeCrac.getContingencies(), nativeCrac.getContingencyEquipments());
createCnecs(nativeCrac.getAssessedElements(), nativeCrac.getAssessedElementsWithContingencies(), nativeCrac.getCurrentLimits(), nativeCrac.getVoltageLimits(), nativeCrac.getAngleLimits(), cracCreationParameters.getDefaultMonitoredSides());
OnConstraintUsageRuleHelper onConstraintUsageRuleAdder = new OnConstraintUsageRuleHelper(creationContext.getCnecCreationContexts(), nativeCrac.getAssessedElements(), nativeCrac.getAssessedElementsWithRemedialAction());
createRemedialActions(nativeCrac.getRemedialActions(), nativeCrac.getTopologyAction(), nativeCrac.getRotatingMachineAction(), nativeCrac.getShuntCompensatorModifications(), nativeCrac.getTapPositionAction(), nativeCrac.getStaticPropertyRanges(), nativeCrac.getContingencyWithRemedialAction(), onConstraintUsageRuleAdder,
nativeCrac.getSchemeRemedialActions(), nativeCrac.getRemedialActionScheme(), nativeCrac.getStage(), nativeCrac.getGridStateAlterationCollection(), nativeCrac.getTopologyActionAuto(), nativeCrac.getRotatingMachineActionAuto(), nativeCrac.getShuntCompensatorModificationAuto(), nativeCrac.getTapPositionActionAuto());
Map<String, String> overridingData = nativeCrac.getOverridingCracData(offsetDateTime);
PropertyBags contingencies = CsaProfileCracUtils.overrideData(nativeCrac.getContingencies(), overridingData, CsaProfileConstants.OverridingObjectsFields.CONTINGENCY);
PropertyBags assessedElements = CsaProfileCracUtils.overrideData(nativeCrac.getAssessedElements(), overridingData, CsaProfileConstants.OverridingObjectsFields.ASSESSED_ELEMENT);
PropertyBags assessedElementsWithContingencies = CsaProfileCracUtils.overrideData(nativeCrac.getAssessedElementsWithContingencies(), overridingData, CsaProfileConstants.OverridingObjectsFields.ASSESSED_ELEMENT_WITH_CONTINGENCY);
PropertyBags currentLimits = CsaProfileCracUtils.overrideData(nativeCrac.getCurrentLimits(), overridingData, CsaProfileConstants.OverridingObjectsFields.CURRENT_LIMIT);
PropertyBags voltageLimits = CsaProfileCracUtils.overrideData(nativeCrac.getVoltageLimits(), overridingData, CsaProfileConstants.OverridingObjectsFields.VOLTAGE_LIMIT);
PropertyBags angleLimits = CsaProfileCracUtils.overrideData(nativeCrac.getAngleLimits(), overridingData, CsaProfileConstants.OverridingObjectsFields.VOLTAGE_ANGLE_LIMIT);
PropertyBags assessedElementsWithRemedialAction = CsaProfileCracUtils.overrideData(nativeCrac.getAssessedElementsWithRemedialAction(), overridingData, CsaProfileConstants.OverridingObjectsFields.ASSESSED_ELEMENT_WITH_REMEDIAL_ACTION);
PropertyBags contingenciesWithRemedialAction = CsaProfileCracUtils.overrideData(nativeCrac.getContingencyWithRemedialAction(), overridingData, CsaProfileConstants.OverridingObjectsFields.CONTINGENCY_WITH_REMEDIAL_ACTION);
PropertyBags remedialActions = CsaProfileCracUtils.overrideData(nativeCrac.getRemedialActions(), overridingData, CsaProfileConstants.OverridingObjectsFields.REMEDIAL_ACTION);
PropertyBags remedialActionSchemes = CsaProfileCracUtils.overrideData(nativeCrac.getRemedialActionScheme(), overridingData, CsaProfileConstants.OverridingObjectsFields.REMEDIAL_ACTION_SCHEME);
PropertyBags gridStateAlterations = CsaProfileCracUtils.overrideData(nativeCrac.getGridStateAlterationCollection(), overridingData, CsaProfileConstants.OverridingObjectsFields.GRID_STATE_ALTERATION);
PropertyBags staticPropertyRanges = CsaProfileCracUtils.overrideData(nativeCrac.getStaticPropertyRanges(), overridingData, CsaProfileConstants.OverridingObjectsFields.RANGE_CONSTRAINT);

createContingencies(contingencies, nativeCrac.getContingencyEquipments());
createCnecs(assessedElements, assessedElementsWithContingencies, currentLimits, voltageLimits, angleLimits, cracCreationParameters.getDefaultMonitoredSides());
OnConstraintUsageRuleHelper onConstraintUsageRuleAdder = new OnConstraintUsageRuleHelper(creationContext.getCnecCreationContexts(), assessedElements, assessedElementsWithRemedialAction);
createRemedialActions(remedialActions, nativeCrac.getTopologyAction(), nativeCrac.getRotatingMachineAction(), nativeCrac.getShuntCompensatorModifications(), nativeCrac.getTapPositionAction(), staticPropertyRanges, contingenciesWithRemedialAction, onConstraintUsageRuleAdder,
nativeCrac.getSchemeRemedialActions(), remedialActionSchemes, nativeCrac.getStage(), gridStateAlterations, nativeCrac.getTopologyActionAuto(), nativeCrac.getRotatingMachineActionAuto(), nativeCrac.getShuntCompensatorModificationAuto(),
nativeCrac.getTapPositionActionAuto());
creationContext.buildCreationReport();
return creationContext.creationSuccess(crac);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,13 +114,31 @@ public static void checkNormalEnabled(PropertyBag propertyBag, String remedialAc
}
}

public static String removePrefix(String mridWithPrefix) {
return mridWithPrefix.substring(mridWithPrefix.lastIndexOf("_") + 1);
public static boolean checkProfileValidityInterval(PropertyBag propertyBag, OffsetDateTime importTimestamp) {
String startTime = propertyBag.get(CsaProfileConstants.REQUEST_HEADER_START_DATE);
String endTime = propertyBag.get(CsaProfileConstants.REQUEST_HEADER_END_DATE);
return isValidInterval(importTimestamp, startTime, endTime);
}

public static boolean checkProfileKeyword(PropertyBag propertyBag, CsaProfileConstants.CsaProfileKeywords csaProfileKeyword) {
String keyword = propertyBag.get(CsaProfileConstants.REQUEST_HEADER_KEYWORD);
return csaProfileKeyword.toString().equals(keyword);
}

public static Set<String> addFileToSet(Map<String, Set<String>> map, String contextName, String keyword) {
Set<String> returnSet = map.getOrDefault(keyword, new HashSet<>());
returnSet.add(contextName);
return returnSet;
}

public static PropertyBags overrideData(PropertyBags propertyBags, Map<String, String> dataMap, CsaProfileConstants.OverridingObjectsFields overridingObjectsFields) {
for (PropertyBag propertyBag : propertyBags) {
String id = propertyBag.getId(overridingObjectsFields.getObjectName());
String data = dataMap.get(id);
if (data != null) {
propertyBag.put(overridingObjectsFields.getInitialFieldName(), data);
}
}
return propertyBags;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -488,11 +488,11 @@ private BiMap<PropertyBag, PropertyBag> associateTwoPropertyBags(Set<PropertyBag
Map<String, PropertyBag> propertyBags2ByIdMap = new HashMap<>();
BiMap<PropertyBag, PropertyBag> propertyBags1ToPropertyBags2BiMap = HashBiMap.create();

propertyBags2.forEach(propertyBag -> propertyBags2ByIdMap.put(CsaProfileCracUtils.removePrefix(propertyBag.get(key2)), propertyBag));
propertyBags2.forEach(propertyBag -> propertyBags2ByIdMap.put(propertyBag.getId(key2), propertyBag));

propertyBags1.forEach(propertyBag -> {
if (propertyBags2ByIdMap.containsKey(CsaProfileCracUtils.removePrefix(propertyBag.get(key1)))) {
PropertyBag testedPropertyBag = propertyBags2ByIdMap.get(CsaProfileCracUtils.removePrefix(propertyBag.get(key1)));
if (propertyBags2ByIdMap.containsKey(propertyBag.getId(key1))) {
PropertyBag testedPropertyBag = propertyBags2ByIdMap.get(propertyBag.getId(key1));
if (isAssociatedWithOnlyOneScheme(propertyBags1ToPropertyBags2BiMap, testedPropertyBag)) {
propertyBags1ToPropertyBags2BiMap.put(propertyBag, testedPropertyBag);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

import com.farao_community.farao.commons.FaraoException;
import com.farao_community.farao.data.crac_creation.creator.csa_profile.crac_creator.CsaProfileConstants;
import com.farao_community.farao.data.crac_creation.creator.csa_profile.crac_creator.CsaProfileCracUtils;
import com.farao_community.farao.data.crac_creation.creator.csa_profile.crac_creator.CsaProfileElementaryCreationContext;
import com.powsybl.triplestore.api.PropertyBag;
import com.powsybl.triplestore.api.PropertyBags;
Expand Down Expand Up @@ -36,17 +35,17 @@ public OnConstraintUsageRuleHelper(Set<CsaProfileElementaryCreationContext> csaP
private void readAssessedElementsCombinableWithRemedialActions(PropertyBags assessedElements) {
List<String> nativeIdsOfCnecsCombinableWithRas = assessedElements.stream()
.filter(element -> element.getId(CsaProfileConstants.REQUEST_ASSESSED_ELEMENT_IS_COMBINABLE_WITH_REMEDIAL_ACTION) != null && Boolean.parseBoolean(element.getId(CsaProfileConstants.REQUEST_ASSESSED_ELEMENT_IS_COMBINABLE_WITH_REMEDIAL_ACTION)))
.map(element -> CsaProfileCracUtils.removePrefix(element.get(CsaProfileConstants.REQUEST_ASSESSED_ELEMENT))).collect(Collectors.toList());
.map(element -> element.getId(CsaProfileConstants.REQUEST_ASSESSED_ELEMENT)).collect(Collectors.toList());
Map<String, Set<String>> nativeToFaraoCnecIdsCombinableWithRas = getImportedCnecsNativeIdsToFaraoIds();
nativeToFaraoCnecIdsCombinableWithRas.keySet().retainAll(nativeIdsOfCnecsCombinableWithRas);
nativeToFaraoCnecIdsCombinableWithRas.values().stream().flatMap(Set::stream).forEach(importedCnecsCombinableWithRas::add);
}

private void readAssessedElementsWithRemedialAction(PropertyBags assessedElementsWithRemedialAction) {
assessedElementsWithRemedialAction.stream().filter(this::checkNormalEnabled).filter(propertyBag -> getImportedCnecsNativeIdsToFaraoIds().containsKey(CsaProfileCracUtils.removePrefix(propertyBag.get(CsaProfileConstants.REQUEST_ASSESSED_ELEMENT)))).forEach(propertyBag -> {
assessedElementsWithRemedialAction.stream().filter(this::checkNormalEnabled).filter(propertyBag -> getImportedCnecsNativeIdsToFaraoIds().containsKey(propertyBag.getId(CsaProfileConstants.REQUEST_ASSESSED_ELEMENT))).forEach(propertyBag -> {
String combinationConstraintKind = propertyBag.get(CsaProfileConstants.COMBINATION_CONSTRAINT_KIND);
String remedialActionId = CsaProfileCracUtils.removePrefix(propertyBag.get(CsaProfileConstants.REQUEST_REMEDIAL_ACTION));
String assessedElementId = CsaProfileCracUtils.removePrefix(propertyBag.get(CsaProfileConstants.REQUEST_ASSESSED_ELEMENT));
String remedialActionId = propertyBag.getId(CsaProfileConstants.REQUEST_REMEDIAL_ACTION);
String assessedElementId = propertyBag.getId(CsaProfileConstants.REQUEST_ASSESSED_ELEMENT);

Set<String> faraoCnecIds = getImportedCnecsNativeIdsToFaraoIds().get(assessedElementId);

Expand Down
Loading

0 comments on commit dc780f3

Please sign in to comment.