-
Notifications
You must be signed in to change notification settings - Fork 5
/
ThermalSource.java
313 lines (287 loc) · 14.6 KB
/
ThermalSource.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
/*
* © 2021. 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.FailureException;
import edu.ie3.datamodel.exceptions.SourceException;
import edu.ie3.datamodel.io.factory.input.*;
import edu.ie3.datamodel.models.input.OperatorInput;
import edu.ie3.datamodel.models.input.thermal.CylindricalStorageInput;
import edu.ie3.datamodel.models.input.thermal.ThermalBusInput;
import edu.ie3.datamodel.models.input.thermal.ThermalHouseInput;
import edu.ie3.datamodel.models.input.thermal.ThermalStorageInput;
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;
/**
* Interface that provides the capability to build thermal {@link
* edu.ie3.datamodel.models.input.AssetInput} entities from persistent data e.g. .csv files or
* databases
*
* @version 0.1
* @since 08.04.20
*/
public class ThermalSource extends EntitySource {
// general fields
private final TypeSource typeSource;
// factories
private final ThermalBusInputFactory thermalBusInputFactory;
private final CylindricalStorageInputFactory cylindricalStorageInputFactory;
private final ThermalHouseInputFactory thermalHouseInputFactory;
public ThermalSource(TypeSource typeSource, DataSource dataSource) {
this.typeSource = typeSource;
this.dataSource = dataSource;
this.thermalBusInputFactory = new ThermalBusInputFactory();
this.cylindricalStorageInputFactory = new CylindricalStorageInputFactory();
this.thermalHouseInputFactory = new ThermalHouseInputFactory();
}
/**
* Returns a unique set of {@link ThermalBusInput} instances.
*
* <p>This set has to be unique in the sense of object uniqueness but also in the sense of {@link
* java.util.UUID} uniqueness of the provided {@link ThermalBusInput} which has to be checked
* manually, as {@link ThermalBusInput#equals(Object)} is NOT restricted on the uuid of {@link
* ThermalBusInput}.
*
* @return a set of object and uuid unique {@link ThermalBusInput} entities
*/
public Set<ThermalBusInput> getThermalBuses() throws SourceException {
return getThermalBuses(typeSource.getOperators());
}
/**
* Returns a set of {@link ThermalBusInput} instances. This set has to be unique in the sense of
* object uniqueness but also in the sense of {@link java.util.UUID} uniqueness of the provided
* {@link ThermalBusInput} which has to be checked manually, as {@link
* ThermalBusInput#equals(Object)} is NOT restricted on the uuid of {@link ThermalBusInput}.
*
* <p>In contrast to {@link #getThermalBuses()} this interface provides the ability to pass in an
* already existing set of {@link OperatorInput} entities, the {@link ThermalBusInput} instances
* depend on. Doing so, already loaded nodes can be recycled to improve performance and prevent
* unnecessary loading operations.
*
* <p>If something fails during the creation process it's up to the concrete implementation of an
* empty set or a set with all entities that has been able to be build is returned.
*
* @param operators a set of object and uuid unique {@link OperatorInput} that should be used for
* the returning instances
* @return a set of object and uuid unique {@link ThermalBusInput} entities
*/
public Set<ThermalBusInput> getThermalBuses(Set<OperatorInput> operators) throws SourceException {
return Try.scanCollection(
buildAssetInputEntities(ThermalBusInput.class, thermalBusInputFactory, operators),
ThermalBusInput.class)
.transformF(SourceException::new)
.getOrThrow();
}
/**
* Returns a unique set of instances of all entities implementing the {@link ThermalStorageInput}
* abstract class.
*
* <p>This set has to be unique in the sense of object uniqueness but also in the sense of {@link
* java.util.UUID} uniqueness of the provided {@link ThermalStorageInput} which has to be checked
* manually, as {@link ThermalStorageInput#equals(Object)} is NOT restricted on the uuid of {@link
* ThermalStorageInput}.
*
* @return a set of object and uuid unique {@link ThermalStorageInput} entities
*/
public Set<ThermalStorageInput> getThermalStorages() throws SourceException {
return new HashSet<>(getCylindricStorages());
}
/**
* Returns a unique set of instances of all entities implementing the {@link ThermalStorageInput}
* abstract class. This set has to be unique in the sense of object uniqueness but also in the
* sense of {@link java.util.UUID} uniqueness of the provided {@link ThermalStorageInput} which
* has to be checked manually, as {@link ThermalStorageInput#equals(Object)} is NOT restricted on
* the uuid of {@link ThermalStorageInput}.
*
* <p>In contrast to {@link #getThermalStorages()} this interface provides the ability to pass in
* an already existing set of {@link OperatorInput} entities, the {@link ThermalStorageInput}
* instances depend on. Doing so, already loaded nodes can be recycled to improve performance and
* prevent unnecessary loading operations.
*
* <p>If something fails during the creation process it's up to the concrete implementation of an
* empty set or a set with all entities that has been able to be build is returned.
*
* @param operators a set of object and uuid unique {@link OperatorInput} that should be used for
* the returning instances
* @param thermalBuses a set of object and uuid unique {@link ThermalBusInput} that should be used
* for the returning instances
* @return a set of object and uuid unique {@link ThermalStorageInput} entities
*/
public Set<ThermalStorageInput> getThermalStorages(
Set<OperatorInput> operators, Set<ThermalBusInput> thermalBuses) throws SourceException {
return new HashSet<>(getCylindricStorages(operators, thermalBuses));
}
/**
* Returns a unique set of {@link ThermalHouseInput} instances.
*
* <p>This set has to be unique in the sense of object uniqueness but also in the sense of {@link
* java.util.UUID} uniqueness of the provided {@link ThermalHouseInput} which has to be checked
* manually, as {@link ThermalHouseInput#equals(Object)} is NOT restricted on the uuid of {@link
* ThermalHouseInput}.
*
* @return a set of object and uuid unique {@link ThermalHouseInput} entities
*/
public Set<ThermalHouseInput> getThermalHouses() throws SourceException {
return buildThermalHouseInputEntities(thermalHouseInputFactory)
.transformF(SourceException::new)
.getOrThrow();
}
/**
* Returns a set of {@link ThermalHouseInput} instances. This set has to be unique in the sense of
* object uniqueness but also in the sense of {@link java.util.UUID} uniqueness of the provided
* {@link ThermalHouseInput} which has to be checked manually, as {@link
* ThermalHouseInput#equals(Object)} is NOT restricted on the uuid of {@link ThermalHouseInput}.
*
* <p>In contrast to {@link #getThermalHouses()} this interface provides the ability to pass in an
* already existing set of {@link OperatorInput} entities, the {@link ThermalHouseInput} instances
* depend on. Doing so, already loaded nodes can be recycled to improve performance and prevent
* unnecessary loading operations.
*
* <p>If something fails during the creation process it's up to the concrete implementation of an
* empty set or a set with all entities that has been able to be build is returned.
*
* @param operators a set of object and uuid unique {@link OperatorInput} that should be used for
* the returning instances
* @param thermalBuses a set of object and uuid unique {@link ThermalBusInput} that should be used
* for the returning instances
* @return a set of object and uuid unique {@link ThermalHouseInput} entities
*/
public Set<ThermalHouseInput> getThermalHouses(
Set<OperatorInput> operators, Set<ThermalBusInput> thermalBuses) throws SourceException {
return buildThermalHouseInputEntities(thermalHouseInputFactory, operators, thermalBuses)
.transformF(SourceException::new)
.getOrThrow();
}
/**
* Returns a unique set of {@link CylindricalStorageInput} instances.
*
* <p>This set has to be unique in the sense of object uniqueness but also in the sense of {@link
* java.util.UUID} uniqueness of the provided {@link CylindricalStorageInput} which has to be
* checked manually, as {@link CylindricalStorageInput#equals(Object)} is NOT restricted on the
* uuid of {@link CylindricalStorageInput}.
*
* @return a set of object and uuid unique {@link CylindricalStorageInput} entities
*/
public Set<CylindricalStorageInput> getCylindricStorages() throws SourceException {
return buildCylindricalStorageInputEntities(cylindricalStorageInputFactory)
.transformF(SourceException::new)
.getOrThrow();
}
/**
* Returns a set of {@link CylindricalStorageInput} instances. This set has to be unique in the
* sense of object uniqueness but also in the sense of {@link java.util.UUID} uniqueness of the
* provided {@link CylindricalStorageInput} which has to be checked manually, as {@link
* CylindricalStorageInput#equals(Object)} is NOT restricted on the uuid of {@link
* CylindricalStorageInput}.
*
* <p>In contrast to {@link #getCylindricStorages()} this interface provides the ability to pass
* in an already existing set of {@link OperatorInput} entities, the {@link
* CylindricalStorageInput} instances depend on. Doing so, already loaded nodes can be recycled to
* improve performance and prevent unnecessary loading operations.
*
* <p>If something fails during the creation process it's up to the concrete implementation of an
* empty set or a set with all entities that has been able to be build is returned.
*
* @param operators a set of object and uuid unique {@link OperatorInput} that should be used for
* the returning instances
* @param thermalBuses a set of object and uuid unique {@link ThermalBusInput} that should be used
* for the returning instances
* @return a set of object and uuid unique {@link CylindricalStorageInput} entities
*/
public Set<CylindricalStorageInput> getCylindricStorages(
Set<OperatorInput> operators, Set<ThermalBusInput> thermalBuses) throws SourceException {
return Try.scanCollection(
buildCylindricalStorageInputEntities(
cylindricalStorageInputFactory, operators, thermalBuses),
CylindricalStorageInput.class)
.transformF(SourceException::new)
.getOrThrow();
}
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
protected Stream<Try<ThermalUnitInputEntityData, SourceException>>
buildThermalUnitInputEntityData(
AssetInputEntityData assetInputEntityData, Collection<ThermalBusInput> thermalBuses) {
// get the raw data
Map<String, String> fieldsToAttributes = assetInputEntityData.getFieldsToValues();
// get the thermal bus input for this chp unit
String thermalBusUuid = fieldsToAttributes.get("thermalbus");
Optional<ThermalBusInput> thermalBus =
thermalBuses.stream()
.filter(storage -> storage.getUuid().toString().equalsIgnoreCase(thermalBusUuid))
.findFirst();
// remove fields that are passed as objects to constructor
fieldsToAttributes.keySet().removeAll(new HashSet<>(Collections.singletonList("thermalbus")));
// if the type is not present we return an empty element and
// log a warning
if (thermalBus.isEmpty()) {
String skippingMessage =
buildSkippingMessage(
assetInputEntityData.getTargetClass().getSimpleName(),
fieldsToAttributes.get("uuid"),
fieldsToAttributes.get("id"),
"thermalBus: " + thermalBusUuid);
return Stream.of(new Failure<>(new SourceException("Failure due to: " + skippingMessage)));
}
return Stream.of(
new Success<>(
new ThermalUnitInputEntityData(
assetInputEntityData.getFieldsToValues(),
assetInputEntityData.getTargetClass(),
assetInputEntityData.getOperatorInput(),
thermalBus.get())));
}
public Try<Set<ThermalHouseInput>, FailureException> buildThermalHouseInputEntities(
ThermalHouseInputFactory factory) throws SourceException {
Set<ThermalBusInput> thermalBuses = getThermalBuses();
return Try.scanCollection(
assetInputEntityDataStream(ThermalHouseInput.class, typeSource.getOperators())
.flatMap(
assetInputEntityData ->
buildThermalUnitInputEntityData(assetInputEntityData, thermalBuses)
.map(factory::get))
.collect(Collectors.toSet()),
ThermalHouseInput.class);
}
public Try<Set<ThermalHouseInput>, FailureException> buildThermalHouseInputEntities(
ThermalHouseInputFactory factory,
Collection<OperatorInput> operators,
Collection<ThermalBusInput> thermalBuses) {
return Try.scanCollection(
assetInputEntityDataStream(ThermalHouseInput.class, operators)
.flatMap(
assetInputEntityData ->
buildThermalUnitInputEntityData(assetInputEntityData, thermalBuses)
.map(factory::get))
.collect(Collectors.toSet()),
ThermalHouseInput.class);
}
public Try<Set<CylindricalStorageInput>, FailureException> buildCylindricalStorageInputEntities(
CylindricalStorageInputFactory factory) throws SourceException {
Set<ThermalBusInput> thermalBuses = getThermalBuses();
return Try.scanCollection(
assetInputEntityDataStream(CylindricalStorageInput.class, typeSource.getOperators())
.flatMap(
assetInputEntityData ->
buildThermalUnitInputEntityData(assetInputEntityData, thermalBuses)
.map(factory::get))
.collect(Collectors.toSet()),
CylindricalStorageInput.class);
}
public Set<Try<CylindricalStorageInput, FactoryException>> buildCylindricalStorageInputEntities(
CylindricalStorageInputFactory factory,
Collection<OperatorInput> operators,
Collection<ThermalBusInput> thermalBuses) {
return assetInputEntityDataStream(CylindricalStorageInput.class, operators)
.flatMap(
assetInputEntityData ->
buildThermalUnitInputEntityData(assetInputEntityData, thermalBuses)
.map(factory::get))
.collect(Collectors.toSet());
}
}