|
| 1 | +/** |
| 2 | + * Copyright (c) 2016, RTE (http://www.rte-france.com) |
| 3 | + * This Source Code Form is subject to the terms of the Mozilla Public |
| 4 | + * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 5 | + * file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 6 | + * SPDX-License-Identifier: MPL-2.0 |
| 7 | + */ |
| 8 | +package com.powsybl.iidm.serde; |
| 9 | + |
| 10 | +import com.powsybl.commons.extensions.ExtensionSerDe; |
| 11 | +import com.powsybl.commons.io.DeserializerContext; |
| 12 | +import com.powsybl.commons.io.TreeDataReader; |
| 13 | +import com.powsybl.commons.report.ReportNode; |
| 14 | +import com.powsybl.iidm.network.Network; |
| 15 | +import com.powsybl.iidm.network.ValidationLevel; |
| 16 | +import com.powsybl.iidm.network.util.Networks; |
| 17 | +import com.powsybl.iidm.serde.anonymizer.Anonymizer; |
| 18 | + |
| 19 | +import java.util.*; |
| 20 | + |
| 21 | +import static com.powsybl.iidm.serde.IidmSerDeConstants.CURRENT_IIDM_VERSION; |
| 22 | + |
| 23 | +/** |
| 24 | + * @author Geoffroy Jamgotchian {@literal <geoffroy.jamgotchian at rte-france.com>} |
| 25 | + */ |
| 26 | +//FIXME: Fixes performance regression (https://github.com/powsybl/powsybl-core/pull/3469). This file has to be removed on Powsybl upgrade 6.7.2 or 6.8.0 |
| 27 | +public class NetworkDeserializerContext extends AbstractNetworkSerDeContext<ImportOptions> implements DeserializerContext { |
| 28 | + |
| 29 | + private final TreeDataReader reader; |
| 30 | + |
| 31 | + private final List<DeserializationEndTask> endTasks = new ArrayList<>(); |
| 32 | + private final ImportOptions options; |
| 33 | + |
| 34 | + private final Map<String, String> extensionVersions; |
| 35 | + |
| 36 | + private ValidationLevel networkValidationLevel; |
| 37 | + |
| 38 | + private Set<String> ignoredEquipments = new HashSet<>(); |
| 39 | + private EnumSet<DeserializationEndTask.Step> alreadyProcessedEndTasksSteps = EnumSet.noneOf(DeserializationEndTask.Step.class); |
| 40 | + |
| 41 | + public NetworkDeserializerContext(Anonymizer anonymizer, TreeDataReader reader) { |
| 42 | + this(anonymizer, reader, new ImportOptions(), CURRENT_IIDM_VERSION, Collections.emptyMap()); |
| 43 | + } |
| 44 | + |
| 45 | + public NetworkDeserializerContext(Anonymizer anonymizer, TreeDataReader reader, ImportOptions options, IidmVersion version, |
| 46 | + Map<String, String> extensionVersions) { |
| 47 | + super(anonymizer, version); |
| 48 | + this.reader = Objects.requireNonNull(reader); |
| 49 | + this.options = Objects.requireNonNull(options); |
| 50 | + this.extensionVersions = extensionVersions; |
| 51 | + } |
| 52 | + |
| 53 | + @Override |
| 54 | + public TreeDataReader getReader() { |
| 55 | + return reader; |
| 56 | + } |
| 57 | + |
| 58 | + public void addEndTask(DeserializationEndTask.Step step, Runnable task) { |
| 59 | + endTasks.add(new DeserializationEndTask(step, task)); |
| 60 | + } |
| 61 | + |
| 62 | + public void executeEndTasks(Network network, DeserializationEndTask.Step step, ReportNode reportNode) { |
| 63 | + if (alreadyProcessedEndTasksSteps.contains(step)) { |
| 64 | + // Skip if step was already processed |
| 65 | + return; |
| 66 | + } |
| 67 | + alreadyProcessedEndTasksSteps.add(step); |
| 68 | + Networks.executeWithReportNode(network, reportNode, () -> endTasks.stream() |
| 69 | + .filter(t -> t.getStep() == step |
| 70 | + || step == DeserializationEndTask.Step.AFTER_EXTENSIONS && |
| 71 | + t.getStep() == DeserializationEndTask.Step.BEFORE_EXTENSIONS && !t.isProcessed()) // If no "extensions" tag was found, BEFORE_EXTENSIONS tasks were not processed. |
| 72 | + .forEach(t -> { |
| 73 | + t.setProcessed(true); |
| 74 | + t.getTask().run(); |
| 75 | + })); |
| 76 | + } |
| 77 | + |
| 78 | + @Override |
| 79 | + public ImportOptions getOptions() { |
| 80 | + return options; |
| 81 | + } |
| 82 | + |
| 83 | + public boolean containsExtensionVersion(String extensionName, String version) { |
| 84 | + return version != null && version.equals(extensionVersions.get(extensionName)); |
| 85 | + } |
| 86 | + |
| 87 | + public Optional<String> getExtensionVersion(ExtensionSerDe<?, ?> extensionSerDe) { |
| 88 | + return Optional.ofNullable(extensionVersions.get(extensionSerDe.getExtensionName())); |
| 89 | + } |
| 90 | + |
| 91 | + public NetworkDeserializerContext setNetworkValidationLevel(ValidationLevel validationLevel) { |
| 92 | + this.networkValidationLevel = validationLevel; |
| 93 | + return this; |
| 94 | + } |
| 95 | + |
| 96 | + public ValidationLevel getNetworkValidationLevel() { |
| 97 | + return this.networkValidationLevel; |
| 98 | + } |
| 99 | + |
| 100 | + public void addIgnoredEquipment(String equipment) { |
| 101 | + ignoredEquipments.add(equipment); |
| 102 | + } |
| 103 | + |
| 104 | + public boolean isIgnoredEquipment(String equipment) { |
| 105 | + return ignoredEquipments.contains(equipment); |
| 106 | + } |
| 107 | +} |
0 commit comments