|
9 | 9 |
|
10 | 10 | import com.powsybl.commons.PowsyblException; |
11 | 11 | import com.powsybl.contingency.*; |
| 12 | +import com.powsybl.contingency.contingency.list.ContingencyList; |
12 | 13 | import com.powsybl.iidm.network.*; |
13 | 14 |
|
14 | | -import java.util.ArrayList; |
15 | | -import java.util.HashMap; |
16 | | -import java.util.List; |
17 | | -import java.util.Map; |
| 15 | +import java.nio.file.Files; |
| 16 | +import java.nio.file.Path; |
| 17 | +import java.util.*; |
18 | 18 | import java.util.stream.Collectors; |
19 | 19 |
|
20 | 20 | /** |
|
23 | 23 | public class ContingencyContainerImpl implements ContingencyContainer { |
24 | 24 |
|
25 | 25 | private final Map<String, List<String>> elementIdsByContingencyId = new HashMap<>(); |
| 26 | + private Path pathToContingencyJsonFile = null; |
26 | 27 |
|
27 | 28 | @Override |
28 | 29 | public void addContingency(String contingencyId, List<String> elementIds) { |
29 | 30 | elementIdsByContingencyId.put(contingencyId, elementIds); |
30 | 31 | } |
31 | 32 |
|
| 33 | + @Override |
| 34 | + public void addContingencyFromJsonFile(Path pathToJsonFile) { |
| 35 | + pathToContingencyJsonFile = pathToJsonFile; |
| 36 | + } |
| 37 | + |
32 | 38 | private static ContingencyElement createContingencyElement(Network network, String elementId) { |
33 | 39 | Identifiable<?> identifiable = network.getIdentifiable(elementId); |
34 | 40 | if (identifiable == null) { |
@@ -67,6 +73,21 @@ private static ContingencyElement createContingencyElement(Network network, Stri |
67 | 73 |
|
68 | 74 | protected List<Contingency> createContingencies(Network network) { |
69 | 75 | List<Contingency> contingencies = new ArrayList<>(elementIdsByContingencyId.size()); |
| 76 | + |
| 77 | + if (pathToContingencyJsonFile != null) { |
| 78 | + if (Files.exists(pathToContingencyJsonFile)) { |
| 79 | + ContingencyList contingenciesList; |
| 80 | + contingenciesList = ContingencyList.load(pathToContingencyJsonFile); |
| 81 | + |
| 82 | + for (Contingency contingency : contingenciesList.getContingencies(network)) { |
| 83 | + contingencies.add(new Contingency(contingency.getId(), contingency.getElements())); |
| 84 | + } |
| 85 | + } else { |
| 86 | + throw new PowsyblException("File not found: " + pathToContingencyJsonFile); |
| 87 | + } |
| 88 | + |
| 89 | + } |
| 90 | + |
70 | 91 | for (Map.Entry<String, List<String>> e : elementIdsByContingencyId.entrySet()) { |
71 | 92 | String contingencyId = e.getKey(); |
72 | 93 | List<String> elementIds = e.getValue(); |
|
0 commit comments