Skip to content

Commit

Permalink
Dynamic Simulation - Event configuration (#65)
Browse files Browse the repository at this point in the history
This PR implements the groovy generator for events which are received from the study server when launching the simulation.

Front-end PR: gridsuite/gridstudy-app#1429 (to see Demo)
Back-end PR: gridsuite/study-server#418
  • Loading branch information
thangqp authored Oct 17, 2023
1 parent 4434755 commit a91caee
Show file tree
Hide file tree
Showing 16 changed files with 299 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.gridsuite.ds.server.dto.curve.CurveInfos;
import org.gridsuite.ds.server.dto.event.EventInfos;
import org.gridsuite.ds.server.dto.network.NetworkInfos;
import org.gridsuite.ds.server.dto.solver.SolverInfos;

Expand All @@ -36,4 +37,5 @@ public class DynamicSimulationParametersInfos {
private List<SolverInfos> solvers;
private NetworkInfos network;
private List<CurveInfos> curves;
private List<EventInfos> events;
}
42 changes: 42 additions & 0 deletions src/main/java/org/gridsuite/ds/server/dto/event/EventInfos.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* 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/.
*/

package org.gridsuite.ds.server.dto.event;

import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
* @author Thang PHAM <quyet-thang.pham at rte-france.com>
*/
@AllArgsConstructor
@NoArgsConstructor
@Getter
@Setter
public class EventInfos {

@JsonProperty("uuid")
private UUID id;

private UUID nodeId;

private String equipmentId;

private String equipmentType;

private String eventType;

private List<EventPropertyInfos> properties = new ArrayList<>();

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

package org.gridsuite.ds.server.dto.event;

import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.gridsuite.ds.server.utils.PropertyType;

import java.util.UUID;

/**
* @author Thang PHAM <quyet-thang.pham at rte-france.com>
*/
@AllArgsConstructor
@NoArgsConstructor
@Getter
@Setter
public class EventPropertyInfos {

@JsonProperty("uuid")
private UUID id;

private String name;

private String value;

private PropertyType type;

}
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,10 @@ public Mono<UUID> runAndSaveResult(String receiver, UUID networkUuid, String var
parameters.setStartTime(parametersInfos.getStartTime().intValue()); // TODO remove intValue() when correct startTime to double in powsyble
parameters.setStopTime(parametersInfos.getStopTime().intValue()); // TODO remove intValue() when correct stopTime to double in powsyble

// groovy scripts
String script = scriptObj.getScript();
byte[] dynamicModel = script.getBytes(StandardCharsets.UTF_8);
byte[] eventModel = parametersService.getEventModel();

byte[] eventModel = parametersService.getEventModel(parametersInfos.getEvents());
byte[] curveModel = parametersService.getCurveModel(parametersInfos.getCurves());

DynamicSimulationRunContext runContext = new DynamicSimulationRunContext(dsProvider, receiver, networkUuid, variantId, dynamicModel, eventModel, curveModel, parameters);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
* @author Thang PHAM <quyet-thang.pham at rte-france.com>
*/
public interface CurveGroovyGeneratorService {
String VARIABLE_PATH_DELIMETER = "/";
String RESOURCE_PATH_DELIMETER = "/";
String CURVES_TEMPLATE_DIR = "templates/curve";

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* 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/.
*/

package org.gridsuite.ds.server.service.parameters;

import org.gridsuite.ds.server.dto.event.EventInfos;

import java.util.List;

/**
* @author Thang PHAM <quyet-thang.pham at rte-france.com>
*/
public interface EventGroovyGeneratorService {
String RESOURCE_PATH_DELIMETER = "/";
String EVENTS_TEMPLATE_DIR = "templates/event";

/**
* Generate a script groovy which contains events
* @param events given list of events
* @return a script groovy which contains events
*/
String generate(List<EventInfos> events);
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import com.powsybl.dynamicsimulation.DynamicSimulationParameters;
import org.gridsuite.ds.server.dto.DynamicSimulationParametersInfos;
import org.gridsuite.ds.server.dto.curve.CurveInfos;
import org.gridsuite.ds.server.dto.event.EventInfos;

import java.util.List;

Expand All @@ -27,7 +28,7 @@ public interface ParametersService {
String SOLVERS_PAR = "solvers.par";
String PARAMETERS_JSON = "parameters.json";

byte[] getEventModel();
byte[] getEventModel(List<EventInfos> events);

byte[] getCurveModel(List<CurveInfos> curves);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*
* 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/.
*/

package org.gridsuite.ds.server.service.parameters.impl;

import com.powsybl.commons.PowsyblException;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.gridsuite.ds.server.dto.event.EventInfos;
import org.gridsuite.ds.server.dto.event.EventPropertyInfos;
import org.gridsuite.ds.server.service.parameters.EventGroovyGeneratorService;
import org.gridsuite.ds.server.utils.PropertyType;
import org.gridsuite.ds.server.utils.Utils;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Service;
import org.stringtemplate.v4.ST;

import java.io.IOException;
import java.nio.charset.Charset;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
* @author Thang PHAM <quyet-thang.pham at rte-france.com>
*/
@Service
public class EventGroovyGeneratorServiceImpl implements EventGroovyGeneratorService {
@Override
public String generate(List<EventInfos> events) {
Objects.requireNonNull(events);

String eventsTemplate;
String eventTemplate;
String eventPropertyTemplate;
try {
eventsTemplate = IOUtils.toString(new ClassPathResource(EVENTS_TEMPLATE_DIR + RESOURCE_PATH_DELIMETER + "events.st").getInputStream(), Charset.defaultCharset());
eventTemplate = IOUtils.toString(new ClassPathResource(EVENTS_TEMPLATE_DIR + RESOURCE_PATH_DELIMETER + "event.st").getInputStream(), Charset.defaultCharset());
eventPropertyTemplate = IOUtils.toString(new ClassPathResource(EVENTS_TEMPLATE_DIR + RESOURCE_PATH_DELIMETER + "eventProperty.st").getInputStream(), Charset.defaultCharset());
} catch (IOException e) {
throw new PowsyblException("Unable to load templates for groovy script generation : " + e.getMessage());
}
// config root template
ST eventsST = new ST(eventsTemplate);

String[] eventStringList = events.stream().map(event -> generateEvent(eventTemplate, eventPropertyTemplate, event)).toArray(String[]::new);
eventsST.add("events", eventStringList);

return eventsST.render();
}

private String generateEvent(String eventTemplate, String eventPropertyTemplate, EventInfos event) {
ST eventST = new ST(eventTemplate);
eventST.add("eventType", event.getEventType());

// --- add properties ---
String[] propertyStringList = event.getProperties().stream()
.map(property -> generateEventProperty(eventPropertyTemplate, property))
.filter(property -> !StringUtils.isEmpty(property)).toArray(String[]::new);
eventST.add("properties", propertyStringList);

return eventST.render();
}

private String generateEventProperty(String eventPropertyTemplate, EventPropertyInfos property) {
ST eventPropertyST = new ST(eventPropertyTemplate);
String value = property.getValue();
if (StringUtils.isEmpty(value)) {
return null;
}

// value => "value" when export groovy string value
if (property.getType() == PropertyType.STRING) {
value = Utils.convertStringToList(value).stream()
.map(elem -> "\"" + elem + "\"")
.collect(Collectors.joining(", "));
}

eventPropertyST.add("name", property.getName());
eventPropertyST.add("value", value);

return eventPropertyST.render();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@
import org.gridsuite.ds.server.dto.DynamicSimulationParametersInfos;
import org.gridsuite.ds.server.dto.XmlSerializableParameter;
import org.gridsuite.ds.server.dto.curve.CurveInfos;
import org.gridsuite.ds.server.dto.event.EventInfos;
import org.gridsuite.ds.server.dto.network.NetworkInfos;
import org.gridsuite.ds.server.dto.solver.SolverInfos;
import org.gridsuite.ds.server.service.parameters.CurveGroovyGeneratorService;
import org.gridsuite.ds.server.service.parameters.EventGroovyGeneratorService;
import org.gridsuite.ds.server.service.parameters.ParametersService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -41,18 +43,28 @@ public class ParametersServiceImpl implements ParametersService {

private final CurveGroovyGeneratorService curveGroovyGeneratorService;

private final EventGroovyGeneratorService eventGroovyGeneratorService;

@Autowired
public ParametersServiceImpl(CurveGroovyGeneratorService curveGroovyGeneratorService) {
public ParametersServiceImpl(CurveGroovyGeneratorService curveGroovyGeneratorService, EventGroovyGeneratorService eventGroovyGeneratorService) {
this.curveGroovyGeneratorService = curveGroovyGeneratorService;
this.eventGroovyGeneratorService = eventGroovyGeneratorService;
}

@Override
public byte[] getEventModel() {
try (InputStream is = getClass().getResourceAsStream(PARAMETERS_DIR + RESOURCE_PATH_DELIMETER + EVENTS_GROOVY)) {
// read the events.groovy in the "parameters" resources
return is.readAllBytes();
} catch (IOException e) {
throw new UncheckedIOException(e);
public byte[] getEventModel(List<EventInfos> events) {
if (events != null) {
String generatedGroovyEvents = eventGroovyGeneratorService.generate(events);
LOGGER.info(generatedGroovyEvents);
return generatedGroovyEvents.getBytes(StandardCharsets.UTF_8);
} else {
// TODO remove reading from hard file
try (InputStream is = getClass().getResourceAsStream(PARAMETERS_DIR + RESOURCE_PATH_DELIMETER + EVENTS_GROOVY)) {
// read the events.groovy in the "parameters" resources
return is.readAllBytes();
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
}

Expand Down
18 changes: 18 additions & 0 deletions src/main/java/org/gridsuite/ds/server/utils/PropertyType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
* 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/.
*/
package org.gridsuite.ds.server.utils;

/**
* @author Thang PHAM <quyet-thang.pham at rte-france.com>
*/
public enum PropertyType {
ENUM,
BOOLEAN,
INTEGER,
FLOAT,
STRING,
}
17 changes: 17 additions & 0 deletions src/main/java/org/gridsuite/ds/server/utils/Utils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package org.gridsuite.ds.server.utils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public final class Utils {
private Utils() {
// never called
}

public static List<String> convertStringToList(String stringArray) {
List<String> converted = new ArrayList<>();
converted.addAll(Arrays.asList(stringArray.split(",")));
return converted;
}
}
3 changes: 3 additions & 0 deletions src/main/resources/templates/event/event.st
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<eventType> {
<properties; separator="\n">
}
1 change: 1 addition & 0 deletions src/main/resources/templates/event/eventProperty.st
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<name> <value>
11 changes: 11 additions & 0 deletions src/main/resources/templates/event/events.st
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/**
* Copyright (c) 2020, 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/.
*/


import com.powsybl.iidm.network.Branch

<events; separator="\n">
Loading

0 comments on commit a91caee

Please sign in to comment.