Skip to content

Commit 3833a48

Browse files
Merge pull request #132 from ie3-institute/sp/#131-adapt-scheduler
Adapting to new scheduling protocol
2 parents 3dba4fc + e64d076 commit 3833a48

File tree

8 files changed

+73
-53
lines changed

8 files changed

+73
-53
lines changed

CHANGELOG.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
88

99
### Changed
1010
- Updating to gradle 8.4 [#133](https://github.com/ie3-institute/simonaAPI/issues/133)
11+
- Adapted to changed SIMONA scheduler protocol [#131](https://github.com/ie3-institute/simonaAPI/issues/131)
1112

1213
## [0.2.0] - 2023-08-01
1314

@@ -17,8 +18,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1718
- JavaDoc for many message classes [#59](https://github.com/ie3-institute/simonaAPI/issues/59)
1819
- Implemented `ExtEvDataTest` as part of [#77](https://github.com/ie3-institute/simonaAPI/issues/77)
1920

20-
### Fixed
21-
2221
### Changed
2322
- Refactored `ExtTrigger` -> `ExtSimMessage` and `ExtTriggerResponse` -> `ExtSimMessageResponse` [#5](https://github.com/ie3-institute/simonaAPI/issues/5)
2423
- Renamed messages to ease understanding [#62](https://github.com/ie3-institute/simonaAPI/issues/62)

src/main/java/edu/ie3/simona/api/simulation/ExtSimulation.java

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import edu.ie3.simona.api.simulation.ontology.*;
1313
import java.util.ArrayList;
1414
import java.util.List;
15+
import java.util.Optional;
1516

1617
/**
1718
* Every external simulation must extend this class in order to get triggered by the main
@@ -48,24 +49,24 @@ private boolean takeAndHandleMessage() throws InterruptedException {
4849
// take() will block until an object is ready for us
4950
final ControlMessageToExt msg = data.receiveMessageQueue.take();
5051

51-
if (msg.getClass().equals(ActivityStartTrigger.class)) {
52-
final ActivityStartTrigger activityStartTrigger = (ActivityStartTrigger) msg;
53-
List<Long> newTriggers;
52+
if (msg.getClass().equals(ActivationMessage.class)) {
53+
final ActivationMessage activationMessage = (ActivationMessage) msg;
54+
Optional<Long> newTrigger;
5455

55-
if (activityStartTrigger.tick() == -1L) {
56-
newTriggers = initialize(); // this is blocking until initialization has finished
56+
if (activationMessage.tick() == -1L) {
57+
newTrigger = initialize(); // this is blocking until initialization has finished
5758
} else {
58-
newTriggers =
59+
newTrigger =
5960
doActivity(
60-
activityStartTrigger
61+
activationMessage
6162
.tick()); // this is blocking until processing of this tick has finished
6263
}
63-
data.send(new CompletionMessage(newTriggers));
64+
data.send(new CompletionMessage(newTrigger));
6465

65-
return newTriggers.isEmpty();
66-
} else if (msg.getClass().equals(Terminate.class)) {
67-
final Terminate terminateMsg = (Terminate) msg;
68-
terminate(terminateMsg.simulationSuccessful());
66+
return newTrigger.isEmpty();
67+
} else if (msg.getClass().equals(TerminationMessage.class)) {
68+
final TerminationMessage terminationMsg = (TerminationMessage) msg;
69+
terminate(terminationMsg.simulationSuccessful());
6970
data.send(new TerminationCompleted());
7071

7172
return true;
@@ -77,22 +78,23 @@ private boolean takeAndHandleMessage() throws InterruptedException {
7778
/**
7879
* This method is called when the external simulation needs to be initialized
7980
*
80-
* @return a list of future ticks at which this external simulation wants to be triggered.
81+
* @return The first regular tick at which this external simulation wants to be triggered, if
82+
* applicable.
8183
*/
82-
protected abstract List<Long> initialize();
84+
protected abstract Optional<Long> initialize();
8385

8486
/**
8587
* This method is called for every tick of the external simulation that is triggered.
8688
*
8789
* @param tick The current tick
88-
* @return a list of future ticks at which this external simulation wants to be triggered.
90+
* @return The next tick at which this external simulation wants to be triggered, if applicable.
8991
*/
90-
protected abstract List<Long> doActivity(long tick);
92+
protected abstract Optional<Long> doActivity(long tick);
9193

9294
/**
9395
* This method is called when the main simulation wants to terminate.
9496
*
95-
* @param simulationSuccessful whether the simulation was run successfully or has ended with an
97+
* @param simulationSuccessful Whether the simulation was run successfully or has ended with an
9698
* error
9799
*/
98100
protected void terminate(Boolean simulationSuccessful) {

src/main/java/edu/ie3/simona/api/simulation/ontology/ActivityStartTrigger.java renamed to src/main/java/edu/ie3/simona/api/simulation/ontology/ActivationMessage.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,9 @@
66

77
package edu.ie3.simona.api.simulation.ontology;
88

9-
public record ActivityStartTrigger(long tick) implements ControlMessageToExt {}
9+
/**
10+
* Message that the external simulation is activated with by SIMONA
11+
*
12+
* @param tick The current tick
13+
*/
14+
public record ActivationMessage(long tick) implements ControlMessageToExt {}

src/main/java/edu/ie3/simona/api/simulation/ontology/CompletionMessage.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,12 @@
66

77
package edu.ie3.simona.api.simulation.ontology;
88

9-
import java.util.Collection;
9+
import java.util.Optional;
1010

11-
public record CompletionMessage(Collection<Long> newTriggers)
11+
/**
12+
* Message that is returned to SIMONA
13+
*
14+
* @param nextActivation The tick that the external simulation would like to be activated at again
15+
*/
16+
public record CompletionMessage(Optional<Long> nextActivation)
1217
implements ControlResponseMessageFromExt {}

src/main/java/edu/ie3/simona/api/simulation/ontology/Terminate.java

Lines changed: 0 additions & 9 deletions
This file was deleted.

src/main/java/edu/ie3/simona/api/simulation/ontology/TerminationCompleted.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@
66

77
package edu.ie3.simona.api.simulation.ontology;
88

9+
/** Message returned to SIMONA indicating that the external simulation terminated */
910
public record TerminationCompleted() implements ControlResponseMessageFromExt {}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/*
2+
* © 2022. TU Dortmund University,
3+
* Institute of Energy Systems, Energy Efficiency and Energy Economics,
4+
* Research group Distribution grid planning and operation
5+
*/
6+
7+
package edu.ie3.simona.api.simulation.ontology;
8+
9+
/**
10+
* Message that is sent once SIMONA is terminating, indicating that the external simulation should
11+
* shut down as well
12+
*
13+
* @param simulationSuccessful Whether SIMONA terminated successfully
14+
*/
15+
public record TerminationMessage(Boolean simulationSuccessful) implements ControlMessageToExt {}

src/test/groovy/edu/ie3/simona/api/simulation/ExtSimulationSpec.groovy

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ import akka.actor.ActorSystem
44
import akka.testkit.TestProbe
55
import akka.testkit.javadsl.TestKit
66
import edu.ie3.simona.api.data.ExtData
7-
import edu.ie3.simona.api.simulation.ontology.ActivityStartTrigger
7+
import edu.ie3.simona.api.simulation.ontology.ActivationMessage
88
import edu.ie3.simona.api.simulation.ontology.CompletionMessage
99
import edu.ie3.simona.api.simulation.ontology.ControlMessageToExt
10-
import edu.ie3.simona.api.simulation.ontology.Terminate
10+
import edu.ie3.simona.api.simulation.ontology.TerminationMessage
1111
import edu.ie3.simona.api.simulation.ontology.TerminationCompleted
1212
import spock.lang.Shared
1313
import spock.lang.Specification
@@ -28,22 +28,22 @@ class ExtSimulationSpec extends Specification {
2828
*/
2929
private class TestSimulation extends ExtSimulation {
3030

31-
private List<Long> initReturnTicks
32-
private List<Long> activityReturnTicks
31+
private Optional<Long> initReturnTicks
32+
private Optional<Long> activationReturnTick
3333

34-
TestSimulation(List<Long> initReturnTicks, List<Long> activityReturnTicks) {
35-
this.initReturnTicks = initReturnTicks
36-
this.activityReturnTicks = activityReturnTicks
34+
TestSimulation(Optional<Long> initReturnTick, Optional<Long> activationReturnTick) {
35+
this.initReturnTicks = initReturnTick
36+
this.activationReturnTick = activationReturnTick
3737
}
3838

3939
@Override
40-
protected List<Long> initialize() {
40+
protected Optional<Long> initialize() {
4141
return this.initReturnTicks
4242
}
4343

4444
@Override
45-
protected List<Long> doActivity(long tick) {
46-
return this.activityReturnTicks
45+
protected Optional<Long> doActivity(long tick) {
46+
return this.activationReturnTick
4747
}
4848
}
4949

@@ -63,39 +63,41 @@ class ExtSimulationSpec extends Specification {
6363
def "An ExtSimulation should handle initialization"() {
6464
given:
6565
def tick = -1L
66-
def newTicks = [0L]
66+
def newTick = Optional.of(0L)
6767
def testProbe = new TestProbe(actorSystem)
6868
def extSimData = new ExtSimAdapterData(testProbe.ref(), new String[0])
69-
def extSim = new TestSimulation(newTicks, [-2L])
69+
def extSim = new TestSimulation(newTick, Optional.of(-2L))
7070
extSim.setup(extSimData, new ArrayList<ExtData>())
7171

7272
when:
73-
extSimData.queueExtMsg(new ActivityStartTrigger(tick))
73+
extSimData.queueExtMsg(new ActivationMessage(tick))
7474
def finishedActual = handleMessage.invoke(extSim)
7575

7676
then:
7777
finishedActual == false
78-
testProbe.expectMsg(new CompletionMessage(newTicks))
78+
testProbe.expectMsg(new CompletionMessage(newTick))
7979
}
8080

8181
def "An ExtSimulation should handle activation and return given new triggers"() {
8282
given:
8383
def testProbe = new TestProbe(actorSystem)
8484
def extSimData = new ExtSimAdapterData(testProbe.ref(), new String[0])
85-
def extSim = new TestSimulation([-2L], newTicks)
85+
def newTickOpt = newTick.isEmpty() ?
86+
Optional.<Long>empty() : Optional.of(newTick.first())
87+
def extSim = new TestSimulation(Optional.of(-2L), newTickOpt)
8688
extSim.setup(extSimData, new ArrayList<ExtData>())
8789

8890
when:
89-
extSimData.queueExtMsg(new ActivityStartTrigger(tick))
91+
extSimData.queueExtMsg(new ActivationMessage(tick))
9092
def finishedActual = handleMessage.invoke(extSim)
9193

9294
then:
9395
finishedActual == finished
94-
testProbe.expectMsg(new CompletionMessage(newTicks))
96+
testProbe.expectMsg(new CompletionMessage(newTickOpt))
9597

9698
where:
97-
tick | newTicks || finished
98-
0L | [900L, 1800L] || false
99+
tick | newTick || finished
100+
0L | [900L] || false
99101
3600L | [7200L] || false
100102
7200L | [] || true
101103
10800L | [] || true
@@ -105,11 +107,11 @@ class ExtSimulationSpec extends Specification {
105107
given:
106108
def testProbe = new TestProbe(actorSystem)
107109
def extSimData = new ExtSimAdapterData(testProbe.ref(), new String[0])
108-
def extSim = new TestSimulation([], [])
110+
def extSim = new TestSimulation(Optional.empty(), Optional.empty())
109111
extSim.setup(extSimData, new ArrayList<ExtData>())
110112

111113
when:
112-
extSimData.queueExtMsg(new Terminate(simlulationSuccessful))
114+
extSimData.queueExtMsg(new TerminationMessage(simlulationSuccessful))
113115
def finishedActual = handleMessage.invoke(extSim)
114116

115117
then:
@@ -128,7 +130,7 @@ class ExtSimulationSpec extends Specification {
128130
given:
129131
def testProbe = new TestProbe(actorSystem)
130132
def extSimData = new ExtSimAdapterData(testProbe.ref(), new String[0])
131-
def extSim = new TestSimulation([], [])
133+
def extSim = new TestSimulation(Optional.empty(), Optional.empty())
132134
extSim.setup(extSimData, new ArrayList<ExtData>())
133135

134136
when:

0 commit comments

Comments
 (0)