Skip to content

Commit 9a2f537

Browse files
authored
Merge pull request #271 from ie3-institute/ms/#267-refactoring-data-connections
Refactoring data connections and container.
2 parents db9afe6 + 7acd136 commit 9a2f537

File tree

56 files changed

+1868
-1065
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

56 files changed

+1868
-1065
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
99
### Changed
1010
- Removed Jenkinsfile [#290](https://github.com/ie3-institute/simonaAPI/issues/290)
1111
- Adapted dependabot workflow and added CODEOWNERS [#294](https://github.com/ie3-institute/simonaAPI/issues/294)
12+
- Refactoring external data connection [#267](https://github.com/ie3-institute/simonaAPI/issues/267)
13+
- Refactoring data containers [#268](https://github.com/ie3-institute/simonaAPI/issues/268)
1214

1315
## [0.9.0] - 2025-05-09
1416

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,40 @@
11
# Data connections
2+
3+
This API defines some data connections in order to exchange data between SIMONA and an external simulation.
4+
5+
1. Input data connections
6+
2. Output data connections
7+
3. Bidirectional data connections
8+
9+
## Input data connections
10+
11+
These data connections are used to provide SIMONA with external data. Each input data connection contains two references
12+
for SIMONA actors. The first reference specifies the SIMONA service that will receive the external data. The second
13+
reference is for the external simulation adapter. This adapter receives a message to schedule the data service in SIMONA.
14+
15+
The process of sending data to the service and requesting scheduling is handled by the `sendExtMsg` method.
16+
To send a message, simply call the method with the message as input.
17+
18+
Currently, the following input data connections are provided:
19+
- ExtPrimaryDataConnection
20+
21+
22+
## Output data connections
23+
24+
These data connections are used to provide SIMONA response messages to the external simulation. Each output data connection
25+
has a queue for messages sent by SIMONA. The result data connection itself cannot send messages to SIMONA.
26+
27+
Currently, the following input data connections are provided:
28+
- ExtResultListener
29+
30+
31+
## Bidirectional data connections
32+
33+
The bidirectional data connection combines the functionality of both input and output data connections. These data connections
34+
can be used to send data to SIMONA and receive responses. One additional feature is that bidirectional data connections
35+
can be used to request responses, e.g. SIMONA results.
36+
37+
Currently, the following input data connections are provided:
38+
- ExtEmDataConnection
39+
- ExtEvDataConnection
40+
- ExtResultDataConnection

docs/readthedocs/simulations/externalsimulation.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,20 @@ method should provide a new tick as long as there are future activities.
3232

3333
The method `getDataConnections` returns all [data connections](/connections/connections) that are used to connect the
3434
external simulation to SIMONA.
35+
36+
37+
## Communication between SIMONA and an external simulation
38+
39+
Depending on the connections used by the external simulation, the following communication structures are used:
40+
41+
- Input connections: external simulation -> simonaAPI -> SIMONA
42+
- Output connections: SIMONA -> simonaAPI -> external simulation
43+
44+
Bidirectional connections can use both structures.
45+
46+
47+
## Co-simulations
48+
49+
This API provides an extension to the class `edu.ie3.simona.api.simulation.ExtSimulation` in the form of
50+
`edu.ie3.simona.api.simulation.ExtCoSimulation`. This class contains some methods to simplify the definition of external
51+
co-simulations.

src/main/java/edu/ie3/simona/api/data/DataQueueExtSimulationExtSimulator.java

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

src/main/java/edu/ie3/simona/api/data/ExtDataContainer.java

Lines changed: 0 additions & 10 deletions
This file was deleted.
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
/*
2+
* © 2024. 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.data;
8+
9+
import edu.ie3.simona.api.data.container.ExtDataContainer;
10+
import java.util.Optional;
11+
import java.util.concurrent.LinkedBlockingDeque;
12+
import java.util.concurrent.TimeUnit;
13+
import java.util.function.Function;
14+
15+
/** Data queue to allow data flow between SimonaAPI and an external simulation */
16+
public final class ExtDataContainerQueue<V extends ExtDataContainer> {
17+
private final LinkedBlockingDeque<V> receiverTriggerDeque = new LinkedBlockingDeque<>();
18+
19+
/** Returns the number of elements in this queue. */
20+
public int size() {
21+
return receiverTriggerDeque.size();
22+
}
23+
24+
/**
25+
* Method for adding an {@link ExtDataContainer} to the queue.
26+
*
27+
* @param data to be added
28+
* @throws InterruptedException if the thread running this has been interrupted during the
29+
* blocking operation
30+
*/
31+
public void queueData(V data) throws InterruptedException {
32+
receiverTriggerDeque.putLast(data);
33+
}
34+
35+
/**
36+
* Method to retrieve and remove an {@link ExtDataContainer} from the queue. This method waits
37+
* (blocks) until data is added to the queue.
38+
*
39+
* @return a data container
40+
* @throws InterruptedException if the thread running this has been interrupted during the
41+
* blocking operation
42+
*/
43+
public V takeContainer() throws InterruptedException {
44+
return receiverTriggerDeque.takeFirst();
45+
}
46+
47+
/**
48+
* Method to retrieve and remove an {@link ExtDataContainer} from the queue. This method waits
49+
* (blocks) until either data is added to the queue or the specified wait time is reached.
50+
*
51+
* @param timeout maximal time to wait for data
52+
* @param unit unit of the timeout
53+
* @return an option for a data container
54+
* @throws InterruptedException if the thread running this has been interrupted during the
55+
* blocking operation
56+
*/
57+
public Optional<V> pollContainer(long timeout, TimeUnit unit) throws InterruptedException {
58+
return Optional.ofNullable(receiverTriggerDeque.pollFirst(timeout, unit));
59+
}
60+
61+
/**
62+
* Method to retrieve only a part of a container from the queue. This method waits (blocks) until
63+
* data is added to the queue.
64+
*
65+
* @param extractor function to extract a part of the container
66+
* @return the extracted part
67+
* @param <R> type of returned value
68+
* @throws InterruptedException if the thread running this has been interrupted during the
69+
* blocking operation
70+
*/
71+
public <R> R takeData(Function<V, R> extractor) throws InterruptedException {
72+
// removes the first container from the queue
73+
V data = receiverTriggerDeque.takeFirst();
74+
R result = extractor.apply(data);
75+
76+
// if the container is not empty, it should remain in the queue.
77+
// else the container needs to be removed
78+
if (!data.isEmpty()) {
79+
receiverTriggerDeque.putFirst(data);
80+
}
81+
82+
return result;
83+
}
84+
85+
/**
86+
* Method to retrieve only a part of a container from the queue. This method waits (blocks) until
87+
* either data is added to the queue or the specified wait time is reached.
88+
*
89+
* @param extractor function to extract a part of the container
90+
* @param timeout maximal time to wait for data
91+
* @param unit unit of the timeout
92+
* @return an option for the extracted part
93+
* @param <R> type of returned value
94+
* @throws InterruptedException if the thread running this has been interrupted during the
95+
* blocking operation
96+
*/
97+
public <R> Optional<R> pollData(Function<V, R> extractor, long timeout, TimeUnit unit)
98+
throws InterruptedException {
99+
// removes the first container from the queue
100+
Optional<V> containerOption = pollContainer(timeout, unit);
101+
102+
if (containerOption.isPresent()) {
103+
V data = containerOption.get();
104+
R result = extractor.apply(data);
105+
106+
// if the container is not empty, it should remain in the queue.
107+
// else the container needs to be removed
108+
if (!data.isEmpty()) {
109+
receiverTriggerDeque.putFirst(data);
110+
}
111+
112+
return Optional.of(result);
113+
}
114+
115+
return Optional.empty();
116+
}
117+
}

src/main/java/edu/ie3/simona/api/data/ExtInputDataConnection.java

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

src/main/java/edu/ie3/simona/api/data/ExtInputDataContainer.java

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

src/main/java/edu/ie3/simona/api/data/ExtOutputDataConnection.java

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

0 commit comments

Comments
 (0)