-
Notifications
You must be signed in to change notification settings - Fork 5
/
CsvJointGridContainerSource.java
94 lines (82 loc) · 3.99 KB
/
CsvJointGridContainerSource.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
/*
* © 2022. 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.csv;
import edu.ie3.datamodel.exceptions.FactoryException;
import edu.ie3.datamodel.exceptions.FileException;
import edu.ie3.datamodel.exceptions.InvalidGridException;
import edu.ie3.datamodel.exceptions.SourceException;
import edu.ie3.datamodel.io.naming.DefaultDirectoryHierarchy;
import edu.ie3.datamodel.io.naming.EntityPersistenceNamingStrategy;
import edu.ie3.datamodel.io.naming.FileNamingStrategy;
import edu.ie3.datamodel.io.source.*;
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.utils.ExceptionUtils;
import edu.ie3.datamodel.utils.Try;
import java.nio.file.Path;
import java.util.*;
/** Convenience class for cases where all used data comes from CSV sources */
public class CsvJointGridContainerSource {
private CsvJointGridContainerSource() {}
public static JointGridContainer read(
String gridName, String csvSep, Path directoryPath, boolean isHierarchic)
throws SourceException, FileException, InvalidGridException {
/* Parameterization */
FileNamingStrategy namingStrategy;
if (isHierarchic) {
// Hierarchic structure
DefaultDirectoryHierarchy fileHierarchy =
new DefaultDirectoryHierarchy(directoryPath, gridName);
namingStrategy = new FileNamingStrategy(new EntityPersistenceNamingStrategy(), fileHierarchy);
fileHierarchy.validate();
} else {
// Flat structure
namingStrategy = new FileNamingStrategy();
}
CsvDataSource dataSource = new CsvDataSource(csvSep, directoryPath, namingStrategy);
/* Instantiating sources */
TypeSource typeSource = new TypeSource(dataSource);
RawGridSource rawGridSource = new RawGridSource(typeSource, dataSource);
ThermalSource thermalSource = new ThermalSource(typeSource, dataSource);
SystemParticipantSource systemParticipantSource =
new SystemParticipantSource(typeSource, thermalSource, rawGridSource, dataSource);
GraphicSource graphicSource = new GraphicSource(typeSource, rawGridSource, dataSource);
/* validating sources */
List<FactoryException> validationException =
Try.getExceptions(
Try.ofVoid(typeSource::validate, FactoryException.class),
Try.ofVoid(rawGridSource::validate, FactoryException.class),
Try.ofVoid(systemParticipantSource::validate, FactoryException.class),
Try.ofVoid(graphicSource::validate, FactoryException.class));
if (!validationException.isEmpty()) {
throw new FactoryException(
"Validation failed due to: " + ExceptionUtils.getFullMessages(validationException));
}
/* Loading models */
Try<RawGridElements, SourceException> rawGridElements =
Try.of(rawGridSource::getGridData, SourceException.class);
Try<SystemParticipants, SourceException> systemParticipants =
Try.of(systemParticipantSource::getSystemParticipants, SourceException.class);
Try<GraphicElements, SourceException> graphicElements =
Try.of(graphicSource::getGraphicElements, SourceException.class);
List<? extends Exception> exceptions =
Try.getExceptions(List.of(rawGridElements, systemParticipants, graphicElements));
if (!exceptions.isEmpty()) {
throw new SourceException(
exceptions.size() + " error(s) occurred while reading sources. ", exceptions);
} else {
// getOrThrow should not throw an exception in this context, because all exception are
// filtered and thrown before
return new JointGridContainer(
gridName,
rawGridElements.getOrThrow(),
systemParticipants.getOrThrow(),
graphicElements.getOrThrow());
}
}
}