Skip to content

Commit

Permalink
Refactor groovy extension (#179)
Browse files Browse the repository at this point in the history
* Refactor GroovyExtension hierarchy
* Refactor GeneratorSynchronous
* Add Model Builder hierarchy
* Rename classes

Signed-off-by: lisrte <laurent.issertial@rte-france.com>
  • Loading branch information
Lisrte authored Feb 21, 2023
1 parent 5fe3c06 commit 9659223
Show file tree
Hide file tree
Showing 20 changed files with 362 additions and 431 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* 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.dsl.DslException
import com.powsybl.dynamicsimulation.DynamicModel

/**
* @author Laurent Issertial <laurent.issertial at rte-france.com>
*/
abstract class AbstractDynamicModelBuilder implements ModelBuilder<DynamicModel> {

String dynamicModelId
String staticId
String parameterSetId

void dynamicModelId(String dynamicModelId) {
this.dynamicModelId = dynamicModelId
}

void staticId(String staticId) {
this.staticId = staticId
}

void parameterSetId(String parameterSetId) {
this.parameterSetId = parameterSetId
}

void checkData() {
if (!staticId) {
throw new DslException("'staticId' field is not set")
}
if (!parameterSetId) {
throw new DslException("'parameterSetId' field is not set")
}
if (!dynamicModelId) {
dynamicModelId = staticId
}
}

@Override
abstract DynamicModel build();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* 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.dynawaltz.DynaWaltzProvider

import java.util.function.Consumer

/**
* @author Laurent Issertial <laurent.issertial at rte-france.com>
*/
abstract class AbstractPowsyblDynawoGroovyExtension<T> {

protected List<String> modelTags

abstract protected ModelBuilder<T> createBuilder(String currentTag)

String getName() {
return DynaWaltzProvider.NAME
}

void load(Binding binding, Consumer<T> consumer) {
modelTags.forEach {
binding.setVariable(it, { Closure<Void> closure ->
def cloned = closure.clone()
ModelBuilder<T> builder = createBuilder(it)
cloned.delegate = builder
cloned()
consumer.accept(builder.build())
})
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/*
* 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

/**
* @author Laurent Issertial <laurent.issertial at rte-france.com>
*/
interface ModelBuilder<T> {

T build()
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,74 +6,52 @@
*/
package com.powsybl.dynawaltz.dsl.automatons

import com.powsybl.iidm.network.Branch

import com.powsybl.dynawaltz.DynaWaltzProvider
import com.powsybl.dynawaltz.models.automatons.CurrentLimitAutomaton

import java.util.function.Consumer

import com.google.auto.service.AutoService
import com.powsybl.dsl.DslException
import com.powsybl.dynamicsimulation.DynamicModel
import com.powsybl.dynamicsimulation.groovy.DynamicModelGroovyExtension
import com.powsybl.dynawaltz.dsl.AbstractDynamicModelBuilder
import com.powsybl.dynawaltz.dsl.AbstractPowsyblDynawoGroovyExtension
import com.powsybl.dynawaltz.models.automatons.CurrentLimitAutomaton
import com.powsybl.iidm.network.Branch

/**
* An implementation of {@link DynamicModelGroovyExtension} that adds the <pre>CurrentLimitAutomaton</pre> keyword to the DSL
*
* @author Marcos de Miguel <demiguelm at aia.es>
*/
@AutoService(DynamicModelGroovyExtension.class)
class CurrentLimitAutomatonGroovyExtension implements DynamicModelGroovyExtension {
class CurrentLimitAutomatonGroovyExtension extends AbstractPowsyblDynawoGroovyExtension<DynamicModel> implements DynamicModelGroovyExtension {

static class CurrentLimitAutomatonSpec {
String dynamicModelId
String staticId
Branch.Side side
String parameterSetId
CurrentLimitAutomatonGroovyExtension() {
modelTags = ["CurrentLimitAutomaton"]
}

void dynamicModelId(String dynamicModelId) {
this.dynamicModelId = dynamicModelId
}
@Override
protected CurrentLimitAutomatonBuilder createBuilder(String currentTag) {
new CurrentLimitAutomatonBuilder()
}

void staticId(String staticId) {
this.staticId = staticId
}
static class CurrentLimitAutomatonBuilder extends AbstractDynamicModelBuilder {

void parameterSetId(String parameterSetId) {
this.parameterSetId = parameterSetId
}
Branch.Side side

void side(Branch.Side side) {
this.side = side
}
}

String getName() {
return DynaWaltzProvider.NAME
}

void load(Binding binding, Consumer<DynamicModel> consumer) {
binding.CurrentLimitAutomaton = { Closure<Void> closure ->
def cloned = closure.clone()
CurrentLimitAutomatonSpec currentLimitAutomatonSpec = new CurrentLimitAutomatonSpec()

cloned.delegate = currentLimitAutomatonSpec
cloned()

if (!currentLimitAutomatonSpec.staticId) {
throw new DslException("'staticId' field is not set");
}
if (!currentLimitAutomatonSpec.parameterSetId) {
throw new DslException("'parameterSetId' field is not set")
}
if (!currentLimitAutomatonSpec.side) {
throw new DslException("'side' field is not set");
@Override
void checkData() {
super.checkData()
if (!side) {
throw new DslException("'side' field is not set")
}
}

String dynamicModelId = currentLimitAutomatonSpec.dynamicModelId ? currentLimitAutomatonSpec.dynamicModelId : currentLimitAutomatonSpec.staticId
consumer.accept(new CurrentLimitAutomaton(dynamicModelId, currentLimitAutomatonSpec.staticId, currentLimitAutomatonSpec.parameterSetId, currentLimitAutomatonSpec.side))
@Override
CurrentLimitAutomaton build() {
checkData()
new CurrentLimitAutomaton(dynamicModelId, staticId, parameterSetId, side)
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* 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.events

import com.powsybl.dsl.DslException
import com.powsybl.dynamicsimulation.EventModel
import com.powsybl.dynawaltz.dsl.ModelBuilder

/**
* @author Laurent Issertial <laurent.issertial at rte-france.com>
*/
abstract class AbstractEventModelBuilder implements ModelBuilder<EventModel> {

String eventModelId
String staticId
double startTime

void eventModelId(String eventModelId) {
this.eventModelId = eventModelId
}

void staticId(String staticId) {
this.staticId = staticId
}

void startTime(double startTime) {
this.startTime = startTime
}

void checkData() {
if (!staticId) {
throw new DslException("'staticId' field is not set")
}
if (!startTime) {
throw new DslException("'startTime' field is not set")
}
if (!eventModelId) {
eventModelId = staticId
}
}

@Override
abstract EventModel build();
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,10 @@
*/
package com.powsybl.dynawaltz.dsl.events

import java.util.function.Consumer

import com.google.auto.service.AutoService
import com.powsybl.dsl.DslException
import com.powsybl.dynamicsimulation.EventModel
import com.powsybl.dynamicsimulation.groovy.EventModelGroovyExtension

import com.powsybl.dynawaltz.DynaWaltzProvider
import com.powsybl.dynawaltz.dsl.AbstractPowsyblDynawoGroovyExtension
import com.powsybl.dynawaltz.models.events.EventQuadripoleDisconnection

/**
Expand All @@ -22,26 +18,21 @@ import com.powsybl.dynawaltz.models.events.EventQuadripoleDisconnection
* @author Marcos de Miguel <demiguelm at aia.es>
*/
@AutoService(EventModelGroovyExtension.class)
class EventQuadripoleDisconnectionGroovyExtension implements EventModelGroovyExtension {
class EventQuadripoleDisconnectionGroovyExtension extends AbstractPowsyblDynawoGroovyExtension<EventModel> implements EventModelGroovyExtension {

static class EventQuadripoleDisconnectionSpec {
String eventModelId
String staticId
double startTime
boolean disconnectOrigin
boolean disconnectExtremity
EventQuadripoleDisconnectionGroovyExtension() {
modelTags = ["EventQuadripoleDisconnection"]
}

void eventModelId(String eventModelId) {
this.eventModelId = eventModelId
}
@Override
protected EventQuadripoleDisconnectionBuilder createBuilder(String currentTag) {
new EventQuadripoleDisconnectionBuilder()
}

void staticId(String staticId) {
this.staticId = staticId
}
static class EventQuadripoleDisconnectionBuilder extends AbstractEventModelBuilder {

void startTime(double startTime) {
this.startTime = startTime
}
boolean disconnectOrigin
boolean disconnectExtremity

void disconnectOrigin(boolean disconnectOrigin) {
this.disconnectOrigin = disconnectOrigin
Expand All @@ -50,32 +41,11 @@ class EventQuadripoleDisconnectionGroovyExtension implements EventModelGroovyExt
void disconnectExtremity(boolean disconnectExtremity) {
this.disconnectExtremity = disconnectExtremity
}
}

String getName() {
return DynaWaltzProvider.NAME
}

void load(Binding binding, Consumer<EventModel> consumer) {
binding.EventQuadripoleDisconnection = { Closure<Void> closure ->
def cloned = closure.clone()
EventQuadripoleDisconnectionSpec eventQuadripoleDisconnectionSpec = new EventQuadripoleDisconnectionSpec()

cloned.delegate = eventQuadripoleDisconnectionSpec
cloned()

if (!eventQuadripoleDisconnectionSpec.staticId) {
throw new DslException("'staticId' field is not set");
}
if (!eventQuadripoleDisconnectionSpec.startTime) {
throw new DslException("'startTime' field is not set")
}

String eventModelId = eventQuadripoleDisconnectionSpec.eventModelId ? eventQuadripoleDisconnectionSpec.eventModelId : eventQuadripoleDisconnectionSpec.staticId
consumer.accept(new EventQuadripoleDisconnection(eventModelId, eventQuadripoleDisconnectionSpec.staticId,
eventQuadripoleDisconnectionSpec.startTime, eventQuadripoleDisconnectionSpec.disconnectOrigin,
eventQuadripoleDisconnectionSpec.disconnectExtremity))
@Override
EventQuadripoleDisconnection build() {
checkData()
new EventQuadripoleDisconnection(eventModelId, staticId, startTime, disconnectOrigin, disconnectExtremity)
}
}

}
Loading

0 comments on commit 9659223

Please sign in to comment.