-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
PostContingencyResult status fix (#405)
Parse aggregatedResults.xml and use it for post contingency results Signed-off-by: lisrte <[email protected]>
- Loading branch information
Showing
20 changed files
with
402 additions
and
75 deletions.
There are no files selected for viewing
74 changes: 74 additions & 0 deletions
74
commons/src/main/java/com/powsybl/dynawo/commons/AbstractXmlParser.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/** | ||
* 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/. | ||
* SPDX-License-Identifier: MPL-2.0 | ||
*/ | ||
package com.powsybl.dynawo.commons; | ||
|
||
import com.powsybl.commons.exceptions.UncheckedXmlStreamException; | ||
|
||
import javax.xml.XMLConstants; | ||
import javax.xml.stream.XMLInputFactory; | ||
import javax.xml.stream.XMLStreamException; | ||
import javax.xml.stream.XMLStreamReader; | ||
import java.io.IOException; | ||
import java.io.Reader; | ||
import java.io.UncheckedIOException; | ||
import java.nio.charset.StandardCharsets; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Objects; | ||
import java.util.function.Consumer; | ||
|
||
/** | ||
* @author Laurent Issertial {@literal <laurent.issertial at rte-france.com>} | ||
*/ | ||
public abstract class AbstractXmlParser<T> { | ||
|
||
public List<T> parse(Path path) { | ||
List<T> series = new ArrayList<>(); | ||
parse(path, series::add); | ||
return series; | ||
} | ||
|
||
public void parse(Path path, Consumer<T> consumer) { | ||
Objects.requireNonNull(path); | ||
if (!Files.exists(path)) { | ||
return; | ||
} | ||
try (Reader reader = Files.newBufferedReader(path, StandardCharsets.UTF_8)) { | ||
parse(reader, consumer); | ||
} catch (IOException e) { | ||
throw new UncheckedIOException(e); | ||
} catch (XMLStreamException e) { | ||
throw new UncheckedXmlStreamException(e); | ||
} | ||
} | ||
|
||
public List<T> parse(Reader reader) throws XMLStreamException { | ||
List<T> series = new ArrayList<>(); | ||
parse(reader, series::add); | ||
return series; | ||
} | ||
|
||
public void parse(Reader reader, Consumer<T> consumer) throws XMLStreamException { | ||
XMLInputFactory factory = XMLInputFactory.newInstance(); | ||
factory.setProperty(XMLConstants.ACCESS_EXTERNAL_DTD, ""); | ||
factory.setProperty(XMLConstants.ACCESS_EXTERNAL_SCHEMA, ""); | ||
XMLStreamReader xmlReader = null; | ||
try { | ||
xmlReader = factory.createXMLStreamReader(reader); | ||
read(xmlReader, consumer); | ||
} finally { | ||
if (xmlReader != null) { | ||
xmlReader.close(); | ||
} | ||
} | ||
} | ||
|
||
protected abstract void read(XMLStreamReader reader, Consumer<T> consumer) throws XMLStreamException; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
dynaflow/src/main/java/com/powsybl/dynaflow/results/FailedCriterion.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
/** | ||
* 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/. | ||
* SPDX-License-Identifier: MPL-2.0 | ||
*/ | ||
package com.powsybl.dynaflow.results; | ||
|
||
/** | ||
* scenarioResults or loadIncreaseResults failed criterion | ||
* @param description Failed criterion description | ||
* @param time Failure time (in seconds) | ||
* @author Laurent Issertial {@literal <laurent.issertial at rte-france.com>} | ||
*/ | ||
public record FailedCriterion(String description, double time) { | ||
} |
77 changes: 77 additions & 0 deletions
77
dynaflow/src/main/java/com/powsybl/dynaflow/results/ResultsUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/** | ||
* 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/. | ||
* SPDX-License-Identifier: MPL-2.0 | ||
*/ | ||
package com.powsybl.dynaflow.results; | ||
|
||
import com.powsybl.security.PostContingencyComputationStatus; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
import static com.powsybl.dynaflow.results.Status.CRITERIA_NON_RESPECTED; | ||
import static com.powsybl.security.PostContingencyComputationStatus.*; | ||
|
||
/** | ||
* @author Laurent Issertial {@literal <laurent.issertial at rte-france.com>} | ||
*/ | ||
public final class ResultsUtil { | ||
|
||
private ResultsUtil() { | ||
} | ||
|
||
private static final Logger LOGGER = LoggerFactory.getLogger(ResultsUtil.class); | ||
|
||
public static PostContingencyComputationStatus convertStatus(Status status) { | ||
return switch (status) { | ||
case CONVERGENCE -> CONVERGED; | ||
case DIVERGENCE -> SOLVER_FAILED; | ||
case EXECUTION_PROBLEM, CRITERIA_NON_RESPECTED -> FAILED; | ||
}; | ||
} | ||
|
||
static Optional<ScenarioResult> createScenarioResult(String id, String status, List<FailedCriterion> failedCriteria) { | ||
if (id == null || status == null || failedCriteria == null) { | ||
LOGGER.warn("Inconsistent scenario result entry (id: '{}', status: '{}', failedCriteria: '{}')", id, status, failedCriteria); | ||
} else { | ||
try { | ||
Status statusE = Status.valueOf(status); | ||
boolean isCriterionError = CRITERIA_NON_RESPECTED == statusE; | ||
if (isCriterionError && failedCriteria.isEmpty()) { | ||
LOGGER.warn("ScenarioResult with {} status should have failed criteria", status); | ||
return Optional.empty(); | ||
} else if (!isCriterionError && !failedCriteria.isEmpty()) { | ||
LOGGER.warn("ScenarioResult with {} status should not have failed criteria", status); | ||
return Optional.empty(); | ||
} | ||
return Optional.of(new ScenarioResult(id, statusE, failedCriteria)); | ||
} catch (IllegalArgumentException e) { | ||
logInconsistentEntry("status", status); | ||
} | ||
} | ||
return Optional.empty(); | ||
} | ||
|
||
static Optional<FailedCriterion> createFailedCriterion(String description, String time) { | ||
if (description == null || time == null) { | ||
LOGGER.warn("Inconsistent failed criterion entry (description: '{}', time: '{}')", description, time); | ||
} else { | ||
try { | ||
double timeD = Double.parseDouble(time); | ||
return Optional.of(new FailedCriterion(description, timeD)); | ||
} catch (NumberFormatException e) { | ||
logInconsistentEntry("time", time); | ||
} | ||
} | ||
return Optional.empty(); | ||
} | ||
|
||
private static void logInconsistentEntry(String fieldName, String message) { | ||
LOGGER.warn("Inconsistent {} entry '{}'", fieldName, message); | ||
} | ||
} |
Oops, something went wrong.