-
Notifications
You must be signed in to change notification settings - Fork 5
/
FileNamingStrategy.java
340 lines (310 loc) · 14 KB
/
FileNamingStrategy.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
/*
* © 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.io.IoUtil;
import edu.ie3.datamodel.io.csv.FileNameMetaInformation;
import edu.ie3.datamodel.io.naming.timeseries.IndividualTimeSeriesMetaInformation;
import edu.ie3.datamodel.io.naming.timeseries.LoadProfileTimeSeriesMetaInformation;
import edu.ie3.datamodel.models.UniqueEntity;
import edu.ie3.datamodel.models.timeseries.TimeSeries;
import edu.ie3.datamodel.models.timeseries.TimeSeriesEntry;
import edu.ie3.datamodel.models.timeseries.individual.IndividualTimeSeries;
import edu.ie3.datamodel.models.timeseries.repetitive.LoadProfileInput;
import edu.ie3.datamodel.models.value.Value;
import java.nio.file.Path;
import java.util.Optional;
import java.util.regex.Pattern;
import org.apache.commons.io.FilenameUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* A naming strategy, that combines an {@link EntityPersistenceNamingStrategy} for naming entities
* and a {@link FileHierarchy} for a folder structure.
*/
public class FileNamingStrategy {
private static final Logger logger = LoggerFactory.getLogger(FileNamingStrategy.class);
private final EntityPersistenceNamingStrategy entityPersistenceNamingStrategy;
private final FileHierarchy fileHierarchy;
/**
* Constructor for building the file naming strategy.
*
* @param entityPersistenceNamingStrategy entity naming strategy
* @param fileHierarchy directory hierarchy
*/
public FileNamingStrategy(
EntityPersistenceNamingStrategy entityPersistenceNamingStrategy,
FileHierarchy fileHierarchy) {
this.entityPersistenceNamingStrategy = entityPersistenceNamingStrategy;
this.fileHierarchy = fileHierarchy;
}
/**
* Constructor for building the file naming strategy. Since no directory hierarchy is provided, a
* flat directory hierarchy is used.
*
* @param entityPersistenceNamingStrategy entity naming strategy
*/
public FileNamingStrategy(EntityPersistenceNamingStrategy entityPersistenceNamingStrategy) {
this(entityPersistenceNamingStrategy, new FlatDirectoryHierarchy());
}
/**
* Constructor for building the file naming strategy. Since no entity naming strategy is provided,
* the entity naming strategy is used. Since no directory hierarchy is provided, a flat directory
* hierarchy is used.
*/
public FileNamingStrategy() {
this(new EntityPersistenceNamingStrategy(), new FlatDirectoryHierarchy());
}
/**
* Get the full path to the file with regard to some (not explicitly specified) base directory.
* The path does NOT start or end with any of the known file separators or file extension.
*
* @param cls Targeted class of the given file
* @return An optional sub path to the actual file
*/
public Optional<Path> getFilePath(Class<? extends UniqueEntity> cls) {
// do not adapt orElseGet, see https://www.baeldung.com/java-optional-or-else-vs-or-else-get for
// details
return getFilePath(
getEntityName(cls).orElseGet(() -> ""), getDirectoryPath(cls).orElseGet(() -> Path.of("")));
}
/**
* Get the full path to the file with regard to some (not explicitly specified) base directory.
* The path does NOT start or end with any of the known file separators or file extension.
*
* @param <T> Type of the time series
* @param <E> Type of the entry in the time series
* @param <V> Type of the value, that is carried by the time series entry
* @param timeSeries Time series to derive naming information from
* @return An optional sub path to the actual file
*/
public <T extends TimeSeries<E, V>, E extends TimeSeriesEntry<V>, V extends Value>
Optional<Path> getFilePath(T timeSeries) {
// do not adapt orElseGet, see https://www.baeldung.com/java-optional-or-else-vs-or-else-get for
// details
return getFilePath(
entityPersistenceNamingStrategy.getEntityName(timeSeries).orElseGet(() -> ""),
getDirectoryPath(timeSeries).orElseGet(() -> Path.of("")));
}
/**
* Compose a full file path from directory name and file name. Additionally perform some checks,
* like if the file name itself actually is available
*
* @param fileName File name
* @param subDirectories Sub directory path
* @return Concatenation of sub directory structure and file name
*/
private Optional<Path> getFilePath(String fileName, Path subDirectories) {
if (fileName.isEmpty()) return Optional.empty();
if (!subDirectories.toString().isEmpty()) return Optional.of(subDirectories.resolve(fileName));
else return Optional.of(Path.of(fileName));
}
/**
* Returns the sub directory structure with regard to some (not explicitly specified) base
* directory. The path does NOT start or end with any of the known file separators.
*
* @param cls Targeted class of the given file
* @return An optional sub directory path
*/
public Optional<Path> getDirectoryPath(Class<? extends UniqueEntity> cls) {
Optional<Path> maybeDirectoryName = fileHierarchy.getSubDirectory(cls);
if (maybeDirectoryName.isEmpty()) {
logger.debug("Cannot determine directory name for class '{}'.", cls);
return Optional.empty();
} else {
/* Make sure, the directory path does not start or end with file separator and in between the separator is harmonized */
return Optional.of(
Path.of(
IoUtil.harmonizeFileSeparator(
maybeDirectoryName
.get()
.toString()
.replaceFirst("^" + IoUtil.FILE_SEPARATOR_REGEX, ""))));
}
}
/**
* Returns the sub directory structure with regard to some (not explicitly specified) base
* directory. The path does NOT start or end with any of the known file separators.
*
* @param <T> Type of the time series
* @param <E> Type of the entry in the time series
* @param <V> Type of the value, that is carried by the time series entry
* @param timeSeries Time series to derive naming information from
* @return An optional sub directory path
*/
public <T extends TimeSeries<E, V>, E extends TimeSeriesEntry<V>, V extends Value>
Optional<Path> getDirectoryPath(T timeSeries) {
Optional<Path> maybeDirectoryName = fileHierarchy.getSubDirectory(timeSeries.getClass());
if (maybeDirectoryName.isEmpty()) {
logger.debug("Cannot determine directory name for time series '{}'.", timeSeries);
return Optional.empty();
} else {
/* Make sure, the directory path does not start or end with file separator and in between the separator is harmonized */
return maybeDirectoryName;
}
}
/**
* Returns the pattern to identify individual time series in this instance of the file naming
* strategy considering the {@link EntityPersistenceNamingStrategy} and {@link FileHierarchy}.
*
* @return An individual time series pattern
*/
public Pattern getIndividualTimeSeriesPattern() {
Path subDirectory =
fileHierarchy.getSubDirectory(IndividualTimeSeries.class).orElseGet(() -> Path.of(""));
if (subDirectory.toString().isEmpty()) {
return entityPersistenceNamingStrategy.getIndividualTimeSeriesPattern();
} else {
/* Build the pattern by joining the subdirectory with the file name pattern, harmonizing file separators and
* finally escaping them */
String joined =
FilenameUtils.concat(
subDirectory.toString(),
entityPersistenceNamingStrategy.getIndividualTimeSeriesPattern().pattern());
String harmonized = IoUtil.harmonizeFileSeparator(joined);
String escaped = harmonized.replace("\\", "\\\\");
return Pattern.compile(escaped);
}
}
/**
* Returns the pattern to identify load profile time series in this instance of the file naming
* strategy considering the {@link EntityPersistenceNamingStrategy} and {@link FileHierarchy}.
*
* @return A load profile time series pattern
*/
public Pattern getLoadProfileTimeSeriesPattern() {
Path subDirectory =
fileHierarchy.getSubDirectory(LoadProfileInput.class).orElseGet(() -> Path.of(""));
if (subDirectory.toFile().exists()) {
return entityPersistenceNamingStrategy.getLoadProfileTimeSeriesPattern();
} else {
/* Build the pattern by joining the sub directory with the file name pattern, harmonizing file separators and
* finally escaping them */
String joined =
FilenameUtils.concat(
subDirectory.toString(),
entityPersistenceNamingStrategy.getLoadProfileTimeSeriesPattern().pattern());
String harmonized = IoUtil.harmonizeFileSeparator(joined);
String escaped = harmonized.replace("\\", "\\\\");
return Pattern.compile(escaped);
}
}
/**
* Extracts meta information from a file name, of a time series.
*
* @param path Path to the file
* @return The meeting meta information
* @deprecated since 3.0. Use {@link #timeSeriesMetaInformation(Path)} instead.
*/
@Deprecated(since = "3.0", forRemoval = true)
public FileNameMetaInformation extractTimeSeriesMetaInformation(Path path) {
/* Extract file name from possibly fully qualified path */
Path fileName = path.getFileName();
if (fileName == null)
throw new IllegalArgumentException("Unable to extract file name from path '" + path + "'.");
return extractTimeSeriesMetaInformation(fileName.toString());
}
/**
* Extracts meta information from a file name, of a time series.
*
* @param path Path to the file
* @return The meeting meta information
*/
public TimeSeriesMetaInformation timeSeriesMetaInformation(Path path) {
/* Extract file name from possibly fully qualified path */
Path fileName = path.getFileName();
if (fileName == null)
throw new IllegalArgumentException("Unable to extract file name from path '" + path + "'.");
return timeSeriesMetaInformation(fileName.toString());
}
/**
* Extracts meta information from a file name, of a time series. Here, a file name <u>without</u>
* leading path has to be provided
*
* @param fileName File name
* @return The meeting meta information
* @deprecated since 3.0. Use {@link #timeSeriesMetaInformation(String)} instead.
*/
@Deprecated(since = "3.0", forRemoval = true)
public FileNameMetaInformation extractTimeSeriesMetaInformation(String fileName) {
TimeSeriesMetaInformation meta = timeSeriesMetaInformation(fileName);
if (meta instanceof IndividualTimeSeriesMetaInformation ind) {
return new edu.ie3.datamodel.io.csv.timeseries.IndividualTimeSeriesMetaInformation(ind);
} else if (meta instanceof LoadProfileTimeSeriesMetaInformation load) {
return new edu.ie3.datamodel.io.csv.timeseries.LoadProfileTimeSeriesMetaInformation(load);
} else
throw new IllegalArgumentException(
"Unknown format of '" + fileName + "'. Cannot extract meta information.");
}
/**
* Extracts meta information from a file name, of a time series. Here, a file name <u>without</u>
* leading path has to be provided
*
* @param fileName File name
* @return The meeting meta information
*/
public TimeSeriesMetaInformation timeSeriesMetaInformation(String fileName) {
/* Remove the file ending (ending limited to 255 chars, which is the max file name allowed in NTFS and ext4) */
String withoutEnding = removeFileNameEnding(fileName);
if (getIndividualTimeSeriesPattern().matcher(withoutEnding).matches())
return entityPersistenceNamingStrategy.individualTimesSeriesMetaInformation(withoutEnding);
else if (getLoadProfileTimeSeriesPattern().matcher(withoutEnding).matches())
return entityPersistenceNamingStrategy.loadProfileTimesSeriesMetaInformation(withoutEnding);
else
throw new IllegalArgumentException(
"Unknown format of '" + fileName + "'. Cannot extract meta information.");
}
public IndividualTimeSeriesMetaInformation individualTimeSeriesMetaInformation(String fileName) {
return entityPersistenceNamingStrategy.individualTimesSeriesMetaInformation(
removeFileNameEnding(fileName));
}
public static String removeFileNameEnding(String fileName) {
return fileName.replaceAll("(?:\\.[^.\\\\/\\s]{1,255}){1,2}$", "");
}
/**
* Get the entity name for coordinates
*
* @return the entity name string
*/
public String getIdCoordinateEntityName() {
return entityPersistenceNamingStrategy.getIdCoordinateEntityName();
}
/**
* Get the full path to the id coordinate file with regard to some (not explicitly specified) base
* directory. The path does NOT start or end with any of the known file separators or file
* extension.
*
* @return An optional sub path to the id coordinate file
*/
public Optional<Path> getIdCoordinateFilePath() {
// do not adapt orElseGet, see https://www.baeldung.com/java-optional-or-else-vs-or-else-get for
// details
return getFilePath(
getIdCoordinateEntityName(), fileHierarchy.getBaseDirectory().orElseGet(() -> Path.of("")));
}
/**
* Returns the name of the entity, that should be used for persistence.
*
* @param cls Targeted class of the given file
* @return The name of the entity
*/
public Optional<String> getEntityName(Class<? extends UniqueEntity> cls) {
return entityPersistenceNamingStrategy.getEntityName(cls);
}
/**
* Builds a file name (and only the file name without any directories and extension) of the given
* information.
*
* @param <T> Type of the time series
* @param <E> Type of the entry in the time series
* @param <V> Type of the value, that is carried by the time series entry
* @param timeSeries Time series to derive naming information from
* @return A file name for this particular time series
*/
public <T extends TimeSeries<E, V>, E extends TimeSeriesEntry<V>, V extends Value>
Optional<String> getEntityName(T timeSeries) {
return entityPersistenceNamingStrategy.getEntityName(timeSeries);
}
}