-
Notifications
You must be signed in to change notification settings - Fork 5
/
TimeSeriesMappingSource.java
107 lines (90 loc) · 3.6 KB
/
TimeSeriesMappingSource.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
/*
* © 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.SourceException;
import edu.ie3.datamodel.io.factory.EntityData;
import edu.ie3.datamodel.io.factory.timeseries.TimeSeriesMappingFactory;
import edu.ie3.datamodel.models.input.InputEntity;
import edu.ie3.datamodel.models.input.system.SystemParticipantInput;
import edu.ie3.datamodel.models.timeseries.TimeSeries;
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;
/**
* This interface describes basic function to handle mapping between models and their respective
* time series
*/
public abstract class TimeSeriesMappingSource {
protected final TimeSeriesMappingFactory mappingFactory;
protected TimeSeriesMappingSource() {
this.mappingFactory = new TimeSeriesMappingFactory();
}
/**
* Get a mapping from model {@link UUID} to the time series {@link UUID}
*
* @return That mapping
*/
public Map<UUID, UUID> getMapping() throws SourceException {
return getMappingSourceData()
.map(this::createMappingEntry)
.filter(Try::isSuccess)
.map(t -> (Success<MappingEntry, FactoryException>) t)
.map(Success::get)
.collect(Collectors.toMap(MappingEntry::participant, MappingEntry::timeSeries));
}
/**
* Get a time series identifier to a given model identifier
*
* @param modelIdentifier Identifier of the model
* @return An {@link Optional} to the time series identifier
*/
public Optional<UUID> getTimeSeriesUuid(UUID modelIdentifier) throws SourceException {
return Optional.ofNullable(getMapping().get(modelIdentifier));
}
/**
* Extract a stream of maps from the database for the mapping
*
* @return Stream of maps
*/
public abstract Stream<Map<String, String>> getMappingSourceData() throws SourceException;
/** Returns the option for fields found in the source */
public abstract Optional<Set<String>> getSourceFields() throws SourceException;
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
private Try<MappingEntry, FactoryException> createMappingEntry(
Map<String, String> fieldToValues) {
EntityData entityData = new EntityData(fieldToValues, MappingEntry.class);
return mappingFactory.get(entityData);
}
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
/** Class to represent one entry within the participant to time series mapping */
public record MappingEntry(UUID participant, UUID timeSeries) implements InputEntity {
/** Returns the {@link UUID} of the {@link SystemParticipantInput}. */
public UUID getParticipant() {
return participant;
}
/** Returns the {@link UUID} of the {@link TimeSeries}. */
public UUID getTimeSeries() {
return timeSeries;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof MappingEntry that)) return false;
return participant.equals(that.participant) && timeSeries.equals(that.timeSeries);
}
@Override
public int hashCode() {
return Objects.hash(participant, timeSeries);
}
@Override
public String toString() {
return "MappingEntry{" + "participant=" + participant + ", timeSeries=" + timeSeries + '}';
}
}
}