-
Notifications
You must be signed in to change notification settings - Fork 0
Enable external primary data and results #157
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
311a260
Temporary
jo-bao 4225fc6
Implement ExtPrimaryDataService
jo-bao 8424b17
Implement External Data for Results and PrimaryData
jo-bao f48b37a
Merge branch 'dev' into jb/#145-externalprimarydata
jo-bao fe915e4
changelog
jo-bao 485ff5c
Merge remote-tracking branch 'origin/jb/#145-externalprimarydata' int…
jo-bao ae20073
test
jo-bao 21307c7
testing: output passed tests
jo-bao a3a75e6
Fixing jacoco?
jo-bao 2d92537
Merge branch 'dev' into jb/#145-externalprimarydata
jo-bao b01d74c
in development
jo-bao ec630b6
spotless
jo-bao b482800
spotless
jo-bao 24040d8
getFactory in ResultDataFactory
jo-bao c2c59c1
spotless
jo-bao 9132367
Merge branch 'dev' into jb/#145-externalprimarydata
sebastian-peter dd9b0ee
Merge branch 'dev' into jb/#145-externalprimarydata
jo-bao d8ce50b
changes after review
jo-bao e61f1c9
Exception PrimaryData
jo-bao d02e8cf
Fixed PrimaryDataFactory in test
jo-bao 00f335c
Codacy
jo-bao 9ecf2c0
changes after review
jo-bao 3791671
codacy
jo-bao 9f707fa
some renaming
jo-bao 98af7d9
changes after review
jo-bao 05928fc
code beauty
jo-bao File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
src/main/java/edu/ie3/simona/api/data/primarydata/ExtPrimaryData.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/* | ||
* © 2024. TU Dortmund University, | ||
* Institute of Energy Systems, Energy Efficiency and Energy Economics, | ||
* Research group Distribution grid planning and operation | ||
*/ | ||
|
||
package edu.ie3.simona.api.data.primarydata; | ||
|
||
import edu.ie3.datamodel.models.value.Value; | ||
import edu.ie3.simona.api.data.ExtData; | ||
import edu.ie3.simona.api.data.ontology.ScheduleDataServiceMessage; | ||
import edu.ie3.simona.api.data.primarydata.ontology.PrimaryDataMessageFromExt; | ||
import edu.ie3.simona.api.data.primarydata.ontology.ProvidePrimaryData; | ||
import edu.ie3.simona.api.exceptions.ConvertionException; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.UUID; | ||
import org.apache.pekko.actor.ActorRef; | ||
|
||
public class ExtPrimaryData implements ExtData { | ||
|
||
/** Actor reference to service that handles ev data within SIMONA */ | ||
private final ActorRef dataService; | ||
|
||
/** Actor reference to adapter that handles scheduler control flow in SIMONA */ | ||
private final ActorRef extSimAdapter; | ||
|
||
/** Factory to convert an external object to PSDM primary data */ | ||
private final PrimaryDataFactory factory; | ||
|
||
public ExtPrimaryData(ActorRef dataService, ActorRef extSimAdapter, PrimaryDataFactory factory) { | ||
this.dataService = dataService; | ||
this.extSimAdapter = extSimAdapter; | ||
this.factory = factory; | ||
} | ||
|
||
/** Provide primary data from an external simulation in one tick. */ | ||
public void providePrimaryData(Long tick, Map<String, Object> primaryData) { | ||
Map<UUID, Value> convertedMap = new HashMap<>(); | ||
primaryData.forEach( | ||
(k, v) -> { | ||
try { | ||
convertedMap.put(UUID.fromString(k), factory.convert(v)); | ||
} catch (ConvertionException e) { | ||
throw new RuntimeException(e); | ||
} | ||
}); | ||
sendExtMsg(new ProvidePrimaryData(tick, convertedMap)); | ||
} | ||
|
||
/** | ||
* Send information from the external simulation to SIMONA's external primary data service. | ||
* Furthermore, ExtSimAdapter within SIMONA is instructed to activate the ev data service with the | ||
* current tick. | ||
* | ||
* @param msg the data/information that is sent to SIMONA's external primary data service | ||
*/ | ||
public void sendExtMsg(PrimaryDataMessageFromExt msg) { | ||
dataService.tell(msg, ActorRef.noSender()); | ||
// we need to schedule data receiver activation with scheduler | ||
extSimAdapter.tell(new ScheduleDataServiceMessage(dataService), ActorRef.noSender()); | ||
} | ||
jo-bao marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} |
22 changes: 22 additions & 0 deletions
22
src/main/java/edu/ie3/simona/api/data/primarydata/ExtPrimaryDataSimulation.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
/* | ||
* © 2024. TU Dortmund University, | ||
* Institute of Energy Systems, Energy Efficiency and Energy Economics, | ||
* Research group Distribution grid planning and operation | ||
*/ | ||
|
||
package edu.ie3.simona.api.data.primarydata; | ||
|
||
import edu.ie3.simona.api.data.ExtDataSimulation; | ||
|
||
/** | ||
* An external simulation that provides primary data should implement this interface and handle the | ||
* ExtPrimaryData that is handed over. | ||
*/ | ||
public interface ExtPrimaryDataSimulation extends ExtDataSimulation { | ||
|
||
/** Hand over an ExtPrimaryData which enables communication regarding primary data. */ | ||
void setExtPrimaryData(ExtPrimaryData extPrimaryData); | ||
|
||
/** Should implement the convertion of the external format to the PSDM format of primary data. */ | ||
PrimaryDataFactory getPrimaryDataFactory(); | ||
} |
17 changes: 17 additions & 0 deletions
17
src/main/java/edu/ie3/simona/api/data/primarydata/PrimaryDataFactory.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
/* | ||
* © 2024. TU Dortmund University, | ||
* Institute of Energy Systems, Energy Efficiency and Energy Economics, | ||
* Research group Distribution grid planning and operation | ||
*/ | ||
|
||
package edu.ie3.simona.api.data.primarydata; | ||
|
||
import edu.ie3.datamodel.models.value.Value; | ||
import edu.ie3.simona.api.exceptions.ConvertionException; | ||
|
||
/** Interface that should be implemented by an external simulation. */ | ||
public interface PrimaryDataFactory { | ||
|
||
/** Should convert an object to a primary data value with a check if the object is primary data */ | ||
Value convert(Object entity) throws ConvertionException; | ||
} |
12 changes: 12 additions & 0 deletions
12
src/main/java/edu/ie3/simona/api/data/primarydata/ontology/PrimaryDataMessageFromExt.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
/* | ||
* © 2024. TU Dortmund University, | ||
* Institute of Energy Systems, Energy Efficiency and Energy Economics, | ||
* Research group Distribution grid planning and operation | ||
*/ | ||
|
||
package edu.ie3.simona.api.data.primarydata.ontology; | ||
|
||
import edu.ie3.simona.api.data.ontology.DataMessageFromExt; | ||
|
||
/** Messages that are sent from an external primary data simulation to SIMONA */ | ||
public interface PrimaryDataMessageFromExt extends DataMessageFromExt {} |
15 changes: 15 additions & 0 deletions
15
src/main/java/edu/ie3/simona/api/data/primarydata/ontology/ProvidePrimaryData.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
/* | ||
* © 2024. TU Dortmund University, | ||
* Institute of Energy Systems, Energy Efficiency and Energy Economics, | ||
* Research group Distribution grid planning and operation | ||
*/ | ||
|
||
package edu.ie3.simona.api.data.primarydata.ontology; | ||
|
||
import edu.ie3.datamodel.models.value.Value; | ||
import java.util.Map; | ||
import java.util.UUID; | ||
|
||
/** Message that provides primary data from an external primary data simulation */ | ||
public record ProvidePrimaryData(long tick, Map<UUID, Value> primaryData) | ||
implements PrimaryDataMessageFromExt {} |
113 changes: 113 additions & 0 deletions
113
src/main/java/edu/ie3/simona/api/data/results/ExtResultData.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
/* | ||
* © 2024. TU Dortmund University, | ||
* Institute of Energy Systems, Energy Efficiency and Energy Economics, | ||
* Research group Distribution grid planning and operation | ||
*/ | ||
|
||
package edu.ie3.simona.api.data.results; | ||
|
||
import edu.ie3.datamodel.models.result.ResultEntity; | ||
import edu.ie3.simona.api.data.ExtData; | ||
import edu.ie3.simona.api.data.ontology.ScheduleDataServiceMessage; | ||
import edu.ie3.simona.api.data.results.ontology.ProvideResultEntities; | ||
import edu.ie3.simona.api.data.results.ontology.RequestResultEntities; | ||
import edu.ie3.simona.api.data.results.ontology.ResultDataMessageFromExt; | ||
import edu.ie3.simona.api.data.results.ontology.ResultDataResponseMessageToExt; | ||
import edu.ie3.simona.api.exceptions.ConvertionException; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.concurrent.LinkedBlockingQueue; | ||
import org.apache.pekko.actor.ActorRef; | ||
|
||
public class ExtResultData implements ExtData { | ||
|
||
/** Data message queue containing messages from SIMONA */ | ||
public final LinkedBlockingQueue<ResultDataResponseMessageToExt> receiveTriggerQueue = | ||
new LinkedBlockingQueue<>(); | ||
|
||
/** Actor reference to service that handles ev data within SIMONA */ | ||
private final ActorRef dataService; | ||
|
||
/** Actor reference to adapter that handles scheduler control flow in SIMONA */ | ||
private final ActorRef extSimAdapter; | ||
|
||
private final ResultDataFactory factory; | ||
|
||
public ExtResultData(ActorRef dataService, ActorRef extSimAdapter, ResultDataFactory factory) { | ||
this.dataService = dataService; | ||
this.extSimAdapter = extSimAdapter; | ||
this.factory = factory; | ||
} | ||
|
||
/** Method that an external simulation can request results from SIMONA as a list. */ | ||
public List<ResultEntity> requestResults() throws InterruptedException { | ||
sendExtMsg(new RequestResultEntities()); | ||
return receiveWithType(ProvideResultEntities.class).results(); | ||
} | ||
|
||
/** | ||
* Method that an external simulation can request results from SIMONA as a map string to object. | ||
*/ | ||
public Map<String, Object> requestResultObjects() | ||
throws ConvertionException, InterruptedException { | ||
return convertResultsList(requestResults()); | ||
} | ||
|
||
protected Map<String, Object> convertResultsList(List<ResultEntity> results) | ||
throws ConvertionException { | ||
Map<String, Object> resultsMap = new HashMap<>(); | ||
Object convertedResult; | ||
for (ResultEntity res : results) { | ||
convertedResult = factory.convert(res); | ||
resultsMap.put(res.getUuid().toString(), convertedResult); | ||
} | ||
return resultsMap; | ||
} | ||
|
||
/** | ||
* Send information from the external simulation to SIMONA's external data service. Furthermore, | ||
* ExtSimAdapter within SIMONA is instructed to activate the external data service with the | ||
* current tick. | ||
* | ||
* @param msg the data/information that is sent to SIMONA's result data service | ||
*/ | ||
public void sendExtMsg(ResultDataMessageFromExt msg) { | ||
dataService.tell(msg, ActorRef.noSender()); | ||
// we need to schedule data receiver activation with scheduler | ||
extSimAdapter.tell(new ScheduleDataServiceMessage(dataService), ActorRef.noSender()); | ||
} | ||
|
||
/** Queues message from SIMONA that should be handled by the external simulation. */ | ||
public void queueExtResponseMsg(ResultDataResponseMessageToExt msg) throws InterruptedException { | ||
receiveTriggerQueue.put(msg); | ||
} | ||
|
||
/** | ||
* Waits until a message of given type is added to the queue. If the message has a different type, | ||
* a RuntimeException is thrown. This method blocks until having received a response from SIMONA. | ||
* | ||
* @param expectedMessageClass the expected class of the message to be received | ||
* @return a message of the expected type once it has been received | ||
* @param <T> the type of the expected message | ||
* @throws InterruptedException if the thread running this has been interrupted during the | ||
* blocking operation | ||
*/ | ||
@SuppressWarnings("unchecked") | ||
private <T extends ResultDataResponseMessageToExt> T receiveWithType( | ||
Class<T> expectedMessageClass) throws InterruptedException { | ||
|
||
// blocks until actor puts something here | ||
ResultDataResponseMessageToExt msg = receiveTriggerQueue.take(); | ||
|
||
if (msg.getClass().equals(expectedMessageClass)) { | ||
return (T) msg; | ||
} else | ||
throw new RuntimeException( | ||
"Received unexpected message '" | ||
+ msg | ||
+ "', expected type '" | ||
+ expectedMessageClass | ||
+ "'"); | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
src/main/java/edu/ie3/simona/api/data/results/ExtResultDataSimulation.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
/* | ||
* © 2024. TU Dortmund University, | ||
* Institute of Energy Systems, Energy Efficiency and Energy Economics, | ||
* Research group Distribution grid planning and operation | ||
*/ | ||
|
||
package edu.ie3.simona.api.data.results; | ||
|
||
import edu.ie3.simona.api.data.ExtDataSimulation; | ||
|
||
/** | ||
* An external simulation that needs results from SIMONA should implement this interface and handle | ||
* the ExtResultsData that is handed over. | ||
*/ | ||
public interface ExtResultDataSimulation extends ExtDataSimulation { | ||
|
||
/** Hand over an ExtPrimaryData which enables communication regarding primary data. */ | ||
void setExtResultData(ExtResultData extResultData); | ||
|
||
/** Should implement the convertion of the PSDM format to the external format of result data. */ | ||
ResultDataFactory getResultDataFactory(); | ||
} |
19 changes: 19 additions & 0 deletions
19
src/main/java/edu/ie3/simona/api/data/results/ResultDataFactory.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
/* | ||
* © 2024. TU Dortmund University, | ||
* Institute of Energy Systems, Energy Efficiency and Energy Economics, | ||
* Research group Distribution grid planning and operation | ||
*/ | ||
|
||
package edu.ie3.simona.api.data.results; | ||
|
||
import edu.ie3.datamodel.models.result.ResultEntity; | ||
import edu.ie3.simona.api.exceptions.ConvertionException; | ||
|
||
public interface ResultDataFactory { | ||
|
||
/** | ||
* Should convert a result entity to an object, that can be read by the external simulation, with | ||
* a check if the object is primary data | ||
*/ | ||
Object convert(ResultEntity entity) throws ConvertionException; | ||
} |
14 changes: 14 additions & 0 deletions
14
src/main/java/edu/ie3/simona/api/data/results/ontology/ProvideResultEntities.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
/* | ||
* © 2024. TU Dortmund University, | ||
* Institute of Energy Systems, Energy Efficiency and Energy Economics, | ||
* Research group Distribution grid planning and operation | ||
*/ | ||
|
||
package edu.ie3.simona.api.data.results.ontology; | ||
|
||
import edu.ie3.datamodel.models.result.ResultEntity; | ||
import java.util.List; | ||
|
||
/** Provides a list of results from SIMONA to an external simulation. */ | ||
public record ProvideResultEntities(List<ResultEntity> results) | ||
implements ResultDataResponseMessageToExt {} |
10 changes: 10 additions & 0 deletions
10
src/main/java/edu/ie3/simona/api/data/results/ontology/RequestResultEntities.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
/* | ||
* © 2024. TU Dortmund University, | ||
* Institute of Energy Systems, Energy Efficiency and Energy Economics, | ||
* Research group Distribution grid planning and operation | ||
*/ | ||
|
||
package edu.ie3.simona.api.data.results.ontology; | ||
|
||
/** Request calculated results from SIMONA in the current tick */ | ||
public record RequestResultEntities() implements ResultDataMessageFromExt {} | ||
jo-bao marked this conversation as resolved.
Show resolved
Hide resolved
|
10 changes: 10 additions & 0 deletions
10
src/main/java/edu/ie3/simona/api/data/results/ontology/ResultDataMessageFromExt.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
/* | ||
* © 2024. TU Dortmund University, | ||
* Institute of Energy Systems, Energy Efficiency and Energy Economics, | ||
* Research group Distribution grid planning and operation | ||
*/ | ||
|
||
package edu.ie3.simona.api.data.results.ontology; | ||
|
||
/** Messages that are sent from an external simulation to the SIMONA */ | ||
public interface ResultDataMessageFromExt {} |
12 changes: 12 additions & 0 deletions
12
src/main/java/edu/ie3/simona/api/data/results/ontology/ResultDataResponseMessageToExt.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
/* | ||
* © 2024. TU Dortmund University, | ||
* Institute of Energy Systems, Energy Efficiency and Energy Economics, | ||
* Research group Distribution grid planning and operation | ||
*/ | ||
|
||
package edu.ie3.simona.api.data.results.ontology; | ||
|
||
import edu.ie3.simona.api.data.ontology.DataResponseMessageToExt; | ||
|
||
/** Messages that are sent from SIMONA to the external simulation that needs results */ | ||
public interface ResultDataResponseMessageToExt extends DataResponseMessageToExt {} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.