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

com.farao_community.farao.data.crac_impl.AbstractCnecAdderImpl Maven / Gradle / Ivy

There is a newer version: 5.0.0
Show newest version
/*
 * Copyright (c) 2020, 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.farao_community.farao.data.crac_impl;

import com.farao_community.farao.commons.FaraoException;
import com.farao_community.farao.data.crac_api.*;
import com.farao_community.farao.data.crac_api.cnec.CnecAdder;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

import static java.lang.String.format;

/**
 * @author Joris Mancini {@literal }
 */
public abstract class AbstractCnecAdderImpl> extends AbstractIdentifiableAdder implements CnecAdder {

    protected CracImpl owner;
    protected Map networkElementsIdAndName = new HashMap<>();
    protected Instant instant;
    protected String contingencyId;
    protected boolean optimized = false;
    protected boolean monitored = false;
    protected double reliabilityMargin = .0;
    protected String operator;

    protected AbstractCnecAdderImpl(CracImpl owner) {
        Objects.requireNonNull(owner);
        this.owner = owner;
    }

    protected void checkCnec() {
        checkId();
        AdderUtils.assertAttributeNotEmpty(networkElementsIdAndName.entrySet(), "Cnec", "network element", "withNetworkElement()");
        AdderUtils.assertAttributeNotNull(instant, "Cnec", "instant", "withInstant()");

        if (instant.equals(Instant.PREVENTIVE)) {
            if (contingencyId != null) {
                throw new FaraoException("You cannot define a contingency for a preventive cnec.");
            }
        } else {
            if (contingencyId == null) {
                throw new FaraoException("You must define a contingency for a non-preventive cnec.");
            } else if (owner.getContingency(contingencyId) == null) {
                throw new FaraoException(String.format("Contingency %s of Cnec %s does not exist in the crac. Use crac.newContingency() first.", contingencyId, id));
            }
        }
        networkElementsIdAndName.entrySet().forEach(entry -> this.owner.addNetworkElement(entry.getKey(), entry.getValue()));

        if (owner.getCnec(id) != null) {
            throw new FaraoException(format("Cannot add a cnec with an already existing ID - %s.", id));
        }
    }

    protected State getState() {
        State state;
        if (instant != Instant.PREVENTIVE) {
            state = owner.addState(owner.getContingency(contingencyId), instant);
        } else {
            state = owner.addPreventiveState();
        }
        return state;
    }

    @Override
    public J withInstant(Instant instant) {
        this.instant = instant;
        return (J) this;
    }

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

    @Override
    public J withReliabilityMargin(double reliabilityMargin) {
        this.reliabilityMargin = reliabilityMargin;
        return (J) this;
    }

    @Override
    public J withNetworkElement(String networkElementId, String networkElementName) {
        this.networkElementsIdAndName.put(networkElementId, networkElementName);
        return (J) this;
    }

    @Override
    public J withNetworkElement(String networkElementId) {
        return withNetworkElement(networkElementId, networkElementId);
    }

    @Override
    public J withOperator(String operator) {
        this.operator = operator;
        return (J) this;
    }

    @Override
    public J withOptimized() {
        return withOptimized(true);
    }

    @Override
    public J withMonitored() {
        return withMonitored(true);
    }

    @Override
    public J withOptimized(boolean optimized) {
        this.optimized = optimized;
        return (J) this;
    }

    @Override
    public J withMonitored(boolean monitored) {
        this.monitored = monitored;
        return (J) this;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy