Skip to content

Commit

Permalink
Merge branch 'main' into refactor_builders
Browse files Browse the repository at this point in the history
  • Loading branch information
flo-dup authored Nov 6, 2024
2 parents 379c33a + f66287a commit a914bb2
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ public class ModelConfigs {
private static final Logger LOGGER = LoggerFactory.getLogger(ModelConfigs.class);

private ModelConfig defaultModelConfig;
private final Map<String, ModelConfig> modelConfigMap;
private final SortedMap<String, ModelConfig> modelConfigMap;

ModelConfigs(Map<String, ModelConfig> modelConfigMap, String defaultModelConfigName) {
ModelConfigs(SortedMap<String, ModelConfig> modelConfigMap, String defaultModelConfigName) {
this.modelConfigMap = Objects.requireNonNull(modelConfigMap);
if (defaultModelConfigName != null) {
this.defaultModelConfig = Objects.requireNonNull(modelConfigMap.get(defaultModelConfigName));
Expand Down Expand Up @@ -58,7 +58,6 @@ Set<String> 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(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public Map<String, ModelConfigs> deserialize(JsonParser parser, DeserializationC
private static ModelConfigs parseModelConfigs(JsonParser parser) {
var parsingContext = new Object() {
String defaultLib = null;
final Map<String, ModelConfig> libs = new HashMap<>();
final SortedMap<String, ModelConfig> libs = new TreeMap<>();
};
JsonUtil.parseObject(parser, name ->
switch (name) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -68,50 +65,50 @@ void loadConfigTest() throws IOException {
objectMapper.registerModule(module);
Map<String, ModelConfigs> 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<ModelConfig> listModelConfigs(ModelConfigs modelConfigs) {
return modelConfigs.getModelsName().stream()
.map(modelConfigs::getModelConfig)
.toList();
}
}

0 comments on commit a914bb2

Please sign in to comment.