From abd2ca8fc357cd090fd4c426bc025575623cdd03 Mon Sep 17 00:00:00 2001 From: lisrte Date: Fri, 11 Oct 2024 17:19:08 +0200 Subject: [PATCH] Sort : - models in ModelConfigs by model name - ModelConfigsHandler::builderConfigs by category - ModelConfigsHandler::eventBuilderConfigs by model name Signed-off-by: lisrte --- .../powsybl/dynawo/builders/ModelConfigs.java | 5 +- .../dynawo/builders/ModelConfigsHandler.java | 18 ++++--- .../ModelConfigsJsonDeserializer.java | 2 +- .../builders/ModelConfigLoaderTest.java | 53 +++++++++---------- 4 files changed, 39 insertions(+), 39 deletions(-) diff --git a/dynawo-simulation/src/main/java/com/powsybl/dynawo/builders/ModelConfigs.java b/dynawo-simulation/src/main/java/com/powsybl/dynawo/builders/ModelConfigs.java index 3d7bc16cc..c5795ed79 100644 --- a/dynawo-simulation/src/main/java/com/powsybl/dynawo/builders/ModelConfigs.java +++ b/dynawo-simulation/src/main/java/com/powsybl/dynawo/builders/ModelConfigs.java @@ -22,9 +22,9 @@ public class ModelConfigs { private static final Logger LOGGER = LoggerFactory.getLogger(ModelConfigs.class); private ModelConfig defaultModelConfig; - private final Map modelConfigMap; + private final SortedMap modelConfigMap; - ModelConfigs(Map modelConfigMap, String defaultModelConfigName) { + ModelConfigs(SortedMap modelConfigMap, String defaultModelConfigName) { this.modelConfigMap = Objects.requireNonNull(modelConfigMap); if (defaultModelConfigName != null) { this.defaultModelConfig = Objects.requireNonNull(modelConfigMap.get(defaultModelConfigName)); @@ -58,7 +58,6 @@ Set getModelsName() { } void addModelConfigs(ModelConfigs modelConfigsToMerge) { - modelConfigMap.putAll(modelConfigsToMerge.modelConfigMap); if (hasDefaultModelConfig() && modelConfigsToMerge.hasDefaultModelConfig()) { LOGGER.warn("Default model configs {} & {} found, the first one will be kept", defaultModelConfig.lib(), diff --git a/dynawo-simulation/src/main/java/com/powsybl/dynawo/builders/ModelConfigsHandler.java b/dynawo-simulation/src/main/java/com/powsybl/dynawo/builders/ModelConfigsHandler.java index 699af3b26..4c1d86bec 100644 --- a/dynawo-simulation/src/main/java/com/powsybl/dynawo/builders/ModelConfigsHandler.java +++ b/dynawo-simulation/src/main/java/com/powsybl/dynawo/builders/ModelConfigsHandler.java @@ -13,10 +13,7 @@ import com.powsybl.dynamicsimulation.EventModel; import com.powsybl.iidm.network.Network; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.ServiceLoader; +import java.util.*; import java.util.stream.Collectors; /** @@ -40,11 +37,18 @@ private ModelConfigsHandler() { return configs1; }) )); - builderConfigs = modelConfigLoaders.stream().flatMap(ModelConfigLoader::loadBuilderConfigs).toList(); + builderConfigs = modelConfigLoaders.stream() + .flatMap(ModelConfigLoader::loadBuilderConfigs) + .sorted(Comparator.comparing(BuilderConfig::getCategory)) + .toList(); builderConfigs.forEach(bc -> modelConfigsCat.get(bc.getCategory()).getModelsName() .forEach(lib -> builderConstructorByName.put(lib, bc.getBuilderConstructor()))); - eventBuilderConfigs = modelConfigLoaders.stream().flatMap(ModelConfigLoader::loadEventBuilderConfigs).toList(); - eventBuilderConstructorByName = eventBuilderConfigs.stream().collect(Collectors.toMap(e -> e.getEventModelInfo().name(), EventBuilderConfig::getBuilderConstructor)); + eventBuilderConfigs = modelConfigLoaders.stream() + .flatMap(ModelConfigLoader::loadEventBuilderConfigs) + .sorted(Comparator.comparing(e -> e.getEventModelInfo().name())) + .toList(); + eventBuilderConstructorByName = eventBuilderConfigs.stream() + .collect(Collectors.toMap(e -> e.getEventModelInfo().name(), EventBuilderConfig::getBuilderConstructor)); } public static ModelConfigsHandler getInstance() { diff --git a/dynawo-simulation/src/main/java/com/powsybl/dynawo/builders/ModelConfigsJsonDeserializer.java b/dynawo-simulation/src/main/java/com/powsybl/dynawo/builders/ModelConfigsJsonDeserializer.java index 18f131a45..e946a5c92 100644 --- a/dynawo-simulation/src/main/java/com/powsybl/dynawo/builders/ModelConfigsJsonDeserializer.java +++ b/dynawo-simulation/src/main/java/com/powsybl/dynawo/builders/ModelConfigsJsonDeserializer.java @@ -40,7 +40,7 @@ public Map deserialize(JsonParser parser, DeserializationC private static ModelConfigs parseModelConfigs(JsonParser parser) { var parsingContext = new Object() { String defaultLib = null; - final Map libs = new HashMap<>(); + final SortedMap libs = new TreeMap<>(); }; JsonUtil.parseObject(parser, name -> switch (name) { diff --git a/dynawo-simulation/src/test/java/com/powsybl/dynawo/builders/ModelConfigLoaderTest.java b/dynawo-simulation/src/test/java/com/powsybl/dynawo/builders/ModelConfigLoaderTest.java index f5006c838..1740587e4 100644 --- a/dynawo-simulation/src/test/java/com/powsybl/dynawo/builders/ModelConfigLoaderTest.java +++ b/dynawo-simulation/src/test/java/com/powsybl/dynawo/builders/ModelConfigLoaderTest.java @@ -14,10 +14,7 @@ import org.junit.jupiter.api.Test; import java.io.IOException; -import java.util.Collections; -import java.util.HashMap; -import java.util.List; -import java.util.Map; +import java.util.*; import static org.assertj.core.api.Assertions.assertThat; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -68,50 +65,50 @@ void loadConfigTest() throws IOException { objectMapper.registerModule(module); Map configs = objectMapper.readValue(json, new TypeReference<>() { }); - assertThat(configs.keySet()).containsExactlyInAnyOrder("synchronousGenerators"); + assertThat(configs.keySet()).containsExactly("synchronousGenerators"); ModelConfigs synchroGens = configs.get("synchronousGenerators"); - assertThat(synchroGens.getModelsName()).containsExactlyInAnyOrder( - "Wecc", + assertThat(synchroGens.getModelsName()).containsExactly( + "WT4AWeccCurrentSource", "WT4BWeccCurrentSource", - "WT4AWeccCurrentSource"); + "Wecc"); ModelConfig defaultModel = new ModelConfig("WT4BWeccCurrentSource", List.of("SYNCHRONIZED", "CONTROLLABLE")); - assertThat(listModelConfigs(synchroGens)).containsExactlyInAnyOrder( - new ModelConfig("PhotovoltaicsWeccCurrentSource", "Wecc", "WTG4A", List.of("SYNCHRONIZED"), "Photovoltaics Wecc generator", new VersionInterval(new DynawoVersion(1, 3, 0), new DynawoVersion(1, 4, 0), "Deleted")), - defaultModel, - new ModelConfig("WT4AWeccCurrentSource", null, null, Collections.emptyList(), "WT4A Wecc generator", new VersionInterval(new DynawoVersion(1, 6, 0)))); assertEquals(defaultModel, synchroGens.getDefaultModelConfig()); - assertThat(synchroGens.getModelInfos()).map(ModelInfo::formattedInfo).containsExactlyInAnyOrder( - "Wecc (PhotovoltaicsWeccCurrentSource): Photovoltaics Wecc generator (Dynawo Version 1.3.0 - 1.4.0 (Deleted))", - "WT4BWeccCurrentSource (Dynawo Version 1.5.0)", - "WT4AWeccCurrentSource: WT4A Wecc generator (Dynawo Version 1.6.0)"); - assertThat(synchroGens.getModelInfos(DynawoVersion.createFromString("1.5.0"))).map(ModelInfo::name).containsExactlyInAnyOrder("WT4BWeccCurrentSource"); + assertThat(synchroGens.getModelInfos()) + .containsExactly( + new ModelConfig("WT4AWeccCurrentSource", null, null, Collections.emptyList(), "WT4A Wecc generator", new VersionInterval(new DynawoVersion(1, 6, 0))), + defaultModel, + new ModelConfig("PhotovoltaicsWeccCurrentSource", "Wecc", "WTG4A", List.of("SYNCHRONIZED"), "Photovoltaics Wecc generator", new VersionInterval(new DynawoVersion(1, 3, 0), new DynawoVersion(1, 4, 0), "Deleted"))) + // Check formatted info + .map(ModelInfo::formattedInfo) + .containsExactly( + "WT4AWeccCurrentSource: WT4A Wecc generator (Dynawo Version 1.6.0)", + "WT4BWeccCurrentSource (Dynawo Version 1.5.0)", + "Wecc (PhotovoltaicsWeccCurrentSource): Photovoltaics Wecc generator (Dynawo Version 1.3.0 - 1.4.0 (Deleted))"); + assertThat(synchroGens.getModelInfos(DynawoVersion.createFromString("1.5.0"))) + .map(ModelInfo::name) + .hasSize(1) + .containsExactly("WT4BWeccCurrentSource"); } @Test void mergeModelConfigs() { ModelConfig defaultModel = new ModelConfig("AA"); - ModelConfigs modelConfigs1 = new ModelConfigs(new HashMap<>(Map.of(defaultModel.name(), defaultModel)), defaultModel.name()); + ModelConfigs modelConfigs1 = new ModelConfigs(new TreeMap<>(Map.of(defaultModel.name(), defaultModel)), defaultModel.name()); ModelConfig mc1 = new ModelConfig("BB"); ModelConfig mc2 = new ModelConfig("CC"); - ModelConfigs modelConfigs2 = new ModelConfigs(new HashMap<>(Map.of(mc1.name(), mc1, mc2.name(), mc2)), mc1.name()); + ModelConfigs modelConfigs2 = new ModelConfigs(new TreeMap<>(Map.of(mc1.name(), mc1, mc2.name(), mc2)), mc1.name()); modelConfigs1.addModelConfigs(modelConfigs2); - assertThat(listModelConfigs(modelConfigs1)).containsExactlyInAnyOrder( + assertThat(modelConfigs1.getModelInfos()).containsExactly( defaultModel, new ModelConfig("BB"), mc2); assertEquals(defaultModel, modelConfigs1.getDefaultModelConfig()); - ModelConfigs modelConfigs3 = new ModelConfigs(new HashMap<>(Map.of(mc2.name(), mc2)), null); - ModelConfigs modelConfigs4 = new ModelConfigs(new HashMap<>(Map.of(defaultModel.name(), defaultModel)), defaultModel.name()); + ModelConfigs modelConfigs3 = new ModelConfigs(new TreeMap<>(Map.of(mc2.name(), mc2)), null); + ModelConfigs modelConfigs4 = new ModelConfigs(new TreeMap<>(Map.of(defaultModel.name(), defaultModel)), defaultModel.name()); modelConfigs3.addModelConfigs(modelConfigs4); assertEquals(defaultModel, modelConfigs3.getDefaultModelConfig()); } - - private List listModelConfigs(ModelConfigs modelConfigs) { - return modelConfigs.getModelsName().stream() - .map(modelConfigs::getModelConfig) - .toList(); - } }