Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prioritize simplifiers #344

Merged
merged 1 commit into from
May 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,12 @@ public DynaWaltzContext(Network network, String workingVariantId, List<BlackBoxM

private Stream<BlackBoxModel> simplifyModels(Stream<BlackBoxModel> inputBbm, ReportNode reportNode) {
Stream<BlackBoxModel> 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;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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 <laurent.issertial at rte-france.com>
*/
public interface ModelsRemovalSimplifier {

Predicate<BlackBoxModel> getModelRemovalPredicate(ReportNode reportNode);
}
Original file line number Diff line number Diff line change
@@ -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/.
Expand All @@ -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 <laurent.issertial at rte-france.com>
*/
public interface ModelsSimplifier {
public interface ModelsSubstitutionSimplifier {

Stream<BlackBoxModel> simplifyModels(Stream<BlackBoxModel> models, Network network, DynaWaltzParameters dynaWaltzParameters, ReportNode reportNode);
Function<BlackBoxModel, BlackBoxModel> getModelSubstitutionFunction(Network network, DynaWaltzParameters dynaWaltzParameters, ReportNode reportNode);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;

Expand All @@ -33,9 +34,15 @@
class ModelsSimplifierTest {

@Test
void loadSimplifiers() {
List<ModelsSimplifier> simplifiers = Lists.newArrayList(ServiceLoader.load(ModelsSimplifier.class));
assertEquals(2, simplifiers.size());
void loadRemovalSimplifiers() {
List<ModelsRemovalSimplifier> simplifiers = Lists.newArrayList(ServiceLoader.load(ModelsRemovalSimplifier.class));
assertEquals(1, simplifiers.size());
}

@Test
void loadSubstitutionSimplifiers() {
List<ModelsSubstitutionSimplifier> simplifiers = Lists.newArrayList(ServiceLoader.load(ModelsSubstitutionSimplifier.class));
assertEquals(1, simplifiers.size());
}

@Test
Expand Down Expand Up @@ -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<BlackBoxModel> simplifyModels(Stream<BlackBoxModel> models, Network network, DynaWaltzParameters dynaWaltzParameters, ReportNode reportNode) {
return models.filter(m -> !m.getDynamicModelId().equalsIgnoreCase("BBM_LOAD"));
public Predicate<BlackBoxModel> 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<BlackBoxModel> simplifyModels(Stream<BlackBoxModel> models, Network network, DynaWaltzParameters dynaWaltzParameters, ReportNode reportNode) {
return models.map(m -> {
public Function<BlackBoxModel, BlackBoxModel> 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")
Expand All @@ -86,7 +93,7 @@ public Stream<BlackBoxModel> simplifyModels(Stream<BlackBoxModel> models, Networ
.build();
}
return m;
});
};
}
}
}
Loading