Skip to content

Commit 1155374

Browse files
committed
Adding value supplier method.
1 parent 2abbd59 commit 1155374

File tree

5 files changed

+88
-7
lines changed

5 files changed

+88
-7
lines changed

src/main/java/edu/ie3/datamodel/io/source/PowerValueSource.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import edu.ie3.datamodel.models.value.PValue;
1111
import java.time.ZonedDateTime;
1212
import java.util.Optional;
13+
import java.util.function.Supplier;
1314
import javax.measure.quantity.Energy;
1415
import javax.measure.quantity.Power;
1516
import tech.units.indriya.ComparableQuantity;
@@ -30,6 +31,16 @@ public sealed interface PowerValueSource<
3031
*/
3132
Optional<PValue> getValue(ID data);
3233

34+
/**
35+
* Method to get a supplier for the next power value based on the provided input data. Depending
36+
* on the implementation the supplier will either always return the same value or each time a
37+
* random value. To return one constant value please use {@link #getValue(InputData)}.
38+
*
39+
* @param data input data that is used to calculate the next power value.
40+
* @return A supplier for an option on the value at the given time step.
41+
*/
42+
Supplier<Optional<PValue>> getValueSupplier(ID data);
43+
3344
/**
3445
* Method to determine the next timestamp for which data is present.
3546
*

src/main/java/edu/ie3/datamodel/io/source/csv/CsvLoadProfileSource.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import java.nio.file.Path;
2121
import java.util.*;
2222
import java.util.function.Function;
23+
import java.util.function.Supplier;
2324
import java.util.stream.Collectors;
2425
import javax.measure.quantity.Energy;
2526
import javax.measure.quantity.Power;
@@ -74,6 +75,11 @@ public Optional<PValue> getValue(TimeSeriesInputValue data) {
7475
return loadProfileTimeSeries.getValue(data.time());
7576
}
7677

78+
@Override
79+
public Supplier<Optional<PValue>> getValueSupplier(TimeSeriesInputValue data) {
80+
return loadProfileTimeSeries.supplyValue(data.time());
81+
}
82+
7783
@Override
7884
public P getProfile() {
7985
return loadProfileTimeSeries.getLoadProfile();

src/main/java/edu/ie3/datamodel/io/source/sql/SqlLoadProfileSource.java

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import java.util.Map;
2525
import java.util.Optional;
2626
import java.util.Set;
27+
import java.util.function.Supplier;
2728
import java.util.stream.Collectors;
2829
import javax.measure.quantity.Energy;
2930
import javax.measure.quantity.Power;
@@ -101,12 +102,20 @@ public Set<LoadProfileEntry<V>> getEntries() {
101102
@Override
102103
public Optional<PValue> getValue(TimeSeriesInputValue data) {
103104
ZonedDateTime time = data.time();
105+
return queryForValue(time).map(loadValue -> loadValue.getValue(time, profile));
106+
}
104107

105-
Set<LoadProfileEntry<V>> entries =
106-
getEntries(queryTime, ps -> ps.setInt(1, TimeSeriesUtils.calculateQuarterHourOfDay(time)));
107-
if (entries.isEmpty()) return Optional.empty();
108-
if (entries.size() > 1) log.warn("Retrieved more than one result value, using the first");
109-
return entries.stream().findFirst().map(entry -> entry.getValue().getValue(time, profile));
108+
@Override
109+
public Supplier<Optional<PValue>> getValueSupplier(TimeSeriesInputValue data) {
110+
ZonedDateTime time = data.time();
111+
Optional<LoadValues<P>> loadValueOption = queryForValue(time);
112+
113+
if (loadValueOption.isPresent()) {
114+
LoadValues<P> loadValue = loadValueOption.get();
115+
return () -> Optional.of(loadValue.getValue(time, profile));
116+
} else {
117+
return Optional::empty;
118+
}
110119
}
111120

112121
@Override
@@ -138,6 +147,15 @@ private Set<LoadProfileEntry<V>> getEntries(String query, SqlDataSource.AddParam
138147
.collect(Collectors.toSet());
139148
}
140149

150+
private Optional<LoadValues<P>> queryForValue(ZonedDateTime time) {
151+
Set<LoadProfileEntry<V>> entries =
152+
getEntries(queryTime, ps -> ps.setInt(1, TimeSeriesUtils.calculateQuarterHourOfDay(time)));
153+
if (entries.isEmpty()) return Optional.empty();
154+
if (entries.size() > 1) log.warn("Retrieved more than one result value, using the first");
155+
156+
return entries.stream().findFirst().map(LoadProfileEntry::getValue);
157+
}
158+
141159
/**
142160
* Build a {@link LoadProfileEntry} of type {@code V}, whereas the underlying {@link Value} does
143161
* not need any additional information.

src/main/java/edu/ie3/datamodel/models/timeseries/repetitive/LoadProfileTimeSeries.java

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import edu.ie3.datamodel.utils.TimeSeriesUtils;
1212
import java.time.ZonedDateTime;
1313
import java.util.*;
14+
import java.util.function.Supplier;
1415
import java.util.stream.Collectors;
1516
import javax.measure.quantity.Energy;
1617
import javax.measure.quantity.Power;
@@ -21,8 +22,8 @@
2122
*/
2223
public class LoadProfileTimeSeries<P extends LoadProfile, V extends LoadValues<P>>
2324
extends RepetitiveTimeSeries<LoadProfileEntry<V>, V, PValue> {
24-
private final P loadProfile;
25-
private final Map<Integer, V> valueMapping;
25+
protected final P loadProfile;
26+
protected final Map<Integer, V> valueMapping;
2627

2728
/**
2829
* The maximum average power consumption per quarter-hour calculated over all seasons and weekday
@@ -76,6 +77,20 @@ public Set<LoadProfileEntry<V>> getEntries() {
7677
return set;
7778
}
7879

80+
/**
81+
* Method to get a supplier for the next power value based on the provided input time. Depending
82+
* on the implementation the supplier will either always return the same value or each time a
83+
* random value. To return one constant value please use {@link #getValue(ZonedDateTime)}.
84+
*
85+
* @param time Queried time.
86+
* @return A supplier for an option on the value at the given time step.
87+
*/
88+
public Supplier<Optional<PValue>> supplyValue(ZonedDateTime time) {
89+
int quarterHour = TimeSeriesUtils.calculateQuarterHourOfDay(time);
90+
LoadValues<P> loadValue = valueMapping.get(quarterHour);
91+
return () -> Optional.ofNullable(loadValue.getValue(time, loadProfile));
92+
}
93+
7994
@Override
8095
public Optional<ZonedDateTime> getPreviousDateTime(ZonedDateTime time) {
8196
return Optional.of(time.minusMinutes(15));
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* © 2025. TU Dortmund University,
3+
* Institute of Energy Systems, Energy Efficiency and Energy Economics,
4+
* Research group Distribution grid planning and operation
5+
*/
6+
package edu.ie3.datamodel.models.timeseries
7+
8+
import edu.ie3.datamodel.io.source.LoadProfileSource
9+
import edu.ie3.util.TimeUtil
10+
import spock.lang.Specification
11+
12+
class RandomLoadProfileTimeSeriesTest extends Specification {
13+
14+
15+
def "A RandomLoadProfileTimeSeries should supply a random value correctly"() {
16+
given:
17+
def lpts = LoadProfileSource.randomLoadProfile.getTimeSeries()
18+
def time = TimeUtil.withDefaults.toZonedDateTime("2020-01-01T00:00:00Z")
19+
20+
def supplier = lpts.supplyValue(time)
21+
22+
when:
23+
def values = [
24+
supplier.get(),
25+
supplier.get(),
26+
supplier.get()
27+
].collect { it.get()}
28+
then:
29+
values.size() > 1
30+
}
31+
}

0 commit comments

Comments
 (0)