Skip to content

Commit

Permalink
Fix code smells (#296)
Browse files Browse the repository at this point in the history
Signed-off-by: lisrte <laurent.issertial@rte-france.com>
  • Loading branch information
Lisrte authored Nov 7, 2023
1 parent 94bad23 commit f542676
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 85 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,40 +39,11 @@ public static void update(Network targetNetwork, Network sourceNetwork, boolean
for (DanglingLine sourceDangling : sourceNetwork.getDanglingLines()) {
update(targetNetwork.getDanglingLine(sourceDangling.getId()).getTerminal(), sourceDangling.getTerminal());
}
for (HvdcLine sourceHvdcLine : sourceNetwork.getHvdcLines()) {
Terminal targetTerminal1 = targetNetwork.getHvdcLine(sourceHvdcLine.getId()).getConverterStation(HvdcLine.Side.ONE).getTerminal();
Terminal targetTerminal2 = targetNetwork.getHvdcLine(sourceHvdcLine.getId()).getConverterStation(HvdcLine.Side.TWO).getTerminal();
Terminal sourceTerminal1 = sourceHvdcLine.getConverterStation(HvdcLine.Side.ONE).getTerminal();
Terminal sourceTerminal2 = sourceHvdcLine.getConverterStation(HvdcLine.Side.TWO).getTerminal();
update(targetTerminal1, sourceTerminal1);
update(targetTerminal2, sourceTerminal2);
}
for (TwoWindingsTransformer sourceTwoWindingsTransformer : sourceNetwork.getTwoWindingsTransformers()) {
Terminal targetTerminal1 = targetNetwork.getTwoWindingsTransformer(sourceTwoWindingsTransformer.getId()).getTerminal1();
Terminal targetTerminal2 = targetNetwork.getTwoWindingsTransformer(sourceTwoWindingsTransformer.getId()).getTerminal2();
Terminal sourceTerminal1 = sourceTwoWindingsTransformer.getTerminal1();
Terminal sourceTerminal2 = sourceTwoWindingsTransformer.getTerminal2();
update(targetTerminal1, sourceTerminal1);
update(targetTerminal2, sourceTerminal2);

PhaseTapChanger sourcePhaseTapChanger = sourceTwoWindingsTransformer.getPhaseTapChanger();
PhaseTapChanger targetPhaseTapChanger = targetNetwork.getTwoWindingsTransformer(sourceTwoWindingsTransformer.getId()).getPhaseTapChanger();
if (targetPhaseTapChanger != null) {
targetPhaseTapChanger.setTapPosition(sourcePhaseTapChanger.getTapPosition());
}
updateHvdcLines(targetNetwork, sourceNetwork.getHvdcLines());
updateTwoWindingsTransformers(targetNetwork, sourceNetwork.getTwoWindingsTransformers());
updateThreeWindingsTransformers(targetNetwork, sourceNetwork.getThreeWindingsTransformers());

RatioTapChanger sourceRatioTapChanger = sourceTwoWindingsTransformer.getRatioTapChanger();
RatioTapChanger targetRatioTapChanger = targetNetwork.getTwoWindingsTransformer(sourceTwoWindingsTransformer.getId()).getRatioTapChanger();
if (targetRatioTapChanger != null) {
targetRatioTapChanger.setTapPosition(sourceRatioTapChanger.getTapPosition());
}
}
for (ThreeWindingsTransformer sourceThreeWindingsTransformer : sourceNetwork.getThreeWindingsTransformers()) {
ThreeWindingsTransformer targetThreeWindingsTransformer = targetNetwork.getThreeWindingsTransformer(sourceThreeWindingsTransformer.getId());
update(targetThreeWindingsTransformer.getLeg1(), sourceThreeWindingsTransformer.getLeg1());
update(targetThreeWindingsTransformer.getLeg2(), sourceThreeWindingsTransformer.getLeg2());
update(targetThreeWindingsTransformer.getLeg3(), sourceThreeWindingsTransformer.getLeg3());
}
for (Generator sourceGenerator : sourceNetwork.getGenerators()) {
update(targetNetwork.getGenerator(sourceGenerator.getId()).getTerminal(), sourceGenerator.getTerminal());
}
Expand Down Expand Up @@ -105,6 +76,49 @@ public static void update(Network targetNetwork, Network sourceNetwork, boolean
}
}

private static void updateHvdcLines(Network targetNetwork, Iterable<HvdcLine> hvdcLines) {
for (HvdcLine sourceHvdcLine : hvdcLines) {
Terminal targetTerminal1 = targetNetwork.getHvdcLine(sourceHvdcLine.getId()).getConverterStation(HvdcLine.Side.ONE).getTerminal();
Terminal targetTerminal2 = targetNetwork.getHvdcLine(sourceHvdcLine.getId()).getConverterStation(HvdcLine.Side.TWO).getTerminal();
Terminal sourceTerminal1 = sourceHvdcLine.getConverterStation(HvdcLine.Side.ONE).getTerminal();
Terminal sourceTerminal2 = sourceHvdcLine.getConverterStation(HvdcLine.Side.TWO).getTerminal();
update(targetTerminal1, sourceTerminal1);
update(targetTerminal2, sourceTerminal2);
}
}

private static void updateTwoWindingsTransformers(Network targetNetwork, Iterable<TwoWindingsTransformer> twoWindingsTransformers) {
for (TwoWindingsTransformer sourceTwoWindingsTransformer : twoWindingsTransformers) {
Terminal targetTerminal1 = targetNetwork.getTwoWindingsTransformer(sourceTwoWindingsTransformer.getId()).getTerminal1();
Terminal targetTerminal2 = targetNetwork.getTwoWindingsTransformer(sourceTwoWindingsTransformer.getId()).getTerminal2();
Terminal sourceTerminal1 = sourceTwoWindingsTransformer.getTerminal1();
Terminal sourceTerminal2 = sourceTwoWindingsTransformer.getTerminal2();
update(targetTerminal1, sourceTerminal1);
update(targetTerminal2, sourceTerminal2);

PhaseTapChanger sourcePhaseTapChanger = sourceTwoWindingsTransformer.getPhaseTapChanger();
PhaseTapChanger targetPhaseTapChanger = targetNetwork.getTwoWindingsTransformer(sourceTwoWindingsTransformer.getId()).getPhaseTapChanger();
if (targetPhaseTapChanger != null) {
targetPhaseTapChanger.setTapPosition(sourcePhaseTapChanger.getTapPosition());
}

RatioTapChanger sourceRatioTapChanger = sourceTwoWindingsTransformer.getRatioTapChanger();
RatioTapChanger targetRatioTapChanger = targetNetwork.getTwoWindingsTransformer(sourceTwoWindingsTransformer.getId()).getRatioTapChanger();
if (targetRatioTapChanger != null) {
targetRatioTapChanger.setTapPosition(sourceRatioTapChanger.getTapPosition());
}
}
}

private static void updateThreeWindingsTransformers(Network targetNetwork, Iterable<ThreeWindingsTransformer> threeWindingsTransformers) {
for (ThreeWindingsTransformer sourceThreeWindingsTransformer : threeWindingsTransformers) {
ThreeWindingsTransformer targetThreeWindingsTransformer = targetNetwork.getThreeWindingsTransformer(sourceThreeWindingsTransformer.getId());
update(targetThreeWindingsTransformer.getLeg1(), sourceThreeWindingsTransformer.getLeg1());
update(targetThreeWindingsTransformer.getLeg2(), sourceThreeWindingsTransformer.getLeg2());
update(targetThreeWindingsTransformer.getLeg3(), sourceThreeWindingsTransformer.getLeg3());
}
}

private static void update(ThreeWindingsTransformer.Leg target, ThreeWindingsTransformer.Leg source) {
update(target.getTerminal(), source.getTerminal());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,34 +42,20 @@ private static void serialize(LoadFlowParameters lfParameters, DynaFlowParameter
try {
jsonGenerator.writeStartObject();
jsonGenerator.writeObjectFieldStart("dfl-config");
if (dynaFlowParameters.getSvcRegulationOn() != null) {
jsonGenerator.writeBooleanField("SVCRegulationOn", dynaFlowParameters.getSvcRegulationOn());
}
if (dynaFlowParameters.getShuntRegulationOn() != null) {
jsonGenerator.writeBooleanField("ShuntRegulationOn", dynaFlowParameters.getShuntRegulationOn());
}
if (dynaFlowParameters.getAutomaticSlackBusOn() != null) {
jsonGenerator.writeBooleanField("AutomaticSlackBusOn", dynaFlowParameters.getAutomaticSlackBusOn());
}
if (dynaFlowParameters.getDsoVoltageLevel() != null) {
jsonGenerator.writeNumberField("DsoVoltageLevel", dynaFlowParameters.getDsoVoltageLevel());
}
jsonGenerator.writeBooleanField("InfiniteReactiveLimits", lfParameters.isNoGeneratorReactiveLimits());
writeNonNullField(jsonGenerator, "SVCRegulationOn", dynaFlowParameters.getSvcRegulationOn());
writeNonNullField(jsonGenerator, "ShuntRegulationOn", dynaFlowParameters.getShuntRegulationOn());
writeNonNullField(jsonGenerator, "AutomaticSlackBusOn", dynaFlowParameters.getAutomaticSlackBusOn());
writeNonNullField(jsonGenerator, "DsoVoltageLevel", dynaFlowParameters.getDsoVoltageLevel());
jsonGenerator.writeBooleanField("InfiniteReactiveLimits", !lfParameters.isUseReactiveLimits());

if (dynaFlowParameters.getActivePowerCompensation() != null) {
jsonGenerator.writeStringField("ActivePowerCompensation", dynaFlowParameters.getActivePowerCompensation().name());
}
if (dynaFlowParameters.getSettingPath() != null) {
jsonGenerator.writeStringField("SettingPath", dynaFlowParameters.getSettingPath());
}
if (dynaFlowParameters.getAssemblingPath() != null) {
jsonGenerator.writeStringField("AssemblingPath", dynaFlowParameters.getAssemblingPath());
}
if (dynaFlowParameters.getStartTime() != null) {
jsonGenerator.writeNumberField("StartTime", dynaFlowParameters.getStartTime());
}
if (dynaFlowParameters.getStopTime() != null) {
jsonGenerator.writeNumberField("StopTime", dynaFlowParameters.getStopTime());
}
writeNonNullField(jsonGenerator, "SettingPath", dynaFlowParameters.getSettingPath());
writeNonNullField(jsonGenerator, "AssemblingPath", dynaFlowParameters.getAssemblingPath());
writeNonNullField(jsonGenerator, "StartTime", dynaFlowParameters.getStartTime());
writeNonNullField(jsonGenerator, "StopTime", dynaFlowParameters.getStopTime());

if (dynaFlowParameters.getPrecision() != null && !Double.isNaN(dynaFlowParameters.getPrecision())) {
jsonGenerator.writeNumberField("Precision", dynaFlowParameters.getPrecision());
}
Expand All @@ -84,9 +70,8 @@ private static void serialize(LoadFlowParameters lfParameters, DynaFlowParameter
}
jsonGenerator.writeEndArray();
}
if (dynaFlowParameters.getTimeStep() != null) {
jsonGenerator.writeNumberField("TimeStep", dynaFlowParameters.getTimeStep());
}

writeNonNullField(jsonGenerator, "TimeStep", dynaFlowParameters.getTimeStep());
if (dynaFlowParameters.getStartingPointMode() != null) {
jsonGenerator.writeStringField("StartingPointMode", dynaFlowParameters.getStartingPointMode().getName());
}
Expand All @@ -97,4 +82,22 @@ private static void serialize(LoadFlowParameters lfParameters, DynaFlowParameter
throw new UncheckedIOException(e);
}
}

private static void writeNonNullField(JsonGenerator jsonGenerator, String fieldName, Boolean value) throws IOException {
if (value != null) {
jsonGenerator.writeBooleanField(fieldName, value);
}
}

private static void writeNonNullField(JsonGenerator jsonGenerator, String fieldName, Double value) throws IOException {
if (value != null) {
jsonGenerator.writeNumberField(fieldName, value);
}
}

private static void writeNonNullField(JsonGenerator jsonGenerator, String fieldName, String value) throws IOException {
if (value != null) {
jsonGenerator.writeStringField(fieldName, value);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
*/
package com.powsybl.dynaflow.xml;

import com.google.common.base.Supplier;
import com.google.common.base.Suppliers;
import com.powsybl.commons.PowsyblException;
import com.powsybl.commons.exceptions.UncheckedXmlStreamException;
Expand All @@ -31,6 +30,7 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.function.Supplier;

/**
* @author Marcos de Miguel {@literal <demiguelm at aia.es>}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,17 @@
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

import java.io.IOException;
import java.nio.file.FileSystem;
import java.nio.file.Files;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Stream;

import static org.junit.jupiter.api.Assertions.*;

Expand Down Expand Up @@ -66,28 +70,21 @@ void test() {
curves.forEach(this::validateCurve);
}

@Test
void testModelIdStaticIdDefined() {
@ParameterizedTest(name = "{1}")
@MethodSource("provideFileError")
void testScriptError(String fileName, String error) {
List<CurveGroovyExtension> extensions = validateGroovyExtension();
CurvesSupplier supplier = new GroovyCurvesSupplier(fileSystem.getPath("/curves_dynamicModelId_staticId.groovy"), extensions);
CurvesSupplier supplier = new GroovyCurvesSupplier(fileSystem.getPath(fileName), extensions);
DslException exception = assertThrows(DslException.class, () -> supplier.get(network));
assertEquals("Both staticId and dynamicModelId are defined", exception.getMessage());
assertEquals(error, exception.getMessage());
}

@Test
void testVariableNotDefined() {
List<CurveGroovyExtension> extensions = validateGroovyExtension();
CurvesSupplier supplier = new GroovyCurvesSupplier(fileSystem.getPath("/curves_variable.groovy"), extensions);
DslException exception = assertThrows(DslException.class, () -> supplier.get(network));
assertEquals("'variables' field is not set", exception.getMessage());
}

@Test
void testVariablesNotDefined() {
List<CurveGroovyExtension> extensions = validateGroovyExtension();
CurvesSupplier supplier = new GroovyCurvesSupplier(fileSystem.getPath("/curves_variables.groovy"), extensions);
DslException exception = assertThrows(DslException.class, () -> supplier.get(network));
assertEquals("'variables' field is not set", exception.getMessage());
private static Stream<Arguments> provideFileError() {
return Stream.of(
Arguments.of("/curves_dynamicModelId_staticId.groovy", "Both staticId and dynamicModelId are defined"),
Arguments.of("/curves_variable.groovy", "'variables' field is not set"),
Arguments.of("/curves_variables.groovy", "'variables' field is not set")
);
}

private List<CurveGroovyExtension> validateGroovyExtension() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
*/
public abstract class AbstractEvent extends AbstractPureDynamicBlackBoxModel implements EventModel {

private final Identifiable<?> equipment;
private final Identifiable<? extends Identifiable<?>> equipment;
private final double startTime;

protected AbstractEvent(Identifiable<?> equipment, double startTime, String eventPrefix) {
Expand Down
10 changes: 5 additions & 5 deletions dynawaltz/src/main/java/com/powsybl/dynawaltz/xml/JobsXml.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,10 @@ private static void writeModeler(XMLStreamWriter writer, DynaWaltzContext contex
writer.writeAttribute("dydFile", DYD_FILENAME);

writer.writeEmptyElement(DYN_URI, "precompiledModels");
writer.writeAttribute("useStandardModels", "true");
writer.writeAttribute("useStandardModels", Boolean.toString(true));

writer.writeEmptyElement(DYN_URI, "modelicaModels");
writer.writeAttribute("useStandardModels", "false");
writer.writeAttribute("useStandardModels", Boolean.toString(false));

writer.writeEndElement();
}
Expand All @@ -85,15 +85,15 @@ private static void writeOutput(XMLStreamWriter writer, DynaWaltzContext context
writer.writeAttribute("directory", "outputs");

writer.writeEmptyElement(DYN_URI, "dumpInitValues");
writer.writeAttribute("local", "false");
writer.writeAttribute("global", "false");
writer.writeAttribute("local", Boolean.toString(false));
writer.writeAttribute("global", Boolean.toString(false));

writer.writeEmptyElement(DYN_URI, "timeline");
writer.writeAttribute("exportMode", "TXT");

writer.writeEmptyElement(DYN_URI, "finalState");
writer.writeAttribute("exportIIDMFile", Boolean.toString(context.getDynaWaltzParameters().isWriteFinalState()));
writer.writeAttribute("exportDumpFile", "false");
writer.writeAttribute("exportDumpFile", Boolean.toString(false));

if (context.withCurves()) {
writer.writeEmptyElement(DYN_URI, "curves");
Expand Down

0 comments on commit f542676

Please sign in to comment.