Skip to content

Commit

Permalink
Fix code smells
Browse files Browse the repository at this point in the history
Signed-off-by: Florian Dupuy <florian.dupuy@rte-france.com>
  • Loading branch information
flo-dup committed Apr 21, 2023
1 parent 41e3534 commit caf4497
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,20 +51,17 @@ public class DynaWaltzProvider implements DynamicSimulationProvider {
private static final String WORKING_DIR_PREFIX = "powsybl_dynawaltz_";
private static final String OUTPUT_IIDM_FILENAME = "outputIIDM.xml";

private final PlatformConfig platformConfig;

private final DynaWaltzConfig dynaWaltzConfig;

public DynaWaltzProvider() {
this(PlatformConfig.defaultConfig());
}

public DynaWaltzProvider(PlatformConfig platformConfig) {
this(platformConfig, DynaWaltzConfig.load(platformConfig));
this(DynaWaltzConfig.load(platformConfig));
}

public DynaWaltzProvider(PlatformConfig platformConfig, DynaWaltzConfig dynawoConfig) {
this.platformConfig = Objects.requireNonNull(platformConfig);
public DynaWaltzProvider(DynaWaltzConfig dynawoConfig) {
this.dynaWaltzConfig = Objects.requireNonNull(dynawoConfig);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
*/
public final class ParametersXml {

public static final String PARAMETERS_SET_ELEMENT_NAME = "parametersSet";

private ParametersXml() {
}

Expand Down Expand Up @@ -80,10 +82,9 @@ public static ParametersSet load(Path parametersFile, String parameterSetId) {
private static List<ParametersSet> readAndClose(XMLStreamReader xmlReader) throws XMLStreamException {
List<ParametersSet> parametersSets = new ArrayList<>();
skipComments(xmlReader);
com.powsybl.commons.xml.XmlUtil.readUntilEndElement("parametersSet", xmlReader, () -> {
com.powsybl.commons.xml.XmlUtil.readUntilEndElement(PARAMETERS_SET_ELEMENT_NAME, xmlReader, () -> {
if (!xmlReader.getLocalName().equals("set")) {
xmlReader.close();
throw new PowsyblException("Unexpected element: " + xmlReader.getLocalName());
closeAndThrowException(xmlReader, xmlReader.getLocalName());
}
String parameterSetIdRead = xmlReader.getAttributeValue(null, "id");
ParametersSet parametersSet = new ParametersSet(parameterSetIdRead);
Expand All @@ -98,13 +99,12 @@ private static ParametersSet readOneSetAndClose(XMLStreamReader xmlReader, Strin
ParametersSet parametersSet = new ParametersSet(parameterSetId);
AtomicBoolean found = new AtomicBoolean(false);
skipComments(xmlReader);
com.powsybl.commons.xml.XmlUtil.readUntilEndElement("parametersSet", xmlReader, () -> {
com.powsybl.commons.xml.XmlUtil.readUntilEndElement(PARAMETERS_SET_ELEMENT_NAME, xmlReader, () -> {
if (found.get()) {
return;
}
if (!xmlReader.getLocalName().equals("set")) {
xmlReader.close();
throw new PowsyblException("Unexpected element: " + xmlReader.getLocalName());
closeAndThrowException(xmlReader, xmlReader.getLocalName());
}
if (xmlReader.getAttributeValue(null, "id").equals(parameterSetId)) {
fillParametersSet(xmlReader, parametersSet);
Expand All @@ -130,7 +130,7 @@ private static void fillParametersSet(XMLStreamReader xmlReader, ParametersSet p
String origName = xmlReader.getAttributeValue(null, "origName");
parametersSet.addReference(name, type, origData, origName);
} else {
throw new PowsyblException("Unexpected element: " + xmlReader.getLocalName());
closeAndThrowException(xmlReader, xmlReader.getLocalName());
}
});
}
Expand All @@ -149,6 +149,11 @@ private static void skipComments(XMLStreamReader xmlReader) throws XMLStreamExce
}
}

private static void closeAndThrowException(XMLStreamReader xmlReader, String unexpectedElement) throws XMLStreamException {
xmlReader.close();
throw new PowsyblException("Unexpected element: " + unexpectedElement);
}

public static void write(Path workingDir, DynaWaltzContext context) throws IOException, XMLStreamException {
Objects.requireNonNull(workingDir);

Expand All @@ -159,7 +164,7 @@ public static void write(Path workingDir, DynaWaltzContext context) throws IOExc

// Write parameterSet that needs to be generated (OmegaRef...)
Path file = workingDir.resolve(context.getSimulationParFile());
XmlUtil.write(file, context, "parametersSet", ParametersXml::write);
XmlUtil.write(file, context, PARAMETERS_SET_ELEMENT_NAME, ParametersXml::write);
}

private static void write(XMLStreamWriter writer, DynaWaltzContext context) throws XMLStreamException {
Expand All @@ -175,7 +180,7 @@ private static void write(Collection<ParametersSet> parametersSets, String filen
try {
xmlWriter.writeStartDocument(StandardCharsets.UTF_8.toString(), "1.0");
xmlWriter.setPrefix("", DYN_URI);
xmlWriter.writeStartElement(DYN_URI, "parametersSet");
xmlWriter.writeStartElement(DYN_URI, PARAMETERS_SET_ELEMENT_NAME);
xmlWriter.writeNamespace("", DYN_URI);
for (ParametersSet parametersSet : parametersSets) {
writeParametersSet(xmlWriter, parametersSet);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
*/
package com.powsybl.dynawo.it;

import com.powsybl.commons.config.PlatformConfig;
import com.powsybl.commons.datasource.ResourceDataSource;
import com.powsybl.commons.datasource.ResourceSet;
import com.powsybl.dynamicsimulation.*;
Expand Down Expand Up @@ -43,7 +42,7 @@ class DynaWaltzTest extends AbstractDynawoTest {
@BeforeEach
void setUp() throws Exception {
super.setUp();
provider = new DynaWaltzProvider(PlatformConfig.defaultConfig(), new DynaWaltzConfig("/dynawo", false));
provider = new DynaWaltzProvider(new DynaWaltzConfig("/dynawo", false));
parameters = new DynamicSimulationParameters()
.setStartTime(1)
.setStopTime(100);
Expand Down

0 comments on commit caf4497

Please sign in to comment.