-
Notifications
You must be signed in to change notification settings - Fork 5
/
EntitySource.java
368 lines (335 loc) · 15.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
/*
* © 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.SourceException;
import edu.ie3.datamodel.io.factory.EntityFactory;
import edu.ie3.datamodel.io.factory.SimpleEntityData;
import edu.ie3.datamodel.io.factory.input.AssetInputEntityData;
import edu.ie3.datamodel.io.factory.input.ConnectorInputEntityData;
import edu.ie3.datamodel.io.factory.input.NodeAssetInputEntityData;
import edu.ie3.datamodel.io.factory.input.TypedConnectorInputEntityData;
import edu.ie3.datamodel.models.UniqueEntity;
import edu.ie3.datamodel.models.input.*;
import edu.ie3.datamodel.models.result.ResultEntity;
import edu.ie3.datamodel.utils.Try;
import edu.ie3.datamodel.utils.Try.*;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;
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 static final String FIELDS_TO_VALUES_MAP = "fieldsToValuesMap";
DataSource dataSource;
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
protected String buildSkippingMessage(
String entityDesc, String entityUuid, String entityId, String missingElementsString) {
return "Skipping "
+ entityDesc
+ " with uuid "
+ entityUuid
+ " and id "
+ entityId
+ ". Not all required entities found or map is missing entity key!\nMissing elements:\n"
+ missingElementsString;
}
/**
* Method for retrieving an element from a map. If the map doesn't contain the key an error
* message is build and returned instead.
*
* <p>Should not be used for other purposes than creating error messages.
*
* @param map with value
* @param key for the value
* @param mapName name of the map used for the error message
* @return either the value or an error message
*/
protected String safeMapGet(Map<String, String> map, String key, String mapName) {
return Optional.ofNullable(map.get(key))
.orElse(
"Key '"
+ key
+ "' not found"
+ (mapName.isEmpty() ? "!" : " in map '" + mapName + "'!"));
}
/**
* Returns an {@link Optional} of the first {@link UniqueEntity} element of this collection
* matching the provided UUID or an empty {@code Optional} if no matching entity can be found.
*
* @param entityUuid uuid of the entity that should be looked for
* @param entities collection of entities that should be
* @param <T> type of the entity that will be returned, derived from the provided collection
* @return either an optional containing the first entity that has the provided uuid or an empty
* optional if no matching entity with the provided uuid can be found
*/
protected <T extends UniqueEntity> Optional<T> findFirstEntityByUuid(
UUID entityUuid, Collection<T> entities) {
return entities.stream()
.parallel()
.filter(uniqueEntity -> uniqueEntity.getUuid().equals(entityUuid))
.findFirst();
}
/**
* Checks if the requested type of asset can be found in the provided collection of types based on
* the provided fields to values mapping. The provided fields to values mapping needs to have one
* and only one field with key {@link #TYPE} and a corresponding UUID value. If the type can be
* found in the provided collection based on the UUID it is returned wrapped in a {@link Success}.
* Otherwise, a {@link Failure} is returned and a warning is logged.
*
* @param types a collection of types that should be used for searching
* @param fieldsToAttributes the field name to value mapping incl. the key {@link #TYPE}
* @param skippedClassString debug string of the class that will be skipping
* @param <T> the type of the resulting type instance
* @return a {@link Success} containing the type or a {@link Failure} if the type cannot be found
*/
protected <T extends AssetTypeInput> Try<T, SourceException> getAssetType(
Collection<T> types, Map<String, String> fieldsToAttributes, String skippedClassString) {
Optional<T> assetType =
Optional.ofNullable(fieldsToAttributes.get(TYPE))
.flatMap(typeUuid -> findFirstEntityByUuid(UUID.fromString(typeUuid), types));
// if the type is not present we return an empty element and
// log a warning
if (assetType.isEmpty()) {
String skippingMessage =
buildSkippingMessage(
skippedClassString,
safeMapGet(fieldsToAttributes, "uuid", FIELDS_TO_VALUES_MAP),
safeMapGet(fieldsToAttributes, "id", FIELDS_TO_VALUES_MAP),
TYPE + ": " + safeMapGet(fieldsToAttributes, TYPE, FIELDS_TO_VALUES_MAP));
return new Failure<>(new SourceException("Failure due to: " + skippingMessage));
}
return new Success<>(assetType.get());
}
/**
* Finds the required asset type and if present, adds it to the untyped entity data
*
* @param untypedEntityData Untyped entity data to enrich
* @param availableTypes Yet available asset types
* @param <T> Type of the asset type
* @return {@link Try} to enhanced data
*/
protected <T extends AssetTypeInput>
Try<TypedConnectorInputEntityData<T>, SourceException> findAndAddType(
ConnectorInputEntityData untypedEntityData, Collection<T> availableTypes) {
Try<T, SourceException> assetTypeOption =
getAssetType(
availableTypes,
untypedEntityData.getFieldsToValues(),
untypedEntityData.getClass().getSimpleName());
return assetTypeOption.map(assetType -> addTypeToEntityData(untypedEntityData, assetType));
}
/**
* Enriches the given, untyped entity data with the provided asset type
*
* @param untypedEntityData Untyped entity data to enrich
* @param assetType Asset type to add
* @param <T> Type of the asset type
* @return The enriched entity data
*/
protected <T extends AssetTypeInput> TypedConnectorInputEntityData<T> addTypeToEntityData(
ConnectorInputEntityData untypedEntityData, T assetType) {
Map<String, String> fieldsToAttributes = untypedEntityData.getFieldsToValues();
// remove fields that are passed as objects to constructor
fieldsToAttributes.keySet().remove(TYPE);
// build result object
return new TypedConnectorInputEntityData<>(
fieldsToAttributes,
untypedEntityData.getTargetClass(),
untypedEntityData.getOperatorInput(),
untypedEntityData.getNodeA(),
untypedEntityData.getNodeB(),
assetType);
}
/**
* Returns either the first instance of a {@link OperatorInput} in the provided collection of or
* {@link OperatorInput#NO_OPERATOR_ASSIGNED}
*
* @param operators the collections of {@link OperatorInput}s that should be searched in
* @param operatorUuid the operator uuid that is requested
* @return either the first found instancen of {@link OperatorInput} or {@link
* OperatorInput#NO_OPERATOR_ASSIGNED}
*/
protected OperatorInput getFirstOrDefaultOperator(
Collection<OperatorInput> operators,
Optional<UUID> operatorUuid,
String entityClassName,
String requestEntityUuid) {
if (operatorUuid.isEmpty()) {
log.warn(
"Input source for class '{}' is missing the 'operator' field. "
+ "This is okay, but you should consider fixing the file by adding the field. "
+ "Defaulting to 'NO OPERATOR ASSIGNED'",
entityClassName);
return OperatorInput.NO_OPERATOR_ASSIGNED;
} else {
return findFirstEntityByUuid(operatorUuid.get(), operators)
.orElseGet(
() -> {
log.debug(
"Cannot find operator with uuid '{}' for element '{}' and uuid '{}'. Defaulting to 'NO OPERATOR ASSIGNED'.",
operatorUuid,
entityClassName,
requestEntityUuid);
return OperatorInput.NO_OPERATOR_ASSIGNED;
});
}
}
/**
* 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
*
* @param assetInputEntityDataStream a stream consisting of {@link AssetInputEntityData} that is
* enriched with {@link NodeInput} data
* @param nodes a collection of {@link NodeInput} entities that should be used to build the data
* @return stream of the entity data wrapped in a {@link Try}
*/
protected Stream<Try<NodeAssetInputEntityData, SourceException>> nodeAssetInputEntityDataStream(
Stream<AssetInputEntityData> assetInputEntityDataStream, Collection<NodeInput> nodes) {
return assetInputEntityDataStream
.parallel()
.map(
assetInputEntityData -> {
// get the raw data
Map<String, String> fieldsToAttributes = assetInputEntityData.getFieldsToValues();
// get the node of the entity
UUID nodeUuid = UUID.fromString(fieldsToAttributes.get(NODE));
Optional<NodeInput> node = findFirstEntityByUuid(nodeUuid, nodes);
// if the node is not present we return an empty element and
// log a warning
if (node.isEmpty()) {
String skippingMessage =
buildSkippingMessage(
assetInputEntityData.getTargetClass().getSimpleName(),
fieldsToAttributes.get("uuid"),
fieldsToAttributes.get("id"),
NODE + ": " + nodeUuid);
return new Failure<>(new SourceException("Failure due to: " + skippingMessage));
}
// remove fields that are passed as objects to constructor
fieldsToAttributes.keySet().remove(NODE);
return new Success<>(
new NodeAssetInputEntityData(
fieldsToAttributes,
assetInputEntityData.getTargetClass(),
assetInputEntityData.getOperatorInput(),
node.get()));
});
}
/**
* 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 collection of {@link OperatorInput} entities that should be used to build
* the data
* @param <T> type of the entity that should be build
* @return stream of the entity data wrapped in a {@link Try}
*/
protected <T extends AssetInput> Stream<AssetInputEntityData> assetInputEntityDataStream(
Class<T> entityClass, Collection<OperatorInput> operators) {
return dataSource
.getSourceData(entityClass)
.map(
fieldsToAttributes ->
assetInputEntityDataStream(entityClass, fieldsToAttributes, operators));
}
protected <T extends AssetInput> AssetInputEntityData assetInputEntityDataStream(
Class<T> entityClass,
Map<String, String> fieldsToAttributes,
Collection<OperatorInput> operators) {
// get the operator of the entity
Optional<UUID> operatorUuid =
Optional.ofNullable(fieldsToAttributes.get(OPERATOR))
.filter(s -> !s.isBlank())
.map(UUID::fromString);
OperatorInput operator =
getFirstOrDefaultOperator(
operators,
operatorUuid,
entityClass.getSimpleName(),
safeMapGet(fieldsToAttributes, "uuid", FIELDS_TO_VALUES_MAP));
// remove fields that are passed as objects to constructor
fieldsToAttributes.keySet().removeAll(new HashSet<>(Collections.singletonList(OPERATOR)));
return new AssetInputEntityData(fieldsToAttributes, entityClass, operator);
}
/**
* Returns a stream of {@link SimpleEntityData} for result entity classes, using a
* fields-to-attributes map.
*
* @param entityClass the entity class that should be build
* @param <T> Type of the {@link ResultEntity} to expect
* @return stream of {@link SimpleEntityData}
*/
protected <T extends ResultEntity> Stream<SimpleEntityData> simpleEntityDataStream(
Class<T> entityClass) {
return dataSource
.getSourceData(entityClass)
.map(fieldsToAttributes -> new SimpleEntityData(fieldsToAttributes, entityClass));
}
protected <T extends AssetInput> Stream<Try<T, FactoryException>> assetInputEntityStream(
Class<T> entityClass,
EntityFactory<T, AssetInputEntityData> factory,
Collection<OperatorInput> operators) {
return assetInputEntityDataStream(entityClass, operators).map(factory::get);
}
/**
* Returns a stream of {@link Try} entities that can be build by using {@link
* NodeAssetInputEntityData} and their corresponding factory.
*
* @param entityClass the entity class that should be build
* @param factory the factory that should be used for the building process
* @param nodes a collection of {@link NodeInput} entities that should be used to build the
* entities
* @param operators a collection of {@link OperatorInput} entities should be used to build the
* entities
* @param <T> Type of the {@link AssetInput} to expect
* @return stream of tries of the entities that has been built by the factory
*/
protected <T extends AssetInput> Stream<Try<T, FactoryException>> nodeAssetEntityStream(
Class<T> entityClass,
EntityFactory<T, NodeAssetInputEntityData> factory,
Collection<NodeInput> nodes,
Collection<OperatorInput> operators) {
return nodeAssetInputEntityDataStream(assetInputEntityDataStream(entityClass, operators), nodes)
.map(factory::get);
}
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
public <T extends AssetInput> Set<Try<T, FactoryException>> buildNodeAssetEntities(
Class<T> entityClass,
EntityFactory<T, NodeAssetInputEntityData> factory,
Collection<NodeInput> nodes,
Collection<OperatorInput> operators) {
return nodeAssetEntityStream(entityClass, factory, nodes, operators)
.collect(Collectors.toSet());
}
public <T extends AssetInput> Set<Try<T, FactoryException>> buildAssetInputEntities(
Class<T> entityClass,
EntityFactory<T, AssetInputEntityData> factory,
Collection<OperatorInput> operators) {
return assetInputEntityStream(entityClass, factory, operators).collect(Collectors.toSet());
}
@SuppressWarnings("unchecked")
public <T extends InputEntity> Set<Try<T, FactoryException>> buildEntities(
Class<T> entityClass, EntityFactory<? extends InputEntity, SimpleEntityData> factory) {
return dataSource
.getSourceData(entityClass)
.map(
fieldsToAttributes -> {
SimpleEntityData data = new SimpleEntityData(fieldsToAttributes, entityClass);
return (Try<T, FactoryException>) factory.get(data);
})
.collect(Collectors.toSet());
}
}