Skip to content

Commit

Permalink
Merge pull request #609 from ie3-institute/sp/#607-profile-parse-method
Browse files Browse the repository at this point in the history
Sp/#607 profile parse method
  • Loading branch information
t-ober committed Jun 4, 2022
2 parents 006baa4 + 382d917 commit 7412410
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
*/
package edu.ie3.datamodel.models.profile;

import edu.ie3.datamodel.exceptions.ParsingException;

/**
* German standard electricity load profiles, defined by the bdew (Bundesverband der Energie- und
* Wasserwirtschaft; engl.Federal Association of the Energy and Water Industry). For more details
Expand Down Expand Up @@ -36,8 +38,8 @@ public enum BdewStandardLoadProfile implements StandardLoadProfile {
* @return The corresponding bdew load profile or throw {@link IllegalArgumentException}, if no
* matching load profile can be found
*/
public static BdewStandardLoadProfile get(String key) {
return (BdewStandardLoadProfile) LoadProfile.getProfile(BdewStandardLoadProfile.values(), key);
public static BdewStandardLoadProfile get(String key) throws ParsingException {
return LoadProfile.getProfile(BdewStandardLoadProfile.values(), key);
}

@Override
Expand Down
33 changes: 20 additions & 13 deletions src/main/java/edu/ie3/datamodel/models/profile/LoadProfile.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import java.io.Serializable;
import java.util.Arrays;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public interface LoadProfile extends Serializable {
/** @return The identifying String */
Expand All @@ -25,27 +24,35 @@ public interface LoadProfile extends Serializable {
static LoadProfile parse(String key) throws ParsingException {
if (key == null || key.isEmpty()) return LoadProfile.DefaultLoadProfiles.NO_LOAD_PROFILE;

String filterKey = getUniformKey(key);
return Stream.concat(
Arrays.stream(BdewStandardLoadProfile.values()),
Arrays.stream(NbwTemperatureDependantLoadProfile.values()))
.filter(profile -> profile.getKey().equals(filterKey))
.findFirst()
.orElseThrow(
() ->
new ParsingException("Cannot parse \"" + key + "\" to a valid known load profile"));
return LoadProfile.getProfile(getAllProfiles(), key);
}

static LoadProfile getProfile(LoadProfile[] profiles, String key) {
static LoadProfile[] getAllProfiles() {
final LoadProfile[][] all =
new LoadProfile[][] {
BdewStandardLoadProfile.values(), NbwTemperatureDependantLoadProfile.values()
};

return Arrays.stream(all).flatMap(Arrays::stream).toArray(LoadProfile[]::new);
}

/**
* Looks for load profile with given key and returns it.
*
* @param profiles we search within
* @param key to look for
* @return the matching load profile
*/
static <T extends LoadProfile> T getProfile(T[] profiles, String key) throws ParsingException {
return Arrays.stream(profiles)
.filter(loadProfile -> loadProfile.getKey().equalsIgnoreCase(getUniformKey(key)))
.findFirst()
.orElseThrow(
() ->
new IllegalArgumentException(
new ParsingException(
"No predefined load profile with key '"
+ key
+ "' found. Please provide one of the following keys:"
+ "' found. Please provide one of the following keys: "
+ Arrays.stream(profiles)
.map(LoadProfile::getKey)
.collect(Collectors.joining(", "))));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
*/
package edu.ie3.datamodel.models.profile;

import edu.ie3.datamodel.exceptions.ParsingException;

/** Temperature dependant determined by NBW (accessed 05/2022) */
public enum NbwTemperatureDependantLoadProfile implements TemperatureDependantLoadProfile {
// heat pumps
Expand All @@ -26,9 +28,8 @@ public enum NbwTemperatureDependantLoadProfile implements TemperatureDependantLo
* @return The corresponding nbw load profile or throw {@link IllegalArgumentException}, if no
* matching load profile can be found
*/
public static NbwTemperatureDependantLoadProfile get(String key) {
return (NbwTemperatureDependantLoadProfile)
LoadProfile.getProfile(NbwTemperatureDependantLoadProfile.values(), key);
public static NbwTemperatureDependantLoadProfile get(String key) throws ParsingException {
return LoadProfile.getProfile(NbwTemperatureDependantLoadProfile.values(), key);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*/
package edu.ie3.datamodel.models.profile;

import edu.ie3.datamodel.exceptions.ParsingException;
import edu.ie3.datamodel.models.timeseries.individual.IndividualTimeSeries;
import edu.ie3.datamodel.models.timeseries.repetitive.RepetitiveTimeSeries;

Expand All @@ -16,4 +17,15 @@
* <p>If you intend to provide distinct values, create either an {@link IndividualTimeSeries} or
* {@link RepetitiveTimeSeries} and assign it to the model via mapping to the model.
*/
public interface StandardLoadProfile extends LoadProfile {}
public interface StandardLoadProfile extends LoadProfile {

/**
* Returns standard load profile corresponding to the given key.
*
* @param key to look for
* @return the matching standard load profile
*/
static StandardLoadProfile parse(String key) throws ParsingException {
return LoadProfile.getProfile(BdewStandardLoadProfile.values(), key);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,22 @@
*/
package edu.ie3.datamodel.models.profile;

import edu.ie3.datamodel.exceptions.ParsingException;

/**
* Temperature dependant load profiles for night storage heating and heat pumps . The profiles rely
* on the VDN description for interruptable loads. For more details see <a
* href="https://www.bdew.de/media/documents/LPuVe-Praxisleitfaden.pdf">here</a>.
*/
public interface TemperatureDependantLoadProfile extends LoadProfile {}
public interface TemperatureDependantLoadProfile extends LoadProfile {

/**
* Returns temperature dependant load profile corresponding to the given key.
*
* @param key to look for
* @return the matching temperature dependant load profile
*/
static TemperatureDependantLoadProfile parse(String key) throws ParsingException {
return LoadProfile.getProfile(NbwTemperatureDependantLoadProfile.values(), key);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,9 @@
* Institute of Energy Systems, Energy Efficiency and Energy Economics,
* Research group Distribution grid planning and operation
*/
package edu.ie3.datamodel.models
package edu.ie3.datamodel.models.profile

import edu.ie3.datamodel.exceptions.ParsingException
import edu.ie3.datamodel.models.profile.BdewStandardLoadProfile
import edu.ie3.datamodel.models.profile.LoadProfile
import edu.ie3.datamodel.models.profile.NbwTemperatureDependantLoadProfile
import spock.lang.Specification

class LoadProfileTest extends Specification {
Expand Down Expand Up @@ -93,6 +90,40 @@ class LoadProfileTest extends Specification {
null || LoadProfile.DefaultLoadProfiles.NO_LOAD_PROFILE
}

def "Standard load profiles can be parsed correctly"() {
when:
StandardLoadProfile actual = StandardLoadProfile.parse(key)

then:
actual == expected

where:
key || expected
"h0" || BdewStandardLoadProfile.H0
"h-0" || BdewStandardLoadProfile.H0
"h_0" || BdewStandardLoadProfile.H0
"H0" || BdewStandardLoadProfile.H0
"H-0" || BdewStandardLoadProfile.H0
"H_0" || BdewStandardLoadProfile.H0
}

def "Tempearture dependent load profiles can be parsed correctly"() {
when:
TemperatureDependantLoadProfile actual = TemperatureDependantLoadProfile.parse(key)

then:
actual == expected

where:
key || expected
"ep1" || NbwTemperatureDependantLoadProfile.EP1
"ep_1" || NbwTemperatureDependantLoadProfile.EP1
"ep_1" || NbwTemperatureDependantLoadProfile.EP1
"ez2" || NbwTemperatureDependantLoadProfile.EZ2
"ez-2" || NbwTemperatureDependantLoadProfile.EZ2
"ez_2" || NbwTemperatureDependantLoadProfile.EZ2
}

def "BDEW load profiles can be gotten by their key"() {
when:
BdewStandardLoadProfile actual = BdewStandardLoadProfile.get(key)
Expand All @@ -113,6 +144,23 @@ class LoadProfileTest extends Specification {
"g_1" || BdewStandardLoadProfile.G1
}

def "Nbw temperature dependant load profiles can be parsed correctly"() {
when:
NbwTemperatureDependantLoadProfile actual = NbwTemperatureDependantLoadProfile.get(key)

then:
actual == expected

where:
key || expected
"ep1" || NbwTemperatureDependantLoadProfile.EP1
"ep_1" || NbwTemperatureDependantLoadProfile.EP1
"ep_1" || NbwTemperatureDependantLoadProfile.EP1
"ez2" || NbwTemperatureDependantLoadProfile.EZ2
"ez-2" || NbwTemperatureDependantLoadProfile.EZ2
"ez_2" || NbwTemperatureDependantLoadProfile.EZ2
}

def "Nbw temperature dependant load profiles can be gotten by their key"() {
when:
NbwTemperatureDependantLoadProfile actual = NbwTemperatureDependantLoadProfile.get(key)
Expand All @@ -136,6 +184,6 @@ class LoadProfileTest extends Specification {

then:
def e = thrown(ParsingException)
e.message == "Cannot parse \"not_a_key\" to a valid known load profile"
e.message == "No predefined load profile with key 'not_a_key' found. Please provide one of the following keys: h0, l0, l1, l2, g0, g1, g2, g3, g4, g5, g6, ep1, ez2"
}
}

0 comments on commit 7412410

Please sign in to comment.