-
Notifications
You must be signed in to change notification settings - Fork 5
/
CsvFileSink.java
405 lines (375 loc) · 16.3 KB
/
CsvFileSink.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
/*
* © 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.sink;
import edu.ie3.datamodel.exceptions.ConnectorException;
import edu.ie3.datamodel.exceptions.ExtractorException;
import edu.ie3.datamodel.exceptions.ProcessorProviderException;
import edu.ie3.datamodel.exceptions.SinkException;
import edu.ie3.datamodel.io.connectors.CsvFileConnector;
import edu.ie3.datamodel.io.csv.BufferedCsvWriter;
import edu.ie3.datamodel.io.extractor.Extractor;
import edu.ie3.datamodel.io.extractor.NestedEntity;
import edu.ie3.datamodel.io.naming.FileNamingStrategy;
import edu.ie3.datamodel.io.processor.ProcessorProvider;
import edu.ie3.datamodel.io.processor.timeseries.TimeSeriesProcessorKey;
import edu.ie3.datamodel.models.UniqueEntity;
import edu.ie3.datamodel.models.input.*;
import edu.ie3.datamodel.models.input.connector.LineInput;
import edu.ie3.datamodel.models.input.connector.SwitchInput;
import edu.ie3.datamodel.models.input.connector.Transformer2WInput;
import edu.ie3.datamodel.models.input.connector.Transformer3WInput;
import edu.ie3.datamodel.models.input.container.GraphicElements;
import edu.ie3.datamodel.models.input.container.JointGridContainer;
import edu.ie3.datamodel.models.input.container.RawGridElements;
import edu.ie3.datamodel.models.input.container.SystemParticipants;
import edu.ie3.datamodel.models.input.system.*;
import edu.ie3.datamodel.models.result.ResultEntity;
import edu.ie3.datamodel.models.timeseries.TimeSeries;
import edu.ie3.datamodel.models.timeseries.TimeSeriesEntry;
import edu.ie3.datamodel.models.value.Value;
import edu.ie3.util.StringUtils;
import java.io.IOException;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Sink that provides all capabilities to write {@link UniqueEntity}s to .csv-files. Be careful
* about using methods other than {@link #persistJointGrid(JointGridContainer)} because all other
* methods <b>do not check</b> for duplicate entries but only dump the data they received. In
* contrast, when using {@link #persistJointGrid(JointGridContainer)}, all nested entities get
* extracted first and then dumped individually without any duplicate lines.
*
* @version 0.1
* @since 19.03.20
*/
public class CsvFileSink implements InputDataSink, OutputDataSink {
private static final Logger log = LoggerFactory.getLogger(CsvFileSink.class);
private final CsvFileConnector connector;
private final ProcessorProvider processorProvider;
private final String csvSep;
public CsvFileSink(String baseFolderPath) {
this(baseFolderPath, new FileNamingStrategy(), false, ",");
}
/**
* Create an instance of a csv file sink that can be used to persist Unique entities. This
* implementation processes in sequential order. To parallelize this process one might consider
* starting several sinks and use them for specific entities.
*
* @param baseFolderPath the base folder path where the files should be put into
* @param fileNamingStrategy the data sink file naming strategy that should be used
* @param initFiles true if the files should be created during initialization (might create files,
* that only consist of a headline, because no data will be written into them), false
* otherwise
* @param csvSep the csv file separator that should be use
*/
public CsvFileSink(
String baseFolderPath,
FileNamingStrategy fileNamingStrategy,
boolean initFiles,
String csvSep) {
this(baseFolderPath, new ProcessorProvider(), fileNamingStrategy, initFiles, csvSep);
}
/**
* Create an instance of a csv file sink that can be used to persist Unique entities. This
* implementation processes in sequential order. To parallelize this process one might consider
* starting several sinks and use them for specific entities. Be careful when providing your own
* {@link ProcessorProvider} because if you're not 100% sure that it knows about all entities
* you're going to process exceptions might occur. Therefore it is strongly advised to either use
* a constructor without providing the {@link ProcessorProvider} or provide a general {@link
* ProcessorProvider} by calling {@link ProcessorProvider#ProcessorProvider()}
*
* @param baseFolderPath the base folder path where the files should be put into
* @param processorProvider the processor provided that should be used for entity serialization
* @param fileNamingStrategy the data sink file naming strategy that should be used
* @param initFiles true if the files should be created during initialization (might create files,
* that only consist of a headline, because no data will be written into them), false
* otherwise
* @param csvSep the csv file separator that should be use
*/
public CsvFileSink(
String baseFolderPath,
ProcessorProvider processorProvider,
FileNamingStrategy fileNamingStrategy,
boolean initFiles,
String csvSep) {
this.csvSep = csvSep;
this.processorProvider = processorProvider;
this.connector = new CsvFileConnector(baseFolderPath, fileNamingStrategy);
if (initFiles) initFiles(processorProvider, connector);
}
@Override
public <T extends UniqueEntity> void persistAll(Collection<T> entities) {
for (T entity : entities) {
persist(entity);
}
}
@Override
public <T extends UniqueEntity> void persist(T entity) {
/* Distinguish between "regular" input / result models and time series */
if (entity instanceof InputEntity inputEntity) {
persistIncludeNested(inputEntity);
} else if (entity instanceof ResultEntity) {
write(entity);
} else if (entity instanceof TimeSeries<?, ?> timeSeries) {
persistTimeSeries(timeSeries);
} else {
log.error(
"I don't know how to handle an entity of class {}", entity.getClass().getSimpleName());
}
}
@Override
public <C extends InputEntity> void persistIgnoreNested(C entity) {
write(entity);
}
@Override
public <C extends InputEntity> void persistAllIgnoreNested(Collection<C> entities) {
entities.parallelStream().forEach(this::persistIgnoreNested);
}
@Override
public <C extends InputEntity> void persistIncludeNested(C entity) {
if (entity instanceof NestedEntity nestedEntity) {
try {
write(entity);
for (InputEntity ent : Extractor.extractElements(nestedEntity)) {
write(ent);
}
} catch (ExtractorException e) {
log.error(
String.format(
"An error occurred during extraction of nested entity'%s': ",
entity.getClass().getSimpleName()),
e);
}
} else {
write(entity);
}
}
@Override
public <C extends InputEntity> void persistAllIncludeNested(Collection<C> entities) {
entities.parallelStream().forEach(this::persistIncludeNested);
}
@Override
public void persistJointGrid(JointGridContainer jointGridContainer) {
// get raw grid entities with types or operators
RawGridElements rawGridElements = jointGridContainer.getRawGrid();
Set<NodeInput> nodes = rawGridElements.getNodes();
Set<LineInput> lines = rawGridElements.getLines();
Set<Transformer2WInput> transformer2Ws = rawGridElements.getTransformer2Ws();
Set<Transformer3WInput> transformer3Ws = rawGridElements.getTransformer3Ws();
Set<SwitchInput> switches = rawGridElements.getSwitches();
Set<MeasurementUnitInput> measurementUnits = rawGridElements.getMeasurementUnits();
// get system participants with types or operators
SystemParticipants systemParticipants = jointGridContainer.getSystemParticipants();
Set<BmInput> bmPlants = systemParticipants.getBmPlants();
Set<ChpInput> chpPlants = systemParticipants.getChpPlants();
Set<EvcsInput> evCS = systemParticipants.getEvCS();
Set<EvInput> evs = systemParticipants.getEvs();
Set<FixedFeedInInput> fixedFeedIns = systemParticipants.getFixedFeedIns();
Set<HpInput> heatPumps = systemParticipants.getHeatPumps();
Set<LoadInput> loads = systemParticipants.getLoads();
Set<PvInput> pvPlants = systemParticipants.getPvPlants();
Set<StorageInput> storages = systemParticipants.getStorages();
Set<WecInput> wecPlants = systemParticipants.getWecPlants();
Set<EmInput> emSystems = systemParticipants.getEmSystems();
// get graphic elements (just for better readability, we could also just get them directly
// below)
GraphicElements graphicElements = jointGridContainer.getGraphics();
// extract types
Set<AssetTypeInput> types =
Stream.of(
lines,
transformer2Ws,
transformer3Ws,
bmPlants,
chpPlants,
evs,
heatPumps,
storages,
wecPlants)
.flatMap(Collection::stream)
.map(Extractor::extractType)
.collect(Collectors.toSet());
// extract operators
Set<OperatorInput> operators =
Stream.of(
nodes,
lines,
transformer2Ws,
transformer3Ws,
switches,
measurementUnits,
bmPlants,
chpPlants,
evCS,
evs,
fixedFeedIns,
heatPumps,
loads,
pvPlants,
storages,
wecPlants,
emSystems)
.flatMap(Collection::stream)
.map(Extractor::extractOperator)
.flatMap(Optional::stream)
.collect(Collectors.toSet());
// persist all entities
Stream.of(
rawGridElements.allEntitiesAsList(),
systemParticipants.allEntitiesAsList(),
graphicElements.allEntitiesAsList(),
types,
operators)
.flatMap(Collection::stream)
.forEach(this::persistIgnoreNested);
}
@Override
public void shutdown() {
// shutdown the connector
connector.shutdown();
}
@Override
public <E extends TimeSeriesEntry<V>, V extends Value> void persistTimeSeries(
TimeSeries<E, V> timeSeries) {
try {
TimeSeriesProcessorKey key = new TimeSeriesProcessorKey(timeSeries);
String[] headerElements = csvHeaderElements(processorProvider.getHeaderElements(key));
BufferedCsvWriter writer = connector.getOrInitWriter(timeSeries, headerElements, csvSep);
persistTimeSeries(timeSeries, writer);
connector.closeTimeSeriesWriter(timeSeries.getUuid());
} catch (ProcessorProviderException e) {
log.error(
"Exception occurred during receiving of header elements. Cannot write this element.", e);
} catch (ConnectorException e) {
log.error("Exception occurred during acquisition of writer.", e);
} catch (IOException e) {
log.error("Exception occurred during closing of writer.", e);
}
}
private <E extends TimeSeriesEntry<V>, V extends Value> void persistTimeSeries(
TimeSeries<E, V> timeSeries, BufferedCsvWriter writer) throws ProcessorProviderException {
try {
Set<LinkedHashMap<String, String>> entityFieldData =
processorProvider.handleTimeSeries(timeSeries);
entityFieldData.forEach(
data -> {
try {
writer.write(csvEntityFieldData(data));
} catch (IOException e) {
log.error("Cannot write the following entity data: '{}'. Exception: {}", data, e);
} catch (SinkException e) {
log.error("Exception occurred during processing the provided data fields: ", e);
}
});
} catch (ProcessorProviderException e) {
log.error("Exception occurred during processor request: ", e);
throw new ProcessorProviderException("Exception occurred during processor request: ", e);
}
}
/**
* Writes a entity into the corresponding CSV file. Does <b>not</b> include any nested entities.
* The header names for the fields will be determined by the given {@link ProcessorProvider}.
*
* @param entity the entity to write
* @param <C> bounded to be all unique entities
*/
private <C extends UniqueEntity> void write(C entity) {
LinkedHashMap<String, String> entityFieldData;
try {
entityFieldData = csvEntityFieldData(processorProvider.handleEntity(entity));
String[] headerElements = processorProvider.getHeaderElements(entity.getClass());
BufferedCsvWriter writer =
connector.getOrInitWriter(entity.getClass(), headerElements, csvSep);
writer.write(entityFieldData);
} catch (ProcessorProviderException e) {
log.error(
"Exception occurred during receiving of header elements. Cannot write this element.", e);
} catch (ConnectorException e) {
log.error("Exception occurred during retrieval of writer. Cannot write this element.", e);
} catch (IOException e) {
log.error("Exception occurred during writing of this element. Cannot write this element.", e);
} catch (SinkException e) {
log.error(
"Cannot persist provided entity '{}'. Exception: {}",
entity.getClass().getSimpleName(),
e);
}
}
/**
* Initialize files, hence create a file for each expected class that will be processed in the
* future. Please note, that files for time series can only be create on presence of a concrete
* time series, as their file name depends on the individual uuid of the time series.
*
* @param processorProvider the processor provider all files that will be processed is derived
* from
* @param connector the connector to the files
*/
private void initFiles(
final ProcessorProvider processorProvider, final CsvFileConnector connector) {
processorProvider
.getRegisteredClasses()
.forEach(
clz -> {
try {
String[] headerElements =
csvHeaderElements(processorProvider.getHeaderElements(clz));
connector.getOrInitWriter(clz, headerElements, csvSep);
} catch (ProcessorProviderException e) {
log.error(
"Error during receiving of head line elements. Cannot prepare writer for class {}.",
clz,
e);
} catch (ConnectorException e) {
log.error(
"Error during instantiation files. Cannot get or init writer for class {}.",
clz,
e);
}
});
}
/**
* Transforms a provided array of strings to valid csv formatted strings (according to csv
* specification RFC 4180)
*
* @param strings array of strings that should be processed
* @return a new array with valid csv formatted strings
*/
private String[] csvHeaderElements(String[] strings) {
return Arrays.stream(strings)
.map(inputElement -> StringUtils.csvString(inputElement, csvSep))
.toArray(String[]::new);
}
/**
* Transforms a provided map of string to string to valid csv formatted strings (according to csv
* specification RFC 4180)
*
* @param entityFieldData a string to string map that should be processed
* @return a new map with valid csv formatted keys and values strings
*/
private LinkedHashMap<String, String> csvEntityFieldData(
LinkedHashMap<String, String> entityFieldData) {
return entityFieldData.entrySet().stream()
.map(
mapEntry ->
new AbstractMap.SimpleEntry<>(
StringUtils.csvString(mapEntry.getKey(), csvSep),
StringUtils.csvString(mapEntry.getValue(), csvSep)))
.collect(
Collectors.toMap(
AbstractMap.SimpleEntry::getKey,
AbstractMap.SimpleEntry::getValue,
(v1, v2) -> {
throw new IllegalStateException(
"Converting entity data to RFC 4180 compliant strings has lead to duplicate keys. Initial input:\n\t"
+ entityFieldData.entrySet().stream()
.map(entry -> entry.getKey() + " = " + entry.getValue())
.collect(Collectors.joining(",\n\t")));
},
LinkedHashMap::new));
}
}