diff --git a/dynawaltz/src/main/java/com/powsybl/dynawaltz/DynaWaltzContext.java b/dynawaltz/src/main/java/com/powsybl/dynawaltz/DynaWaltzContext.java index 7042d5cdc..34207b28b 100644 --- a/dynawaltz/src/main/java/com/powsybl/dynawaltz/DynaWaltzContext.java +++ b/dynawaltz/src/main/java/com/powsybl/dynawaltz/DynaWaltzContext.java @@ -121,8 +121,12 @@ public DynaWaltzContext(Network network, String workingVariantId, List simplifyModels(Stream inputBbm, ReportNode reportNode) { Stream outputBbm = inputBbm; - for (ModelsSimplifier modelsSimplifier : ServiceLoader.load(ModelsSimplifier.class)) { - outputBbm = modelsSimplifier.simplifyModels(outputBbm, network, dynaWaltzParameters, reportNode); + for (ModelsRemovalSimplifier modelsSimplifier : ServiceLoader.load(ModelsRemovalSimplifier.class)) { + outputBbm = outputBbm.filter(modelsSimplifier.getModelRemovalPredicate(reportNode)); + } + for (ModelsSubstitutionSimplifier modelsSimplifier : ServiceLoader.load(ModelsSubstitutionSimplifier.class)) { + outputBbm = outputBbm.map(modelsSimplifier.getModelSubstitutionFunction(network, dynaWaltzParameters, reportNode)) + .filter(Objects::nonNull); } return outputBbm; } diff --git a/dynawaltz/src/main/java/com/powsybl/dynawaltz/ModelsRemovalSimplifier.java b/dynawaltz/src/main/java/com/powsybl/dynawaltz/ModelsRemovalSimplifier.java new file mode 100644 index 000000000..13ba309b6 --- /dev/null +++ b/dynawaltz/src/main/java/com/powsybl/dynawaltz/ModelsRemovalSimplifier.java @@ -0,0 +1,21 @@ +/** + * 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.dynawaltz; + +import com.powsybl.commons.report.ReportNode; +import com.powsybl.dynawaltz.models.BlackBoxModel; + +import java.util.function.Predicate; + +/** + * @author Laurent Issertial + */ +public interface ModelsRemovalSimplifier { + + Predicate getModelRemovalPredicate(ReportNode reportNode); +} diff --git a/dynawaltz/src/main/java/com/powsybl/dynawaltz/ModelsSimplifier.java b/dynawaltz/src/main/java/com/powsybl/dynawaltz/ModelsSubstitutionSimplifier.java similarity index 62% rename from dynawaltz/src/main/java/com/powsybl/dynawaltz/ModelsSimplifier.java rename to dynawaltz/src/main/java/com/powsybl/dynawaltz/ModelsSubstitutionSimplifier.java index 9e15b52df..78ea58fe7 100644 --- a/dynawaltz/src/main/java/com/powsybl/dynawaltz/ModelsSimplifier.java +++ b/dynawaltz/src/main/java/com/powsybl/dynawaltz/ModelsSubstitutionSimplifier.java @@ -1,5 +1,5 @@ /** - * Copyright (c) 2023, RTE (http://www.rte-france.com) + * 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/. @@ -11,12 +11,12 @@ import com.powsybl.dynawaltz.models.BlackBoxModel; import com.powsybl.iidm.network.Network; -import java.util.stream.Stream; +import java.util.function.Function; /** * @author Laurent Issertial */ -public interface ModelsSimplifier { +public interface ModelsSubstitutionSimplifier { - Stream simplifyModels(Stream models, Network network, DynaWaltzParameters dynaWaltzParameters, ReportNode reportNode); + Function getModelSubstitutionFunction(Network network, DynaWaltzParameters dynaWaltzParameters, ReportNode reportNode); } diff --git a/dynawaltz/src/test/java/com/powsybl/dynawaltz/ModelsSimplifierTest.java b/dynawaltz/src/test/java/com/powsybl/dynawaltz/ModelsSimplifierTest.java index 3155fa2eb..ec4cab7aa 100644 --- a/dynawaltz/src/test/java/com/powsybl/dynawaltz/ModelsSimplifierTest.java +++ b/dynawaltz/src/test/java/com/powsybl/dynawaltz/ModelsSimplifierTest.java @@ -23,7 +23,8 @@ import java.util.Collections; import java.util.List; import java.util.ServiceLoader; -import java.util.stream.Stream; +import java.util.function.Function; +import java.util.function.Predicate; import static org.junit.jupiter.api.Assertions.*; @@ -33,9 +34,15 @@ class ModelsSimplifierTest { @Test - void loadSimplifiers() { - List simplifiers = Lists.newArrayList(ServiceLoader.load(ModelsSimplifier.class)); - assertEquals(2, simplifiers.size()); + void loadRemovalSimplifiers() { + List simplifiers = Lists.newArrayList(ServiceLoader.load(ModelsRemovalSimplifier.class)); + assertEquals(1, simplifiers.size()); + } + + @Test + void loadSubstitutionSimplifiers() { + List simplifiers = Lists.newArrayList(ServiceLoader.load(ModelsSubstitutionSimplifier.class)); + assertEquals(1, simplifiers.size()); } @Test @@ -65,19 +72,19 @@ void simplifyModels() { assertTrue(context.getBlackBoxDynamicModelStream().anyMatch(bbm -> bbm.getDynamicModelId().equalsIgnoreCase("newModel"))); } - @AutoService(ModelsSimplifier.class) - public static class ModelsSimplifierFilter implements ModelsSimplifier { + @AutoService(ModelsRemovalSimplifier.class) + public static class ModelsSimplifierFilter implements ModelsRemovalSimplifier { @Override - public Stream simplifyModels(Stream models, Network network, DynaWaltzParameters dynaWaltzParameters, ReportNode reportNode) { - return models.filter(m -> !m.getDynamicModelId().equalsIgnoreCase("BBM_LOAD")); + public Predicate getModelRemovalPredicate(ReportNode reportNode) { + return m -> !m.getDynamicModelId().equalsIgnoreCase("BBM_LOAD"); } } - @AutoService(ModelsSimplifier.class) - public static class ModelsSimplifierSubstitution implements ModelsSimplifier { + @AutoService(ModelsSubstitutionSimplifier.class) + public static class ModelsSimplifierSubstitution implements ModelsSubstitutionSimplifier { @Override - public Stream simplifyModels(Stream models, Network network, DynaWaltzParameters dynaWaltzParameters, ReportNode reportNode) { - return models.map(m -> { + public Function getModelSubstitutionFunction(Network network, DynaWaltzParameters dynaWaltzParameters, ReportNode reportNode) { + return m -> { if ("BBM_GEN".equalsIgnoreCase(m.getDynamicModelId()) && m instanceof AbstractGenerator gen) { return GeneratorFictitiousBuilder.of(network) .dynamicModelId("newModel") @@ -86,7 +93,7 @@ public Stream simplifyModels(Stream models, Networ .build(); } return m; - }); + }; } } }