-
Notifications
You must be signed in to change notification settings - Fork 5
/
GridContainerValidationUtils.java
377 lines (334 loc) · 15 KB
/
GridContainerValidationUtils.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
/*
* © 2021. TU Dortmund University,
* Institute of Energy Systems, Energy Efficiency and Energy Economics,
* Research group Distribution grid planning and operation
*/
package edu.ie3.datamodel.utils.validation;
import edu.ie3.datamodel.exceptions.*;
import edu.ie3.datamodel.models.UniqueEntity;
import edu.ie3.datamodel.models.input.AssetInput;
import edu.ie3.datamodel.models.input.MeasurementUnitInput;
import edu.ie3.datamodel.models.input.NodeInput;
import edu.ie3.datamodel.models.input.connector.*;
import edu.ie3.datamodel.models.input.container.*;
import edu.ie3.datamodel.models.input.graphics.GraphicInput;
import edu.ie3.datamodel.models.input.system.SystemParticipantInput;
import edu.ie3.datamodel.utils.ContainerUtils;
import edu.ie3.datamodel.utils.Try;
import edu.ie3.datamodel.utils.Try.*;
import java.util.*;
import java.util.stream.Stream;
public class GridContainerValidationUtils extends ValidationUtils {
/** Private Constructor as this class is not meant to be instantiated */
private GridContainerValidationUtils() {
throw new IllegalStateException("Don't try and instantiate a Utility class.");
}
/**
* Checks a complete grid data container
*
* @param gridContainer Grid model to check
* @return a list of try objects either containing an {@link ValidationException} or an empty
* Success
*/
protected static List<Try<Void, ? extends ValidationException>> check(
GridContainer gridContainer) {
Try<Void, InvalidEntityException> isNull = checkNonNull(gridContainer, "grid container");
if (isNull.isFailure()) {
return List.of(isNull);
}
/* sanity check to ensure distinct UUIDs */
List<Try<Void, ? extends ValidationException>> exceptions =
new ArrayList<>(
checkForDuplicates(gridContainer.allEntitiesAsList(), UniqueEntity::getUuid));
exceptions.addAll(checkRawGridElements(gridContainer.getRawGrid()));
exceptions.addAll(
checkSystemParticipants(
gridContainer.getSystemParticipants(), gridContainer.getRawGrid().getNodes()));
exceptions.addAll(
checkGraphicElements(
gridContainer.getGraphics(),
gridContainer.getRawGrid().getNodes(),
gridContainer.getRawGrid().getLines()));
if (gridContainer instanceof SubGridContainer subGridContainer) {
exceptions.add(ConnectorValidationUtils.checkConnectivity(subGridContainer));
}
return exceptions;
}
/**
* Checks the validity of given {@link RawGridElements}. The single elements are checked as well
* as the fact, that none of the assets is connected to a node, that is not in the set of nodes.
*
* @param rawGridElements Raw grid elements
* @return a list of try objects either containing an {@link ValidationException} or an empty
* Success
*/
protected static List<Try<Void, ? extends ValidationException>> checkRawGridElements(
RawGridElements rawGridElements) {
Try<Void, InvalidEntityException> isNull = checkNonNull(rawGridElements, "raw grid elements");
if (isNull.isFailure()) {
return List.of(isNull);
}
/* sanity check to ensure distinct UUIDs */
List<Try<Void, ? extends ValidationException>> exceptions =
new ArrayList<>(
checkForDuplicates(rawGridElements.allEntitiesAsList(), UniqueEntity::getUuid));
/* Checking nodes */
Set<NodeInput> nodes = rawGridElements.getNodes();
nodes.forEach(NodeValidationUtils::check);
/* Checking lines */
rawGridElements
.getLines()
.forEach(
line -> {
exceptions.add(checkNodeAvailability(line, nodes));
exceptions.addAll(ConnectorValidationUtils.check(line));
});
/* Checking two winding transformers */
rawGridElements
.getTransformer2Ws()
.forEach(
transformer -> {
exceptions.add(checkNodeAvailability(transformer, nodes));
exceptions.addAll(ConnectorValidationUtils.check(transformer));
});
/* Checking three winding transformers */
rawGridElements
.getTransformer3Ws()
.forEach(
transformer -> {
exceptions.add(checkNodeAvailability(transformer, nodes));
exceptions.addAll(ConnectorValidationUtils.check(transformer));
});
/* Checking switches
* Because of the fact, that a transformer with switch gear in "upstream" direction has it's corresponding node in
* upper grid connected to a switch, instead of to the transformer directly: Collect all nodes at the end of the
* upstream switch chain and add them to the set of allowed nodes */
HashSet<NodeInput> validSwitchNodes = new HashSet<>(nodes);
validSwitchNodes.addAll(
Stream.of(rawGridElements.getTransformer2Ws(), rawGridElements.getTransformer2Ws())
.flatMap(Set::stream)
.parallel()
.map(
transformer ->
ContainerUtils.traverseAlongSwitchChain(transformer.getNodeA(), rawGridElements)
.getLast())
.toList());
rawGridElements
.getSwitches()
.forEach(
switcher -> {
exceptions.add(checkNodeAvailability(switcher, validSwitchNodes));
exceptions.addAll(ConnectorValidationUtils.check(switcher));
});
/* Checking measurement units */
rawGridElements
.getMeasurementUnits()
.forEach(
measurement -> {
exceptions.add(checkNodeAvailability(measurement, nodes));
exceptions.add(MeasurementUnitValidationUtils.check(measurement));
});
exceptions.addAll(checkRawGridTypeIds(rawGridElements));
return exceptions;
}
/**
* Checks the validity of type ids of every entity.
*
* @param rawGridElements the raw grid elements
* @return a list of try objects either containing an {@link DuplicateEntitiesException} or an
* empty Success
*/
protected static List<Try<Void, DuplicateEntitiesException>> checkRawGridTypeIds(
RawGridElements rawGridElements) {
List<Try<Void, DuplicateEntitiesException>> exceptions = new ArrayList<>();
exceptions.addAll(checkForDuplicates(rawGridElements.getNodes(), AssetInput::getId));
exceptions.addAll(checkForDuplicates(rawGridElements.getLines(), AssetInput::getId));
exceptions.addAll(checkForDuplicates(rawGridElements.getTransformer2Ws(), AssetInput::getId));
exceptions.addAll(checkForDuplicates(rawGridElements.getTransformer3Ws(), AssetInput::getId));
exceptions.addAll(checkForDuplicates(rawGridElements.getSwitches(), AssetInput::getId));
exceptions.addAll(checkForDuplicates(rawGridElements.getMeasurementUnits(), AssetInput::getId));
return exceptions;
}
/**
* Checks the validity of each and every system participant. Moreover, it checks, if the systems
* are connected to a node that is not in the provided set
*
* @param systemParticipants The system participants
* @param nodes Set of already known nodes
* @return a list of try objects either containing an {@link ValidationException} or an empty
* Success
*/
protected static List<Try<Void, ? extends ValidationException>> checkSystemParticipants(
SystemParticipants systemParticipants, Set<NodeInput> nodes) {
Try<Void, InvalidEntityException> isNull =
checkNonNull(systemParticipants, "system participants");
if (isNull.isFailure()) {
return List.of(isNull);
}
// sanity check for distinct uuids
List<Try<Void, ? extends ValidationException>> exceptions =
new ArrayList<>(
checkForDuplicates(systemParticipants.allEntitiesAsList(), UniqueEntity::getUuid));
exceptions.addAll(checkSystemParticipants(systemParticipants.getBmPlants(), nodes));
exceptions.addAll(checkSystemParticipants(systemParticipants.getChpPlants(), nodes));
exceptions.addAll(checkSystemParticipants(systemParticipants.getEvcs(), nodes));
exceptions.addAll(checkSystemParticipants(systemParticipants.getFixedFeedIns(), nodes));
exceptions.addAll(checkSystemParticipants(systemParticipants.getHeatPumps(), nodes));
exceptions.addAll(checkSystemParticipants(systemParticipants.getLoads(), nodes));
exceptions.addAll(checkSystemParticipants(systemParticipants.getPvPlants(), nodes));
exceptions.addAll(checkSystemParticipants(systemParticipants.getStorages(), nodes));
exceptions.addAll(checkSystemParticipants(systemParticipants.getWecPlants(), nodes));
exceptions.addAll(checkSystemParticipantsTypeIds(systemParticipants));
return exceptions;
}
/**
* Checks the validity of specific system participant. Moreover, it checks, if the systems are
* connected to a node that is not in the provided set
*
* @param participants a set of specific system participants
* @param nodes Set of already known nodes
* @return a list of try objects either containing an {@link ValidationException} or an empty
* Success
*/
protected static List<Try<Void, ? extends ValidationException>> checkSystemParticipants(
Set<? extends SystemParticipantInput> participants, Set<NodeInput> nodes) {
return participants.stream()
.map(
entity -> {
List<Try<Void, ? extends ValidationException>> exceptions = new ArrayList<>();
exceptions.add(checkNodeAvailability(entity, nodes));
exceptions.addAll(SystemParticipantValidationUtils.check(entity));
return exceptions;
})
.flatMap(List::stream)
.toList();
}
/**
* Checks the validity of type ids of every entity.
*
* @param systemParticipants the system participants
* @return a list of try objects either containing an {@link DuplicateEntitiesException} or an
* empty Success
*/
protected static List<Try<Void, DuplicateEntitiesException>> checkSystemParticipantsTypeIds(
SystemParticipants systemParticipants) {
List<Try<Void, DuplicateEntitiesException>> exceptions = new ArrayList<>();
exceptions.addAll(checkForDuplicates(systemParticipants.getBmPlants(), AssetInput::getId));
exceptions.addAll(checkForDuplicates(systemParticipants.getChpPlants(), AssetInput::getId));
exceptions.addAll(checkForDuplicates(systemParticipants.getEvcs(), AssetInput::getId));
exceptions.addAll(checkForDuplicates(systemParticipants.getEvs(), AssetInput::getId));
exceptions.addAll(checkForDuplicates(systemParticipants.getFixedFeedIns(), AssetInput::getId));
exceptions.addAll(checkForDuplicates(systemParticipants.getHeatPumps(), AssetInput::getId));
exceptions.addAll(checkForDuplicates(systemParticipants.getLoads(), AssetInput::getId));
exceptions.addAll(checkForDuplicates(systemParticipants.getPvPlants(), AssetInput::getId));
exceptions.addAll(checkForDuplicates(systemParticipants.getStorages(), AssetInput::getId));
exceptions.addAll(checkForDuplicates(systemParticipants.getWecPlants(), AssetInput::getId));
return exceptions;
}
/**
* Checks the given graphic elements for validity
*
* @param graphicElements Elements to check
* @param nodes Already known and checked nodes
* @param lines Already known and checked lines
* @return a list of try objects either containing an {@link ValidationException} or an empty
* Success
*/
protected static List<Try<Void, ? extends ValidationException>> checkGraphicElements(
GraphicElements graphicElements, Set<NodeInput> nodes, Set<LineInput> lines) {
Try<Void, InvalidEntityException> isNull = checkNonNull(graphicElements, "graphic elements");
if (isNull.isFailure()) {
return List.of(isNull);
}
// sanity check for distinct uuids
List<Try<Void, ? extends ValidationException>> exceptions =
new ArrayList<>(
checkForDuplicates(graphicElements.allEntitiesAsList(), UniqueEntity::getUuid));
graphicElements
.getNodeGraphics()
.forEach(
graphic -> {
exceptions.addAll(GraphicValidationUtils.check(graphic));
exceptions.add(
Try.ofVoid(
!nodes.contains(graphic.getNode()),
() ->
buildGraphicExceptionMessage(
graphic, "node", graphic.getNode().getUuid())));
});
graphicElements
.getLineGraphics()
.forEach(
graphic -> {
exceptions.addAll(GraphicValidationUtils.check(graphic));
exceptions.add(
Try.ofVoid(
!lines.contains(graphic.getLine()),
() ->
buildGraphicExceptionMessage(
graphic, "line", graphic.getLine().getUuid())));
});
return exceptions;
}
/**
* Checks if the node(s) of the given {@link AssetInput} are in the collection of provided already
* determined nodes.
*
* @param input asset to examine
* @param nodes permissible, already known nodes
* @return either an {@link InvalidGridException} wrapped in a {@link Failure} or an empty {@link
* Success}
*/
private static Try<Void, InvalidGridException> checkNodeAvailability(
AssetInput input, Collection<NodeInput> nodes) {
boolean available;
if (input instanceof Transformer3WInput transformer) {
available =
!nodes.containsAll(
Arrays.asList(
transformer.getNodeA(), transformer.getNodeB(), transformer.getNodeC()));
} else if (input instanceof ConnectorInput connector) {
available = !nodes.containsAll(Arrays.asList(connector.getNodeA(), connector.getNodeB()));
} else if (input instanceof SystemParticipantInput participant) {
available = !nodes.contains(participant.getNode());
} else if (input instanceof MeasurementUnitInput measurementUnit) {
available = !nodes.contains(measurementUnit.getNode());
} else {
return Failure.ofVoid(
new InvalidGridException(
"Checking the node availability of"
+ input.getClass().getSimpleName()
+ " is not implemented."));
}
return Try.ofVoid(
available,
() ->
new InvalidGridException(
input.getClass().getSimpleName()
+ " "
+ input
+ " is connected to a node that is not in the set of nodes."));
}
/**
* Creates a {@link InvalidEntityException} for graphic inputs.
*
* @param graphic input
* @param type of the graphic
* @param asset uuid of the referred asset
* @return a {@link Failure}
*/
private static InvalidEntityException buildGraphicExceptionMessage(
GraphicInput graphic, String type, UUID asset) {
return new InvalidEntityException(
"The "
+ type
+ " graphic with uuid '"
+ graphic.getUuid()
+ "' refers to "
+ type
+ " with uuid '"
+ asset
+ "', that is not a,ong the provided ones.",
graphic);
}
}