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

Clean CSA constants #1127

Draft
wants to merge 29 commits into
base: main
Choose a base branch
from
Draft

Clean CSA constants #1127

wants to merge 29 commits into from

Conversation

bqth29
Copy link
Collaborator

@bqth29 bqth29 commented Sep 9, 2024

Please check if the PR fulfills these requirements

  • The commit message follows our guidelines
  • Tests for the changes have been added (for bug fixes / features)
  • Docs have been added / updated (for bug fixes / features)

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?

  • Yes
  • No

If yes, please check if the following requirements are fulfilled

  • The Breaking Change or Deprecated label has been added
  • The migration steps are described in the following section

What changes might users need to make in their application due to this PR? (migration steps)

Other information:

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]>
@bqth29 bqth29 added the cleaning This issue or pull request only concerns improving the overall state of the code label Sep 9, 2024
@bqth29 bqth29 marked this pull request as ready for review September 9, 2024 14:07
…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]>
@bqth29 bqth29 requested review from benrejebmoh and jipea September 9, 2024 14:49
@bqth29 bqth29 added the swe-csa This issue or pull request addresses a feature or a bug relative to the SWE CSA process label Sep 11, 2024
@bqth29 bqth29 changed the base branch from experiment/csa-tap-changers to main September 23, 2024 07:42
bqth29 and others added 9 commits September 23, 2024 09:46
# 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]>
@bqth29 bqth29 added the PR: waiting-for-review This PR is waiting to be reviewed label Oct 14, 2024
@@ -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);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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())) {
Copy link
Collaborator

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();
Copy link
Collaborator

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)

Comment on lines 195 to 199
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);
}
Copy link
Collaborator

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 {
Copy link
Collaborator

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);
Copy link
Collaborator

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) {
Copy link
Collaborator

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.

Comment on lines +55 to +91
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);
}
Copy link
Collaborator

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?).

Comment on lines +23 to +25
public static final String START_DATE = "startDate";
public static final String END_DATE = "endDate";
public static final String KEYWORD = "keyword";
Copy link
Collaborator

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);
Copy link
Collaborator

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

@phiedw phiedw added PR: waiting-for-correction This PR is waiting to be corrected by its author and removed PR: waiting-for-review This PR is waiting to be reviewed labels Nov 6, 2024
bqth29 and others added 4 commits November 7, 2024 09:26
# 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]>
@bqth29 bqth29 marked this pull request as draft December 20, 2024 13:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cleaning This issue or pull request only concerns improving the overall state of the code PR: waiting-for-correction This PR is waiting to be corrected by its author swe-csa This issue or pull request addresses a feature or a bug relative to the SWE CSA process
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants