-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Dynamic Simulation - Event configuration (#65)
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
Showing
16 changed files
with
299 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
src/main/java/org/gridsuite/ds/server/dto/event/EventInfos.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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<>(); | ||
|
||
} |
37 changes: 37 additions & 0 deletions
37
src/main/java/org/gridsuite/ds/server/dto/event/EventPropertyInfos.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
src/main/java/org/gridsuite/ds/server/service/parameters/EventGroovyGeneratorService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 88 additions & 0 deletions
88
...java/org/gridsuite/ds/server/service/parameters/impl/EventGroovyGeneratorServiceImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
src/main/java/org/gridsuite/ds/server/utils/PropertyType.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<eventType> { | ||
<properties; separator="\n"> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<name> <value> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"> |
Oops, something went wrong.