-
Notifications
You must be signed in to change notification settings - Fork 5
/
EntitySource.java
414 lines (383 loc) · 18.5 KB
/
EntitySource.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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
/*
* © 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.source;
import edu.ie3.datamodel.exceptions.FactoryException;
import edu.ie3.datamodel.exceptions.FailedValidationException;
import edu.ie3.datamodel.exceptions.SourceException;
import edu.ie3.datamodel.exceptions.ValidationException;
import edu.ie3.datamodel.io.factory.EntityData;
import edu.ie3.datamodel.io.factory.EntityFactory;
import edu.ie3.datamodel.io.factory.input.AssetInputEntityData;
import edu.ie3.datamodel.io.factory.input.NodeAssetInputEntityData;
import edu.ie3.datamodel.models.Entity;
import edu.ie3.datamodel.models.UniqueEntity;
import edu.ie3.datamodel.models.input.*;
import edu.ie3.datamodel.utils.Try;
import edu.ie3.datamodel.utils.Try.*;
import java.util.*;
import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.apache.commons.lang3.function.TriFunction;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/** Class that provides all functionalities to build entities */
public abstract class EntitySource {
protected static final Logger log = LoggerFactory.getLogger(EntitySource.class);
// field names
protected static final String OPERATOR = "operator";
protected static final String NODE = "node";
protected static final String TYPE = "type";
protected final DataSource dataSource;
protected EntitySource(DataSource dataSource) {
this.dataSource = dataSource;
}
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
public abstract void validate() throws ValidationException;
/**
* Method for validating a single source.
*
* @param entityClass class to be validated
* @param validator used to validate
* @param <C> type of the class
*/
protected final <C extends Entity> Try<Void, ValidationException> validate(
Class<? extends C> entityClass, SourceValidator<C> validator) {
return Try.of(() -> dataSource.getSourceFields(entityClass), SourceException.class)
.transformF(
se ->
(ValidationException)
new FailedValidationException(
"Validation for entity "
+ entityClass
+ " failed because of an error related to its source.",
se))
.flatMap(
fieldsOpt ->
fieldsOpt
.map(fields -> validator.validate(fields, entityClass))
.orElse(Try.Success.empty()));
}
/**
* Enhances given entity data with an entity from the given entity map. The linked entity is
* chosen by taking into account the UUID found by retrieving the field with given fieldName from
* entityData.
*
* @param entityData The entity data to be enhanced, which also provides a link to another entity
* via UUID
* @param fieldName The field name of the field that provides the UUID of the linked entity
* @param linkedEntities A map of UUID to entities, of which one should be linked to given entity
* data
* @param createEntityData The function that creates the resulting entity data given entityData
* and the linked entity
* @param <E> Type of input entity data
* @param <T> Type of the linked entity
* @param <R> Type of resulting entity data that combines the given entityData and linked entity
* @return {@link Try} to enhanced data
*/
protected static <E extends EntityData, T extends Entity, R extends E>
Try<R, SourceException> enrichEntityData(
E entityData,
String fieldName,
Map<UUID, T> linkedEntities,
BiFunction<E, T, R> createEntityData) {
return getLinkedEntity(entityData, fieldName, linkedEntities)
.map(
linkedEntity -> {
Map<String, String> fieldsToAttributes = entityData.getFieldsToValues();
// remove fields that are passed as objects to constructor
fieldsToAttributes.keySet().remove(fieldName);
// build resulting entity data
return createEntityData.apply(entityData, linkedEntity);
});
}
/**
* Enhances given entity data with two entities from the given entity maps. The linked entities
* are chosen by taking into account the UUIDs found by retrieving the fields with given
* fieldName1 and fieldName2 from entityData.
*
* @param entityData The entity data to be enhanced, which also provides links to two other
* entities via UUID
* @param fieldName1 The field name of the field that provides the UUID of the first linked entity
* @param linkedEntities1 The first map of UUID to entities, of which one should be linked to
* given entity data
* @param fieldName2 The field name of the field that provides the UUID of the second linked
* entity
* @param linkedEntities2 The second map of UUID to entities, of which one should be linked to
* given entity data
* @param createEntityData The function that creates the resulting entity data given entityData
* and the linked entities
* @param <E> Type of input entity data
* @param <T1> Type of the first linked entity
* @param <T2> Type of the second linked entity
* @param <R> Type of resulting entity data that combines the given entityData and two linked
* entities
* @return {@link Try} to enhanced data
*/
protected static <
E extends EntityData, T1 extends UniqueEntity, T2 extends UniqueEntity, R extends E>
Try<R, SourceException> enrichEntityData(
E entityData,
String fieldName1,
Map<UUID, T1> linkedEntities1,
String fieldName2,
Map<UUID, T2> linkedEntities2,
TriFunction<E, T1, T2, R> createEntityData) {
return getLinkedEntity(entityData, fieldName1, linkedEntities1)
.flatMap(
linkedEntity1 ->
getLinkedEntity(entityData, fieldName2, linkedEntities2)
.map(
linkedEntity2 -> {
Map<String, String> fieldsToAttributes = entityData.getFieldsToValues();
// remove fields that are passed as objects to constructor
fieldsToAttributes.keySet().remove(fieldName1);
fieldsToAttributes.keySet().remove(fieldName2);
// build resulting entity data
return createEntityData.apply(entityData, linkedEntity1, linkedEntity2);
}));
}
/**
* Checks if the linked entity can be found in the provided map of entities. The linked entities
* are chosen by taking into account the UUIDs found by retrieving the fields with given
* fieldName1 and fieldName2 from entityData.
*
* @param entityData The entity data of the entity that provides a link to another entity via UUID
* @param fieldName The field name of the field that provides the UUID of the linked entity
* @param linkedEntities A map of UUID to entities, of which one should be linked to given entity
* data
* @param <T> the type of the resulting linked entity instance
* @return a {@link Success} containing the entity or a {@link Failure} if the entity cannot be
* found
*/
protected static <T extends Entity> Try<T, SourceException> getLinkedEntity(
EntityData entityData, String fieldName, Map<UUID, T> linkedEntities) {
return Try.of(() -> entityData.getUUID(fieldName), FactoryException.class)
.transformF(
exception ->
new SourceException(
"Extracting UUID field "
+ fieldName
+ " from entity data "
+ entityData.toString()
+ " failed.",
exception))
.flatMap(
entityUuid ->
getEntity(entityUuid, linkedEntities)
.transformF(
exception ->
new SourceException(
"Linked "
+ fieldName
+ " with UUID "
+ entityUuid
+ " was not found for entity "
+ entityData,
exception)));
}
/**
* Enhances given entity data with an entity from the given entity map or the default value. The
* linked entity is possibly chosen by taking into account the UUID found by retrieving the field
* with given fieldName from entityData. If no entity is linked, the default value is used.
*
* @param entityData The entity data to be enhanced, which also might provide a link to another
* entity via UUID
* @param fieldName The field name of the field that might provide the UUID of the linked entity
* @param linkedEntities A map of UUID to entities, of which one should be linked to given entity
* data
* @param defaultEntity The default linked entity to use, if no actual linked entity could be
* found
* @param createEntityData The function that creates the resulting entity data given entityData
* and the linked entity (either retrieved from the map or the standard entity)
* @param <E> Type of input entity data
* @param <T> Type of the linked entity
* @param <R> Type of resulting entity data that combines the given entityData and linked entity
* @return {@link Try} to enhanced data
*/
protected static <E extends EntityData, T extends UniqueEntity, R extends E>
Try<R, SourceException> optionallyEnrichEntityData(
E entityData,
String fieldName,
Map<UUID, T> linkedEntities,
T defaultEntity,
BiFunction<E, T, R> createEntityData) {
return entityData
.getFieldOptional(fieldName)
.filter(s -> !s.isBlank())
.map(
// Entity data includes a non-empty UUID String for the desired entity
uuidString ->
Try.of(() -> UUID.fromString(uuidString), IllegalArgumentException.class)
.transformF(
iae ->
// Parsing error still results in a failure, ...
new SourceException(
String.format(
"Exception while trying to parse UUID of field \"%s\" with value \"%s\"",
fieldName, uuidString),
iae))
.flatMap(
entityUuid ->
getEntity(entityUuid, linkedEntities)
// ... as well as a provided entity UUID that does not match any
// given data
.transformF(
exception ->
new SourceException(
"Linked "
+ fieldName
+ " with UUID "
+ entityUuid
+ " was not found for entity "
+ entityData,
exception))))
.orElseGet(
() -> {
// No UUID was given (column does not exist, or field is empty).
// This is totally fine - we successfully return the default value
log.debug(
"Input source for class {} is missing the '{}' field. "
+ "Default value '{}' is used.",
entityData.getTargetClass().getSimpleName(),
fieldName,
defaultEntity);
return new Try.Success<>(defaultEntity);
})
.map(
linkedEntity -> {
Map<String, String> fieldsToAttributes = entityData.getFieldsToValues();
// remove fields that are passed as objects to constructor
fieldsToAttributes.keySet().remove(fieldName);
// build resulting entity data
return createEntityData.apply(entityData, linkedEntity);
});
}
private static <T> Try<T, SourceException> getEntity(UUID uuid, Map<UUID, T> entityMap) {
return Optional.ofNullable(entityMap.get(uuid))
// We either find a matching entity for given UUID, thus return a success
.map(entity -> Try.of(() -> entity, SourceException.class))
// ... or find no matching entity, returning a failure.
.orElse(
new Try.Failure<>(
new SourceException("Entity with uuid " + uuid + " was not provided.")));
}
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
/**
* Returns a stream of {@link Try} entities that can be built by using {@link
* NodeAssetInputEntityData} and their corresponding factory.
*
* @param entityClass the entity class that should be build
* @param nodes a map of UUID to {@link NodeInput} entities that should be used to build the
* entities
* @param operators a map of UUID to {@link OperatorInput} entities should be used to build the
* entities
* @return stream of tries of the entities that has been built by the factory
*/
protected Stream<Try<NodeAssetInputEntityData, SourceException>> buildNodeAssetEntityData(
Class<? extends AssetInput> entityClass,
Map<UUID, OperatorInput> operators,
Map<UUID, NodeInput> nodes) {
return nodeAssetInputEntityDataStream(buildAssetInputEntityData(entityClass, operators), nodes);
}
/**
* Returns a stream of tries of {@link NodeAssetInputEntityData} that can be used to build
* instances of several subtypes of {@link UniqueEntity} by a corresponding {@link EntityFactory}
* that consumes this data.
*
* @param assetInputEntityDataStream a stream consisting of {@link AssetInputEntityData} that is
* enriched with {@link NodeInput} data
* @param nodes a map of UUID to {@link NodeInput} entities that should be used to build the data
* @return stream of the entity data wrapped in a {@link Try}
*/
protected static Stream<Try<NodeAssetInputEntityData, SourceException>>
nodeAssetInputEntityDataStream(
Stream<Try<AssetInputEntityData, SourceException>> assetInputEntityDataStream,
Map<UUID, NodeInput> nodes) {
return assetInputEntityDataStream
.parallel()
.map(
assetInputEntityDataTry ->
assetInputEntityDataTry.flatMap(
assetInputEntityData ->
enrichEntityData(
assetInputEntityData, NODE, nodes, NodeAssetInputEntityData::new)));
}
/**
* Returns a stream of optional {@link AssetInputEntityData} that can be used to build instances
* of several subtypes of {@link UniqueEntity} by a corresponding {@link EntityFactory} that
* consumes this data.
*
* @param entityClass the entity class that should be build
* @param operators a map of UUID to {@link OperatorInput} entities that should be used to build
* the data
* @return stream of the entity data wrapped in a {@link Try}
*/
protected Stream<Try<AssetInputEntityData, SourceException>> buildAssetInputEntityData(
Class<? extends AssetInput> entityClass, Map<UUID, OperatorInput> operators) {
return assetInputEntityDataStream(buildEntityData(entityClass), operators);
}
/**
* Returns a stream of tries of {@link AssetInputEntityData} that can be used to build instances
* of several subtypes of {@link UniqueEntity} by a corresponding {@link EntityFactory} that
* consumes this data.
*
* @param entityDataStream a stream consisting of {@link EntityData} that is enriched with {@link
* OperatorInput} data
* @param operators map of UUID to {@link OperatorInput} entities that should be used to build the
* data
* @return stream of the entity data wrapped in a {@link Try}
*/
protected static Stream<Try<AssetInputEntityData, SourceException>> assetInputEntityDataStream(
Stream<Try<EntityData, SourceException>> entityDataStream,
Map<UUID, OperatorInput> operators) {
return entityDataStream
.parallel()
.map(
entityDataTry ->
entityDataTry.flatMap(
entityData ->
optionallyEnrichEntityData(
entityData,
OPERATOR,
operators,
OperatorInput.NO_OPERATOR_ASSIGNED,
AssetInputEntityData::new)));
}
/**
* Returns a stream of optional {@link EntityData} that can be used to build instances of several
* subtypes of {@link Entity} by a corresponding {@link EntityFactory} that consumes this data.
*
* @param entityClass the entity class that should be build
* @return stream of the entity data wrapped in a {@link Try}
*/
protected Stream<Try<EntityData, SourceException>> buildEntityData(
Class<? extends Entity> entityClass) {
return Try.of(() -> dataSource.getSourceData(entityClass), SourceException.class)
.convert(
data ->
data.map(
fieldsToAttributes ->
new Success<>(new EntityData(fieldsToAttributes, entityClass))),
exception -> Stream.of(Failure.of(exception)));
}
protected static <S extends UniqueEntity> Map<UUID, S> unpackMap(
Stream<Try<S, FactoryException>> inputStream, Class<S> entityClass) throws SourceException {
return unpack(inputStream, entityClass)
.collect(Collectors.toMap(UniqueEntity::getUuid, Function.identity()));
}
protected static <S extends Entity> Set<S> unpackSet(
Stream<Try<S, FactoryException>> inputStream, Class<S> entityClass) throws SourceException {
return unpack(inputStream, entityClass).collect(Collectors.toSet());
}
protected static <S, E extends Exception> Stream<S> unpack(
Stream<Try<S, E>> inputStream, Class<S> clazz) throws SourceException {
return Try.scanStream(inputStream, clazz.getSimpleName())
.transformF(SourceException::new)
.getOrThrow();
}
}