-
Notifications
You must be signed in to change notification settings - Fork 5
/
SystemParticipantEntityData.java
152 lines (136 loc) · 5.98 KB
/
SystemParticipantEntityData.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/*
* © 2023. TU Dortmund University,
* Institute of Energy Systems, Energy Efficiency and Energy Economics,
* Research group Distribution grid planning and operation
*/
package edu.ie3.datamodel.io.factory.input.participant;
import edu.ie3.datamodel.exceptions.SourceException;
import edu.ie3.datamodel.io.factory.input.NodeAssetInputEntityData;
import edu.ie3.datamodel.models.UniqueEntity;
import edu.ie3.datamodel.models.input.NodeInput;
import edu.ie3.datamodel.models.input.OperatorInput;
import edu.ie3.datamodel.models.input.system.EmInput;
import edu.ie3.datamodel.utils.Try;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.UUID;
/**
* Data used for those classes of {@link
* edu.ie3.datamodel.models.input.system.SystemParticipantInput}, including an (optional) link to an
* {@link EmInput} entity.
*/
public class SystemParticipantEntityData extends NodeAssetInputEntityData {
/** Energy management unit that is managing the system participant. Can be null. */
private final EmInput em;
/**
* Creates a new SystemParticipantEntityData object for an operated, always on system participant
* input
*
* @param fieldsToAttributes attribute map: field name to value
* @param entityClass class of the entity to be created with this data
* @param node input node
* @param em The energy management unit that is managing the system participant. Null, if the
* system participant is not managed.
*/
public SystemParticipantEntityData(
Map<String, String> fieldsToAttributes,
Class<? extends UniqueEntity> entityClass,
NodeInput node,
EmInput em) {
super(fieldsToAttributes, entityClass, node);
this.em = em;
}
public Optional<EmInput> getEm() {
return Optional.ofNullable(em);
}
/**
* Creates a new SystemParticipantEntityData object for an operable system participant input
*
* @param fieldsToAttributes attribute map: field name to value
* @param entityClass class of the entity to be created with this data
* @param operator operator input
* @param node input node
* @param em The energy management unit that is managing the system participant. Null, if the
* system participant is not managed.
*/
public SystemParticipantEntityData(
Map<String, String> fieldsToAttributes,
Class<? extends UniqueEntity> entityClass,
OperatorInput operator,
NodeInput node,
EmInput em) {
super(fieldsToAttributes, entityClass, operator, node);
this.em = em;
}
/**
* Creates a new SystemParticipantEntityData object based on a given {@link
* NodeAssetInputEntityData} object and given energy management unit
*
* @param nodeAssetInputEntityData The node asset entity data object to use attributes of
* @param em The energy management unit that is managing the system participant. Null, if the
* system participant is not managed.
*/
public SystemParticipantEntityData(
NodeAssetInputEntityData nodeAssetInputEntityData, EmInput em) {
super(nodeAssetInputEntityData, nodeAssetInputEntityData.getNode());
this.em = em;
}
@Override
public String toString() {
return "SystemParticipantEntityData{"
+ "em="
+ getEm().map(EmInput::toString).orElse("")
+ ", node="
+ getNode().getUuid()
+ ", operatorInput="
+ getOperatorInput().getUuid()
+ ", fieldsToValues="
+ getFieldsToValues()
+ ", targetClass="
+ getTargetClass()
+ '}';
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
if (!super.equals(o)) return false;
SystemParticipantEntityData that = (SystemParticipantEntityData) o;
return getEm().equals(that.getEm());
}
@Override
public int hashCode() {
return Objects.hash(super.hashCode(), getEm());
}
public static Try<SystemParticipantEntityData, SourceException> build(
NodeAssetInputEntityData nodeAssetInputEntityData, Map<UUID, EmInput> ems) {
Map<String, String> fieldsToAttributes = nodeAssetInputEntityData.getFieldsToValues();
Try<Optional<EmInput>, SourceException> tryEm =
Optional.ofNullable(
nodeAssetInputEntityData.getUUID(SystemParticipantInputEntityFactory.EM))
.map(
// System participant has been given a proper UUID. This means we either...
emUuid ->
Optional.ofNullable(ems.get(emUuid))
// ... find a matching EmInput for given UUID, thus return a success with
// the EM
.map(
emInput ->
(Try<Optional<EmInput>, SourceException>)
new Try.Success<Optional<EmInput>, SourceException>(
Optional.of(emInput)))
// ... or find no matching EmInput, returning a failure.
.orElse(new Try.Failure<>(new SourceException(""))))
// If, on the other hand, no UUID was given (column does not exist, or field is empty),
// this is totally fine - we return an "empty success"
.orElse(new Try.Success<>(Optional.empty()));
return tryEm.map(
// if the operation was successful, transform and return to the data
optionalEm -> {
// remove fields that are passed as objects to constructor
fieldsToAttributes.keySet().remove(SystemParticipantInputEntityFactory.EM);
return new SystemParticipantEntityData(nodeAssetInputEntityData, optionalEm.orElse(null));
});
}
}