Skip to content

Commit

Permalink
Fix code smell
Browse files Browse the repository at this point in the history
Use optional criteriaFilePath

Signed-off-by: lisrte <[email protected]>
  • Loading branch information
Lisrte committed Sep 19, 2024
1 parent 19ff085 commit 8b0265a
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ void testIeee14WithDump() throws IOException {

@Test
void testIeee14WithSimulationCriteria() {
ReportNode reportNode = ReportNode.newRootReportNode().withMessageTemplate("integrationTest", "Integration test").build();
ReportNode reportNode = ReportNode.newRootReportNode().withMessageTemplate("integrationTest", "Integration test").build();
Supplier<DynamicSimulationResult> resultSupplier = setupIEEE14Simulation(reportNode);
dynawoSimulationParameters.setCriteriaFilePath(Path.of(Objects.requireNonNull(getClass().getResource("/ieee14/criteria.crt")).getPath()));
DynamicSimulationResult result = resultSupplier.get();
Expand Down
5 changes: 5 additions & 0 deletions dynawo-simulation/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@
<groupId>${project.groupId}</groupId>
<artifactId>powsybl-dynawo-commons</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jdk8</artifactId>
<version>2.17.1</version>
</dependency>

<!-- Test dependencies -->
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*/
package com.powsybl.dynawo;

import com.powsybl.commons.PowsyblException;
import com.powsybl.commons.report.ReportNode;
import com.powsybl.computation.AbstractExecutionHandler;
import com.powsybl.computation.Command;
Expand Down Expand Up @@ -217,8 +218,12 @@ private void writeInputFiles(Path workingDir) throws IOException {
Files.copy(dumpFilePath, workingDir.resolve(dumpFileParameters.dumpFile()), StandardCopyOption.REPLACE_EXISTING);
}
}
if (parameters.hasCriteriaFile()) {
Files.copy(parameters.getCriteriaFilePath(), workingDir.resolve(parameters.getCriteriaFileName()), StandardCopyOption.REPLACE_EXISTING);
}
parameters.getCriteriaFilePath().ifPresent(filePath -> {
try {
Files.copy(filePath, workingDir.resolve(filePath.getFileName()), StandardCopyOption.REPLACE_EXISTING);
} catch (IOException e) {
throw new PowsyblException("Simulation criteria file error", e);
}
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ public String getFileName() {
private ExportMode timelineExportMode = DEFAULT_TIMELINE_EXPORT_MODE;
private LogLevel logLevelFilter = DEFAULT_LOG_LEVEL_FILTER;
private EnumSet<SpecificLog> specificLogs = EnumSet.noneOf(SpecificLog.class);
private Path criteriaFilePath = null;
private Optional<Path> criteriaFilePath = Optional.empty();

/**
* Loads parameters from the default platform configuration.
Expand Down Expand Up @@ -310,16 +310,16 @@ public DynawoSimulationParameters addSpecificLog(SpecificLog specificLog) {
return this;
}

public Path getCriteriaFilePath() {
public Optional<Path> getCriteriaFilePath() {
return criteriaFilePath;
}

public String getCriteriaFileName() {
return hasCriteriaFile() ? criteriaFilePath.getFileName().toString() : null;
public Optional<String> getCriteriaFileName() {
return getCriteriaFilePath().map(c -> c.getFileName().toString());
}

public DynawoSimulationParameters setCriteriaFilePath(Path criteriaFilePath) {
this.criteriaFilePath = criteriaFilePath;
this.criteriaFilePath = Optional.ofNullable(criteriaFilePath);
return this;
}

Expand All @@ -330,8 +330,4 @@ private void setCriteriaFilePath(String criteriaPathName, FileSystem fileSystem)
}
setCriteriaFilePath(criteriaPath);
}

public boolean hasCriteriaFile() {
return criteriaFilePath != null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.datatype.jdk8.Jdk8Module;
import com.google.auto.service.AutoService;
import com.powsybl.commons.json.JsonUtil;
import com.powsybl.dynamicsimulation.DynamicSimulationParameters;
Expand Down Expand Up @@ -56,7 +57,8 @@ private interface SerializationSpec {

private static ObjectMapper createMapper() {
return JsonUtil.createObjectMapper()
.addMixIn(DynawoSimulationParameters.class, SerializationSpec.class);
.addMixIn(DynawoSimulationParameters.class, SerializationSpec.class)
.registerModule(new Jdk8Module());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import javax.xml.stream.XMLStreamWriter;
import java.io.IOException;
import java.nio.file.Path;
import java.util.Optional;

import static com.powsybl.dynawo.xml.DynawoSimulationConstants.*;
import static com.powsybl.dynawo.xml.DynawoSimulationXmlConstants.DYN_URI;
Expand Down Expand Up @@ -80,18 +81,18 @@ private static void writeModeler(XMLStreamWriter writer, DynawoSimulationParamet
}

private static void writeSimulation(XMLStreamWriter writer, DynawoSimulationParameters parameters, DynamicSimulationParameters dynamicSimulationParameters) throws XMLStreamException {
boolean hasCriteriaFile = parameters.hasCriteriaFile();
if (hasCriteriaFile) {
Optional<String> criteriaFileName = parameters.getCriteriaFileName();
if (criteriaFileName.isPresent()) {
writer.writeStartElement(DYN_URI, "simulation");
} else {
writer.writeEmptyElement(DYN_URI, "simulation");
}
writer.writeAttribute("startTime", Double.toString(dynamicSimulationParameters.getStartTime()));
writer.writeAttribute("stopTime", Double.toString(dynamicSimulationParameters.getStopTime()));
writer.writeAttribute("precision", Double.toString(parameters.getPrecision()));
if (hasCriteriaFile) {
if (criteriaFileName.isPresent()) {
writer.writeEmptyElement(DYN_URI, "criteria");
writer.writeAttribute("criteriaFile", parameters.getCriteriaFileName());
writer.writeAttribute("criteriaFile", criteriaFileName.get());
writer.writeEndElement();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import java.nio.file.Path;
import java.util.*;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.*;

/**
Expand Down Expand Up @@ -102,8 +103,8 @@ void checkParameters() throws IOException {
assertEquals(timelinExportMode, parameters.getTimelineExportMode());
assertEquals(logLevel, parameters.getLogLevelFilter());
assertEquals(specificLogs, parameters.getSpecificLogs());
assertEquals(criteriaFileName, parameters.getCriteriaFileName());
assertEquals(fileSystem.getPath(USER_HOME + criteriaFileName), parameters.getCriteriaFilePath());
assertThat(parameters.getCriteriaFileName()).hasValue(criteriaFileName);
assertThat(parameters.getCriteriaFilePath()).hasValue(fileSystem.getPath(USER_HOME + criteriaFileName));
}

@Test
Expand Down Expand Up @@ -210,7 +211,7 @@ void checkDefaultParameters() throws IOException {
assertEquals(DynawoSimulationParameters.DEFAULT_MERGE_LOADS, parameters.isMergeLoads());
assertEquals(DynawoSimulationParameters.DEFAULT_USE_MODEL_SIMPLIFIERS, parameters.isUseModelSimplifiers());
assertEquals(DynawoSimulationParameters.DEFAULT_TIMELINE_EXPORT_MODE, parameters.getTimelineExportMode());
assertFalse(parameters.hasCriteriaFile());
assertTrue(parameters.getCriteriaFilePath().isEmpty());
}

@Test
Expand Down

0 comments on commit 8b0265a

Please sign in to comment.