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

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

The newest version!
/*
 * Copyright (c) 2024, 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.openrao.commons.OpenRaoException;
import com.powsybl.openrao.data.cracapi.Instant;
import com.powsybl.openrao.data.cracapi.cnec.Cnec;
import com.powsybl.openrao.data.cracapi.usagerule.OnConstraint;
import com.powsybl.openrao.data.cracapi.usagerule.OnConstraintAdder;
import com.powsybl.openrao.data.cracapi.usagerule.UsageMethod;

import java.util.Objects;

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

/**
 * @author Thomas Bouquet 
 */
public class OnConstraintAdderImpl, S extends Cnec> implements OnConstraintAdder {
    public static final String ON_CONSTRAINT = "OnConstraint";
    private final T owner;
    private String instantId;
    private String cnecId;
    private UsageMethod usageMethod;

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

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

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

    @Override
    public OnConstraintAdderImpl withCnec(String cnecId) {
        this.cnecId = cnecId;
        return this;
    }

    @Override
    public T add() {
        assertAttributeNotNull(instantId, ON_CONSTRAINT, "instant", "withInstant()");
        assertAttributeNotNull(cnecId, ON_CONSTRAINT, "cnec", "withCnec()");
        assertAttributeNotNull(usageMethod, ON_CONSTRAINT, "usage method", "withUsageMethod()");

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

        S cnec = (S) owner.getCrac().getCnec(cnecId);
        if (Objects.isNull(cnec)) {
            throw new OpenRaoException(String.format("Cnec %s does not exist in crac. Consider adding it first.", cnecId));
        }

        AbstractRemedialActionAdder.checkOnConstraintUsageRules(instant, cnec);

        OnConstraint onConstraint = new OnConstraintImpl<>(usageMethod, instant, cnec);
        owner.addUsageRule(onConstraint);
        return owner;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy