-
Notifications
You must be signed in to change notification settings - Fork 5
/
DefaultDirectoryHierarchy.java
354 lines (325 loc) · 13.5 KB
/
DefaultDirectoryHierarchy.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
/*
* © 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.naming;
import edu.ie3.datamodel.exceptions.FileException;
import edu.ie3.datamodel.io.source.TimeSeriesMappingSource;
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.connector.type.LineTypeInput;
import edu.ie3.datamodel.models.input.connector.type.Transformer2WTypeInput;
import edu.ie3.datamodel.models.input.connector.type.Transformer3WTypeInput;
import edu.ie3.datamodel.models.input.graphics.GraphicInput;
import edu.ie3.datamodel.models.input.system.*;
import edu.ie3.datamodel.models.input.system.characteristic.WecCharacteristicInput;
import edu.ie3.datamodel.models.input.system.type.*;
import edu.ie3.datamodel.models.input.thermal.ThermalBusInput;
import edu.ie3.datamodel.models.input.thermal.ThermalUnitInput;
import edu.ie3.datamodel.models.result.NodeResult;
import edu.ie3.datamodel.models.result.connector.LineResult;
import edu.ie3.datamodel.models.result.connector.SwitchResult;
import edu.ie3.datamodel.models.result.connector.Transformer2WResult;
import edu.ie3.datamodel.models.result.connector.Transformer3WResult;
import edu.ie3.datamodel.models.result.system.*;
import edu.ie3.datamodel.models.result.thermal.ThermalUnitResult;
import edu.ie3.datamodel.models.timeseries.TimeSeries;
import edu.ie3.datamodel.models.timeseries.repetitive.LoadProfileInput;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.apache.commons.io.FilenameUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/** Default directory hierarchy for input models */
public class DefaultDirectoryHierarchy implements FileHierarchy {
private static final Logger logger = LoggerFactory.getLogger(DefaultDirectoryHierarchy.class);
/** Use the unix file separator here. */
protected static final String FILE_SEPARATOR = File.separator;
/** Base directory for this specific grid model. The base path should be a directory. */
private final Path baseDirectory;
/** The project's directory beneath the {@code baseDirectory} */
private final Path projectDirectory;
/** Mapping from sub directories to if they are mandatory or not */
private final Map<Path, Boolean> subDirectories;
/** Set of additional paths (sub directory trees), that are permissible to exist */
private final Path inputTree;
private final Path resultTree;
public DefaultDirectoryHierarchy(String baseDirectory, String gridName) {
/* Prepare the base path */
String baseDirectoryNormalized =
FilenameUtils.normalizeNoEndSeparator(baseDirectory, true) + FILE_SEPARATOR;
this.baseDirectory = Paths.get(baseDirectoryNormalized).toAbsolutePath();
this.projectDirectory =
Paths.get(
baseDirectoryNormalized
+ FilenameUtils.normalizeNoEndSeparator(gridName, true)
+ FILE_SEPARATOR)
.toAbsolutePath();
/* Prepare the sub directories by appending the relative path to base path and mapping to information about being mandatory */
this.subDirectories =
Arrays.stream(SubDirectories.values())
.collect(
Collectors.toMap(
subDirectory ->
Paths.get(
FilenameUtils.concat(
this.projectDirectory.toString(), subDirectory.getRelPath())),
SubDirectories::isMandatory));
inputTree =
Paths.get(
FilenameUtils.concat(
projectDirectory.toString(), SubDirectories.Constants.INPUT_SUB_TREE));
resultTree =
Paths.get(
FilenameUtils.concat(
projectDirectory.toString(), SubDirectories.Constants.RESULT_SUB_TREE));
}
/**
* Checks, if the structure beneath {@link #baseDirectory} is okay.
*
* @throws FileException if not
*/
@Override
public void validate() throws FileException {
if (!Files.exists(projectDirectory))
throw new FileException("The path '" + projectDirectory + "' does not exist.");
if (!Files.isDirectory(projectDirectory))
throw new FileException("The path '" + projectDirectory + "' has to be a directory.");
checkExpectedDirectories();
checkFurtherDirectoryElements();
}
/**
* Validates, that all mandatory directories are apparent and are actually directories. Optional
* directories do not need to be there, but need to be directories.
*
* @throws FileException if either the directory is not there or an optional directory is not a
* directory.
*/
private void checkExpectedDirectories() throws FileException {
for (Map.Entry<Path, Boolean> entry : subDirectories.entrySet()) {
Path subDirectory = entry.getKey();
boolean mandatory = entry.getValue();
if (mandatory) {
if (!Files.exists(subDirectory))
throw new FileException("The mandatory directory '" + subDirectory + "' does not exist.");
if (!Files.isDirectory(subDirectory))
throw new FileException(
"The mandatory directory '" + subDirectory + "' is not a directory.");
} else {
if (Files.exists(subDirectory) && !Files.isDirectory(subDirectory))
throw new FileException(
"The optional directory '" + subDirectory + "' is not a directory.");
logger.debug("The optional directory '{}' exists.", subDirectory);
}
}
}
/**
* Checks the elements, that are further available underneath the {@link this#baseDirectory}. If
* there is a directory, that is neither mandatory, nor optional, raise an Exception.
*
* @throws FileException if there is an unexpected directory
*/
private void checkFurtherDirectoryElements() throws FileException {
try (Stream<Path> apparentElementsStream = Files.list(projectDirectory)) {
for (Path apparentPath : apparentElementsStream.toList()) {
if (Files.isDirectory(apparentPath)
&& !subDirectories.containsKey(apparentPath)
&& apparentPath.compareTo(inputTree) != 0
&& apparentPath.compareTo(resultTree) != 0)
throw new FileException(
"There is a directory '"
+ apparentPath
+ "' apparent, that is not supported by the default directory hierarchy.");
}
} catch (IOException e) {
throw new FileException(
"Cannot get the list of apparent elements in '" + projectDirectory + "'.", e);
}
}
/**
* Creates all mandatory subdirectories of this default directory hierarchy
*
* @throws IOException If the creation of sub directories is not possible
*/
public void createDirs() throws IOException {
createDirs(false);
}
/**
* Creates all subdirectories of this default directory hierarchy. Upon request, also the optional
* directories are created.
*
* @param withOptionals if true, also optional directories get created.
* @throws IOException If the creation of sub directories is not possible
*/
public void createDirs(boolean withOptionals) throws IOException {
Files.createDirectories(projectDirectory);
for (Map.Entry<Path, Boolean> entry : subDirectories.entrySet()) {
Path directoryPath = entry.getKey();
boolean isMandatory = entry.getValue();
if (isMandatory || withOptionals) {
Files.createDirectories(directoryPath);
}
}
}
/**
* Gives the {@link #baseDirectory}).
*
* @return An Option to the base directory as a string
*/
@Override
public Optional<String> getBaseDirectory() {
return Optional.of(this.baseDirectory.toString());
}
/**
* Gives the correct sub directory (w.r.t. {@link #baseDirectory}) for the provided class.
*
* @param cls Class to define the sub directory for
* @param fileSeparator The file separator to use
* @return An Option to the regarding sub directory as a string
*/
@Override
public Optional<String> getSubDirectory(Class<? extends UniqueEntity> cls, String fileSeparator) {
/* Go through all sub directories and check, if the given class belongs to one of the classes mapped to the sub directories. */
Optional<SubDirectories> maybeSubDirectory =
Arrays.stream(SubDirectories.values())
.filter(
subDirectory ->
subDirectory.getRelevantClasses().stream()
.anyMatch(definedClass -> definedClass.isAssignableFrom(cls)))
.findFirst();
if (maybeSubDirectory.isEmpty()) {
logger.debug("Don't know a fitting sub directory for class '{}'.", cls.getSimpleName());
return Optional.empty();
} else {
/* Build the full path and then refer it to the base directory */
Path fullPath =
Paths.get(
FilenameUtils.concat(
this.projectDirectory.toString(), maybeSubDirectory.get().getRelPath()));
String relPath = this.baseDirectory.relativize(fullPath).toString();
return Optional.of(relPath);
}
}
private enum SubDirectories {
GRID_INPUT(
Constants.INPUT_SUB_TREE + FILE_SEPARATOR + "grid" + FILE_SEPARATOR,
true,
Stream.of(
LineInput.class,
SwitchInput.class,
Transformer2WInput.class,
Transformer3WInput.class,
MeasurementUnitInput.class,
NodeInput.class)
.collect(Collectors.toSet())),
GRID_RESULT(
Constants.RESULT_SUB_TREE + FILE_SEPARATOR + "grid" + FILE_SEPARATOR,
false,
Stream.of(
LineResult.class,
SwitchResult.class,
Transformer2WResult.class,
Transformer3WResult.class,
NodeResult.class)
.collect(Collectors.toSet())),
GLOBAL(
Constants.INPUT_SUB_TREE + FILE_SEPARATOR + "global" + FILE_SEPARATOR,
true,
Stream.of(
LineTypeInput.class,
Transformer2WTypeInput.class,
Transformer3WTypeInput.class,
BmTypeInput.class,
ChpTypeInput.class,
EvTypeInput.class,
HpTypeInput.class,
StorageTypeInput.class,
WecTypeInput.class,
OperatorInput.class,
WecCharacteristicInput.class,
RandomLoadParameters.class,
LoadProfileInput.class)
.collect(Collectors.toSet())),
PARTICIPANTS_INPUT(
Constants.INPUT_SUB_TREE + FILE_SEPARATOR + "participants" + FILE_SEPARATOR,
true,
Stream.of(
BmInput.class,
ChpInput.class,
EvInput.class,
EvcsInput.class,
FixedFeedInInput.class,
HpInput.class,
LoadInput.class,
PvInput.class,
StorageInput.class,
WecInput.class)
.collect(Collectors.toSet())),
PARTICIPANTS_RESULTS(
Constants.RESULT_SUB_TREE + FILE_SEPARATOR + "participants" + FILE_SEPARATOR,
false,
Stream.of(
BmResult.class,
ChpResult.class,
EvResult.class,
EvcsResult.class,
FixedFeedInResult.class,
HpResult.class,
LoadResult.class,
PvResult.class,
StorageResult.class,
WecResult.class,
EmResult.class,
FlexOptionsResult.class)
.collect(Collectors.toSet())),
TIME_SERIES(
PARTICIPANTS_INPUT.relPath + "time_series" + FILE_SEPARATOR,
false,
Stream.of(TimeSeries.class, TimeSeriesMappingSource.MappingEntry.class)
.collect(Collectors.toSet())),
THERMAL_INPUT(
Constants.INPUT_SUB_TREE + FILE_SEPARATOR + "thermal" + FILE_SEPARATOR,
false,
Stream.of(ThermalUnitInput.class, ThermalBusInput.class).collect(Collectors.toSet())),
THERMAL_RESULTS(
Constants.RESULT_SUB_TREE + FILE_SEPARATOR + "thermal" + FILE_SEPARATOR,
false,
Stream.of(ThermalUnitResult.class).collect(Collectors.toSet())),
GRAPHICS(
Constants.INPUT_SUB_TREE + FILE_SEPARATOR + "graphics" + FILE_SEPARATOR,
false,
Stream.of(GraphicInput.class).collect(Collectors.toSet()));
private final String relPath;
private final boolean mandatory;
private final Set<Class<?>> relevantClasses;
public String getRelPath() {
return relPath;
}
public boolean isMandatory() {
return mandatory;
}
public Set<Class<?>> getRelevantClasses() {
return relevantClasses;
}
SubDirectories(String relPath, boolean mandatory, Set<Class<?>> relevantClasses) {
this.relPath = relPath;
this.mandatory = mandatory;
this.relevantClasses = Collections.unmodifiableSet(relevantClasses);
}
private static class Constants {
private static final String INPUT_SUB_TREE = "input";
private static final String RESULT_SUB_TREE = "results";
}
}
}