Skip to content

Commit c77b7f9

Browse files
authored
feat: Add automata to mapping possibilities (#9)
1 parent 744951b commit c77b7f9

19 files changed

+464
-45
lines changed

src/main/java/org/gridsuite/mapping/server/MappingConstants.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ private MappingConstants() {
1717

1818
public static String EQUIPMENT_ID = "equipment.id";
1919
public static String IMPORT = "import com.powsybl.iidm.network.";
20+
public static String AUTOMATON_IMPORT = "import com.powsybl.dynawaltz.automatons.CurrentLimitAutomaton\nimport com.powsybl.iidm.network.Branch";
2021
public static String DEFAULT_MAPPING_NAME = "default";
2122

2223
public static final String CASE_API_VERSION = "v1";
@@ -33,4 +34,5 @@ private MappingConstants() {
3334
// Loads
3435
public static final String LOAD_TYPE_PROPERTY = "loadType";
3536

37+
public static final String CURRENT_LIMIT_MODEL_CLASS = "CurrentLimitAutomaton";
3638
}

src/main/java/org/gridsuite/mapping/server/dto/InputMapping.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import io.swagger.v3.oas.annotations.media.Schema;
1010
import lombok.AllArgsConstructor;
1111
import lombok.Data;
12+
import org.gridsuite.mapping.server.dto.automata.AbstractAutomaton;
1213
import org.gridsuite.mapping.server.model.MappingEntity;
1314

1415
import java.util.List;
@@ -27,15 +28,20 @@ public class InputMapping implements Mapping {
2728
@Schema(description = "Mapping rules")
2829
private List<Rule> rules;
2930

31+
@Schema(description = "Mapping automata")
32+
private List<AbstractAutomaton> automata;
33+
3034
public MappingEntity convertMappingToEntity() {
3135
MappingEntity convertedMapping = new MappingEntity();
3236
convertedMapping.setName(name);
3337
convertedMapping.setRules(rules.stream().map(rule -> rule.convertRuleToEntity(convertedMapping)).collect(Collectors.toList()));
38+
convertedMapping.setAutomata(automata.stream().map(automaton -> automaton.convertAutomatonToEntity(convertedMapping)).collect(Collectors.toList()));
3439
return convertedMapping;
3540
}
3641

3742
public InputMapping(MappingEntity mappingEntity) {
3843
name = mappingEntity.getName();
3944
rules = mappingEntity.getRules().stream().map(ruleEntity -> new Rule(ruleEntity)).collect(Collectors.toList());
45+
automata = mappingEntity.getAutomata().stream().map(automatonEntity -> AbstractAutomaton.instantiateFromEntity(automatonEntity)).collect(Collectors.toList());
4046
}
4147
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/**
2+
* Copyright (c) 2021, RTE (http://www.rte-france.com)
3+
* This Source Code Form is subject to the terms of the Mozilla Public
4+
* License, v. 2.0. If a copy of the MPL was not distributed with this
5+
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
6+
*/
7+
package org.gridsuite.mapping.server.dto.automata;
8+
9+
import com.fasterxml.jackson.annotation.JsonProperty;
10+
import com.fasterxml.jackson.annotation.JsonSubTypes;
11+
import com.fasterxml.jackson.annotation.JsonTypeInfo;
12+
import io.swagger.v3.oas.annotations.media.Schema;
13+
import lombok.Data;
14+
import org.gridsuite.mapping.server.model.AutomatonEntity;
15+
import org.gridsuite.mapping.server.model.MappingEntity;
16+
import org.gridsuite.mapping.server.utils.AutomatonFamily;
17+
import org.springframework.http.HttpStatus;
18+
import org.springframework.web.client.HttpClientErrorException;
19+
20+
import java.util.ArrayList;
21+
22+
/**
23+
* @author Mathieu Scalbert <mathieu.scalbert at rte-france.com>
24+
*/
25+
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "family", visible = true)
26+
@JsonSubTypes({
27+
@JsonSubTypes.Type(value = CurrentLimitAutomaton.class, name = "CURRENT_LIMIT")})
28+
@Data
29+
public abstract class AbstractAutomaton {
30+
@Schema(description = "Automaton family")
31+
@JsonProperty
32+
private AutomatonFamily family;
33+
34+
@Schema(description = "Mapped Model Instance ID")
35+
private String model;
36+
37+
@Schema(description = "Element watched by the automaton")
38+
private String watchedElement;
39+
40+
public abstract ArrayList<BasicProperty> convertToBasicProperties();
41+
42+
public abstract AutomatonEntity convertAutomatonToEntity(MappingEntity parentMapping);
43+
44+
public static AbstractAutomaton instantiateFromEntity(AutomatonEntity automatonEntity) {
45+
if (automatonEntity.getFamily() == AutomatonFamily.CURRENT_LIMIT) {
46+
return new CurrentLimitAutomaton(automatonEntity);
47+
} else {
48+
throw new HttpClientErrorException(HttpStatus.BAD_REQUEST);
49+
}
50+
}
51+
}
52+
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package org.gridsuite.mapping.server.dto.automata;
2+
3+
import lombok.AllArgsConstructor;
4+
import lombok.Data;
5+
6+
@Data
7+
@AllArgsConstructor
8+
public class BasicProperty {
9+
private String name;
10+
11+
private String value;
12+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/**
2+
* Copyright (c) 2021, RTE (http://www.rte-france.com)
3+
* This Source Code Form is subject to the terms of the Mozilla Public
4+
* License, v. 2.0. If a copy of the MPL was not distributed with this
5+
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
6+
*/
7+
package org.gridsuite.mapping.server.dto.automata;
8+
9+
import lombok.*;
10+
import org.gridsuite.mapping.server.model.AutomatonEntity;
11+
import org.gridsuite.mapping.server.model.AutomatonPropertyEntity;
12+
import org.gridsuite.mapping.server.model.MappingEntity;
13+
import org.gridsuite.mapping.server.utils.PropertyType;
14+
15+
import java.util.ArrayList;
16+
import java.util.Optional;
17+
import java.util.UUID;
18+
19+
/**
20+
* @author Mathieu Scalbert <mathieu.scalbert at rte-france.com>
21+
*/
22+
@Data
23+
@EqualsAndHashCode(callSuper = true)
24+
@NoArgsConstructor
25+
public class CurrentLimitAutomaton extends AbstractAutomaton {
26+
private String side;
27+
28+
public ArrayList<BasicProperty> convertToBasicProperties() {
29+
ArrayList<BasicProperty> propertiesList = new ArrayList<>();
30+
propertiesList.add(new BasicProperty("side", side));
31+
return propertiesList;
32+
}
33+
34+
public CurrentLimitAutomaton(AutomatonEntity automatonEntity) {
35+
this.setFamily(automatonEntity.getFamily());
36+
this.setModel(automatonEntity.getModel());
37+
this.setWatchedElement(automatonEntity.getWatchedElement());
38+
// TODO Create generic function for all properties
39+
Optional<AutomatonPropertyEntity> foundSideProperty = automatonEntity.getProperties().stream().filter(property -> property.getName().equals("side")).findAny();
40+
if (foundSideProperty.isPresent()) {
41+
side = foundSideProperty.get().getValue();
42+
}
43+
}
44+
45+
public AutomatonEntity convertAutomatonToEntity(MappingEntity parentMapping) {
46+
UUID createdId = UUID.randomUUID();
47+
AutomatonEntity convertedAutomaton = new AutomatonEntity();
48+
convertedAutomaton.setAutomatonId(createdId);
49+
convertedAutomaton.setFamily(this.getFamily());
50+
convertedAutomaton.setModel(this.getModel());
51+
convertedAutomaton.setWatchedElement(this.getWatchedElement());
52+
convertedAutomaton.setMapping(parentMapping);
53+
ArrayList<AutomatonPropertyEntity> convertedProperties = new ArrayList<>();
54+
AutomatonPropertyEntity convertedProperty = new AutomatonPropertyEntity();
55+
convertedProperty.setAutomatonId(createdId);
56+
convertedProperty.setName("side");
57+
convertedProperty.setValue(this.getSide());
58+
convertedProperty.setType(PropertyType.STRING);
59+
convertedProperties.add(convertedProperty);
60+
convertedAutomaton.setProperties(convertedProperties);
61+
return convertedAutomaton;
62+
}
63+
}
64+
65+
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/**
2+
* Copyright (c) 2021, RTE (http://www.rte-france.com)
3+
* This Source Code Form is subject to the terms of the Mozilla Public
4+
* License, v. 2.0. If a copy of the MPL was not distributed with this
5+
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
6+
*/
7+
package org.gridsuite.mapping.server.model;
8+
9+
import lombok.*;
10+
import org.gridsuite.mapping.server.utils.AutomatonFamily;
11+
import javax.persistence.*;
12+
import java.util.List;
13+
import java.util.UUID;
14+
import java.util.stream.Collectors;
15+
16+
/**
17+
* @author Mathieu Scalbert <mathieu.scalbert at rte-france.com>
18+
*/
19+
@Entity
20+
@Builder
21+
@Table(name = "automata", indexes = {@Index(name = "automaton_mappingName_index", columnList = "mappingName")})
22+
@NoArgsConstructor
23+
@AllArgsConstructor
24+
@Getter
25+
@Setter
26+
public class AutomatonEntity extends AbstractManuallyAssignedIdentifierEntity<UUID> {
27+
28+
@Id
29+
@Column(name = "automaton_id")
30+
private UUID automatonId;
31+
32+
@Column(name = "type", nullable = false)
33+
@Enumerated
34+
private AutomatonFamily family;
35+
36+
@Column(name = "model", nullable = false)
37+
private String model;
38+
39+
@Column(name = "watched_element", nullable = false)
40+
private String watchedElement;
41+
42+
@OneToMany(targetEntity = AutomatonPropertyEntity.class, mappedBy = "automaton", cascade = CascadeType.ALL, orphanRemoval = true)
43+
private List<AutomatonPropertyEntity> properties;
44+
45+
@ManyToOne(fetch = FetchType.LAZY)
46+
@JoinColumn(name = "mappingName", foreignKey = @ForeignKey(name = "mapping_automata_fk"), referencedColumnName = "name")
47+
private MappingEntity mapping;
48+
49+
@Override
50+
public UUID getId() {
51+
return automatonId;
52+
}
53+
54+
public AutomatonEntity(MappingEntity mapping, AutomatonEntity automatonToCopy) {
55+
UUID newID = UUID.randomUUID();
56+
this.automatonId = newID;
57+
this.mapping = mapping;
58+
this.family = automatonToCopy.getFamily();
59+
this.model = automatonToCopy.getModel();
60+
this.watchedElement = automatonToCopy.getWatchedElement();
61+
this.properties = automatonToCopy.getProperties().stream().map(automatonPropertyEntity -> new AutomatonPropertyEntity(newID, automatonPropertyEntity)).collect(Collectors.toList());
62+
63+
}
64+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/**
2+
* Copyright (c) 2021, RTE (http://www.rte-france.com)
3+
* This Source Code Form is subject to the terms of the Mozilla Public
4+
* License, v. 2.0. If a copy of the MPL was not distributed with this
5+
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
6+
*/
7+
package org.gridsuite.mapping.server.model;
8+
9+
import lombok.AllArgsConstructor;
10+
import lombok.Getter;
11+
import lombok.NoArgsConstructor;
12+
import lombok.Setter;
13+
import org.gridsuite.mapping.server.utils.PropertyType;
14+
15+
import javax.persistence.*;
16+
import java.io.Serializable;
17+
import java.util.UUID;
18+
19+
/**
20+
* @author Mathieu Scalbert <mathieu.scalbert at rte-france.com>
21+
*/
22+
23+
@Inheritance
24+
@NoArgsConstructor
25+
@AllArgsConstructor
26+
@Getter
27+
@Setter
28+
@Entity
29+
@Table(name = "automaton_properties", indexes = {@Index(name = "property_automaton_id_index", columnList = "automaton_id")})
30+
@IdClass(AutomatonPropertyId.class)
31+
public class AutomatonPropertyEntity implements Serializable {
32+
33+
@Id
34+
@Column(name = "automaton_id")
35+
private UUID automatonId;
36+
37+
@Id
38+
@Column(name = "name")
39+
private String name;
40+
41+
@Column(name = "value")
42+
private String value;
43+
44+
@Column(name = "type")
45+
private PropertyType type;
46+
47+
@ManyToOne(fetch = FetchType.LAZY)
48+
@JoinColumn(name = "automaton_id", foreignKey = @ForeignKey(name = "automata_property_fk"))
49+
@MapsId("automatonId")
50+
private AutomatonEntity automaton;
51+
52+
public AutomatonPropertyEntity(UUID automatonId, AutomatonPropertyEntity automatonPropertyEntity) {
53+
this.automatonId = automatonId;
54+
this.name = automatonPropertyEntity.getName();
55+
this.type = automatonPropertyEntity.getType();
56+
this.value = automatonPropertyEntity.getValue();
57+
58+
}
59+
60+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/**
2+
* Copyright (c) 2021, RTE (http://www.rte-france.com)
3+
* This Source Code Form is subject to the terms of the Mozilla Public
4+
* License, v. 2.0. If a copy of the MPL was not distributed with this
5+
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
6+
*/
7+
package org.gridsuite.mapping.server.model;
8+
9+
import lombok.AllArgsConstructor;
10+
import lombok.Getter;
11+
import lombok.NoArgsConstructor;
12+
import lombok.Setter;
13+
14+
import javax.persistence.Embeddable;
15+
import java.io.Serializable;
16+
import java.util.Objects;
17+
import java.util.UUID;
18+
19+
/**
20+
* @author Mathieu Scalbert <mathieu.scalbert at rte-france.com>
21+
*/
22+
@NoArgsConstructor
23+
@AllArgsConstructor
24+
@Embeddable
25+
@Getter
26+
@Setter
27+
public class AutomatonPropertyId implements Serializable {
28+
29+
private String name;
30+
31+
private UUID automatonId;
32+
33+
@Override
34+
public boolean equals(Object o) {
35+
if (this == o) {
36+
return true;
37+
}
38+
if (o == null || getClass() != o.getClass()) {
39+
return false;
40+
}
41+
AutomatonPropertyId automatonPropertyIdClass = (AutomatonPropertyId) o;
42+
return name.equals(automatonPropertyIdClass.name) &&
43+
automatonId.equals(automatonPropertyIdClass.automatonId);
44+
}
45+
46+
@Override
47+
public int hashCode() {
48+
return Objects.hash(name, automatonId);
49+
}
50+
51+
}

src/main/java/org/gridsuite/mapping/server/model/MappingEntity.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ public class MappingEntity extends AbstractManuallyAssignedIdentifierEntity<Stri
2828
@OneToMany(targetEntity = RuleEntity.class, mappedBy = "mapping", cascade = CascadeType.ALL, orphanRemoval = true)
2929
private List<RuleEntity> rules;
3030

31+
@OneToMany(targetEntity = AutomatonEntity.class, mappedBy = "mapping", cascade = CascadeType.ALL, orphanRemoval = true)
32+
private List<AutomatonEntity> automata;
33+
3134
@Override
3235
public String getId() {
3336
return name;
@@ -36,5 +39,6 @@ public String getId() {
3639
public MappingEntity(String name, MappingEntity mappingToCopy) {
3740
this.name = name;
3841
this.rules = mappingToCopy.getRules().stream().map(ruleEntity -> new RuleEntity(this, ruleEntity)).collect(Collectors.toList());
42+
this.automata = mappingToCopy.getAutomata().stream().map(automatonEntity -> new AutomatonEntity(this, automatonEntity)).collect(Collectors.toList());
3943
}
4044
}

src/main/java/org/gridsuite/mapping/server/service/implementation/MappingServiceImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public RenameObject renameMapping(String oldName, String newName) {
7575
} else if (oldName.equals(DEFAULT_MAPPING_NAME)) {
7676
// In case of naming of new mapping, save it to db.
7777
try {
78-
mappingRepository.save(new MappingEntity(newName, new ArrayList<>()));
78+
mappingRepository.save(new MappingEntity(newName, new ArrayList<>(), new ArrayList<>()));
7979
return new RenameObject(DEFAULT_MAPPING_NAME, newName);
8080

8181
} catch (DataIntegrityViolationException ex) {

0 commit comments

Comments
 (0)