All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.powsybl.openrao.data.crac.io.cim.parameters.JsonCimCracCreationParameters Maven / Gradle / Ivy

There is a newer version: 6.2.1
Show newest version
/*
 * Copyright (c) 2022, 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/.
 */
package com.powsybl.openrao.data.crac.io.cim.parameters;

import com.powsybl.openrao.commons.OpenRaoException;
import com.powsybl.openrao.data.crac.api.parameters.JsonCracCreationParameters;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonToken;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.google.auto.service.AutoService;

import java.io.IOException;
import java.util.*;

/**
 * @author Baptiste Seguinot {@literal }
 */
@AutoService(JsonCracCreationParameters.ExtensionSerializer.class)
public class JsonCimCracCreationParameters implements JsonCracCreationParameters.ExtensionSerializer {

    private static final String RANGE_ACTION_GROUPS = "range-action-groups";
    private static final String RANGE_ACTION_SPEEDS = "range-action-speeds";
    private static final String RANGE_ACTION_ID = "range-action-id";
    private static final String SPEED = "speed";
    private static final String TIMESERIES_MRIDS = "timeseries-mrids";
    private static final String VOLTAGE_CNECS_CREATION_PARAMETERS = "voltage-cnecs-creation-parameters";

    @Override
    public void serialize(CimCracCreationParameters cimParameters, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
        jsonGenerator.writeStartObject();
        serializeTimeseriesMrids(cimParameters.getTimeseriesMrids(), jsonGenerator);
        serializeRangeActionGroups(cimParameters.getRangeActionGroupsAsString(), jsonGenerator);
        serializeRangeActionSpeedSet(cimParameters.getRangeActionSpeedSet(), jsonGenerator);
        serializeVoltageCnecsCreationParameters(cimParameters.getVoltageCnecsCreationParameters(), jsonGenerator);
        jsonGenerator.writeEndObject();
    }

    @Override
    public CimCracCreationParameters deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException {
        return deserializeAndUpdate(jsonParser, deserializationContext, new CimCracCreationParameters());
    }

    @Override
    public CimCracCreationParameters deserializeAndUpdate(JsonParser jsonParser, DeserializationContext deserializationContext, CimCracCreationParameters parameters) throws IOException {
        while (!jsonParser.nextToken().isStructEnd()) {
            switch (jsonParser.getCurrentName()) {
                case TIMESERIES_MRIDS:
                    jsonParser.nextToken();
                    parameters.setTimeseriesMrids(jsonParser.readValueAs(Set.class));
                    break;
                case RANGE_ACTION_GROUPS:
                    jsonParser.nextToken();
                    parameters.setRangeActionGroupsAsString(jsonParser.readValueAs(ArrayList.class));
                    break;
                case RANGE_ACTION_SPEEDS:
                    jsonParser.nextToken();
                    parameters.setRemedialActionSpeed(deserializeRangeActionSpeedSet(jsonParser));
                    break;
                case VOLTAGE_CNECS_CREATION_PARAMETERS:
                    jsonParser.nextToken();
                    parameters.setVoltageCnecsCreationParameters(JsonVoltageCnecsCreationParameters.deserialize(jsonParser));
                    break;
                default:
                    throw new OpenRaoException("Unexpected field: " + jsonParser.getCurrentName());
            }
        }

        return parameters;
    }

    @Override
    public String getExtensionName() {
        return "CimCracCreatorParameters";
    }

    @Override
    public String getCategoryName() {
        return "crac-creation-parameters";
    }

    @Override
    public Class getExtensionClass() {
        return CimCracCreationParameters.class;
    }

    private void serializeTimeseriesMrids(Set timeseriesMrids, JsonGenerator jsonGenerator) throws IOException {
        if (!timeseriesMrids.isEmpty()) {
            serializeStringArray(TIMESERIES_MRIDS, timeseriesMrids.stream().sorted(String::compareTo).toList(), jsonGenerator);
        }
    }

    private void serializeRangeActionGroups(List rangeActionGroupsAsString, JsonGenerator jsonGenerator) throws IOException {
        serializeStringArray(RANGE_ACTION_GROUPS, rangeActionGroupsAsString, jsonGenerator);
    }

    private void serializeRangeActionSpeedSet(Set rangeActionSpeedSet, JsonGenerator jsonGenerator) throws IOException {
        if (!rangeActionSpeedSet.isEmpty()) {
            jsonGenerator.writeFieldName(RANGE_ACTION_SPEEDS);
            jsonGenerator.writeStartArray();
            for (RangeActionSpeed rangeActionSpeeds : rangeActionSpeedSet) {
                jsonGenerator.writeStartObject();
                jsonGenerator.writeStringField(RANGE_ACTION_ID, rangeActionSpeeds.getRangeActionId());
                jsonGenerator.writeNumberField(SPEED, rangeActionSpeeds.getSpeed());
                jsonGenerator.writeEndObject();
            }
            jsonGenerator.writeEndArray();
        }
    }

    private void serializeVoltageCnecsCreationParameters(VoltageCnecsCreationParameters voltageCnecsCreationParameters, JsonGenerator jsonGenerator) throws IOException {
        if (voltageCnecsCreationParameters != null) {
            jsonGenerator.writeFieldName(VOLTAGE_CNECS_CREATION_PARAMETERS);
            JsonVoltageCnecsCreationParameters.serialize(voltageCnecsCreationParameters, jsonGenerator);
        }
    }

    private void serializeStringArray(String fieldName, List content, JsonGenerator jsonGenerator) throws IOException {
        if (!content.isEmpty()) {
            jsonGenerator.writeArrayFieldStart(fieldName);
            for (String string : content) {
                jsonGenerator.writeString(string);
            }
            jsonGenerator.writeEndArray();
        }
    }

    private Set deserializeRangeActionSpeedSet(JsonParser jsonParser) throws IOException {
        Set ids = new HashSet<>();
        Set set = new HashSet<>();
        while (jsonParser.nextToken() != JsonToken.END_ARRAY) {
            String rangeActionId = null;
            Integer speed = null;
            while (!jsonParser.nextToken().isStructEnd()) {
                switch (jsonParser.getCurrentName()) {
                    case RANGE_ACTION_ID:
                        rangeActionId = jsonParser.nextTextValue();
                        break;
                    case SPEED:
                        speed = jsonParser.nextIntValue(Integer.MAX_VALUE);
                        break;
                    default:
                        throw new OpenRaoException("Unexpected field: " + jsonParser.getCurrentName());
                }
            }
            if (rangeActionId == null) {
                throw new OpenRaoException(String.format("Missing range action ID in %s", RANGE_ACTION_SPEEDS));
            }
            if (speed == null) {
                throw new OpenRaoException(String.format("Missing speed in %s", RANGE_ACTION_SPEEDS));
            }
            if (ids.contains(rangeActionId)) {
                throw new OpenRaoException(String.format("Range action %s has two or more associated %s", rangeActionId, SPEED));
            }
            ids.add(rangeActionId);
            set.add(new RangeActionSpeed(rangeActionId, speed));
        }
        return set;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy