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

Quality: code cleaning #2975

Merged
merged 20 commits into from
Aug 5, 2024
Merged
Show file tree
Hide file tree
Changes from 17 commits
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 @@ -15,7 +15,6 @@
import com.powsybl.security.results.PostContingencyResult;

import java.util.*;
import java.util.stream.Collectors;

/**
* @author Geoffroy Jamgotchian {@literal <geoffroy.jamgotchian at rte-france.com>}
Expand Down Expand Up @@ -90,7 +89,7 @@ public void afterAction(RunningContext runningContext, String actionId) {
@Override
public void afterPostContingencyAnalysis() {
onFinalStateResult(new SecurityAnalysisResult(preContingencyResult, preContingencyStatus,
postContingencyResults.entrySet().stream().map(Map.Entry::getValue).collect(Collectors.toList())));
new ArrayList<>(postContingencyResults.values())));
}

public abstract void onFinalStateResult(SecurityAnalysisResult result);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@
import java.io.OutputStreamWriter;
import java.io.PrintStream;
import java.io.Writer;
import java.nio.charset.StandardCharsets;
import java.nio.file.FileSystem;
import java.nio.file.Files;
import java.nio.file.Path;
Expand Down Expand Up @@ -283,7 +282,7 @@ public void run(CommandLine line, ToolRunningContext context) throws Exception {
ParallelLoadFlowActionSimulator actionSimulator = new ParallelLoadFlowActionSimulator(network,
context.getLongTimeExecutionComputationManager(), taskCount, config, applyIfSolved, resultHandlers);

String dsl = new String(Files.readAllBytes(dslFile), StandardCharsets.UTF_8);
String dsl = Files.readString(dslFile);
actionSimulator.run(dsl, contingencies);
} else {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -202,18 +202,11 @@ private void writeSubstations() throws IOException {
}

private boolean isOnlyMainCc() {
switch (config.getExportScope()) {
case ONLY_MAIN_CC:
case ONLY_MAIN_CC_AND_CONNECTABLE_GENERATORS_AND_SHUNTS:
case ONLY_MAIN_CC_AND_CONNECTABLE_GENERATORS_AND_SHUNTS_AND_ALL_LOADS:
return true;

case ALL:
return false;

default:
throw new IllegalStateException();
}
return switch (config.getExportScope()) {
case ONLY_MAIN_CC, ONLY_MAIN_CC_AND_CONNECTABLE_GENERATORS_AND_SHUNTS, ONLY_MAIN_CC_AND_CONNECTABLE_GENERATORS_AND_SHUNTS_AND_ALL_LOADS ->
true;
case ALL -> false;
};
}

private boolean connectedComponentToExport(int numCC) {
Expand Down Expand Up @@ -335,43 +328,38 @@ private void writeLines(AmplExportContext context, TableFormatter formatter) thr
for (Line l : getSortedIdentifiables(network.getLineStream())) {
Terminal t1 = l.getTerminal1();
Terminal t2 = l.getTerminal2();
Bus bus1 = AmplUtil.getBus(t1);
Bus bus2 = AmplUtil.getBus(t2);
if (bus1 != null && bus2 != null && bus1 == bus2) {
LOGGER.warn("Skipping line '{}' connected to the same bus at both sides", l.getId());
continue;
}
String bus1Id = AmplUtil.getBusId(bus1);
String bus2Id = AmplUtil.getBusId(bus2);
if (isOnlyMainCc() && !(isBusExported(context, bus1Id) || isBusExported(context, bus2Id))) {
continue;
if (addVoltageLevelIdsToExport(context, t1, t2, l.getId())) {
columnsExporter.writeLinesToFormatter(formatter, l);
addExtensions(mapper.getInt(AmplSubset.BRANCH, l.getId()), l);
}
context.voltageLevelIdsToExport.add(t1.getVoltageLevel().getId());
context.voltageLevelIdsToExport.add(t2.getVoltageLevel().getId());
columnsExporter.writeLinesToFormatter(formatter, l);
addExtensions(mapper.getInt(AmplSubset.BRANCH, l.getId()), l);
}
}

private boolean addVoltageLevelIdsToExport(AmplExportContext context, Terminal t1, Terminal t2, String id) {
Bus bus1 = AmplUtil.getBus(t1);
Bus bus2 = AmplUtil.getBus(t2);
if (bus2 != null && bus1 == bus2) {
LOGGER.warn("Skipping line '{}' connected to the same bus at both sides", id);
return false;
}
String bus1Id = AmplUtil.getBusId(bus1);
String bus2Id = AmplUtil.getBusId(bus2);
if (isOnlyMainCc() && !(isBusExported(context, bus1Id) || isBusExported(context, bus2Id))) {
return false;
}
context.voltageLevelIdsToExport.add(t1.getVoltageLevel().getId());
context.voltageLevelIdsToExport.add(t2.getVoltageLevel().getId());
return true;
}

private void writeTieLines(AmplExportContext context, TableFormatter formatter) throws IOException {
for (TieLine l : getSortedIdentifiables(network.getTieLineStream())) {
Terminal t1 = l.getDanglingLine1().getTerminal();
Terminal t2 = l.getDanglingLine2().getTerminal();
Bus bus1 = AmplUtil.getBus(t1);
Bus bus2 = AmplUtil.getBus(t2);
if (bus1 != null && bus2 != null && bus1 == bus2) {
LOGGER.warn("Skipping line '{}' connected to the same bus at both sides", l.getId());
continue;
}
String bus1Id = AmplUtil.getBusId(bus1);
String bus2Id = AmplUtil.getBusId(bus2);
if (isOnlyMainCc() && !(isBusExported(context, bus1Id) || isBusExported(context, bus2Id))) {
continue;
if (addVoltageLevelIdsToExport(context, t1, t2, l.getId())) {
columnsExporter.writeTieLineToFormatter(formatter, l);
addExtensions(mapper.getInt(AmplSubset.BRANCH, l.getId()), l);
}
context.voltageLevelIdsToExport.add(t1.getVoltageLevel().getId());
context.voltageLevelIdsToExport.add(t2.getVoltageLevel().getId());
columnsExporter.writeTieLineToFormatter(formatter, l);
addExtensions(mapper.getInt(AmplSubset.BRANCH, l.getId()), l);
}
EtienneLt marked this conversation as resolved.
Show resolved Hide resolved
}

Expand All @@ -383,16 +371,12 @@ private void writeTwoWindingsTransformers(AmplExportContext context, TableFormat
Bus bus2 = AmplUtil.getBus(t2);
if (bus1 != null && bus1 == bus2) {
LOGGER.warn("Skipping transformer '{}' connected to the same bus at both sides", twt.getId());
continue;
}
if (isOnlyMainCc() && !(isBusExported(context, AmplUtil.getBusId(bus1)) || isBusExported(context,
AmplUtil.getBusId(bus2)))) {
continue;
} else if (!isOnlyMainCc() || isBusExported(context, AmplUtil.getBusId(bus1)) || isBusExported(context, AmplUtil.getBusId(bus2))) {
context.voltageLevelIdsToExport.add(t1.getVoltageLevel().getId());
context.voltageLevelIdsToExport.add(t2.getVoltageLevel().getId());
columnsExporter.writeTwoWindingsTranformerToFormatter(formatter, twt);
addExtensions(mapper.getInt(AmplSubset.BRANCH, twt.getId()), twt);
}
context.voltageLevelIdsToExport.add(t1.getVoltageLevel().getId());
context.voltageLevelIdsToExport.add(t2.getVoltageLevel().getId());
columnsExporter.writeTwoWindingsTranformerToFormatter(formatter, twt);
addExtensions(mapper.getInt(AmplSubset.BRANCH, twt.getId()), twt);
}
}

Expand Down Expand Up @@ -428,17 +412,17 @@ private void writeThreeWindingsTransformers(AmplExportContext context, TableForm

if (!isOnlyMainCc() || isBusExported(context, middleBusId) || isBusExported(context, bus1Id)) {
columnsExporter.writeThreeWindingsTransformerLegToFormatter(formatter, twt, middleBusNum, middleVlNum,
ThreeSides.ONE);
ThreeSides.ONE);
addExtensions(num1, twt);
}
if (!isOnlyMainCc() || isBusExported(context, middleBusId) || isBusExported(context, bus2Id)) {
columnsExporter.writeThreeWindingsTransformerLegToFormatter(formatter, twt, middleBusNum, middleVlNum,
ThreeSides.TWO);
ThreeSides.TWO);
addExtensions(num2, twt);
}
if (!isOnlyMainCc() || isBusExported(context, middleBusId) || isBusExported(context, bus3Id)) {
columnsExporter.writeThreeWindingsTransformerLegToFormatter(formatter, twt, middleBusNum, middleVlNum,
ThreeSides.THREE);
ThreeSides.THREE);
addExtensions(num3, twt);
}
}
Expand All @@ -450,15 +434,14 @@ private void writeDanglingLines(AmplExportContext context, TableFormatter format
Bus bus1 = AmplUtil.getBus(t);
String bus1Id = AmplUtil.getBusId(bus1);
String middleBusId = AmplUtil.getDanglingLineMiddleBusId(dl);
if (isOnlyMainCc() && !(isBusExported(context, bus1Id) || isBusExported(context, middleBusId))) {
continue;
if (!isOnlyMainCc() || isBusExported(context, bus1Id) || isBusExported(context, middleBusId)) {
VoltageLevel vl = t.getVoltageLevel();
String middleVlId = AmplUtil.getDanglingLineMiddleVoltageLevelId(dl);
context.voltageLevelIdsToExport.add(vl.getId());
context.voltageLevelIdsToExport.add(middleVlId);
columnsExporter.writeDanglingLineToFormatter(formatter, dl);
addExtensions(mapper.getInt(AmplSubset.BRANCH, dl.getId()), dl);
}
VoltageLevel vl = t.getVoltageLevel();
String middleVlId = AmplUtil.getDanglingLineMiddleVoltageLevelId(dl);
context.voltageLevelIdsToExport.add(vl.getId());
context.voltageLevelIdsToExport.add(middleVlId);
columnsExporter.writeDanglingLineToFormatter(formatter, dl);
addExtensions(mapper.getInt(AmplSubset.BRANCH, dl.getId()), dl);
}
}

Expand Down Expand Up @@ -517,16 +500,10 @@ private void writePhaseTapChangers() throws IOException {
}

private boolean exportLoad(AmplExportContext context, String busId) {
switch (config.getExportScope()) {
case ALL:
case ONLY_MAIN_CC_AND_CONNECTABLE_GENERATORS_AND_SHUNTS_AND_ALL_LOADS:
return true;
case ONLY_MAIN_CC:
case ONLY_MAIN_CC_AND_CONNECTABLE_GENERATORS_AND_SHUNTS:
return isBusExported(context, busId);
default:
throw new IllegalStateException();
}
return switch (config.getExportScope()) {
case ALL, ONLY_MAIN_CC_AND_CONNECTABLE_GENERATORS_AND_SHUNTS_AND_ALL_LOADS -> true;
case ONLY_MAIN_CC, ONLY_MAIN_CC_AND_CONNECTABLE_GENERATORS_AND_SHUNTS -> isBusExported(context, busId);
};
}

private void writeLoads(AmplExportContext context) throws IOException {
Expand All @@ -548,20 +525,19 @@ private void writeLoads(AmplExportContext context) throws IOException {
}
if (!exportLoad(context, busId)) {
skipped.add(l.getId());
continue;
} else {
context.loadsToExport.add(l.getId());
columnsExporter.writeLoadtoFormatter(formatter, l);
addExtensions(mapper.getInt(AmplSubset.LOAD, l.getId()), l);
}
context.loadsToExport.add(l.getId());

columnsExporter.writeLoadtoFormatter(formatter, l);
addExtensions(mapper.getInt(AmplSubset.LOAD, l.getId()), l);
}
for (DanglingLine dl : getSortedIdentifiables(network.getDanglingLineStream(DanglingLineFilter.UNPAIRED))) {
String middleBusId = AmplUtil.getDanglingLineMiddleBusId(dl);
if (!exportLoad(context, middleBusId)) {
skipped.add(dl.getId());
continue;
} else {
columnsExporter.writeDanglingLineLoadToFormatter(formatter, dl);
}
columnsExporter.writeDanglingLineLoadToFormatter(formatter, dl);
}
if (!skipped.isEmpty()) {
LOGGER.trace("Skip loads {} because not connected and not connectable", skipped);
Expand All @@ -570,17 +546,12 @@ private void writeLoads(AmplExportContext context) throws IOException {
}

private boolean exportGeneratorOrShunt(AmplExportContext context, String busId, String conBusId) {
switch (config.getExportScope()) {
case ALL:
return true;
case ONLY_MAIN_CC:
return isBusExported(context, busId);
case ONLY_MAIN_CC_AND_CONNECTABLE_GENERATORS_AND_SHUNTS:
case ONLY_MAIN_CC_AND_CONNECTABLE_GENERATORS_AND_SHUNTS_AND_ALL_LOADS:
return isBusExported(context, conBusId);
default:
throw new IllegalStateException();
}
return switch (config.getExportScope()) {
case ALL -> true;
case ONLY_MAIN_CC -> isBusExported(context, busId);
case ONLY_MAIN_CC_AND_CONNECTABLE_GENERATORS_AND_SHUNTS, ONLY_MAIN_CC_AND_CONNECTABLE_GENERATORS_AND_SHUNTS_AND_ALL_LOADS ->
isBusExported(context, conBusId);
};
}

private void writeShunts(AmplExportContext context) throws IOException {
Expand Down Expand Up @@ -748,7 +719,7 @@ private void writeLccConverterStations() throws IOException {
AmplConstants.LOCALE,
columnsExporter.getLccConverterStationsColumns())) {

for (HvdcConverterStation hvdcStation : getSortedIdentifiables(network.getHvdcConverterStationStream())) {
for (HvdcConverterStation<?> hvdcStation : getSortedIdentifiables(network.getHvdcConverterStationStream())) {
if (hvdcStation.getHvdcType().equals(HvdcType.LCC)) {
LccConverterStation lccStation = (LccConverterStation) hvdcStation;
columnsExporter.writeLccConverterStationToFormatter(formatter, lccStation);
Expand All @@ -768,7 +739,7 @@ private void writeVscConverterStations() throws IOException {
AmplConstants.LOCALE,
columnsExporter.getVscConverterStationsColumns())) {

for (HvdcConverterStation hvdcStation : getSortedIdentifiables(network.getHvdcConverterStationStream())) {
for (HvdcConverterStation<?> hvdcStation : getSortedIdentifiables(network.getHvdcConverterStationStream())) {
if (hvdcStation.getHvdcType().equals(HvdcType.VSC)) {
VscConverterStation vscStation = (VscConverterStation) hvdcStation;
columnsExporter.writeVscConverterStationToFormatter(formatter, vscStation);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,18 +144,18 @@ void writeBattery() throws IOException {
void writeThreeWindingsTransformer() throws IOException {
Network network = ThreeWindingsTransformerNetworkFactory.createWithCurrentLimits();
network.getThreeWindingsTransformer("3WT").getLeg1()
.newPhaseTapChanger()
.beginStep()
.setRho(1)
.setR(0.1)
.setX(1.)
.setB(0.)
.setG(0.)
.setAlpha(0)
.endStep()
.setTapPosition(0)
.setLowTapPosition(0)
.add();
.newPhaseTapChanger()
.beginStep()
.setRho(1)
.setR(0.1)
.setX(1.)
.setB(0.)
.setG(0.)
.setAlpha(0)
.endStep()
.setTapPosition(0)
.setLowTapPosition(0)
.add();

MemDataSource dataSource = new MemDataSource();
export(network, new Properties(), dataSource);
Expand Down Expand Up @@ -195,10 +195,10 @@ void writeTieLine() throws IOException {
Network network = EurostagTutorialExample1Factory.createWithTieLine();
for (DanglingLine danglingLine : network.getDanglingLines()) {
danglingLine.newCurrentLimits()
.setPermanentLimit(100.0)
.beginTemporaryLimit().setName("20'").setValue(120.0).setAcceptableDuration(20 * 60).endTemporaryLimit()
.beginTemporaryLimit().setName("10'").setValue(140.0).setAcceptableDuration(10 * 60).endTemporaryLimit()
.add();
.setPermanentLimit(100.0)
.beginTemporaryLimit().setName("20'").setValue(120.0).setAcceptableDuration(20 * 60).endTemporaryLimit()
.beginTemporaryLimit().setName("10'").setValue(140.0).setAcceptableDuration(10 * 60).endTemporaryLimit()
.add();
}

Properties properties = new Properties();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,22 +40,12 @@ enum BoundaryEquipmentType {
}

EquipmentAtBoundaryConversion createConversion(Context context) {
EquipmentAtBoundaryConversion c = null;
switch (type) {
case AC_LINE_SEGMENT:
c = new ACLineSegmentConversion(propertyBags.get(0), context);
break;
case SWITCH:
c = new SwitchConversion(propertyBags.get(0), context);
break;
case TRANSFORMER:
c = new TwoWindingsTransformerConversion(propertyBags, context);
break;
case EQUIVALENT_BRANCH:
c = new EquivalentBranchConversion(propertyBags.get(0), context);
break;
}
return c;
return switch (type) {
case AC_LINE_SEGMENT -> new ACLineSegmentConversion(propertyBags.get(0), context);
case SWITCH -> new SwitchConversion(propertyBags.get(0), context);
case TRANSFORMER -> new TwoWindingsTransformerConversion(propertyBags, context);
case EQUIVALENT_BRANCH -> new EquivalentBranchConversion(propertyBags.get(0), context);
};
}

boolean isAcLineSegmentDisconnected(Context context) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ public boolean exists(ReadOnlyDataSource ds) {
}
// If we are configured to support CIM14,
// check if there is this CIM14 data
return importCim14 && cds.existsCim14();
return IMPORT_CIM_14 && cds.existsCim14();
}

@Override
Expand Down Expand Up @@ -725,5 +725,5 @@ private void copyStream(ReadOnlyDataSource from, DataSource to, String fromName,
// Parameters of importers are only passed to importData method,
// but to decide if we are importers also for CIM 14 files
// we must implement the exists method, that has not access to parameters
private boolean importCim14 = false;
private static final boolean IMPORT_CIM_14 = false;
}
Original file line number Diff line number Diff line change
Expand Up @@ -893,13 +893,11 @@ public StateProfile getProfileForInitialValuesShuntSectionsTapPositions() {
}

public Config setProfileForInitialValuesShuntSectionsTapPositions(String profileForInitialValuesShuntSectionsTapPositions) {
switch (Objects.requireNonNull(profileForInitialValuesShuntSectionsTapPositions)) {
case "SSH":
case "SV":
this.profileForInitialValuesShuntSectionsTapPositions = StateProfile.valueOf(profileForInitialValuesShuntSectionsTapPositions);
break;
default:
throw new CgmesModelException("Unexpected profile used for shunt sections / tap positions state hypothesis: " + profileForInitialValuesShuntSectionsTapPositions);
String forInitialValuesShuntSectionsTapPositions = Objects.requireNonNull(profileForInitialValuesShuntSectionsTapPositions);
if (forInitialValuesShuntSectionsTapPositions.equals("SSH") || forInitialValuesShuntSectionsTapPositions.equals("SV")) {
this.profileForInitialValuesShuntSectionsTapPositions = StateProfile.valueOf(profileForInitialValuesShuntSectionsTapPositions);
} else {
throw new CgmesModelException("Unexpected profile used for shunt sections / tap positions state hypothesis: " + profileForInitialValuesShuntSectionsTapPositions);
}
return this;
}
Expand Down
Loading