Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ private MappingConstants() {

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

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

public static final String CURRENT_LIMIT_MODEL_CLASS = "CurrentLimitAutomaton";
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import lombok.Data;
import org.gridsuite.mapping.server.dto.automata.AbstractAutomaton;
import org.gridsuite.mapping.server.model.MappingEntity;

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

@Schema(description = "Mapping automata")
private List<AbstractAutomaton> automata;

public MappingEntity convertMappingToEntity() {
MappingEntity convertedMapping = new MappingEntity();
convertedMapping.setName(name);
convertedMapping.setRules(rules.stream().map(rule -> rule.convertRuleToEntity(convertedMapping)).collect(Collectors.toList()));
convertedMapping.setAutomata(automata.stream().map(automaton -> automaton.convertAutomatonToEntity(convertedMapping)).collect(Collectors.toList()));
return convertedMapping;
}

public InputMapping(MappingEntity mappingEntity) {
name = mappingEntity.getName();
rules = mappingEntity.getRules().stream().map(ruleEntity -> new Rule(ruleEntity)).collect(Collectors.toList());
automata = mappingEntity.getAutomata().stream().map(automatonEntity -> AbstractAutomaton.instantiateFromEntity(automatonEntity)).collect(Collectors.toList());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/**
* Copyright (c) 2021, 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.mapping.server.dto.automata;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonSubTypes;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import org.gridsuite.mapping.server.model.AutomatonEntity;
import org.gridsuite.mapping.server.model.MappingEntity;
import org.gridsuite.mapping.server.utils.AutomatonFamily;
import org.springframework.http.HttpStatus;
import org.springframework.web.client.HttpClientErrorException;

import java.util.ArrayList;

/**
* @author Mathieu Scalbert <mathieu.scalbert at rte-france.com>
*/
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "family", visible = true)
@JsonSubTypes({
@JsonSubTypes.Type(value = CurrentLimitAutomaton.class, name = "CURRENT_LIMIT")})
@Data
public abstract class AbstractAutomaton {
@Schema(description = "Automaton family")
@JsonProperty
private AutomatonFamily family;

@Schema(description = "Mapped Model Instance ID")
private String model;

@Schema(description = "Element watched by the automaton")
private String watchedElement;

public abstract ArrayList<BasicProperty> convertToBasicProperties();

public abstract AutomatonEntity convertAutomatonToEntity(MappingEntity parentMapping);

public static AbstractAutomaton instantiateFromEntity(AutomatonEntity automatonEntity) {
if (automatonEntity.getFamily() == AutomatonFamily.CURRENT_LIMIT) {
return new CurrentLimitAutomaton(automatonEntity);
} else {
throw new HttpClientErrorException(HttpStatus.BAD_REQUEST);
}
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package org.gridsuite.mapping.server.dto.automata;

import lombok.AllArgsConstructor;
import lombok.Data;

@Data
@AllArgsConstructor
public class BasicProperty {
private String name;

private String value;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/**
* Copyright (c) 2021, 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.mapping.server.dto.automata;

import lombok.*;
import org.gridsuite.mapping.server.model.AutomatonEntity;
import org.gridsuite.mapping.server.model.AutomatonPropertyEntity;
import org.gridsuite.mapping.server.model.MappingEntity;
import org.gridsuite.mapping.server.utils.PropertyType;

import java.util.ArrayList;
import java.util.Optional;
import java.util.UUID;

/**
* @author Mathieu Scalbert <mathieu.scalbert at rte-france.com>
*/
@Data
@EqualsAndHashCode(callSuper = true)
@NoArgsConstructor
public class CurrentLimitAutomaton extends AbstractAutomaton {
private String side;

public ArrayList<BasicProperty> convertToBasicProperties() {
ArrayList<BasicProperty> propertiesList = new ArrayList<>();
propertiesList.add(new BasicProperty("side", side));
return propertiesList;
}

public CurrentLimitAutomaton(AutomatonEntity automatonEntity) {
this.setFamily(automatonEntity.getFamily());
this.setModel(automatonEntity.getModel());
this.setWatchedElement(automatonEntity.getWatchedElement());
// TODO Create generic function for all properties
Optional<AutomatonPropertyEntity> foundSideProperty = automatonEntity.getProperties().stream().filter(property -> property.getName().equals("side")).findAny();
if (foundSideProperty.isPresent()) {
side = foundSideProperty.get().getValue();
}
}

public AutomatonEntity convertAutomatonToEntity(MappingEntity parentMapping) {
UUID createdId = UUID.randomUUID();
AutomatonEntity convertedAutomaton = new AutomatonEntity();
convertedAutomaton.setAutomatonId(createdId);
convertedAutomaton.setFamily(this.getFamily());
convertedAutomaton.setModel(this.getModel());
convertedAutomaton.setWatchedElement(this.getWatchedElement());
convertedAutomaton.setMapping(parentMapping);
ArrayList<AutomatonPropertyEntity> convertedProperties = new ArrayList<>();
AutomatonPropertyEntity convertedProperty = new AutomatonPropertyEntity();
convertedProperty.setAutomatonId(createdId);
convertedProperty.setName("side");
convertedProperty.setValue(this.getSide());
convertedProperty.setType(PropertyType.STRING);
convertedProperties.add(convertedProperty);
convertedAutomaton.setProperties(convertedProperties);
return convertedAutomaton;
}
}


Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/**
* Copyright (c) 2021, 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.mapping.server.model;

import lombok.*;
import org.gridsuite.mapping.server.utils.AutomatonFamily;
import javax.persistence.*;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

/**
* @author Mathieu Scalbert <mathieu.scalbert at rte-france.com>
*/
@Entity
@Builder
@Table(name = "automata", indexes = {@Index(name = "automaton_mappingName_index", columnList = "mappingName")})
@NoArgsConstructor
@AllArgsConstructor
@Getter
@Setter
public class AutomatonEntity extends AbstractManuallyAssignedIdentifierEntity<UUID> {

@Id
@Column(name = "automaton_id")
private UUID automatonId;

@Column(name = "type", nullable = false)
@Enumerated
private AutomatonFamily family;

@Column(name = "model", nullable = false)
private String model;

@Column(name = "watched_element", nullable = false)
private String watchedElement;

@OneToMany(targetEntity = AutomatonPropertyEntity.class, mappedBy = "automaton", cascade = CascadeType.ALL, orphanRemoval = true)
private List<AutomatonPropertyEntity> properties;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "mappingName", foreignKey = @ForeignKey(name = "mapping_automata_fk"), referencedColumnName = "name")
private MappingEntity mapping;

@Override
public UUID getId() {
return automatonId;
}

public AutomatonEntity(MappingEntity mapping, AutomatonEntity automatonToCopy) {
UUID newID = UUID.randomUUID();
this.automatonId = newID;
this.mapping = mapping;
this.family = automatonToCopy.getFamily();
this.model = automatonToCopy.getModel();
this.watchedElement = automatonToCopy.getWatchedElement();
this.properties = automatonToCopy.getProperties().stream().map(automatonPropertyEntity -> new AutomatonPropertyEntity(newID, automatonPropertyEntity)).collect(Collectors.toList());

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/**
* Copyright (c) 2021, 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.mapping.server.model;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.gridsuite.mapping.server.utils.PropertyType;

import javax.persistence.*;
import java.io.Serializable;
import java.util.UUID;

/**
* @author Mathieu Scalbert <mathieu.scalbert at rte-france.com>
*/

@Inheritance
@NoArgsConstructor
@AllArgsConstructor
@Getter
@Setter
@Entity
@Table(name = "automaton_properties", indexes = {@Index(name = "property_automaton_id_index", columnList = "automaton_id")})
@IdClass(AutomatonPropertyId.class)
public class AutomatonPropertyEntity implements Serializable {

@Id
@Column(name = "automaton_id")
private UUID automatonId;

@Id
@Column(name = "name")
private String name;

@Column(name = "value")
private String value;

@Column(name = "type")
private PropertyType type;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "automaton_id", foreignKey = @ForeignKey(name = "automata_property_fk"))
@MapsId("automatonId")
private AutomatonEntity automaton;

public AutomatonPropertyEntity(UUID automatonId, AutomatonPropertyEntity automatonPropertyEntity) {
this.automatonId = automatonId;
this.name = automatonPropertyEntity.getName();
this.type = automatonPropertyEntity.getType();
this.value = automatonPropertyEntity.getValue();

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/**
* Copyright (c) 2021, 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.mapping.server.model;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import javax.persistence.Embeddable;
import java.io.Serializable;
import java.util.Objects;
import java.util.UUID;

/**
* @author Mathieu Scalbert <mathieu.scalbert at rte-france.com>
*/
@NoArgsConstructor
@AllArgsConstructor
@Embeddable
@Getter
@Setter
public class AutomatonPropertyId implements Serializable {

private String name;

private UUID automatonId;

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
AutomatonPropertyId automatonPropertyIdClass = (AutomatonPropertyId) o;
return name.equals(automatonPropertyIdClass.name) &&
automatonId.equals(automatonPropertyIdClass.automatonId);
}

@Override
public int hashCode() {
return Objects.hash(name, automatonId);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ public class MappingEntity extends AbstractManuallyAssignedIdentifierEntity<Stri
@OneToMany(targetEntity = RuleEntity.class, mappedBy = "mapping", cascade = CascadeType.ALL, orphanRemoval = true)
private List<RuleEntity> rules;

@OneToMany(targetEntity = AutomatonEntity.class, mappedBy = "mapping", cascade = CascadeType.ALL, orphanRemoval = true)
private List<AutomatonEntity> automata;

@Override
public String getId() {
return name;
Expand All @@ -36,5 +39,6 @@ public String getId() {
public MappingEntity(String name, MappingEntity mappingToCopy) {
this.name = name;
this.rules = mappingToCopy.getRules().stream().map(ruleEntity -> new RuleEntity(this, ruleEntity)).collect(Collectors.toList());
this.automata = mappingToCopy.getAutomata().stream().map(automatonEntity -> new AutomatonEntity(this, automatonEntity)).collect(Collectors.toList());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public RenameObject renameMapping(String oldName, String newName) {
} else if (oldName.equals(DEFAULT_MAPPING_NAME)) {
// In case of naming of new mapping, save it to db.
try {
mappingRepository.save(new MappingEntity(newName, new ArrayList<>()));
mappingRepository.save(new MappingEntity(newName, new ArrayList<>(), new ArrayList<>()));
return new RenameObject(DEFAULT_MAPPING_NAME, newName);

} catch (DataIntegrityViolationException ex) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import static java.util.stream.Collectors.groupingBy;

import org.gridsuite.mapping.server.dto.*;
import org.gridsuite.mapping.server.dto.automata.AbstractAutomaton;
import org.gridsuite.mapping.server.model.InstanceModelEntity;
import org.gridsuite.mapping.server.model.MappingEntity;
import org.gridsuite.mapping.server.model.ScriptEntity;
Expand Down Expand Up @@ -123,9 +124,11 @@ public Script copyScript(String originalName, String copyName) {
public class SortedMapping implements Mapping {
private String name;
private ArrayList<SortedRules> sortedRules;
private ArrayList<AbstractAutomaton> automata;

public SortedMapping(InputMapping mapping) {
name = mapping.getName();
automata = (ArrayList<AbstractAutomaton>) mapping.getAutomata();
sortedRules = new ArrayList<>();
Map<EquipmentType, List<Rule>> sortingRules = mapping.getRules().stream().collect(groupingBy(Rule::getEquipmentType));
for (EquipmentType type : sortingRules.keySet()) {
Expand Down
Loading