Skip to content

Commit

Permalink
Use logOrThrow method (#3204)
Browse files Browse the repository at this point in the history
  • Loading branch information
cphili committed Jan 31, 2025
1 parent 9edd6ca commit f95a7ab
Show file tree
Hide file tree
Showing 42 changed files with 248 additions and 307 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public NetworkModification toModification() {
case CONSTANT_Q -> 0d;
case CONSTANT_PQ_RATIO -> p0PercentChange;
};
return new PercentChangeLoadModification(loadId, p0PercentChange, q0PercentChange);
return new PercentChangeLoadModification(loadId, p0PercentChange, q0PercentChange, true);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
package com.powsybl.iidm.modification;

import com.powsybl.commons.report.ReportNode;
import com.powsybl.iidm.modification.util.ModificationLogs;
import com.powsybl.iidm.network.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -47,7 +48,7 @@ public void applyModification(Network network, boolean isPlanned, boolean throwE

// Disconnect the identifiable if it exists
if (identifiable == null) {
logOrThrow(throwException, "Identifiable '" + identifiableId + "' not found");
ModificationLogs.logOrThrow(throwException, "Identifiable '" + identifiableId + "' not found");
} else {
disconnectIdentifiable(identifiable, network, isPlanned, throwException, reportNode);
}
Expand Down Expand Up @@ -78,7 +79,7 @@ private boolean disconnect(Identifiable<?> identifiable, boolean throwException)
} else if (identifiable instanceof HvdcLine hvdcLine) {
hasBeenDisconnected = hvdcLine.disconnectConverterStations(openableSwitches, side == null ? null : side.toTwoSides());
} else {
logOrThrow(throwException, String.format("Disconnection not implemented for identifiable '%s'", identifiableId));
ModificationLogs.logOrThrow(throwException, String.format("Disconnection not implemented for identifiable '%s'", identifiableId));
}
return hasBeenDisconnected;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,42 +33,42 @@ public abstract class AbstractNetworkModification implements NetworkModification

@Override
public void apply(Network network) {
apply(network, new DefaultNamingStrategy(), false, LocalComputationManager.getDefault(), ReportNode.NO_OP);
apply(network, new DefaultNamingStrategy(), true, LocalComputationManager.getDefault(), ReportNode.NO_OP);
}

@Override
public boolean apply(Network network, boolean dryRun) {
return apply(network, new DefaultNamingStrategy(), false, LocalComputationManager.getDefault(), ReportNode.NO_OP, dryRun);
return apply(network, new DefaultNamingStrategy(), true, LocalComputationManager.getDefault(), ReportNode.NO_OP, dryRun);
}

@Override
public void apply(Network network, ComputationManager computationManager) {
apply(network, new DefaultNamingStrategy(), false, computationManager, ReportNode.NO_OP);
apply(network, new DefaultNamingStrategy(), true, computationManager, ReportNode.NO_OP);
}

@Override
public boolean apply(Network network, ComputationManager computationManager, boolean dryRun) {
return apply(network, new DefaultNamingStrategy(), false, computationManager, ReportNode.NO_OP, dryRun);
return apply(network, new DefaultNamingStrategy(), true, computationManager, ReportNode.NO_OP, dryRun);
}

@Override
public void apply(Network network, ComputationManager computationManager, ReportNode reportNode) {
apply(network, new DefaultNamingStrategy(), false, computationManager, reportNode);
apply(network, new DefaultNamingStrategy(), true, computationManager, reportNode);
}

@Override
public boolean apply(Network network, ComputationManager computationManager, ReportNode reportNode, boolean dryRun) {
return apply(network, new DefaultNamingStrategy(), false, computationManager, reportNode, dryRun);
return apply(network, new DefaultNamingStrategy(), true, computationManager, reportNode, dryRun);
}

@Override
public void apply(Network network, ReportNode reportNode) {
apply(network, new DefaultNamingStrategy(), false, LocalComputationManager.getDefault(), reportNode);
apply(network, new DefaultNamingStrategy(), true, LocalComputationManager.getDefault(), reportNode);
}

@Override
public boolean apply(Network network, ReportNode reportNode, boolean dryRun) {
return apply(network, new DefaultNamingStrategy(), false, LocalComputationManager.getDefault(), reportNode, dryRun);
return apply(network, new DefaultNamingStrategy(), true, LocalComputationManager.getDefault(), reportNode, dryRun);
}

@Override
Expand All @@ -93,42 +93,42 @@ public boolean apply(Network network, boolean throwException, ComputationManager

@Override
public void apply(Network network, NamingStrategy namingStrategy) {
apply(network, namingStrategy, false, LocalComputationManager.getDefault(), ReportNode.NO_OP);
apply(network, namingStrategy, true, LocalComputationManager.getDefault(), ReportNode.NO_OP);
}

@Override
public boolean apply(Network network, NamingStrategy namingStrategy, boolean dryRun) {
return apply(network, namingStrategy, false, LocalComputationManager.getDefault(), ReportNode.NO_OP, dryRun);
return apply(network, namingStrategy, true, LocalComputationManager.getDefault(), ReportNode.NO_OP, dryRun);
}

@Override
public void apply(Network network, NamingStrategy namingStrategy, ComputationManager computationManager) {
apply(network, namingStrategy, false, computationManager, ReportNode.NO_OP);
apply(network, namingStrategy, true, computationManager, ReportNode.NO_OP);
}

@Override
public boolean apply(Network network, NamingStrategy namingStrategy, ComputationManager computationManager, boolean dryRun) {
return apply(network, namingStrategy, false, computationManager, ReportNode.NO_OP, dryRun);
return apply(network, namingStrategy, true, computationManager, ReportNode.NO_OP, dryRun);
}

@Override
public void apply(Network network, NamingStrategy namingStrategy, ComputationManager computationManager, ReportNode reportNode) {
apply(network, namingStrategy, false, computationManager, reportNode);
apply(network, namingStrategy, true, computationManager, reportNode);
}

@Override
public boolean apply(Network network, NamingStrategy namingStrategy, ComputationManager computationManager, ReportNode reportNode, boolean dryRun) {
return apply(network, namingStrategy, false, computationManager, reportNode, dryRun);
return apply(network, namingStrategy, true, computationManager, reportNode, dryRun);
}

@Override
public void apply(Network network, NamingStrategy namingStrategy, ReportNode reportNode) {
apply(network, namingStrategy, false, LocalComputationManager.getDefault(), reportNode);
apply(network, namingStrategy, true, LocalComputationManager.getDefault(), reportNode);
}

@Override
public boolean apply(Network network, NamingStrategy namingStrategy, ReportNode reportNode, boolean dryRun) {
return apply(network, namingStrategy, false, LocalComputationManager.getDefault(), reportNode, dryRun);
return apply(network, namingStrategy, true, LocalComputationManager.getDefault(), reportNode, dryRun);
}

@Override
Expand Down Expand Up @@ -165,19 +165,6 @@ public boolean apply(Network network, NamingStrategy namingStrategy, boolean thr
return true;
}

/**
* Utility during apply functions, logs or throw the message.
*
* @param throwException if true will throw {@link com.powsybl.commons.PowsyblException} with the given message
*/
protected void logOrThrow(boolean throwException, String message) {
if (throwException) {
throw new PowsyblException(message);
} else {
LOGGER.warn("Error while applying modification : {}", message);
}
}

/**
* Returns the name of the network modification. That name corresponds to the type of network modification
* @return the name of the network modification
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import com.powsybl.iidm.network.Network;
import com.powsybl.iidm.network.StaticVarCompensator;
import com.powsybl.iidm.network.VscConverterStation;
import com.powsybl.iidm.modification.util.ModificationLogs;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -49,7 +50,7 @@ public void apply(Network network, NamingStrategy namingStrategy, boolean throwE
T networkElement = getNetworkElement(network, elementId);

if (networkElement == null) {
logOrThrow(throwException, getElementName() + " '" + elementId + "' not found");
ModificationLogs.logOrThrow(throwException, getElementName() + " '" + elementId + "' not found");
return;
}
if (voltageSetpoint != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import com.powsybl.commons.report.ReportNode;
import com.powsybl.computation.ComputationManager;
import com.powsybl.iidm.modification.topology.NamingStrategy;
import com.powsybl.iidm.modification.util.ModificationLogs;
import com.powsybl.iidm.network.Area;
import com.powsybl.iidm.network.Network;

Expand Down Expand Up @@ -40,7 +41,7 @@ public String getName() {
public void apply(Network network, NamingStrategy namingStrategy, boolean throwException, ComputationManager computationManager, ReportNode reportNode) {
Area area = network.getArea(areaId);
if (area == null) {
logOrThrow(throwException, "Area '" + areaId + "' not found");
ModificationLogs.logOrThrow(throwException, "Area '" + areaId + "' not found");
return;
}
area.setInterchangeTarget(interchangeTarget);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import com.powsybl.commons.report.ReportNode;
import com.powsybl.computation.ComputationManager;
import com.powsybl.iidm.modification.topology.NamingStrategy;
import com.powsybl.iidm.modification.util.ModificationLogs;
import com.powsybl.iidm.network.Battery;
import com.powsybl.iidm.network.Network;

Expand Down Expand Up @@ -42,7 +43,7 @@ public void apply(Network network, NamingStrategy namingStrategy, boolean throwE
ReportNode reportNode) {
Battery battery = network.getBattery(batteryId);
if (battery == null) {
logOrThrow(throwException, "Battery '" + batteryId + "' not found");
ModificationLogs.logOrThrow(throwException, "Battery '" + batteryId + "' not found");
return;
}
if (targetP != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
*/
package com.powsybl.iidm.modification;

import com.powsybl.commons.PowsyblException;
import com.powsybl.commons.report.ReportNode;
import com.powsybl.computation.ComputationManager;
import com.powsybl.iidm.modification.topology.NamingStrategy;
import com.powsybl.iidm.modification.util.ModificationLogs;
import com.powsybl.iidm.network.Network;
import com.powsybl.iidm.network.Switch;

Expand All @@ -36,9 +36,10 @@ public void apply(Network network, NamingStrategy namingStrategy, boolean throwE
ComputationManager computationManager, ReportNode reportNode) {
Switch sw = network.getSwitch(switchId);
if (sw == null) {
throw new PowsyblException("Switch '" + switchId + "' not found");
ModificationLogs.logOrThrow(throwException, "Switch '" + switchId + "' not found");
} else {
sw.setOpen(false);
}
sw.setOpen(false);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,15 @@
*/
package com.powsybl.iidm.modification;

import com.powsybl.commons.PowsyblException;
import com.powsybl.commons.report.ReportNode;
import com.powsybl.computation.ComputationManager;
import com.powsybl.iidm.modification.topology.NamingStrategy;
import com.powsybl.iidm.modification.util.ModificationLogs;
import com.powsybl.iidm.modification.util.VoltageRegulationUtils;
import com.powsybl.iidm.network.*;
import com.powsybl.iidm.network.Generator;
import com.powsybl.iidm.network.IdentifiableType;
import com.powsybl.iidm.network.Network;
import com.powsybl.iidm.network.Terminal;

import java.util.Objects;

Expand All @@ -36,11 +39,12 @@ public String getName() {
public void apply(Network network, NamingStrategy namingStrategy, boolean throwException,
ComputationManager computationManager, ReportNode reportNode) {
Generator g = network.getGenerator(generatorId);

if (g == null) {
throw new PowsyblException("Generator '" + generatorId + "' not found");
ModificationLogs.logOrThrow(throwException, "Generator '" + generatorId + "' not found");
} else {
connect(g);
}

connect(g);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import com.powsybl.commons.report.ReportNode;
import com.powsybl.computation.ComputationManager;
import com.powsybl.iidm.modification.topology.NamingStrategy;
import com.powsybl.iidm.modification.util.ModificationLogs;
import com.powsybl.iidm.network.*;
import com.powsybl.iidm.network.util.SwitchPredicates;
import org.slf4j.Logger;
Expand Down Expand Up @@ -72,7 +73,7 @@ public void apply(Network network, NamingStrategy namingStrategy, boolean throwE

// Connect the element if it exists
if (identifiable == null) {
logOrThrow(throwException, "Identifiable '" + identifiableId + "' not found");
ModificationLogs.logOrThrow(throwException, "Identifiable '" + identifiableId + "' not found");
} else {
connectIdentifiable(identifiable, network, throwException, reportNode);
}
Expand All @@ -88,7 +89,7 @@ private void connectIdentifiable(Identifiable<?> identifiable, Network network,
} else if (identifiable instanceof HvdcLine hvdcLine) {
hasBeenConnected = hvdcLine.connectConverterStations(isTypeSwitchToOperate, side == null ? null : side.toTwoSides());
} else {
logOrThrow(throwException, String.format("Connection not implemented for identifiable '%s'", identifiableId));
ModificationLogs.logOrThrow(throwException, String.format("Connection not implemented for identifiable '%s'", identifiableId));
}
} finally {
network.getReportNodeContext().popReportNode();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import com.powsybl.commons.report.ReportNode;
import com.powsybl.computation.ComputationManager;
import com.powsybl.iidm.modification.topology.NamingStrategy;
import com.powsybl.iidm.modification.util.ModificationLogs;
import com.powsybl.iidm.network.DanglingLine;
import com.powsybl.iidm.network.Network;

Expand Down Expand Up @@ -53,7 +54,7 @@ public void apply(Network network, NamingStrategy namingStrategy, boolean throwE
ReportNode reportNode) {
DanglingLine danglingLine = network.getDanglingLine(getDanglingLineId());
if (danglingLine == null) {
logOrThrow(throwException, "DanglingLine '" + getDanglingLineId() + "' not found");
ModificationLogs.logOrThrow(throwException, "DanglingLine '" + getDanglingLineId() + "' not found");
return;
}
getP0().ifPresent(value -> danglingLine.setP0((isRelativeValue() ? danglingLine.getP0() : 0) + value));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import com.powsybl.commons.report.ReportNode;
import com.powsybl.computation.ComputationManager;
import com.powsybl.iidm.modification.topology.NamingStrategy;
import com.powsybl.iidm.modification.util.ModificationLogs;
import com.powsybl.iidm.modification.util.VoltageRegulationUtils;
import com.powsybl.iidm.network.Generator;
import com.powsybl.iidm.network.IdentifiableType;
Expand Down Expand Up @@ -40,7 +41,7 @@ public void apply(Network network, NamingStrategy namingStrategy, boolean throwE
ComputationManager computationManager, ReportNode reportNode) {
Generator g = network.getGenerator(generatorId);
if (g == null) {
logOrThrow(throwException, "Generator '" + generatorId + "' not found");
ModificationLogs.logOrThrow(throwException, "Generator '" + generatorId + "' not found");
return;
}
if (modifs.getMinP() != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import com.powsybl.commons.report.ReportNode;
import com.powsybl.computation.ComputationManager;
import com.powsybl.iidm.modification.topology.NamingStrategy;
import com.powsybl.iidm.modification.util.ModificationLogs;
import com.powsybl.iidm.network.HvdcLine;
import com.powsybl.iidm.network.Network;
import com.powsybl.iidm.network.extensions.HvdcAngleDroopActivePowerControl;
Expand Down Expand Up @@ -54,7 +55,7 @@ public void apply(Network network, NamingStrategy namingStrategy, boolean throwE
ReportNode reportNode) {
HvdcLine hvdcLine = network.getHvdcLine(hvdcId);
if (hvdcLine == null) {
logOrThrow(throwException, "HvdcLine '" + hvdcId + "' not found");
ModificationLogs.logOrThrow(throwException, "HvdcLine '" + hvdcId + "' not found");
return;
}
if (activePowerSetpoint != null) {
Expand Down
Loading

0 comments on commit f95a7ab

Please sign in to comment.