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

Add a reporter to dynamic simulation and its models suppliers #300

Merged
merged 10 commits into from
Nov 23, 2023
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,18 @@
*/
package com.powsybl.dynawaltz.dsl

import com.powsybl.commons.reporter.Reporter
import com.powsybl.dynamicsimulation.DynamicModel
import com.powsybl.dynamicsimulation.groovy.DynamicModelGroovyExtension
import com.powsybl.dynawaltz.DynaWaltzProvider
import com.powsybl.iidm.network.Network

import java.util.function.Consumer

/**
* @author Laurent Issertial {@literal <laurent.issertial at rte-france.com>}
*/
abstract class AbstractEquipmentGroovyExtension<T> {
abstract class AbstractEquipmentGroovyExtension implements DynamicModelGroovyExtension {

protected static final String MODELS_CONFIG = "models.json"

Expand All @@ -28,21 +32,26 @@ abstract class AbstractEquipmentGroovyExtension<T> {
equipmentConfigs = ModelsSlurper.instance.getEquipmentConfigs(modelConfigUrl, modelTag)
}

abstract protected ModelBuilder<T> createBuilder(Network network, EquipmentConfig equipmentConfig)
abstract protected ModelBuilder<DynamicModel> createBuilder(Network network, EquipmentConfig equipmentConfig, Reporter reporter)

@Override
String getName() {
DynaWaltzProvider.NAME
}

@Override
List<String> getModelNames() {
equipmentConfigs.collect(eq -> eq.lib)
}

void load(Binding binding, Consumer<T> consumer) {
@Override
void load(Binding binding, Consumer<DynamicModel> consumer, Reporter reporter) {
equipmentConfigs.forEach {
binding.setVariable(it.lib, { Closure<Void> closure ->
def cloned = closure.clone()
ModelBuilder<T> builder = createBuilder(binding.getVariable("network") as Network, it)
ModelBuilder<DynamicModel> builder = createBuilder(binding.getVariable("network") as Network,
it,
Reporters.createModelBuilderReporter(reporter, it.lib))
cloned.delegate = builder
cloned()
builder.build()?.tap {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
*/
package com.powsybl.dynawaltz.dsl

import com.powsybl.commons.reporter.Reporter
import com.powsybl.dynamicsimulation.groovy.GroovyExtension
import com.powsybl.dynawaltz.DynaWaltzProvider
import com.powsybl.iidm.network.Network

Expand All @@ -16,12 +18,13 @@ import java.util.function.Consumer
* Superclass for automaton & event groovy extensions
* @author Laurent Issertial {@literal <laurent.issertial at rte-france.com>}
*/
abstract class AbstractPureDynamicGroovyExtension<T> {
abstract class AbstractPureDynamicGroovyExtension<T> implements GroovyExtension<T> {

protected List<String> modelTags

abstract protected ModelBuilder<T> createBuilder(Network network)
abstract protected ModelBuilder<T> createBuilder(Network network, Reporter reporter)

@Override
String getName() {
DynaWaltzProvider.NAME
}
Expand All @@ -30,11 +33,13 @@ abstract class AbstractPureDynamicGroovyExtension<T> {
modelTags
}

void load(Binding binding, Consumer<T> consumer) {
@Override
void load(Binding binding, Consumer<T> consumer, Reporter reporter) {
modelTags.forEach {
binding.setVariable(it, { Closure<Void> closure ->
def cloned = closure.clone()
ModelBuilder<T> builder = createBuilder(binding.getVariable("network") as Network)
ModelBuilder<T> builder = createBuilder(binding.getVariable("network") as Network,
Reporters.createModelBuilderReporter(reporter, it))
cloned.delegate = builder
cloned()
builder.build()?.tap {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
*/
package com.powsybl.dynawaltz.dsl

import com.powsybl.commons.reporter.Reporter
import com.powsybl.dynamicsimulation.DynamicModel
import com.powsybl.dynamicsimulation.groovy.DynamicModelGroovyExtension
import com.powsybl.dynawaltz.DynaWaltzProvider
import com.powsybl.iidm.network.Network

Expand All @@ -15,29 +18,33 @@ import java.util.function.Consumer
/**
* @author Laurent Issertial {@literal <laurent.issertial at rte-france.com>}
*/
abstract class AbstractSimpleEquipmentGroovyExtension<T> {
abstract class AbstractSimpleEquipmentGroovyExtension implements DynamicModelGroovyExtension {

protected EquipmentConfig equipmentConfig

AbstractSimpleEquipmentGroovyExtension(String modelTag) {
equipmentConfig = new EquipmentConfig(modelTag)
}

abstract protected ModelBuilder<DynamicModel> createBuilder(Network network, EquipmentConfig equipmentConfig, Reporter reporter)

abstract protected ModelBuilder<T> createBuilder(Network network, EquipmentConfig equipmentConfig)

@Override
String getName() {
DynaWaltzProvider.NAME
}

@Override
List<String> getModelNames() {
List.of(equipmentConfig.lib)
}

void load(Binding binding, Consumer<T> consumer) {
@Override
void load(Binding binding, Consumer<DynamicModel> consumer, Reporter reporter) {
binding.setVariable(equipmentConfig.lib, { Closure<Void> closure ->
def cloned = closure.clone()
ModelBuilder<T> builder = createBuilder(binding.getVariable("network") as Network, equipmentConfig)
ModelBuilder<DynamicModel> builder = createBuilder(binding.getVariable("network") as Network,
equipmentConfig,
Reporters.createModelBuilderReporter(reporter, equipmentConfig.lib))
cloned.delegate = builder
cloned()
builder.build()?.tap {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
*/
package com.powsybl.dynawaltz.dsl

import com.powsybl.commons.reporter.Reporter
import com.powsybl.iidm.network.Identifiable
import com.powsybl.iidm.network.IdentifiableType
import org.slf4j.Logger

import java.util.function.Function

Expand Down Expand Up @@ -48,12 +48,12 @@ class DslEquipment<T extends Identifiable> {
equipment = equipmentSupplier(staticId)
}

boolean checkEquipmentData(Logger logger, String lib) {
boolean checkEquipmentData(Reporter reporter) {
if (!staticId) {
logger.warn("$lib: '$fieldName' field is not set")
Reporters.reportFieldNotSet(reporter, fieldName)
return false
} else if (!equipment) {
logger.warn("$lib: $equipmentType static id unknown : $staticId")
Reporters.reportStaticIdUnknown(reporter, fieldName, staticId, equipmentType)
return false
}
true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
package com.powsybl.dynawaltz.dsl

import com.google.auto.service.AutoService
import com.powsybl.commons.reporter.Reporter
import com.powsybl.dsl.DslException
import com.powsybl.dynamicsimulation.Curve
import com.powsybl.dynamicsimulation.groovy.CurveGroovyExtension
Expand Down Expand Up @@ -51,6 +52,7 @@ class DynaWaltzCurveGroovyExtension implements CurveGroovyExtension {
}
}

@Override
String getName() {
DynaWaltzProvider.NAME
}
Expand All @@ -76,7 +78,8 @@ class DynaWaltzCurveGroovyExtension implements CurveGroovyExtension {
}
}

void load(Binding binding, Consumer<Curve> consumer) {
@Override
void load(Binding binding, Consumer<Curve> consumer, Reporter reporter) {
binding.curve = { Closure<Void> closure ->
def cloned = closure.clone()
CurvesSpec curveSpec = new CurvesSpec()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
/**
* Copyright (c) 2023, 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.dsl

import com.powsybl.commons.reporter.Report
import com.powsybl.commons.reporter.Reporter
import com.powsybl.commons.reporter.TypedValue
import com.powsybl.iidm.network.IdentifiableType

/**
* @author Laurent Issertial <laurent.issertial at rte-france.com>
*/
final class Reporters {

private Reporters() {
}

static Reporter createModelBuilderReporter(Reporter reporter, String lib) {
reporter.createSubReporter("DSLModelBuilder",
'DSL model builder for ${lib}',
"lib", lib)
}

static void reportModelInstantiation(Reporter reporter, String dynamicId) {
reporter.report(Report.builder()
.withKey("modelInstantiation")
.withDefaultMessage('Model ${dynamicId} instantiation successful')
.withValue("dynamicId", dynamicId)
.withSeverity(TypedValue.TRACE_SEVERITY)
.build())
}

static void reportModelInstantiationFailure(Reporter reporter, String dynamicId) {
reporter.report(Report.builder()
.withKey("modelInstantiation")
.withDefaultMessage('Model ${dynamicId} cannot be instantiated')
.withValue("dynamicId", dynamicId)
.withSeverity(TypedValue.WARN_SEVERITY)
.build())
}

static void reportFieldReplacement(Reporter reporter, String fieldName, String replacementName, String replacement) {
reporter.report(Report.builder()
.withKey("fieldReplacement")
.withDefaultMessage('\'${fieldName}\' field is not set, ${replacementName} ${replacement} will be used instead')
.withValue("fieldName", fieldName)
.withValue("replacementName", replacementName)
.withValue("replacement", replacement)
.withSeverity(TypedValue.TRACE_SEVERITY)
.build())
}

static void reportFieldNotSet(Reporter reporter, String fieldName) {
reporter.report(Report.builder()
.withKey("fieldNotSet")
.withDefaultMessage('\'${fieldName}\' field is not set')
.withValue("fieldName", fieldName)
.withSeverity(TypedValue.WARN_SEVERITY)
.build())
}

static void reportStaticIdUnknown(Reporter reporter, String fieldName, String staticId, String equipmentType) {
reporter.report(Report.builder()
.withKey("fieldNotSet")
.withDefaultMessage('\'${fieldName}\' field value \'${staticId}\' not found for equipment type(s) ${equipmentType}')
.withValue("equipmentType", equipmentType)
.withValue("fieldName", fieldName)
.withValue("staticId", staticId)
.withSeverity(TypedValue.WARN_SEVERITY)
.build())
}

static void reportCrossThreshold(Reporter reporter, String fieldName, double fieldValue, String threshold) {
reporter.report(Report.builder()
.withKey("crossThreshold")
.withDefaultMessage('${fieldName} should be ${threshold} (${fieldValue})')
.withValue("fieldName", fieldName)
.withValue("fieldValue", fieldValue)
.withValue("threshold", threshold)
.withSeverity(TypedValue.WARN_SEVERITY)
.build())
}

static void reportEmptyList(Reporter reporter, String fieldName) {
reporter.report(Report.builder()
.withKey("emptyList")
.withDefaultMessage('\'${fieldName}\' list is empty')
.withValue("fieldName", fieldName)
.withSeverity(TypedValue.WARN_SEVERITY)
.build())
}

static void reportFieldSetWithWrongEquipment(Reporter reporter, String fieldName, IdentifiableType equipmentType, String staticId) {
reportFieldSetWithWrongEquipment(reporter, fieldName, equipmentType.toString() + " " + staticId)
}

static void reportFieldSetWithWrongEquipment(Reporter reporter, String fieldName, String equipment) {
reporter.report(Report.builder()
.withKey("fieldSetWithWrongEquipment")
.withDefaultMessage('\'${fieldName}\' field is set but ${equipment} does not possess this option')
.withValue("fieldName", fieldName)
.withValue("equipment", equipment)
.withSeverity(TypedValue.WARN_SEVERITY)
.build())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*/
package com.powsybl.dynawaltz.dsl.automatons

import com.powsybl.commons.reporter.Reporter
import com.powsybl.dynawaltz.dsl.DslEquipment
import com.powsybl.dynawaltz.dsl.builders.AbstractPureDynamicModelBuilder
import com.powsybl.iidm.network.IdentifiableType
Expand All @@ -20,8 +21,8 @@ abstract class AbstractPhaseShifterModelBuilder extends AbstractPureDynamicModel

protected final DslEquipment<TwoWindingsTransformer> dslTransformer

AbstractPhaseShifterModelBuilder(Network network, String lib) {
super(network, lib)
AbstractPhaseShifterModelBuilder(Network network, String lib, Reporter reporter) {
super(network, lib, reporter)
dslTransformer = new DslEquipment<>(IdentifiableType.TWO_WINDINGS_TRANSFORMER, "transformer")
}

Expand All @@ -32,6 +33,6 @@ abstract class AbstractPhaseShifterModelBuilder extends AbstractPureDynamicModel
@Override
protected void checkData() {
super.checkData()
isInstantiable &= dslTransformer.checkEquipmentData(LOGGER, getLib())
isInstantiable &= dslTransformer.checkEquipmentData(reporter)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,18 @@
package com.powsybl.dynawaltz.dsl.automatons

import com.google.auto.service.AutoService
import com.powsybl.commons.reporter.Reporter
import com.powsybl.dynamicsimulation.DynamicModel
import com.powsybl.dynamicsimulation.groovy.DynamicModelGroovyExtension
import com.powsybl.dynawaltz.dsl.AbstractPureDynamicGroovyExtension
import com.powsybl.dynawaltz.dsl.DslEquipment
import com.powsybl.dynawaltz.dsl.Reporters
import com.powsybl.dynawaltz.dsl.builders.AbstractPureDynamicModelBuilder
import com.powsybl.dynawaltz.models.Side
import com.powsybl.dynawaltz.models.automatons.CurrentLimitAutomaton
import com.powsybl.dynawaltz.models.utils.SideConverter
import com.powsybl.iidm.network.Branch
import com.powsybl.iidm.network.IdentifiableType
import com.powsybl.iidm.network.Network
import com.powsybl.iidm.network.TwoSides

Expand All @@ -33,8 +36,8 @@ class CurrentLimitAutomatonGroovyExtension extends AbstractPureDynamicGroovyExte
}

@Override
protected CurrentLimitAutomatonBuilder createBuilder(Network network) {
new CurrentLimitAutomatonBuilder(network, getLib())
protected CurrentLimitAutomatonBuilder createBuilder(Network network, Reporter reporter) {
new CurrentLimitAutomatonBuilder(network, getLib(), reporter)
}

protected String getLib() {
Expand All @@ -47,10 +50,10 @@ class CurrentLimitAutomatonGroovyExtension extends AbstractPureDynamicGroovyExte
protected Side iMeasurementSide
protected final DslEquipment<Branch> controlledEquipment

CurrentLimitAutomatonBuilder(Network network, String lib) {
super(network, lib)
iMeasurement = new DslEquipment<>("I measurement quadripole", "iMeasurement")
controlledEquipment = new DslEquipment<>("Controlled quadripole", "controlledQuadripole")
CurrentLimitAutomatonBuilder(Network network, String lib, Reporter reporter) {
super(network, lib, reporter)
iMeasurement = new DslEquipment<>("Quadripole", "iMeasurement")
controlledEquipment = new DslEquipment<>("Quadripole", "controlledQuadripole")
}

void iMeasurement(String staticId) {
Expand All @@ -68,10 +71,10 @@ class CurrentLimitAutomatonGroovyExtension extends AbstractPureDynamicGroovyExte
@Override
void checkData() {
super.checkData()
isInstantiable &= controlledEquipment.checkEquipmentData(LOGGER, getLib())
isInstantiable &= iMeasurement.checkEquipmentData(LOGGER, getLib())
isInstantiable &= controlledEquipment.checkEquipmentData(reporter)
isInstantiable &= iMeasurement.checkEquipmentData(reporter)
if (!iMeasurementSide) {
LOGGER.warn("${getLib()}: 'iMeasurementSide' field is not set")
Reporters.reportFieldNotSet(reporter, "iMeasurementSide")
isInstantiable = false
}
}
Expand Down
Loading
Loading