Skip to content

Commit 3a7b213

Browse files
committed
Adding additional methods to ExtendedFlexOptionsResult.
1 parent 4ab44fd commit 3a7b213

File tree

2 files changed

+128
-0
lines changed

2 files changed

+128
-0
lines changed

src/main/java/edu/ie3/simona/api/data/model/em/ExtendedFlexOptionsResult.java

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import java.time.ZonedDateTime;
1111
import java.util.*;
1212
import javax.measure.quantity.Power;
13+
import org.slf4j.Logger;
1314
import tech.units.indriya.ComparableQuantity;
1415

1516
/**
@@ -69,6 +70,19 @@ public ExtendedFlexOptionsResult(
6970
this.disaggregated = disaggregated;
7071
}
7172

73+
/**
74+
* Method for adding disaggregated flex option results to this object.
75+
*
76+
* <p>Note: This method does not check, if the disaggregated flex options match the total flex
77+
* options. To do this, please use the method {@link #checkFlexOptions(Logger)}.
78+
*
79+
* @param uuid of the inferior model
80+
* @param flexOptionsResult the flex options of the inferior model
81+
*/
82+
public void addDisaggregated(UUID uuid, FlexOptionsResult flexOptionsResult) {
83+
this.disaggregated.put(uuid, flexOptionsResult);
84+
}
85+
7286
/** Returns the uuid of the sender ({@link #getInputModel()}) of the results. */
7387
public UUID getSender() {
7488
return getInputModel();
@@ -94,6 +108,69 @@ public Map<UUID, FlexOptionsResult> getDisaggregated() {
94108
return Collections.unmodifiableMap(disaggregated);
95109
}
96110

111+
/**
112+
* Method for checking if the disaggregated flex options match the total flex options.
113+
*
114+
* @param log used for logging
115+
* @return {@code true} if the flex options match, else {@code false}
116+
*/
117+
public boolean checkFlexOptions(Logger log) {
118+
List<ComparableQuantity<Power>> refs = new ArrayList<>();
119+
List<ComparableQuantity<Power>> mins = new ArrayList<>();
120+
List<ComparableQuantity<Power>> maxs = new ArrayList<>();
121+
122+
disaggregated.forEach(
123+
(uuid, flexOptionsResult) -> {
124+
refs.add(flexOptionsResult.getpRef());
125+
mins.add(flexOptionsResult.getpMin());
126+
maxs.add(flexOptionsResult.getpMax());
127+
});
128+
129+
ComparableQuantity<Power> ref = getpRef();
130+
ComparableQuantity<Power> min = getpMin();
131+
ComparableQuantity<Power> max = getpMax();
132+
133+
Optional<ComparableQuantity<Power>> refSum = refs.stream().reduce(ComparableQuantity::add);
134+
Optional<ComparableQuantity<Power>> minSum = mins.stream().reduce(ComparableQuantity::add);
135+
Optional<ComparableQuantity<Power>> maxSum = maxs.stream().reduce(ComparableQuantity::add);
136+
137+
boolean isRefValid = false;
138+
boolean isMinValid = false;
139+
boolean isMaxValid = false;
140+
141+
if (refSum.isPresent()) {
142+
isRefValid = refSum.get().isEquivalentTo(ref);
143+
144+
if (!isRefValid) {
145+
log.warn("Disaggregated reference power does not match total reference power.");
146+
}
147+
} else {
148+
log.warn("Cannot check disaggregated reference power.");
149+
}
150+
151+
if (minSum.isPresent()) {
152+
isMinValid = minSum.get().isEquivalentTo(min);
153+
154+
if (!isMinValid) {
155+
log.warn("Disaggregated minimum power does not match total minimum power.");
156+
}
157+
} else {
158+
log.warn("Cannot check disaggregated minimum power.");
159+
}
160+
161+
if (maxSum.isPresent()) {
162+
isMaxValid = maxSum.get().isEquivalentTo(max);
163+
164+
if (!isMaxValid) {
165+
log.warn("Disaggregated maximum power does not match total maximum power.");
166+
}
167+
} else {
168+
log.warn("Cannot check disaggregated maximum power.");
169+
}
170+
171+
return isRefValid && isMinValid && isMaxValid;
172+
}
173+
97174
@Override
98175
public boolean equals(Object o) {
99176
if (this == o) return true;

src/test/groovy/edu/ie3/simona/api/data/model/em/ExtendedFlexOptionsResultTest.groovy

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package edu.ie3.simona.api.data.model.em
22

33
import edu.ie3.datamodel.models.result.system.FlexOptionsResult
44
import edu.ie3.util.quantities.PowerSystemUnits
5+
import org.slf4j.Logger
6+
import org.slf4j.LoggerFactory
57
import spock.lang.Shared
68
import spock.lang.Specification
79
import tech.units.indriya.ComparableQuantity
@@ -12,6 +14,9 @@ import java.time.ZonedDateTime
1214

1315
class ExtendedFlexOptionsResultTest extends Specification {
1416

17+
@Shared
18+
Logger logger = LoggerFactory.getLogger(ExtendedFlexOptionsResultTest)
19+
1520
@Shared
1621
ZonedDateTime time = ZonedDateTime.now()
1722

@@ -86,4 +91,50 @@ class ExtendedFlexOptionsResultTest extends Specification {
8691
] | true
8792
}
8893

94+
def "The ExtendedFlexOptionsResult should add disaggregated flex options correctly"() {
95+
given:
96+
def result = new ExtendedFlexOptionsResult(time, senderUuid, receiverUuid, pRef, pMin, pMax)
97+
def inferiorUuid1 = UUID.fromString("a246eee3-405c-4af1-9ad2-69ecad2bfb65")
98+
def inferiorUuid2 = UUID.fromString("78676121-f154-4f70-ad50-4384ddf8deed")
99+
100+
def inferiorOptions1 = new FlexOptionsResult(time, inferiorUuid1, pRef, pMin, pMax)
101+
def inferiorOptions2 = new FlexOptionsResult(time, inferiorUuid2, pMin, pMin, pMin)
102+
103+
when:
104+
result.addDisaggregated(inferiorUuid1, inferiorOptions1)
105+
result.addDisaggregated(inferiorUuid2, inferiorOptions2)
106+
107+
then:
108+
result.hasDisaggregated()
109+
result.disaggregated.size() == 2
110+
result.disaggregated.get(inferiorUuid1) == inferiorOptions1
111+
result.disaggregated.get(inferiorUuid2) == inferiorOptions2
112+
}
113+
114+
def "The ExtendedFlexOptionsResult should check if disaggregated flex options match total flex options correctly"() {
115+
given:
116+
def result = new ExtendedFlexOptionsResult(time, senderUuid, receiverUuid, pRef, pMin, pMax)
117+
def inferiorUuid1 = UUID.fromString("a246eee3-405c-4af1-9ad2-69ecad2bfb65")
118+
def inferiorUuid2 = UUID.fromString("78676121-f154-4f70-ad50-4384ddf8deed")
119+
def inferiorUuid3 = UUID.randomUUID()
120+
121+
def inferiorOptions1 = new FlexOptionsResult(time, inferiorUuid1, pRef, pMin, pMin)
122+
def inferiorOptions2 = new FlexOptionsResult(time, inferiorUuid2, pMin, pMin, pMax)
123+
def inferiorOptions3 = new FlexOptionsResult(time, inferiorUuid3, pRef, pRef, pMax)
124+
125+
when:
126+
result.addDisaggregated(inferiorUuid1, inferiorOptions1)
127+
boolean withOnly1 = result.checkFlexOptions(logger)
128+
129+
result.addDisaggregated(inferiorUuid2, inferiorOptions2)
130+
boolean with1And2 = result.checkFlexOptions(logger)
131+
132+
result.addDisaggregated(inferiorUuid3, inferiorOptions3)
133+
boolean with1And2And3 = result.checkFlexOptions(logger)
134+
135+
then:
136+
!withOnly1
137+
with1And2
138+
!with1And2And3
139+
}
89140
}

0 commit comments

Comments
 (0)