-
Notifications
You must be signed in to change notification settings - Fork 7
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
Clean CSA constants #1127
base: main
Are you sure you want to change the base?
Clean CSA constants #1127
Conversation
Signed-off-by: Thomas Bouquet <[email protected]>
Signed-off-by: Thomas Bouquet <[email protected]>
Signed-off-by: Thomas Bouquet <[email protected]>
Signed-off-by: Thomas Bouquet <[email protected]>
Signed-off-by: Thomas Bouquet <[email protected]>
Signed-off-by: Thomas Bouquet <[email protected]>
Signed-off-by: Thomas Bouquet <[email protected]>
Signed-off-by: Thomas Bouquet <[email protected]>
Signed-off-by: Thomas Bouquet <[email protected]>
Signed-off-by: Thomas Bouquet <[email protected]>
…e-constants # Conflicts: # data/crac-io/crac-io-csa-profiles/src/main/java/com/powsybl/openrao/data/cracio/csaprofiles/craccreator/cnec/AbstractCnecCreator.java # data/crac-io/crac-io-csa-profiles/src/main/java/com/powsybl/openrao/data/cracio/csaprofiles/craccreator/cnec/FlowCnecCreator.java # data/crac-io/crac-io-csa-profiles/src/main/java/com/powsybl/openrao/data/cracio/csaprofiles/craccreator/constants/CsaProfileConstants.java # data/crac-io/crac-io-csa-profiles/src/main/java/com/powsybl/openrao/data/cracio/csaprofiles/nc/AssessedElement.java
Signed-off-by: Thomas Bouquet <[email protected]>
# Conflicts: # data/crac-io/crac-io-csa-profiles/src/main/java/com/powsybl/openrao/data/cracio/csaprofiles/craccreator/constants/CsaProfileConstants.java # data/crac-io/crac-io-csa-profiles/src/main/java/com/powsybl/openrao/data/cracio/csaprofiles/craccreator/remedialaction/CsaProfileRemedialActionsCreator.java # data/crac-io/crac-io-csa-profiles/src/main/java/com/powsybl/openrao/data/cracio/csaprofiles/nc/TapChanger.java # data/crac-io/crac-io-csa-profiles/src/main/java/com/powsybl/openrao/data/cracio/csaprofiles/nc/TapPositionAction.java
Signed-off-by: Thomas Bouquet <[email protected]>
Signed-off-by: Thomas Bouquet <[email protected]>
Signed-off-by: Thomas Bouquet <[email protected]>
Signed-off-by: Thomas Bouquet <[email protected]>
Signed-off-by: Thomas Bouquet <[email protected]>
Signed-off-by: Thomas Bouquet <[email protected]>
Signed-off-by: Thomas Bouquet <[email protected]>
@@ -100,24 +100,16 @@ private void addCnec(AssessedElement nativeAssessedElement, Set<AssessedElementW | |||
} | |||
|
|||
// We check whether the AssessedElement is defined using an OperationalLimit | |||
LimitType limitType = getLimit(nativeAssessedElement); | |||
Class<? extends Cnec<?>> limitType = getCnecType(nativeAssessedElement); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Class<? extends Cnec<?>> limitType = getCnecType(nativeAssessedElement); | |
if (nativeVoltageLimitPerId.get(nativeAssessedElement.operationalLimit()) != null) { | |
new VoltageCnecCreator(crac, network, nativeAssessedElement, nativeVoltageLimitPerId.get(nativeAssessedElement.operationalLimit()), new HashSet<>(combinableContingencies), csaProfileCnecCreationContexts, rejectedLinksAssessedElementContingency, cracCreationParameters, borderPerTso, borderPerEic).addVoltageCnecs(); | |
} else if (nativeVoltageAngleLimitPerId.get(nativeAssessedElement.operationalLimit()) != null) { | |
new AngleCnecCreator(crac, network, nativeAssessedElement, nativeVoltageAngleLimitPerId.get(nativeAssessedElement.operationalLimit()), new HashSet<>(combinableContingencies), csaProfileCnecCreationContexts, rejectedLinksAssessedElementContingency, cracCreationParameters, borderPerTso, borderPerEic).addAngleCnecs(); | |
} else { | |
// AssessedElement defined with a CurrentLimit or a conducting equipment | |
new FlowCnecCreator(crac, network, nativeAssessedElement, nativeCurrentLimitPerId.get(nativeAssessedElement.operationalLimit()), new HashSet<>(combinableContingencies), csaProfileCnecCreationContexts, rejectedLinksAssessedElementContingency, cracCreationParameters, borderPerTso, borderPerEic).addFlowCnecs(); | |
} |
@@ -142,10 +141,10 @@ private String getAssociatedRemedialActionScheme(String remedialActionId) { | |||
} | |||
|
|||
RemedialActionScheme nativeRemedialActionScheme = linkedRemedialActionSchemePropertyBags.get(0); | |||
if (!CsaProfileConstants.SIPS.equals(nativeRemedialActionScheme.kind())) { | |||
if (!"RemedialActionSchemeKind.sips".equals(nativeRemedialActionScheme.kind())) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
for better readability and maintainability -> to the top of class as a constant
if (queriedNativeObjects.containsKey(nativeType)) { | ||
return (Set<T>) queriedNativeObjects.get(nativeType); | ||
} | ||
Query query = Arrays.stream(Query.values()).filter(q -> nativeType.equals(q.getNativeClass())).findFirst().orElseThrow(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe you can put this in a static method of query (that would return an optional)
private static boolean checkTimeCoherence(PropertyBag header, OffsetDateTime offsetDateTime) { | ||
String startTime = header.getId(CsaProfileConstants.REQUEST_HEADER_START_DATE); | ||
String endTime = header.getId(CsaProfileConstants.REQUEST_HEADER_END_DATE); | ||
String startTime = header.getId(CsaProfileConstants.START_DATE); | ||
String endTime = header.getId(CsaProfileConstants.END_DATE); | ||
return CsaProfileCracUtils.isValidInterval(offsetDateTime, startTime, endTime); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you not replace this method with CsaProfileCracUtils.checkProfileValidityInterval directly?
return propertyBag.getId(propertyBagName); | ||
} | ||
|
||
public static <T extends NCObject> T fromPropertyBag(PropertyBag propertyBag, Class<T> nativeClass, Map<String, Object> defaultValues) throws IllegalArgumentException, OpenRaoException, NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a way to reduce the number of possible thrown exceptions?
} | ||
|
||
private static String getMrid(PropertyBag propertyBag, Class<?> nativeClass) { | ||
String propertyBagName = Character.toLowerCase(nativeClass.getSimpleName().charAt(0)) + nativeClass.getSimpleName().substring(1); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can you not simply do getSimpleName().toLowerCase() or do you need any other upper case to stay upper case?
private final Class<? extends NCObject> nativeClass; | ||
private final Map<String, Object> defaultValues; | ||
|
||
Query(Class<? extends NCObject> nativeClass, CsaProfileKeyword targetProfilesKeyword, OverridableAttribute overridableAttribute, Map<String, Object> defaultValues) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is there only one OverridableAttribute? Also can we not always set a default vaule for each overridable attribute?
Depending on the answers maybe we can remove the third argument and know the all keys in the map are the overridableAttributes.
public static <T extends NCObject> T fromPropertyBag(PropertyBag propertyBag, Class<T> nativeClass, Map<String, Object> defaultValues) throws IllegalArgumentException, OpenRaoException, NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException { | ||
// Ensure the class is a record | ||
if (!nativeClass.isRecord()) { | ||
throw new IllegalArgumentException("Provided class is not a record"); | ||
} | ||
|
||
// Get the record's components (fields) | ||
RecordComponent[] components = nativeClass.getRecordComponents(); | ||
|
||
// Prepare an array to hold the values for the record's constructor | ||
Object[] constructorArgs = new Object[components.length]; | ||
|
||
// Loop through the components and extract values from the property bag | ||
for (int i = 0; i < components.length; i++) { | ||
RecordComponent component = components[i]; | ||
if ("mrid".equals(component.getName())) { | ||
constructorArgs[i] = getMrid(propertyBag, nativeClass); | ||
} else { | ||
Object parsedValue = parseStringValue(propertyBag.getId(component.getName()), component.getType()); | ||
if (parsedValue == null) { | ||
parsedValue = defaultValues.get(component.getName()); | ||
} | ||
constructorArgs[i] = parsedValue; | ||
} | ||
} | ||
|
||
// Find the canonical constructor of the record class | ||
Constructor<T> constructor = nativeClass.getDeclaredConstructor( | ||
// Get the types of the components (this matches the constructor signature) | ||
Arrays.stream(components) | ||
.map(RecordComponent::getType) | ||
.toArray(Class<?>[]::new) | ||
); | ||
|
||
// Create and return a new instance of the record | ||
return constructor.newInstance(constructorArgs); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This class is very complex and difficult to maintain. It also requires the name of the fields of the records to match perfectly the ones in the xsd. It also changes all the booleans into Booleans which makes the rest of the code a bit less nice (I guess this can be solved by changing the fields in the records to booleans?).
public static final String START_DATE = "startDate"; | ||
public static final String END_DATE = "endDate"; | ||
public static final String KEYWORD = "keyword"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Either rename the class or find somewhere to store the remaining constants, now that most constants are in other classes
throw new OpenRaoImportException(ImportStatus.NOT_FOR_RAO, String.format("Remedial action %s will not be imported because normalAvailable is set to false", nativeRemedialAction.mrid())); | ||
} | ||
String elementaryActionsAggregatorId = isSchemeRemedialAction ? elementaryActionsHelper.getGridStateAlterationCollection(nativeRemedialAction.mrid()) : nativeRemedialAction.mrid(); // collectionIdIfAutoOrElseRemedialActionId | ||
RemedialActionType remedialActionType = getRemedialActionType(nativeRemedialAction.mrid(), elementaryActionsAggregatorId, isSchemeRemedialAction); | ||
RemedialActionAdder<?> remedialActionAdder; | ||
if (remedialActionType.equals(RemedialActionType.NETWORK_ACTION)) { | ||
remedialActionAdder = networkActionCreator.getNetworkActionAdder(elementaryActionsHelper.getTopologyActions(isSchemeRemedialAction), elementaryActionsHelper.getRotatingMachineActions(isSchemeRemedialAction), elementaryActionsHelper.getShuntCompensatorModifications(isSchemeRemedialAction), elementaryActionsHelper.getNativeStaticPropertyRangesPerNativeGridStateAlteration(), nativeRemedialAction.mrid(), elementaryActionsAggregatorId, alterations); | ||
remedialActionAdder = networkActionCreator.getNetworkActionAdder(elementaryActionsHelper.getTopologyActions(isSchemeRemedialAction).getOrDefault(elementaryActionsAggregatorId, Set.of()), elementaryActionsHelper.getRotatingMachineActions(isSchemeRemedialAction).getOrDefault(elementaryActionsAggregatorId, Set.of()), elementaryActionsHelper.getShuntCompensatorModifications(isSchemeRemedialAction).getOrDefault(elementaryActionsAggregatorId, Set.of()), elementaryActionsHelper.getNativeStaticPropertyRangesPerNativeGridStateAlteration(), nativeRemedialAction.mrid(), alterations); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe it's worth adding an argument in the elementaryActoinsHelper methods to pass the elementaryActionsAggregatorId
...sybl/openrao/data/cracio/csaprofiles/craccreator/remedialaction/ElementaryActionsHelper.java
Outdated
Show resolved
Hide resolved
...ofiles/src/main/java/com/powsybl/openrao/data/cracio/csaprofiles/nc/GridStateAlteration.java
Outdated
Show resolved
Hide resolved
# Conflicts: # data/crac/crac-io/crac-io-csa-profiles/src/main/java/com/powsybl/openrao/data/cracio/csaprofiles/craccreator/CsaProfileCracCreator.java # data/crac/crac-io/crac-io-csa-profiles/src/main/java/com/powsybl/openrao/data/cracio/csaprofiles/craccreator/cnec/FlowCnecInstantHelper.java # data/crac/crac-io/crac-io-csa-profiles/src/main/java/com/powsybl/openrao/data/cracio/csaprofiles/craccreator/remedialaction/OnConstraintUsageRuleHelper.java # data/crac/crac-io/crac-io-csa-profiles/src/main/java/com/powsybl/openrao/data/cracio/csaprofiles/nc/AssessedElement.java
Signed-off-by: Thomas Bouquet <[email protected]>
Signed-off-by: Thomas Bouquet <[email protected]>
Please check if the PR fulfills these requirements
Does this PR already have an issue describing the problem?
What kind of change does this PR introduce?
Cleaner way to import CSA profiles with a generic NC parser
What is the current behavior?
What is the new behavior (if this is a feature change)?
Does this PR introduce a breaking change or deprecate an API?
If yes, please check if the following requirements are fulfilled
What changes might users need to make in their application due to this PR? (migration steps)
Other information: