Skip to content

Commit

Permalink
Better reports for full dry-run
Browse files Browse the repository at this point in the history
Signed-off-by: Olivier Perrin <olivier.perrin@rte-france.com>
  • Loading branch information
olperr1 committed May 16, 2024
1 parent 28a2794 commit 6337579
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,39 +43,37 @@ public void apply(Network network, NamingStrategy namingStrategy, boolean throwE
}

public boolean fullDryRun(Network network) {
return fullDryRun(network, new DefaultNamingStrategy(), LocalComputationManager.getDefault(), ReportNode.NO_OP);
return fullDryRun(network, ReportNode.NO_OP);
}

public boolean fullDryRun(Network network, ReportNode reportNode) {
return fullDryRun(network, new DefaultNamingStrategy(), LocalComputationManager.getDefault(), reportNode);
}

public boolean fullDryRun(Network network, NamingStrategy namingStrategy,
ComputationManager computationManager, ReportNode reportNode) {
String templateKey = "networkModificationsDryRun-start";
String messageTemplate = "DRY-RUN: Checking if network modifications can be applied on network ${networkNameOrId}";
ReportNode childReportNode = reportNode.newReportNode()
String templateKey = "networkModificationsDryRun";
String messageTemplate = "Dry-run: Checking if network modifications can be applied on network ${networkNameOrId}";
ReportNode dryRunReportNode = reportNode.newReportNode()
.withMessageTemplate(templateKey, messageTemplate)
.withUntypedValue("networkNameOrId", network.getNameOrId())
.withSeverity(TypedValue.INFO_SEVERITY)
.add();

ReportNode dryRunReportNode = ReportNode.newRootReportNode()
.withMessageTemplate(templateKey, messageTemplate)
.withUntypedValue("networkNameOrId", network.getNameOrId())
.build();
try {
//TODO The following copy performs an XML export/import. It will be more performant to change it to the BIN format.
Network dryRunNetwork = NetworkSerDe.copy(network);
dryRunNetwork.setName(network.getNameOrId() + "_Dry-run");
apply(dryRunNetwork, namingStrategy, true, computationManager, dryRunReportNode);
} catch (PowsyblException powsyblException) {
childReportNode.include(dryRunReportNode);
childReportNode.newReportNode()
dryRunReportNode.newReportNode()
.withMessageTemplate("networkModificationsDryRun-failure",
"DRY-RUN: Network modifications CANNOT successfully be applied on network ${networkNameOrId}")
"Dry-run failed. Error message is: ${dryRunError}")
.withUntypedValue("dryRunError", powsyblException.getMessage())
.withSeverity(TypedValue.INFO_SEVERITY)
.add();
return false;
}
childReportNode.include(dryRunReportNode);
childReportNode.newReportNode()
dryRunReportNode.newReportNode()
.withMessageTemplate("networkModificationsDryRun-success",
"DRY-RUN: Network modifications can successfully be applied on network ${networkNameOrId}")
.withSeverity(TypedValue.INFO_SEVERITY)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
package com.powsybl.iidm.modification;

import com.powsybl.commons.PowsyblException;
import com.powsybl.commons.report.ReportNode;
import com.powsybl.commons.test.TestUtil;
import com.powsybl.iidm.modification.topology.RemoveFeederBay;
import com.powsybl.iidm.modification.topology.RemoveFeederBayBuilder;
import com.powsybl.iidm.network.Network;
Expand All @@ -19,6 +21,9 @@
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.io.StringWriter;

import static org.junit.jupiter.api.Assertions.assertFalse;

/**
Expand Down Expand Up @@ -51,7 +56,7 @@ void test() {
}

@Test
void applicationFailureTest() {
void applicationFailureTest() throws IOException {
String lineId = "NHV1_NHV2_1";
assertTrue(network.getLine(lineId).getTerminal1().isConnected());
assertTrue(network.getLine(lineId).getTerminal2().isConnected());
Expand All @@ -64,13 +69,23 @@ void applicationFailureTest() {
BranchTripping tripping = new BranchTripping(lineId, "VLHV1");
NetworkModificationList task = new NetworkModificationList(removal, tripping);

boolean dryRunIsOk = Assertions.assertDoesNotThrow(() -> task.fullDryRun(network));
ReportNode reportNode = ReportNode.newRootReportNode().withMessageTemplate("test", "test reportNode").build();
boolean dryRunIsOk = Assertions.assertDoesNotThrow(() -> task.fullDryRun(network, reportNode));
// The full dry-run returns that a problem was encountered and that the full NetworkModificationList could not be performed.
// No operation was applied on the network.
assertFalse(dryRunIsOk);
assertNotNull(network.getLine("NHV1_NHV2_1"));
assertTrue(network.getLine("NHV1_NHV2_1").getTerminal1().isConnected());

StringWriter sw1 = new StringWriter();
reportNode.print(sw1);
assertEquals("""
+ test reportNode
+ Dry-run: Checking if network modifications can be applied on network sim1
Connectable NHV1_NHV2_1 removed
Dry-run failed. Error message is: Branch 'NHV1_NHV2_1' not found
""", TestUtil.normalizeLineSeparator(sw1.toString()));

// If we ignore the dry-run result and try to apply the NetworkModificationList, an exception is thrown and
// the network is in an "unstable" state.
Assertions.assertThrows(PowsyblException.class, () -> task.apply(network), "Branch '" + lineId + "' not found");
Expand Down

0 comments on commit 6337579

Please sign in to comment.