com.powsybl.sensitivity.json.JsonSensitivityAnalysisParameters Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of powsybl-sensitivity-analysis-api Show documentation
Show all versions of powsybl-sensitivity-analysis-api Show documentation
An API, and a tool to run sensitivity analysis
/**
* Copyright (c) 2021, RTE (http://www.rte-france.com)
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
* SPDX-License-Identifier: MPL-2.0
*/
package com.powsybl.sensitivity.json;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.powsybl.commons.extensions.ExtensionJsonSerializer;
import com.powsybl.commons.extensions.ExtensionProvider;
import com.powsybl.commons.json.JsonUtil;
import com.powsybl.commons.util.ServiceLoaderCache;
import com.powsybl.sensitivity.SensitivityAnalysisParameters;
import com.powsybl.sensitivity.SensitivityAnalysisProvider;
import java.io.IOException;
import java.io.InputStream;
import java.io.UncheckedIOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;
/**
* @author Sebastien Murgey {@literal }
*/
public final class JsonSensitivityAnalysisParameters {
public static Map getExtensionSerializers() {
List providers = new ServiceLoaderCache<>(SensitivityAnalysisProvider.class).getServices();
return providers.stream()
.flatMap(securityAnalysisProvider -> securityAnalysisProvider.getSpecificParametersSerializer().stream())
.collect(Collectors.toMap(ExtensionProvider::getExtensionName,
securityAnalysisProvider -> securityAnalysisProvider));
}
private JsonSensitivityAnalysisParameters() {
}
public static SensitivityAnalysisParameters read(Path jsonFile) {
return update(new SensitivityAnalysisParameters(), jsonFile);
}
/**
* Reads parameters from a JSON file (will NOT rely on platform config).
*/
public static SensitivityAnalysisParameters read(InputStream jsonStream) {
return update(new SensitivityAnalysisParameters(), jsonStream);
}
/**
* Updates parameters by reading the content of a JSON file.
*/
public static SensitivityAnalysisParameters update(SensitivityAnalysisParameters parameters, Path jsonFile) {
Objects.requireNonNull(jsonFile);
try (InputStream is = Files.newInputStream(jsonFile)) {
return update(parameters, is);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
/**
* Updates parameters by reading the content of a JSON stream.
*/
public static SensitivityAnalysisParameters update(SensitivityAnalysisParameters parameters, InputStream jsonStream) {
try {
ObjectMapper objectMapper = createObjectMapper();
return objectMapper.readerForUpdating(parameters).readValue(jsonStream);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
public static ObjectMapper createObjectMapper() {
return JsonUtil.createObjectMapper()
.registerModule(new SensitivityJsonModule());
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy