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

com.powsybl.iidm.serde.extensions.ActivePowerControlSerDe Maven / Gradle / Ivy

/**
 * Copyright (c) 2019, 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.iidm.serde.extensions;

import com.google.auto.service.AutoService;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSortedSet;
import com.powsybl.commons.extensions.ExtensionSerDe;
import com.powsybl.commons.io.DeserializerContext;
import com.powsybl.commons.io.SerializerContext;
import com.powsybl.iidm.network.Injection;
import com.powsybl.iidm.network.extensions.ActivePowerControl;
import com.powsybl.iidm.network.extensions.ActivePowerControlAdder;
import com.powsybl.iidm.network.extensions.ConnectablePosition;
import com.powsybl.iidm.serde.IidmVersion;
import com.powsybl.iidm.serde.NetworkDeserializerContext;
import com.powsybl.iidm.serde.NetworkSerializerContext;

import java.io.InputStream;
import java.util.List;

/**
 * @author Ghiles Abdellah {@literal }
 */
@AutoService(ExtensionSerDe.class)
public class ActivePowerControlSerDe> extends AbstractVersionableNetworkExtensionSerDe> {

    public ActivePowerControlSerDe() {
        super("activePowerControl", ActivePowerControl.class, "apc",
                new ImmutableMap.Builder>()
                        .put(IidmVersion.V_1_0, ImmutableSortedSet.of("1.0", "1.1"))
                        .put(IidmVersion.V_1_1, ImmutableSortedSet.of("1.0", "1.1"))
                        .put(IidmVersion.V_1_2, ImmutableSortedSet.of("1.0", "1.1"))
                        .put(IidmVersion.V_1_3, ImmutableSortedSet.of("1.0", "1.1"))
                        .put(IidmVersion.V_1_4, ImmutableSortedSet.of("1.0", "1.1"))
                        .put(IidmVersion.V_1_5, ImmutableSortedSet.of("1.0", "1.1"))
                        .put(IidmVersion.V_1_6, ImmutableSortedSet.of("1.0", "1.1"))
                        .put(IidmVersion.V_1_7, ImmutableSortedSet.of("1.0", "1.1"))
                        .put(IidmVersion.V_1_8, ImmutableSortedSet.of("1.0", "1.1"))
                        .put(IidmVersion.V_1_9, ImmutableSortedSet.of("1.0", "1.1"))
                        .put(IidmVersion.V_1_10, ImmutableSortedSet.of("1.0", "1.1"))
                        .put(IidmVersion.V_1_11, ImmutableSortedSet.of("1.0", "1.1"))
                        .put(IidmVersion.V_1_12, ImmutableSortedSet.of("1.0", "1.1"))
                        .put(IidmVersion.V_1_13, ImmutableSortedSet.of("1.2"))
                        .build(),
                new ImmutableMap.Builder()
                        .put("1.0", "http://www.itesla_project.eu/schema/iidm/ext/active_power_control/1_0")
                        .put("1.1", "http://www.powsybl.org/schema/iidm/ext/active_power_control/1_1")
                        .put("1.2", "http://www.powsybl.org/schema/iidm/ext/active_power_control/1_2")
                        .build());
    }

    @Override
    public void write(ActivePowerControl activePowerControl, SerializerContext context) {
        context.getWriter().writeBooleanAttribute("participate", activePowerControl.isParticipate());
        context.getWriter().writeDoubleAttribute("droop", activePowerControl.getDroop());
        NetworkSerializerContext networkContext = (NetworkSerializerContext) context;
        String extVersionStr = networkContext.getExtensionVersion(ConnectablePosition.NAME)
                .orElseGet(() -> getVersion(networkContext.getVersion()));
        if ("1.1".compareTo(extVersionStr) <= 0) {
            context.getWriter().writeDoubleAttribute("participationFactor", activePowerControl.getParticipationFactor());
        }
        if ("1.2".compareTo(extVersionStr) <= 0) {
            // not using writeOptionalDouble and trusting implementation convention: : writeDoubleAttribute does not write NaN values in human-readable formats JSON/XML
            context.getWriter().writeDoubleAttribute("maxTargetP", activePowerControl.getMaxTargetP().orElse(Double.NaN));
            context.getWriter().writeDoubleAttribute("minTargetP", activePowerControl.getMinTargetP().orElse(Double.NaN));
        }
    }

    @Override
    public InputStream getXsdAsStream() {
        return getClass().getResourceAsStream("/xsd/activePowerControl_V1_2.xsd");
    }

    @Override
    public List getXsdAsStreamList() {
        return List.of(getClass().getResourceAsStream("/xsd/activePowerControl_V1_2.xsd"),
                getClass().getResourceAsStream("/xsd/activePowerControl_V1_1.xsd"),
                getClass().getResourceAsStream("/xsd/activePowerControl_V1_0.xsd"));
    }

    @Override
    public ActivePowerControl read(T identifiable, DeserializerContext context) {
        boolean participate = context.getReader().readBooleanAttribute("participate");
        double droop = context.getReader().readDoubleAttribute("droop");
        double participationFactor = Double.NaN;
        double minTargetP = Double.NaN;
        double maxTargetP = Double.NaN;
        NetworkDeserializerContext networkContext = (NetworkDeserializerContext) context;
        String extVersionStr = networkContext.getExtensionVersion(this).orElseThrow(IllegalStateException::new);
        if ("1.1".compareTo(extVersionStr) <= 0) {
            participationFactor = context.getReader().readDoubleAttribute("participationFactor");
        }
        if ("1.2".compareTo(extVersionStr) <= 0) {
            // not using readOptionalDouble and trusting implementation convention: readDoubleAttribute returns Nan if attribute is absent in human-readable formats (JSON / XML)
            maxTargetP = context.getReader().readDoubleAttribute("maxTargetP");
            minTargetP = context.getReader().readDoubleAttribute("minTargetP");
        }
        context.getReader().readEndNode();
        ActivePowerControlAdder activePowerControlAdder = identifiable.newExtension(ActivePowerControlAdder.class);
        return activePowerControlAdder.withParticipate(participate)
                .withDroop(droop)
                .withParticipationFactor(participationFactor)
                .withMinTargetP(minTargetP)
                .withMaxTargetP(maxTargetP)
                .add();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy