Skip to content

Commit

Permalink
Use exporter id instead of AmplExportVersion enum name in properties …
Browse files Browse the repository at this point in the history
…for AmplExporter

Signed-off-by: Pauline Jean-Marie <pauline.jean-marie@artelys.com>
  • Loading branch information
Pauline Jean-Marie committed Nov 14, 2023
1 parent c8dd384 commit eac0484
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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<Parameter> STATIC_PARAMETERS = List.of(EXPORT_SCOPE_PARAMETER, EXPORT_XNODES_PARAMETER,
EXPORT_ACTION_TYPE_PARAMETER, EXPORT_RATIOTAPCHANGER_VT_PARAMETER, TWT_SPLIT_SHUNT_ADMITTANCE_PARAMETER,
Expand Down Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 <nicolas.pierre at artelys.com>}
*/
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<AmplSubset> mapper,
int variantIndex, int faultNum, int actionNum);
}

private static final Map<String, AmplExportVersion> 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<String> 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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<AmplSubset> mapper;
Expand All @@ -59,7 +58,7 @@ public BasicAmplExporter(AmplExportConfig config, Network network, StringToIntMa

@Override
public String getExporterId() {
return EXPORTER_ID;
return AmplExportVersion.V1_0.getExportedId();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 <geoffroy.jamgotchian at rte-france.com>}
Expand Down Expand Up @@ -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]"));

}
}

0 comments on commit eac0484

Please sign in to comment.