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

com.powsybl.openrao.data.cracimpl.OnFlowConstraintInCountryAdderImpl Maven / Gradle / Ivy

The 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.cracimpl;

import com.powsybl.contingency.Contingency;
import com.powsybl.openrao.commons.OpenRaoException;
import com.powsybl.openrao.data.cracapi.Instant;
import com.powsybl.openrao.data.cracapi.usagerule.OnFlowConstraintInCountry;
import com.powsybl.openrao.data.cracapi.usagerule.OnFlowConstraintInCountryAdder;
import com.powsybl.openrao.data.cracapi.usagerule.UsageMethod;
import com.powsybl.iidm.network.Country;

import java.util.Optional;

import static com.powsybl.openrao.data.cracimpl.AdderUtils.assertAttributeNotNull;

/**
 * @author Peter Mitri {@literal }
 */
public class OnFlowConstraintInCountryAdderImpl> implements OnFlowConstraintInCountryAdder {

    public static final String ON_FLOW_CONSTRAINT_IN_COUNTRY = "OnFlowConstraintInCountry";
    private T owner;
    private String instantId;
    private String contingencyId;
    private Country country;
    private UsageMethod usageMethod;

    OnFlowConstraintInCountryAdderImpl(AbstractRemedialActionAdder owner) {
        this.owner = (T) owner;
    }

    @Override
    public OnFlowConstraintInCountryAdder withInstant(String instantId) {
        this.instantId = instantId;
        return this;
    }

    @Override
    public OnFlowConstraintInCountryAdder withContingency(String contingencyId) {
        this.contingencyId = contingencyId;
        return this;
    }

    @Override
    public OnFlowConstraintInCountryAdder withUsageMethod(UsageMethod usageMethod) {
        this.usageMethod = usageMethod;
        return this;
    }

    @Override
    public OnFlowConstraintInCountryAdder withCountry(Country country) {
        this.country = country;
        return this;
    }

    @Override
    public T add() {
        assertAttributeNotNull(instantId, ON_FLOW_CONSTRAINT_IN_COUNTRY, "instant", "withInstant()");
        assertAttributeNotNull(country, ON_FLOW_CONSTRAINT_IN_COUNTRY, "country", "withCountry()");
        assertAttributeNotNull(usageMethod, ON_FLOW_CONSTRAINT_IN_COUNTRY, "usage method", "withUsageMethod()");

        Instant instant = owner.getCrac().getInstant(instantId);
        if (instant.isOutage()) {
            throw new OpenRaoException("OnFlowConstraintInCountry usage rules are not allowed for OUTAGE instant.");
        }
        if (instant.isPreventive()) {
            owner.getCrac().addPreventiveState();
        }

        Optional optionalContingency = Optional.empty();
        if (contingencyId != null) {
            Contingency contingency = owner.getCrac().getContingency(contingencyId);
            if (contingency == null) {
                throw new OpenRaoException(String.format("Contingency %s of OnFlowConstraintInCountry usage rule does not exist in the crac. Use crac.newContingency() first.", contingencyId));
            }
            optionalContingency = Optional.of(contingency);
        }

        OnFlowConstraintInCountry onFlowConstraint = new OnFlowConstraintInCountryImpl(usageMethod, instant, optionalContingency, country);
        owner.addUsageRule(onFlowConstraint);
        return owner;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy