Skip to content

Commit

Permalink
Mutualize code for onConstraint usage rules (#990)
Browse files Browse the repository at this point in the history
  • Loading branch information
bqth29 authored May 2, 2024
1 parent b016492 commit 75e1fbc
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 122 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* 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.data.cracapi.Instant;
import com.powsybl.openrao.data.cracapi.State;
import com.powsybl.openrao.data.cracapi.cnec.Cnec;
import com.powsybl.openrao.data.cracapi.usagerule.UsageMethod;

/**
* @author Thomas Bouquet {@literal <thomas.bouquet at rte-france.com>}
*/
public abstract class AbstractOnConstraintUsageRule<T extends Cnec<?>> extends AbstractUsageRule {
protected Instant instant;
protected T cnec;

protected AbstractOnConstraintUsageRule(UsageMethod usageMethod, Instant instant, T cnec) {
super(usageMethod);
this.instant = instant;
this.cnec = cnec;
}

@Override
public UsageMethod getUsageMethod(State state) {
if (state.isPreventive()) {
return state.getInstant().equals(instant) ? usageMethod : UsageMethod.UNDEFINED;
} else {
return state.getInstant().equals(instant) && state.equals(cnec.getState()) ? usageMethod : UsageMethod.UNDEFINED;
}
}

@Override
public Instant getInstant() {
return instant;
}

public T getCnec() {
return cnec;
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
AbstractOnConstraintUsageRule<?> rule = (AbstractOnConstraintUsageRule<?>) o;
return super.equals(o) && rule.getInstant().equals(instant) && rule.getCnec().equals(cnec);
}

@Override
public int hashCode() {
return cnec.hashCode() * 19 + instant.hashCode() * 47;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,57 +7,20 @@
package com.powsybl.openrao.data.cracimpl;

import com.powsybl.openrao.data.cracapi.Instant;
import com.powsybl.openrao.data.cracapi.State;
import com.powsybl.openrao.data.cracapi.cnec.AngleCnec;
import com.powsybl.openrao.data.cracapi.usagerule.OnAngleConstraint;
import com.powsybl.openrao.data.cracapi.usagerule.UsageMethod;

/**
* @author Peter Mitri {@literal <peter.mitri at rte-france.com>}
*/
public class OnAngleConstraintImpl extends AbstractUsageRule implements OnAngleConstraint {
private final Instant instant;
private final AngleCnec angleCnec;

public class OnAngleConstraintImpl extends AbstractOnConstraintUsageRule<AngleCnec> implements OnAngleConstraint {
OnAngleConstraintImpl(UsageMethod usageMethod, Instant instant, AngleCnec angleCnec) {
super(usageMethod);
this.instant = instant;
this.angleCnec = angleCnec;
super(usageMethod, instant, angleCnec);
}

@Override
public AngleCnec getAngleCnec() {
return angleCnec;
}

@Override
public Instant getInstant() {
return instant;
}

@Override
public UsageMethod getUsageMethod(State state) {
if (state.isPreventive()) {
return state.getInstant().equals(instant) ? usageMethod : UsageMethod.UNDEFINED;
} else {
return state.getInstant().equals(instant) && state.equals(this.angleCnec.getState()) ? usageMethod : UsageMethod.UNDEFINED;
}
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
OnAngleConstraintImpl rule = (OnAngleConstraintImpl) o;
return super.equals(o) && rule.getInstant().equals(instant) && rule.getAngleCnec().equals(angleCnec);
}

@Override
public int hashCode() {
return angleCnec.hashCode() * 19 + instant.hashCode() * 47;
return cnec;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,57 +7,20 @@
package com.powsybl.openrao.data.cracimpl;

import com.powsybl.openrao.data.cracapi.Instant;
import com.powsybl.openrao.data.cracapi.State;
import com.powsybl.openrao.data.cracapi.cnec.FlowCnec;
import com.powsybl.openrao.data.cracapi.usagerule.OnFlowConstraint;
import com.powsybl.openrao.data.cracapi.usagerule.UsageMethod;

/**
* @author Peter Mitri {@literal <peter.mitri at rte-france.com>}
*/
public class OnFlowConstraintImpl extends AbstractUsageRule implements OnFlowConstraint {
private final Instant instant;
private final FlowCnec flowCnec;

public class OnFlowConstraintImpl extends AbstractOnConstraintUsageRule<FlowCnec> implements OnFlowConstraint {
OnFlowConstraintImpl(UsageMethod usageMethod, Instant instant, FlowCnec flowCnec) {
super(usageMethod);
this.instant = instant;
this.flowCnec = flowCnec;
super(usageMethod, instant, flowCnec);
}

@Override
public FlowCnec getFlowCnec() {
return flowCnec;
}

@Override
public Instant getInstant() {
return instant;
}

@Override
public UsageMethod getUsageMethod(State state) {
if (state.isPreventive()) {
return state.getInstant().equals(instant) ? usageMethod : UsageMethod.UNDEFINED;
} else {
return state.getInstant().equals(instant) && state.equals(this.flowCnec.getState()) ? usageMethod : UsageMethod.UNDEFINED;
}
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
OnFlowConstraintImpl rule = (OnFlowConstraintImpl) o;
return super.equals(o) && rule.getInstant().equals(instant) && rule.getFlowCnec().equals(flowCnec);
}

@Override
public int hashCode() {
return flowCnec.hashCode() * 19 + instant.hashCode() * 47;
return cnec;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,58 +7,20 @@
package com.powsybl.openrao.data.cracimpl;

import com.powsybl.openrao.data.cracapi.Instant;
import com.powsybl.openrao.data.cracapi.State;
import com.powsybl.openrao.data.cracapi.cnec.VoltageCnec;
import com.powsybl.openrao.data.cracapi.usagerule.OnVoltageConstraint;
import com.powsybl.openrao.data.cracapi.usagerule.UsageMethod;

/**
* @author Fabrice Buscaylet {@literal <fabrice.buscaylet at artelys.com>}
*/
public class OnVoltageConstraintImpl extends AbstractUsageRule implements OnVoltageConstraint {
private final Instant instant;

private final VoltageCnec voltageCnec;

OnVoltageConstraintImpl(UsageMethod usageMethod, Instant instant, VoltageCnec angleCnec) {
super(usageMethod);
this.instant = instant;
this.voltageCnec = angleCnec;
public class OnVoltageConstraintImpl extends AbstractOnConstraintUsageRule<VoltageCnec> implements OnVoltageConstraint {
OnVoltageConstraintImpl(UsageMethod usageMethod, Instant instant, VoltageCnec voltageCnec) {
super(usageMethod, instant, voltageCnec);
}

@Override
public VoltageCnec getVoltageCnec() {
return voltageCnec;
}

@Override
public Instant getInstant() {
return instant;
}

@Override
public UsageMethod getUsageMethod(State state) {
if (state.isPreventive()) {
return state.getInstant().equals(instant) ? usageMethod : UsageMethod.UNDEFINED;
} else {
return state.getInstant().equals(instant) && state.equals(this.voltageCnec.getState()) ? usageMethod : UsageMethod.UNDEFINED;
}
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
OnVoltageConstraintImpl rule = (OnVoltageConstraintImpl) o;
return super.equals(o) && rule.getInstant().equals(instant) && rule.getVoltageCnec().equals(voltageCnec);
}

@Override
public int hashCode() {
return voltageCnec.hashCode() * 19 + instant.hashCode() * 47;
return cnec;
}
}

0 comments on commit 75e1fbc

Please sign in to comment.