From eac048424f5d67cae971dd37843489d19b1b86fd Mon Sep 17 00:00:00 2001 From: Pauline Jean-Marie Date: Tue, 14 Nov 2023 16:38:51 +0100 Subject: [PATCH] Use exporter id instead of AmplExportVersion enum name in properties for AmplExporter Signed-off-by: Pauline Jean-Marie --- .../powsybl/ampl/converter/AmplExporter.java | 11 ++--- .../converter/version/AmplExportVersion.java | 40 ++++++++++++++++--- .../converter/version/BasicAmplExporter.java | 3 +- .../ampl/converter/AmplNetworkWriterTest.java | 27 ++++++++++++- 4 files changed, 65 insertions(+), 16 deletions(-) diff --git a/ampl-converter/src/main/java/com/powsybl/ampl/converter/AmplExporter.java b/ampl-converter/src/main/java/com/powsybl/ampl/converter/AmplExporter.java index 2ca2e836657..8e8fd3144d8 100644 --- a/ampl-converter/src/main/java/com/powsybl/ampl/converter/AmplExporter.java +++ b/ampl-converter/src/main/java/com/powsybl/ampl/converter/AmplExporter.java @@ -19,10 +19,7 @@ import java.io.IOException; import java.io.UncheckedIOException; -import java.util.Arrays; -import java.util.List; -import java.util.Objects; -import java.util.Properties; +import java.util.*; import java.util.stream.Collectors; /** @@ -58,8 +55,8 @@ public class AmplExporter implements Exporter { private static final Parameter EXPORT_VERSION_PARAMETER = new Parameter(EXPORT_VERSION, ParameterType.STRING, "The version of the export.", - AmplExportVersion.V1_0.name(), - Arrays.stream(AmplExportVersion.values()).map(Enum::name).collect(Collectors.toList())); + AmplExportVersion.V1_0.getExportedId(), + new ArrayList<>(AmplExportVersion.exporterIdValues())); private static final List STATIC_PARAMETERS = List.of(EXPORT_SCOPE_PARAMETER, EXPORT_XNODES_PARAMETER, EXPORT_ACTION_TYPE_PARAMETER, EXPORT_RATIOTAPCHANGER_VT_PARAMETER, TWT_SPLIT_SHUNT_ADMITTANCE_PARAMETER, @@ -96,7 +93,7 @@ public void export(Network network, Properties parameters, DataSource dataSource boolean exportRatioTapChangerVoltageTarget = Parameter.readBoolean(getFormat(), parameters, EXPORT_RATIOTAPCHANGER_VT_PARAMETER, defaultValueConfig); boolean twtSplitShuntAdmittance = Parameter.readBoolean(getFormat(), parameters, TWT_SPLIT_SHUNT_ADMITTANCE_PARAMETER, defaultValueConfig); - AmplExportVersion exportVersion = AmplExportVersion.valueOf( + AmplExportVersion exportVersion = AmplExportVersion.fromExporterId( Parameter.readString(getFormat(), parameters, EXPORT_VERSION_PARAMETER, defaultValueConfig)); AmplExportConfig config = new AmplExportConfig(scope, exportXnodes, actionType, diff --git a/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/AmplExportVersion.java b/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/AmplExportVersion.java index 182050ee823..8ec9bed9783 100644 --- a/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/AmplExportVersion.java +++ b/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/AmplExportVersion.java @@ -9,30 +9,58 @@ import com.powsybl.ampl.converter.AmplExportConfig; import com.powsybl.ampl.converter.AmplSubset; +import com.powsybl.commons.PowsyblException; import com.powsybl.commons.util.StringToIntMapper; import com.powsybl.iidm.network.Network; +import java.util.HashMap; +import java.util.Map; +import java.util.Set; + /** * @author Nicolas Pierre {@literal } */ public enum AmplExportVersion { - /** - * Legacy export - */ - V1_0(BasicAmplExporter.getFactory()); + + V1_0("1.0", BasicAmplExporter.getFactory()); // Legacy export public interface Factory { AmplColumnsExporter create(AmplExportConfig config, Network network, StringToIntMapper mapper, int variantIndex, int faultNum, int actionNum); } + private static final Map VERSION_BY_EXPORTER_ID = new HashMap<>(); + static { + for (AmplExportVersion v : values()) { + VERSION_BY_EXPORTER_ID.put(v.exporterId, v); + } + } + + private final String exporterId; private final Factory factory; - AmplExportVersion(Factory factory) { + AmplExportVersion(String exporterId, Factory factory) { + this.exporterId = exporterId; this.factory = factory; } + public String getExportedId() { + return this.exporterId; + } + public Factory getColumnsExporter() { - return factory; + return this.factory; + } + + public static Set exporterIdValues() { + return VERSION_BY_EXPORTER_ID.keySet(); + } + + public static AmplExportVersion fromExporterId(String exporterId) { + AmplExportVersion version = VERSION_BY_EXPORTER_ID.get(exporterId); + if (version == null) { + throw new PowsyblException("exporterId " + exporterId + " is not in the exporterId possible values: " + exporterIdValues()); + } + return version; } } diff --git a/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/BasicAmplExporter.java b/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/BasicAmplExporter.java index b3df04bbb14..8e64dc10b92 100644 --- a/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/BasicAmplExporter.java +++ b/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/BasicAmplExporter.java @@ -33,7 +33,6 @@ */ public class BasicAmplExporter implements AmplColumnsExporter { - private static final String EXPORTER_ID = "1.0"; private final AmplExportConfig config; private final Network network; private final StringToIntMapper mapper; @@ -59,7 +58,7 @@ public BasicAmplExporter(AmplExportConfig config, Network network, StringToIntMa @Override public String getExporterId() { - return EXPORTER_ID; + return AmplExportVersion.V1_0.getExportedId(); } @Override diff --git a/ampl-converter/src/test/java/com/powsybl/ampl/converter/AmplNetworkWriterTest.java b/ampl-converter/src/test/java/com/powsybl/ampl/converter/AmplNetworkWriterTest.java index 40aa529aa09..284e27ae317 100644 --- a/ampl-converter/src/test/java/com/powsybl/ampl/converter/AmplNetworkWriterTest.java +++ b/ampl-converter/src/test/java/com/powsybl/ampl/converter/AmplNetworkWriterTest.java @@ -22,7 +22,7 @@ import java.util.Properties; import static com.powsybl.commons.test.ComparisonUtils.compareTxt; -import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.*; /** * @author Geoffroy Jamgotchian {@literal } @@ -250,4 +250,29 @@ void writeHeaders() throws IOException { export(network, new Properties(), dataSource); assertEqualsToRef(dataSource, "_headers", "inputs/headers.txt"); } + + @Test + void writeHeadersWithVersion() throws IOException { + Network network = Network.create("dummy_network", "test"); + MemDataSource dataSource = new MemDataSource(); + + Properties properties = new Properties(); + properties.put("iidm.export.ampl.export-version", "1.0"); + + export(network, properties, dataSource); + assertEqualsToRef(dataSource, "_headers", "inputs/headers.txt"); + } + + @Test + void writeHeadersWithUnknownVersion() { + Network network = Network.create("dummy_network", "test"); + MemDataSource dataSource = new MemDataSource(); + + Properties properties = new Properties(); + properties.put("iidm.export.ampl.export-version", "V1_0"); + + Exception e = assertThrows(IllegalArgumentException.class, () -> export(network, properties, dataSource)); + assertTrue(e.getMessage().contains("Value V1_0 of parameter iidm.export.ampl.export-version is not contained in possible values [1.0]")); + + } }