Skip to content

Commit

Permalink
Add a reporter to dynamic simulation and its models suppliers (#2750)
Browse files Browse the repository at this point in the history
Signed-off-by: lisrte <laurent.issertial@rte-france.com>
  • Loading branch information
Lisrte authored Nov 20, 2023
1 parent 33e7ef5 commit 17abb32
Show file tree
Hide file tree
Showing 20 changed files with 162 additions and 71 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,23 @@ public static void compareTxt(InputStream expected, InputStream actual, List<Int

public static void compareTxt(InputStream expected, String actual) {
try {
String expectedStr = TestUtil.normalizeLineSeparator(new String(ByteStreams.toByteArray(expected), StandardCharsets.UTF_8));
String actualStr = TestUtil.normalizeLineSeparator(actual);
assertEquals(expectedStr, actualStr);
compareTxt(new String(ByteStreams.toByteArray(expected), StandardCharsets.UTF_8), actual);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}

public static void compareTxt(String expected, InputStream actual) {
try {
compareTxt(expected, new String(ByteStreams.toByteArray(actual), StandardCharsets.UTF_8));
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}

public static void compareTxt(String expected, String actual) {
String expectedStr = TestUtil.normalizeLineSeparator(expected);
String actualStr = TestUtil.normalizeLineSeparator(actual);
assertEquals(expectedStr, actualStr);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@
public interface CurvesSupplier extends SimulatorInputSupplier<Curve> {

static CurvesSupplier empty() {
return network -> Collections.emptyList();
return (network, reporter) -> Collections.emptyList();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import com.powsybl.commons.Versionable;
import com.powsybl.commons.config.PlatformConfig;
import com.powsybl.commons.config.PlatformConfigNamedProvider;
import com.powsybl.commons.reporter.Reporter;
import com.powsybl.computation.ComputationManager;
import com.powsybl.computation.local.LocalComputationManager;
import com.powsybl.iidm.network.Network;
Expand All @@ -32,14 +33,19 @@ public Runner(DynamicSimulationProvider provider) {
this.provider = Objects.requireNonNull(provider);
}

public CompletableFuture<DynamicSimulationResult> runAsync(Network network, DynamicModelsSupplier dynamicModelsSupplier, EventModelsSupplier eventModelsSupplier, CurvesSupplier curvesSupplier, String workingVariantId,
ComputationManager computationManager, DynamicSimulationParameters parameters, Reporter reporter) {
return provider.run(network, dynamicModelsSupplier, eventModelsSupplier, curvesSupplier, workingVariantId, computationManager, parameters, reporter);
}

public CompletableFuture<DynamicSimulationResult> runAsync(Network network, DynamicModelsSupplier dynamicModelsSupplier, EventModelsSupplier eventModelsSupplier, CurvesSupplier curvesSupplier, String workingVariantId,
ComputationManager computationManager, DynamicSimulationParameters parameters) {
return provider.run(network, dynamicModelsSupplier, eventModelsSupplier, curvesSupplier, workingVariantId, computationManager, parameters);
return provider.run(network, dynamicModelsSupplier, eventModelsSupplier, curvesSupplier, workingVariantId, computationManager, parameters, Reporter.NO_OP);
}

public CompletableFuture<DynamicSimulationResult> runAsync(Network network, DynamicModelsSupplier dynamicModelsSupplier, EventModelsSupplier eventModelsSupplier, CurvesSupplier curvesSupplier, String workingVariantId,
DynamicSimulationParameters parameters) {
return runAsync(network, dynamicModelsSupplier, eventModelsSupplier, curvesSupplier, workingVariantId, LocalComputationManager.getDefault(), parameters);
return runAsync(network, dynamicModelsSupplier, eventModelsSupplier, curvesSupplier, workingVariantId, LocalComputationManager.getDefault(), parameters, Reporter.NO_OP);
}

public CompletableFuture<DynamicSimulationResult> runAsync(Network network, DynamicModelsSupplier dynamicModelsSupplier, EventModelsSupplier eventModelsSupplier, CurvesSupplier curvesSupplier, DynamicSimulationParameters parameters) {
Expand Down Expand Up @@ -70,9 +76,14 @@ public CompletableFuture<DynamicSimulationResult> runAsync(Network network, Dyna
return runAsync(network, dynamicModelsSupplier, DynamicSimulationParameters.load());
}

public DynamicSimulationResult run(Network network, DynamicModelsSupplier dynamicModelsSupplier, EventModelsSupplier eventModelsSupplier, CurvesSupplier curvesSupplier, String workingVariantId, ComputationManager computationManager,
DynamicSimulationParameters parameters, Reporter reporter) {
return runAsync(network, dynamicModelsSupplier, eventModelsSupplier, curvesSupplier, workingVariantId, computationManager, parameters, reporter).join();
}

public DynamicSimulationResult run(Network network, DynamicModelsSupplier dynamicModelsSupplier, EventModelsSupplier eventModelsSupplier, CurvesSupplier curvesSupplier, String workingVariantId, ComputationManager computationManager,
DynamicSimulationParameters parameters) {
return runAsync(network, dynamicModelsSupplier, eventModelsSupplier, curvesSupplier, workingVariantId, computationManager, parameters).join();
return runAsync(network, dynamicModelsSupplier, eventModelsSupplier, curvesSupplier, workingVariantId, computationManager, parameters, Reporter.NO_OP).join();
}

public DynamicSimulationResult run(Network network, DynamicModelsSupplier dynamicModelsSupplier, EventModelsSupplier eventModelsSupplier, CurvesSupplier curvesSupplier, String workingVariantId, DynamicSimulationParameters parameters) {
Expand Down Expand Up @@ -129,8 +140,8 @@ public static Runner find() {
}

public static CompletableFuture<DynamicSimulationResult> runAsync(Network network, DynamicModelsSupplier dynamicModelsSupplier, EventModelsSupplier eventModelsSupplier, CurvesSupplier curvesSupplier,
String workingVariantId, ComputationManager computationManager, DynamicSimulationParameters parameters) {
return find().runAsync(network, dynamicModelsSupplier, eventModelsSupplier, curvesSupplier, workingVariantId, computationManager, parameters);
String workingVariantId, ComputationManager computationManager, DynamicSimulationParameters parameters, Reporter reporter) {
return find().runAsync(network, dynamicModelsSupplier, eventModelsSupplier, curvesSupplier, workingVariantId, computationManager, parameters, reporter);
}

public static CompletableFuture<DynamicSimulationResult> runAsync(Network network, DynamicModelsSupplier dynamicModelsSupplier, EventModelsSupplier eventModelsSupplier, CurvesSupplier curvesSupplier,
Expand Down Expand Up @@ -168,8 +179,8 @@ public static CompletableFuture<DynamicSimulationResult> runAsync(Network networ
}

public static DynamicSimulationResult run(Network network, DynamicModelsSupplier dynamicModelsSupplier, EventModelsSupplier eventModelsSupplier, CurvesSupplier curvesSupplier,
String workingVariantId, ComputationManager computationManager, DynamicSimulationParameters parameters) {
return find().run(network, dynamicModelsSupplier, eventModelsSupplier, curvesSupplier, workingVariantId, computationManager, parameters);
String workingVariantId, ComputationManager computationManager, DynamicSimulationParameters parameters, Reporter reporter) {
return find().run(network, dynamicModelsSupplier, eventModelsSupplier, curvesSupplier, workingVariantId, computationManager, parameters, reporter);
}

public static DynamicSimulationResult run(Network network, DynamicModelsSupplier dynamicModelsSupplier, EventModelsSupplier eventModelsSupplier, CurvesSupplier curvesSupplier,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import com.google.common.collect.Lists;
import com.powsybl.commons.Versionable;
import com.powsybl.commons.config.PlatformConfigNamedProvider;
import com.powsybl.commons.reporter.Reporter;
import com.powsybl.computation.ComputationManager;
import com.powsybl.iidm.network.Network;

Expand All @@ -26,6 +27,6 @@ static List<DynamicSimulationProvider> findAll() {
}

CompletableFuture<DynamicSimulationResult> run(Network network, DynamicModelsSupplier dynamicModelsSupplier, EventModelsSupplier eventModelsSupplier,
CurvesSupplier curvesSupplier, String workingVariantId, ComputationManager computationManager, DynamicSimulationParameters parameters);
CurvesSupplier curvesSupplier, String workingVariantId, ComputationManager computationManager, DynamicSimulationParameters parameters, Reporter reporter);

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@
/**
* @author Marcos de Miguel {@literal <demiguelm at aia.es>}
*/

public interface EventModelsSupplier extends SimulatorInputSupplier<EventModel> {

static EventModelsSupplier empty() {
return network -> Collections.emptyList();
return (network, reporter) -> Collections.emptyList();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

package com.powsybl.dynamicsimulation;

import com.powsybl.commons.reporter.Reporter;
import com.powsybl.iidm.network.Network;

import java.util.List;
Expand All @@ -30,8 +31,13 @@ default String getName() {
* Return a list of <pre>T</pre> objects specific to a given network
*
* @param network The network used to filter the content of the list
* @param reporter the reporter used for functional logs
*
* @return A list of <pre>T</pre> for the given network
*/
List<T> get(Network network);
List<T> get(Network network, Reporter reporter);

default List<T> get(Network network) {
return get(network, Reporter.NO_OP);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import java.util.Collections;
import java.util.concurrent.CompletableFuture;

import com.powsybl.commons.reporter.Reporter;
import com.powsybl.computation.ComputationManager;
import com.powsybl.iidm.network.Network;

Expand All @@ -29,7 +30,7 @@ public String getVersion() {

@Override
public CompletableFuture<DynamicSimulationResult> run(Network network, DynamicModelsSupplier dynamicModelsSupplier, EventModelsSupplier eventModelsSupplier,
CurvesSupplier curvesSupplier, String workingVariantId, ComputationManager computationManager, DynamicSimulationParameters parameters) {
CurvesSupplier curvesSupplier, String workingVariantId, ComputationManager computationManager, DynamicSimulationParameters parameters, Reporter reporter) {
return CompletableFuture.completedFuture(new DynamicSimulationResultImpl(true, null, Collections.emptyMap(), DynamicSimulationResult.emptyTimeLine()));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ private DynamicModelsSupplierMock() {
}

static DynamicModelsSupplier empty() {
return network -> Collections.emptyList();
return (network, reporter) -> Collections.emptyList();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import java.util.concurrent.CompletableFuture;

import com.google.auto.service.AutoService;
import com.powsybl.commons.reporter.Reporter;
import com.powsybl.computation.ComputationManager;
import com.powsybl.iidm.network.Network;

Expand All @@ -31,7 +32,7 @@ public String getVersion() {

@Override
public CompletableFuture<DynamicSimulationResult> run(Network network, DynamicModelsSupplier dynamicModelsSupplier, EventModelsSupplier eventModelsSupplier, CurvesSupplier curvesSupplier,
String workingVariantId, ComputationManager computationManager, DynamicSimulationParameters parameters) {
String workingVariantId, ComputationManager computationManager, DynamicSimulationParameters parameters, Reporter reporter) {
return CompletableFuture.completedFuture(new DynamicSimulationResultImpl(true, null, Collections.emptyMap(), DynamicSimulationResult.emptyTimeLine()));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

import com.powsybl.commons.reporter.Reporter;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
Expand Down Expand Up @@ -71,7 +72,7 @@ void testProviderRunCombinations() {
assertNotNull(DynamicSimulation.run(network, DynamicModelsSupplierMock.empty(), EventModelsSupplier.empty(), parameters));
assertNotNull(DynamicSimulation.run(network, DynamicModelsSupplierMock.empty(), EventModelsSupplier.empty(), CurvesSupplier.empty(), parameters));
assertNotNull(DynamicSimulation.run(network, DynamicModelsSupplierMock.empty(), EventModelsSupplier.empty(), CurvesSupplier.empty(), network.getVariantManager().getWorkingVariantId(), parameters));
assertNotNull(DynamicSimulation.run(network, DynamicModelsSupplierMock.empty(), EventModelsSupplier.empty(), CurvesSupplier.empty(), network.getVariantManager().getWorkingVariantId(), computationManager, parameters));
assertNotNull(DynamicSimulation.run(network, DynamicModelsSupplierMock.empty(), EventModelsSupplier.empty(), CurvesSupplier.empty(), network.getVariantManager().getWorkingVariantId(), computationManager, parameters, Reporter.NO_OP));
}

@Test
Expand All @@ -86,6 +87,6 @@ void testProviderAsyncCombinations() {
assertNotNull(DynamicSimulation.runAsync(network, DynamicModelsSupplierMock.empty(), EventModelsSupplier.empty(), parameters));
assertNotNull(DynamicSimulation.runAsync(network, DynamicModelsSupplierMock.empty(), EventModelsSupplier.empty(), CurvesSupplier.empty(), parameters));
assertNotNull(DynamicSimulation.runAsync(network, DynamicModelsSupplierMock.empty(), EventModelsSupplier.empty(), CurvesSupplier.empty(), network.getVariantManager().getWorkingVariantId(), parameters));
assertNotNull(DynamicSimulation.runAsync(network, DynamicModelsSupplierMock.empty(), EventModelsSupplier.empty(), CurvesSupplier.empty(), network.getVariantManager().getWorkingVariantId(), computationManager, parameters));
assertNotNull(DynamicSimulation.runAsync(network, DynamicModelsSupplierMock.empty(), EventModelsSupplier.empty(), CurvesSupplier.empty(), network.getVariantManager().getWorkingVariantId(), computationManager, parameters, Reporter.NO_OP));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

package com.powsybl.dynamicsimulation.groovy;

import com.powsybl.commons.reporter.Reporter;
import com.powsybl.dsl.ExpressionDslLoader;
import com.powsybl.dsl.GroovyScripts;
import com.powsybl.dynamicsimulation.Curve;
Expand Down Expand Up @@ -48,19 +49,15 @@ public GroovyCurvesSupplier(InputStream is, List<CurveGroovyExtension> extension
}

@Override
public String getName() {
return null;
}

@Override
public List<Curve> get(Network network) {
public List<Curve> get(Network network, Reporter reporter) {
List<Curve> curves = new ArrayList<>();
Reporter groovyReporter = reporter.createSubReporter("groovyCurves", "Groovy Curves Supplier");

Binding binding = new Binding();
binding.setVariable("network", network);

ExpressionDslLoader.prepareClosures(binding);
extensions.forEach(e -> e.load(binding, curves::add));
extensions.forEach(e -> e.load(binding, curves::add, groovyReporter));

GroovyShell shell = new GroovyShell(binding, new CompilerConfiguration());
shell.evaluate(codeSource);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import java.util.List;
import java.util.Objects;

import com.powsybl.commons.reporter.Reporter;
import org.codehaus.groovy.control.CompilerConfiguration;

import com.powsybl.dsl.ExpressionDslLoader;
Expand Down Expand Up @@ -50,19 +51,15 @@ public GroovyDynamicModelsSupplier(InputStream is, List<DynamicModelGroovyExtens
}

@Override
public String getName() {
return null;
}

@Override
public List<DynamicModel> get(Network network) {
public List<DynamicModel> get(Network network, Reporter reporter) {
List<DynamicModel> dynamicModels = new ArrayList<>();
Reporter groovyReporter = reporter.createSubReporter("groovyDynamicModels", "Groovy Dynamic Models Supplier");

Binding binding = new Binding();
binding.setVariable("network", network);

ExpressionDslLoader.prepareClosures(binding);
extensions.forEach(e -> e.load(binding, dynamicModels::add));
extensions.forEach(e -> e.load(binding, dynamicModels::add, groovyReporter));

GroovyShell shell = new GroovyShell(binding, new CompilerConfiguration());
shell.evaluate(codeSource);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import java.util.List;
import java.util.Objects;

import com.powsybl.commons.reporter.Reporter;
import org.codehaus.groovy.control.CompilerConfiguration;

import com.powsybl.dsl.ExpressionDslLoader;
Expand Down Expand Up @@ -50,19 +51,15 @@ public GroovyEventModelsSupplier(InputStream is, List<EventModelGroovyExtension>
}

@Override
public String getName() {
return null;
}

@Override
public List<EventModel> get(Network network) {
public List<EventModel> get(Network network, Reporter reporter) {
List<EventModel> eventModels = new ArrayList<>();
Reporter groovyReporter = reporter.createSubReporter("groovyEventModels", "Groovy Event Models Supplier");

Binding binding = new Binding();
binding.setVariable("network", network);

ExpressionDslLoader.prepareClosures(binding);
extensions.forEach(e -> e.load(binding, eventModels::add));
extensions.forEach(e -> e.load(binding, eventModels::add, groovyReporter));

GroovyShell shell = new GroovyShell(binding, new CompilerConfiguration());
shell.evaluate(codeSource);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

package com.powsybl.dynamicsimulation.groovy;

import com.powsybl.commons.reporter.Reporter;
import com.powsybl.dynamicsimulation.DynamicSimulationProvider;
import groovy.lang.Binding;

Expand Down Expand Up @@ -36,8 +37,9 @@ default String getName() {
* Load the extension into the binding object. Each time an object is created, the consumer is notified.
* @param binding The binding where to register the extension
* @param consumer The consumer to notify on objects creation
* @param reporter the reporter used for functional logs
*/
void load(Binding binding, Consumer<T> consumer);
void load(Binding binding, Consumer<T> consumer, Reporter reporter);

/**
* Return the list of available GroovyExtension of type clazz, compatible with the provider which the name is given
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,11 @@
package com.powsybl.dynamicsimulation.groovy

import com.google.auto.service.AutoService
import com.powsybl.commons.extensions.Extension
import com.powsybl.commons.reporter.Reporter
import com.powsybl.dsl.DslException
import com.powsybl.dsl.ExtendableDslExtension
import com.powsybl.dynamicsimulation.Curve

import java.util.function.Consumer

/**
* @author Mathieu Bague {@literal <mathieu.bague@rte-france.com>}
*/
Expand All @@ -33,7 +31,7 @@ class DummyCurveGroovyExtension implements CurveGroovyExtension {
}
}

void load(Binding binding, Consumer<Curve> consumer) {
void load(Binding binding, Consumer<Curve> consumer, Reporter reporter) {
binding.dummyCurve = { Closure<Void> closure ->
def cloned = closure.clone()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
*/
package com.powsybl.dynamicsimulation.groovy

import com.powsybl.commons.reporter.Reporter

import java.util.function.Consumer

import com.google.auto.service.AutoService
Expand All @@ -31,7 +33,7 @@ class DummyDynamicModelGroovyExtension implements DynamicModelGroovyExtension {
}
}

void load(Binding binding, Consumer<DynamicModel> consumer) {
void load(Binding binding, Consumer<DynamicModel> consumer, Reporter reporter) {
binding.dummyDynamicModel = { Closure<Void> closure ->
def cloned = closure.clone()

Expand Down
Loading

0 comments on commit 17abb32

Please sign in to comment.