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

NTCAnnual values as backup when periods are missing from NTCRed file #217

Merged
merged 6 commits into from
Jan 29, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,7 @@
import com.farao_community.farao.cse.data.xsd.*;

import java.time.OffsetDateTime;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.*;
import java.util.function.Predicate;
import java.util.stream.Collectors;

Expand All @@ -27,7 +24,7 @@ public DailyNtcDocument(OffsetDateTime targetDateTime, NTCReductionsDocument ntc
this.ntcReductionsDocument = ntcReductionsDocument;
}

Map<String, LineInformation> getLineInformationPerLineId(Predicate<TLine> lineSelector) {
Map<String, Optional<LineInformation>> getLineInformationPerLineId(Predicate<TLine> lineSelector) {
Copy link

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Consider simplifying the design by avoiding Optional wrapping.

As previously suggested by @vbochetRTE, wrapping every value in Optional might be overcomplicating the design. Instead, consider only including lines that have actual data in the map.

Here's a simpler approach:

-    Map<String, Optional<LineInformation>> getLineInformationPerLineId(Predicate<TLine> lineSelector) {
+    Map<String, LineInformation> getLineInformationPerLineId(Predicate<TLine> lineSelector) {
         List<TSpecialLines> tSpecialLines = ntcReductionsDocument.getSpecialLines();
         if (tSpecialLines.isEmpty()) {
             return Collections.emptyMap();
         }
         if (tSpecialLines.size() == 1) {
             return tSpecialLines.get(0).getLine().stream()
                 .filter(lineSelector)
+                .filter(tLine -> NtcUtil.getTNtcFromLine(targetDateTime, tLine).isPresent())
                 .collect(Collectors.toMap(
                     TLine::getCode,
-                    tLine -> {
-                        Optional<TNTC> optionalTntc = NtcUtil.getTNtcFromLine(targetDateTime, tLine);
-                        if (optionalTntc.isPresent()) {
-                            return Optional.of(new LineInformation(tLine.getCNtc().value(), optionalTntc.get().getType(), optionalTntc.get().getV().doubleValue()));
-                        } else {
-                            return Optional.empty();
-                        }
-                    }
+                    tLine -> {
+                        TNTC tntc = NtcUtil.getTNtcFromLine(targetDateTime, tLine).get();
+                        return new LineInformation(tLine.getCNtc().value(), tntc.getType(), tntc.getV().doubleValue());
+                    }
                 ));
         }
         throw new CseDataException("Several special lines sections have been defined");
     }

Benefits of this approach:

  1. Simpler and more maintainable code
  2. Better performance by avoiding Optional overhead
  3. Clearer contract: if a line is in the map, it has valid data
  4. Callers don't need to handle Optional unwrapping

Also applies to: 42-47

List<TSpecialLines> tSpecialLines = ntcReductionsDocument.getSpecialLines();
if (tSpecialLines.isEmpty()) {
return Collections.emptyMap();
Expand All @@ -38,8 +35,12 @@ Map<String, LineInformation> getLineInformationPerLineId(Predicate<TLine> lineSe
.collect(Collectors.toMap(
TLine::getCode,
tLine -> {
TNTC tNtc = NtcUtil.getTNtcFromLine(targetDateTime, tLine);
return new LineInformation(tLine.getCNtc().value(), tNtc.getType(), tNtc.getV().doubleValue());
Optional<TNTC> optionalTntc = NtcUtil.getTNtcFromLineFromNtcRedFile(targetDateTime, tLine);
if (optionalTntc.isPresent()) {
return Optional.of(new LineInformation(tLine.getCNtc().value(), optionalTntc.get().getType(), optionalTntc.get().getV().doubleValue()));
} else {
return Optional.empty();
Copy link
Contributor

Choose a reason for hiding this comment

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

Is it necessary to put all line-codes in the Map, even if they are not associated with any data?
Putting in it only the elements that are associated with a real value would make the code lighter, as you wouldn't have to deal with Optionals everywhere.

}
}
));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,7 @@
import com.farao_community.farao.cse.data.xsd.ntc_adapted.*;

import java.time.OffsetDateTime;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.*;
import java.util.function.Predicate;
import java.util.stream.Collectors;

Expand All @@ -27,7 +24,7 @@ public DailyNtcDocumentAdapted(OffsetDateTime targetDateTime, NTCReductionsDocum
this.ntcReductionsDocument = ntcReductionsDocument;
}

Map<String, LineInformation> getLineInformationPerLineId(Predicate<TLine> lineSelector) {
Map<String, Optional<LineInformation>> getLineInformationPerLineId(Predicate<TLine> lineSelector) {
TSpecialLines tSpecialLines = ntcReductionsDocument.getSpecialLinesImport();
if (tSpecialLines == null || tSpecialLines.getLine() == null || tSpecialLines.getLine().isEmpty()) {
return Collections.emptyMap();
Expand All @@ -37,8 +34,12 @@ Map<String, LineInformation> getLineInformationPerLineId(Predicate<TLine> lineSe
.collect(Collectors.toMap(
TLine::getCode,
tLine -> {
TNTC tNtc = NtcUtilAdapted.getTNtcFromLine(targetDateTime, tLine);
return new LineInformation(tLine.getCNtc().value(), tNtc.getType(), tNtc.getV().doubleValue());
Optional<TNTC> optionalTntc = NtcUtilAdapted.getTNtcFromLineFromNtcRedFile(targetDateTime, tLine);
if (optionalTntc.isPresent()) {
return Optional.of(new LineInformation(tLine.getCNtc().value(), optionalTntc.get().getType(), optionalTntc.get().getV().doubleValue()));
} else {
return Optional.empty();
Copy link
Contributor

Choose a reason for hiding this comment

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

}
kahyami marked this conversation as resolved.
Show resolved Hide resolved
}
));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
package com.farao_community.farao.cse.data.ntc;

import com.farao_community.farao.cse.data.xsd.TLine;
import com.farao_community.farao.cse.runner.api.exception.CseInternalException;

import java.util.HashMap;
import java.util.Map;
Expand Down Expand Up @@ -63,12 +64,12 @@ public Map<String, Double> getFlowOnFixedFlowLines() {
if (isImportEcProcess) {
Predicate<com.farao_community.farao.cse.data.xsd.ntc_adapted.TLine> fixedFlowLines = tLine -> tLine.isFixedFlow() && tLine.isModelized();
Map<String, LineInformation> yearlyLineInformationPerLineId = yearlyNtcDocumentAdapted.getLineInformationPerLineId(fixedFlowLines);
Map<String, LineInformation> dailyLineInformationPerLineId = dailyNtcDocumentAdapted != null ? dailyNtcDocumentAdapted.getLineInformationPerLineId(fixedFlowLines) : Map.of();
Map<String, Optional<LineInformation>> dailyLineInformationPerLineId = dailyNtcDocumentAdapted != null ? dailyNtcDocumentAdapted.getLineInformationPerLineId(fixedFlowLines) : Map.of();
return getFlowPerLineId(yearlyLineInformationPerLineId, dailyLineInformationPerLineId);
} else {
Predicate<TLine> fixedFlowLines = tLine -> tLine.isFixedFlow() && tLine.isModelized();
Map<String, LineInformation> yearlyLineInformationPerLineId = yearlyNtcDocument.getLineInformationPerLineId(fixedFlowLines);
Map<String, LineInformation> dailyLineInformationPerLineId = dailyNtcDocument != null ? dailyNtcDocument.getLineInformationPerLineId(fixedFlowLines) : Map.of();
Map<String, Optional<LineInformation>> dailyLineInformationPerLineId = dailyNtcDocument != null ? dailyNtcDocument.getLineInformationPerLineId(fixedFlowLines) : Map.of();
return getFlowPerLineId(yearlyLineInformationPerLineId, dailyLineInformationPerLineId);
}
}
Expand All @@ -81,14 +82,14 @@ public Map<String, Double> getFlowPerCountryOnNotModelizedLines() {

Map<String, Double> getFlowPerCountry(Predicate<TLine> lineSelector) {
Map<String, LineInformation> yearlyLineInformationPerLineId = yearlyNtcDocument.getLineInformationPerLineId(lineSelector);
Map<String, LineInformation> dailyLineInformationPerLineId = dailyNtcDocument != null ? dailyNtcDocument.getLineInformationPerLineId(lineSelector) : Map.of();
Map<String, Optional<LineInformation>> dailyLineInformationPerLineId = dailyNtcDocument != null ? dailyNtcDocument.getLineInformationPerLineId(lineSelector) : Map.of();
Map<String, Double> flowPerLineId = getFlowPerLineId(yearlyLineInformationPerLineId, dailyLineInformationPerLineId);

Map<String, Double> flowPerCountry = new HashMap<>();
flowPerLineId.forEach((lineId, flow) -> {
String country = Optional.ofNullable(yearlyLineInformationPerLineId.get(lineId))
.map(LineInformation::getCountry)
.orElseGet(() -> dailyLineInformationPerLineId.get(lineId).getCountry());
.orElseGet(() -> getCountryFromDailyLineInformation(dailyLineInformationPerLineId, lineId));
double initialFlow = Optional.ofNullable(flowPerCountry.get(country)).orElse(0.);
flowPerCountry.put(country, initialFlow + flow);
});
Expand All @@ -97,20 +98,25 @@ Map<String, Double> getFlowPerCountry(Predicate<TLine> lineSelector) {

Map<String, Double> getFlowPerCountryAdapted(Predicate<com.farao_community.farao.cse.data.xsd.ntc_adapted.TLine> lineSelector) {
Map<String, LineInformation> yearlyLineInformationPerLineId = yearlyNtcDocumentAdapted.getLineInformationPerLineId(lineSelector);
Map<String, LineInformation> dailyLineInformationPerLineId = dailyNtcDocumentAdapted != null ? dailyNtcDocumentAdapted.getLineInformationPerLineId(lineSelector) : Map.of();
Map<String, Optional<LineInformation>> dailyLineInformationPerLineId = dailyNtcDocumentAdapted != null ? dailyNtcDocumentAdapted.getLineInformationPerLineId(lineSelector) : Map.of();
Map<String, Double> flowPerLineId = getFlowPerLineId(yearlyLineInformationPerLineId, dailyLineInformationPerLineId);

Map<String, Double> flowPerCountry = new HashMap<>();
flowPerLineId.forEach((lineId, flow) -> {
String country = Optional.ofNullable(yearlyLineInformationPerLineId.get(lineId))
.map(LineInformation::getCountry)
.orElseGet(() -> dailyLineInformationPerLineId.get(lineId).getCountry());
.orElseGet(() -> getCountryFromDailyLineInformation(dailyLineInformationPerLineId, lineId));
double initialFlow = Optional.ofNullable(flowPerCountry.get(country)).orElse(0.);
flowPerCountry.put(country, initialFlow + flow);
});
return flowPerCountry;
}

private String getCountryFromDailyLineInformation(Map<String, Optional<LineInformation>> dailyLineInformationPerLineId, String lineId) {
return dailyLineInformationPerLineId.get(lineId).map(LineInformation::getCountry)
.orElseThrow(() -> new CseInternalException(String.format("No information available for line %s", lineId)));
}

public Map<String, Double> getNtcPerCountry() {
return isImportEcProcess ? getNtcPerCountryAdapted() : getNtcPerCountryNotAdapted();
}
Expand Down Expand Up @@ -151,20 +157,22 @@ private Map<String, Double> getNtcPerCountryAdapted() {
return ntcPerCountry;
}

private static Map<String, Double> getFlowPerLineId(Map<String, LineInformation> yearlyLinePerId, Map<String, LineInformation> dailyLinePerId) {
private static Map<String, Double> getFlowPerLineId(Map<String, LineInformation> yearlyLinePerId, Map<String, Optional<LineInformation>> dailyLinePerId) {
Map<String, Double> flowPerLine = yearlyLinePerId.entrySet().stream()
.collect(Collectors.toMap(
Map.Entry::getKey,
entry -> entry.getValue().getFlow()
Map.Entry::getKey,
entry -> entry.getValue().getFlow()
));
dailyLinePerId.forEach((lineId, lineInformation) -> {
if (lineInformation.getVariationType().equalsIgnoreCase(NtcUtil.ABSOLUTE)) {
flowPerLine.put(lineId, lineInformation.getFlow());
} else {
double initialFlow = Optional.ofNullable(flowPerLine.get(lineId)).orElse(0.);
flowPerLine.put(lineId, initialFlow + lineInformation.getFlow());
}
});
dailyLinePerId.forEach((lineId, lineInformation) ->
lineInformation.ifPresent(line -> {
if (line.getVariationType().equalsIgnoreCase(NtcUtil.ABSOLUTE)) {
flowPerLine.put(lineId, lineInformation.get().getFlow());
kahyami marked this conversation as resolved.
Show resolved Hide resolved
} else {
double initialFlow = Optional.ofNullable(flowPerLine.get(lineId)).orElse(0.);
flowPerLine.put(lineId, initialFlow + lineInformation.get().getFlow());
}
})
);
return flowPerLine;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,12 @@ private NtcUtil() {
// Should not be instantiated
}

static TNTC getTNtcFromLine(OffsetDateTime targetDateTime, TLine tLine) {
static Optional<TNTC> getTNtcFromLineFromNtcRedFile(OffsetDateTime targetDateTime, TLine tLine) {
return getTNtcFromPeriods(targetDateTime, tLine.getPeriod()).stream()
.collect(toOptional());
}

static TNTC getTNtcFromLineFromYearlyNtc(OffsetDateTime targetDateTime, TLine tLine) {
vbochetRTE marked this conversation as resolved.
Show resolved Hide resolved
return getTNtcFromPeriods(targetDateTime, tLine.getPeriod()).stream()
.collect(toOptional())
.orElseThrow(() -> new CseDataException(String.format("No NTC definition for line %s", tLine.getCode())));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,13 @@ private NtcUtilAdapted() {
// Should not be instantiated
}

static TNTC getTNtcFromLine(OffsetDateTime targetDateTime, TLine tLine) {
static Optional<TNTC> getTNtcFromLineFromNtcRedFile(OffsetDateTime targetDateTime, TLine tLine) {
return getTNtcFromPeriods(targetDateTime, tLine.getPeriod())
.stream()
.collect(toOptional());
}

static TNTC getTNtcFromLineFromYearlyNtc(OffsetDateTime targetDateTime, TLine tLine) {
vbochetRTE marked this conversation as resolved.
Show resolved Hide resolved
return getTNtcFromPeriods(targetDateTime, tLine.getPeriod()).stream()
.collect(toOptional())
.orElseThrow(() -> new CseDataException(String.format("No NTC definition for line %s", tLine.getCode())));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ Map<String, LineInformation> getLineInformationPerLineId(Predicate<TLine> lineSe
.collect(Collectors.toMap(
TLine::getCode,
tLine -> {
TNTC tNtc = NtcUtil.getTNtcFromLine(targetDateTime, tLine);
TNTC tNtc = NtcUtil.getTNtcFromLineFromYearlyNtc(targetDateTime, tLine);
if (tNtc.getType().equalsIgnoreCase(NtcUtil.ABSOLUTE)) {
return new LineInformation(tLine.getCNtc().value(), tNtc.getType(), tNtc.getV().doubleValue());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ Map<String, LineInformation> getLineInformationPerLineId(Predicate<TLine> lineSe
.collect(Collectors.toMap(
TLine::getCode,
tLine -> {
TNTC tNtc = NtcUtilAdapted.getTNtcFromLine(targetDateTime, tLine);
TNTC tNtc = NtcUtilAdapted.getTNtcFromLineFromYearlyNtc(targetDateTime, tLine);
if (tNtc.getType() == null || tNtc.getType().equalsIgnoreCase(NtcUtil.ABSOLUTE)) {
return new LineInformation(tLine.getCNtc().value(), NtcUtil.ABSOLUTE, tNtc.getV().doubleValue());
}
Expand Down
Loading