Skip to content
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

Converting pekko classic to typed. #233

Open
wants to merge 3 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Added Bao and Staudt to the list of reviewers [#216](https://github.com/ie3-institute/simonaAPI/issues/216)
- Documentation for this API [#230](https://github.com/ie3-institute/simonaAPI/issues/230)

### Changed
- Converting pekko classic to typed [#232](https://github.com/ie3-institute/simonaAPI/issues/232)

## [0.6.0] - 2024-12-02

### Added
Expand Down
5 changes: 3 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,9 @@ dependencies{
implementation 'org.apache.logging.log4j:log4j-slf4j-impl' // log4j -> slf4j

// pekko
implementation "org.apache.pekko:pekko-actor_${scalaVersion}:${pekkoVersion}"
testImplementation "org.apache.pekko:pekko-testkit_${scalaVersion}:${pekkoVersion}" // pekko testkit
implementation "org.apache.pekko:pekko-actor-typed_${scalaVersion}:${pekkoVersion}"

testImplementation "org.apache.pekko:pekko-actor-testkit-typed_${scalaVersion}:${pekkoVersion}" // pekko typed testkit

// TESTING
testImplementation 'org.spockframework:spock-core:2.3-groovy-4.0'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@

package edu.ie3.simona.api.data;

import org.apache.pekko.actor.ActorRef;
import edu.ie3.simona.api.data.ontology.DataMessageFromExt;
import edu.ie3.simona.api.simulation.ontology.ControlResponseMessageFromExt;
import org.apache.pekko.actor.typed.ActorRef;

public interface ExtInputDataConnection extends ExtDataConnection {
public interface ExtInputDataConnection<T extends DataMessageFromExt> extends ExtDataConnection {

/**
* Sets the actor refs for data and control flow.
Expand All @@ -17,5 +19,5 @@ public interface ExtInputDataConnection extends ExtDataConnection {
* messages
* @param extSimAdapter actor ref to the extSimAdapter
*/
void setActorRefs(ActorRef dataService, ActorRef extSimAdapter);
void setActorRefs(ActorRef<T> dataService, ActorRef<ControlResponseMessageFromExt> extSimAdapter);
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,16 @@

package edu.ie3.simona.api.data;

import org.apache.pekko.actor.ActorRef;
import edu.ie3.simona.api.data.results.ontology.ResultDataMessageFromExt;
import edu.ie3.simona.api.simulation.ontology.ControlResponseMessageFromExt;
import org.apache.pekko.actor.typed.ActorRef;

/**
* Interface for a connection between SIMONA and an external simulation with data flow from SIMONA
* to external
*/
public interface ExtOutputDataConnection extends ExtDataConnection {
public interface ExtOutputDataConnection<T extends ResultDataMessageFromExt>
extends ExtDataConnection {

/**
* Sets the actor refs for data and control flow
Expand All @@ -23,5 +26,7 @@ public interface ExtOutputDataConnection extends ExtDataConnection {
* @param extSimAdapter actor ref to the extSimAdapter
*/
void setActorRefs(
ActorRef extResultDataService, ActorRef dataServiceActivation, ActorRef extSimAdapter);
ActorRef<T> extResultDataService,
ActorRef<T> dataServiceActivation,
ActorRef<ControlResponseMessageFromExt> extSimAdapter);
}
22 changes: 14 additions & 8 deletions src/main/java/edu/ie3/simona/api/data/em/ExtEmDataConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,23 @@
import edu.ie3.simona.api.data.em.ontology.EmDataMessageFromExt;
import edu.ie3.simona.api.data.em.ontology.ProvideEmSetPointData;
import edu.ie3.simona.api.data.ontology.ScheduleDataServiceMessage;
import java.util.*;
import edu.ie3.simona.api.simulation.ontology.ControlResponseMessageFromExt;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.UUID;
import java.util.stream.Collectors;
import org.apache.pekko.actor.ActorRef;
import org.apache.pekko.actor.typed.ActorRef;
import org.slf4j.Logger;

/** Enables data connection of em data between SIMONA and SimonaAPI */
public class ExtEmDataConnection implements ExtInputDataConnection {
public class ExtEmDataConnection implements ExtInputDataConnection<EmDataMessageFromExt> {

/** Actor reference to service that handles ev data within SIMONA */
private ActorRef emDataService;
private ActorRef<EmDataMessageFromExt> emDataService;

/** Actor reference to adapter that handles scheduler control flow in SIMONA */
private ActorRef extSimAdapter;
private ActorRef<ControlResponseMessageFromExt> extSimAdapter;

/** Assets that provide primary data to SIMONA */
private final Map<String, UUID> extEmMapping;
Expand All @@ -34,7 +38,9 @@ public ExtEmDataConnection(Map<String, UUID> extEmMapping) {
}

@Override
public void setActorRefs(ActorRef emDataService, ActorRef extSimAdapter) {
public void setActorRefs(
ActorRef<EmDataMessageFromExt> emDataService,
ActorRef<ControlResponseMessageFromExt> extSimAdapter) {
this.emDataService = emDataService;
this.extSimAdapter = extSimAdapter;
}
Expand Down Expand Up @@ -74,8 +80,8 @@ public void provideEmData(Long tick, Map<UUID, PValue> emData, Optional<Long> ma
* @param msg the data/information that is sent to SIMONA's external primary data service
*/
public void sendExtMsg(EmDataMessageFromExt msg) {
emDataService.tell(msg, ActorRef.noSender());
emDataService.tell(msg);
// we need to schedule data receiver activation with scheduler
extSimAdapter.tell(new ScheduleDataServiceMessage(emDataService), ActorRef.noSender());
extSimAdapter.tell(new ScheduleDataServiceMessage<>(emDataService));
}
}
17 changes: 10 additions & 7 deletions src/main/java/edu/ie3/simona/api/data/ev/ExtEvDataConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,29 @@
import edu.ie3.simona.api.data.ev.model.EvModel;
import edu.ie3.simona.api.data.ev.ontology.*;
import edu.ie3.simona.api.data.ontology.ScheduleDataServiceMessage;
import edu.ie3.simona.api.simulation.ontology.ControlResponseMessageFromExt;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.UUID;
import java.util.concurrent.LinkedBlockingQueue;
import org.apache.pekko.actor.ActorRef;
import org.apache.pekko.actor.typed.ActorRef;

public class ExtEvDataConnection implements ExtInputDataConnection {
public class ExtEvDataConnection implements ExtInputDataConnection<EvDataMessageFromExt> {
/** Data message queue containing messages from SIMONA */
public final LinkedBlockingQueue<EvDataResponseMessageToExt> receiveTriggerQueue =
new LinkedBlockingQueue<>();

/** Actor reference to service that handles ev data within SIMONA */
private ActorRef dataService;
private ActorRef<EvDataMessageFromExt> dataService;

/** Actor reference to adapter that handles scheduler control flow in SIMONA */
private ActorRef extSimAdapter;
private ActorRef<ControlResponseMessageFromExt> extSimAdapter;

@Override
public void setActorRefs(ActorRef dataService, ActorRef extSimAdapter) {
public void setActorRefs(
ActorRef<EvDataMessageFromExt> dataService,
ActorRef<ControlResponseMessageFromExt> extSimAdapter) {
this.dataService = dataService;
this.extSimAdapter = extSimAdapter;
}
Expand Down Expand Up @@ -99,9 +102,9 @@ public void provideArrivingEvs(Map<UUID, List<EvModel>> arrivals, Optional<Long>
* @param msg the data/information that is sent to SIMONA's ev data service
*/
public void sendExtMsg(EvDataMessageFromExt msg) {
dataService.tell(msg, ActorRef.noSender());
dataService.tell(msg);
// we need to schedule data receiver activation with scheduler
extSimAdapter.tell(new ScheduleDataServiceMessage(dataService), ActorRef.noSender());
extSimAdapter.tell(new ScheduleDataServiceMessage<>(dataService));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,8 @@

package edu.ie3.simona.api.data.ontology;

import java.util.Objects;
import org.apache.pekko.actor.ActorRef;
import edu.ie3.simona.api.simulation.ontology.ControlResponseMessageFromExt;
import org.apache.pekko.actor.typed.ActorRef;

public class ScheduleDataServiceMessage {
private final ActorRef dataService;

public ScheduleDataServiceMessage(ActorRef dataService) {
this.dataService = dataService;
}

public ActorRef getDataService() {
return dataService;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
ScheduleDataServiceMessage that = (ScheduleDataServiceMessage) o;
return dataService.equals(that.dataService);
}

@Override
public int hashCode() {
return Objects.hash(dataService);
}
}
public record ScheduleDataServiceMessage<T extends DataMessageFromExt>(ActorRef<T> dataService)
implements ControlResponseMessageFromExt {}
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,20 @@
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.simulation.ontology.ControlResponseMessageFromExt;
import java.util.*;
import java.util.stream.Collectors;
import org.apache.pekko.actor.ActorRef;
import org.apache.pekko.actor.typed.ActorRef;
import org.slf4j.Logger;

/** Enables data connection of primary data between SIMONA and SimonaAPI */
public class ExtPrimaryDataConnection implements ExtInputDataConnection {
public class ExtPrimaryDataConnection implements ExtInputDataConnection<PrimaryDataMessageFromExt> {

/** Actor reference to service that handles primary data within SIMONA */
private ActorRef dataService;
private ActorRef<PrimaryDataMessageFromExt> dataService;

/** Actor reference to adapter that handles scheduler control flow in SIMONA */
private ActorRef extSimAdapter;
private ActorRef<ControlResponseMessageFromExt> extSimAdapter;

/** Assets that provide primary data to SIMONA */
private final Map<String, UUID> extPrimaryDataMapping;
Expand All @@ -33,7 +34,9 @@ public ExtPrimaryDataConnection(Map<String, UUID> extPrimaryDataMapping) {
}

@Override
public void setActorRefs(ActorRef dataService, ActorRef extSimAdapter) {
public void setActorRefs(
ActorRef<PrimaryDataMessageFromExt> dataService,
ActorRef<ControlResponseMessageFromExt> extSimAdapter) {
this.dataService = dataService;
this.extSimAdapter = extSimAdapter;
}
Expand Down Expand Up @@ -74,8 +77,8 @@ public void providePrimaryData(
* @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());
dataService.tell(msg);
// we need to schedule data receiver activation with scheduler
extSimAdapter.tell(new ScheduleDataServiceMessage(dataService), ActorRef.noSender());
extSimAdapter.tell(new ScheduleDataServiceMessage<>(dataService));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,28 +15,29 @@
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.simulation.ontology.ControlResponseMessageFromExt;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.LinkedBlockingQueue;
import org.apache.pekko.actor.ActorRef;
import org.apache.pekko.actor.typed.ActorRef;

/** Enables data connection of results between SIMONA and SimonaAPI */
public class ExtResultDataConnection implements ExtOutputDataConnection {
public class ExtResultDataConnection implements ExtOutputDataConnection<ResultDataMessageFromExt> {

/** Data message queue containing messages from SIMONA */
public final LinkedBlockingQueue<ResultDataResponseMessageToExt> receiveTriggerQueue =
new LinkedBlockingQueue<>();

/** Actor reference to service that handles result data within SIMONA */
private ActorRef extResultDataService;
private ActorRef<ResultDataMessageFromExt> extResultDataService;

/** Actor reference to the dataServiceAdapter */
private ActorRef dataServiceActivation;
private ActorRef<ResultDataMessageFromExt> dataServiceActivation;

/** Actor reference to adapter that handles scheduler control flow in SIMONA */
private ActorRef extSimAdapter;
private ActorRef<ControlResponseMessageFromExt> extSimAdapter;

/** Map uuid to external id of grid related entities */
private final Map<UUID, String> gridResultAssetMapping;
Expand All @@ -59,7 +60,9 @@ public ExtResultDataConnection(
* @param extSimAdapter actor ref to the extSimAdapter
*/
public void setActorRefs(
ActorRef extResultDataService, ActorRef dataServiceActivation, ActorRef extSimAdapter) {
ActorRef<ResultDataMessageFromExt> extResultDataService,
ActorRef<ResultDataMessageFromExt> dataServiceActivation,
ActorRef<ControlResponseMessageFromExt> extSimAdapter) {
this.extResultDataService = extResultDataService;
this.dataServiceActivation = dataServiceActivation;
this.extSimAdapter = extSimAdapter;
Expand Down Expand Up @@ -112,9 +115,9 @@ protected Map<String, ModelResultEntity> createResultMap(List<ModelResultEntity>
* @param msg the data/information that is sent to SIMONA's result data service
*/
public void sendExtMsg(ResultDataMessageFromExt msg) {
extResultDataService.tell(msg, ActorRef.noSender());
extResultDataService.tell(msg);
// we need to schedule data receiver activation with scheduler
extSimAdapter.tell(new ScheduleDataServiceMessage(dataServiceActivation), ActorRef.noSender());
extSimAdapter.tell(new ScheduleDataServiceMessage<>(dataServiceActivation));
}

/** Queues message from SIMONA that should be handled by the external simulation. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import edu.ie3.simona.api.simulation.ontology.ControlMessageToExt;
import edu.ie3.simona.api.simulation.ontology.ControlResponseMessageFromExt;
import java.util.concurrent.LinkedBlockingQueue;
import org.apache.pekko.actor.ActorRef;
import org.apache.pekko.actor.typed.ActorRef;

public class ExtSimAdapterData {

Expand All @@ -18,19 +18,20 @@ public class ExtSimAdapterData {
new LinkedBlockingQueue<>();

/** Actor reference to the adapter for the phases that handles scheduler control flow in SIMONA */
private final ActorRef extSimAdapter;
private final ActorRef<ControlResponseMessageFromExt> extSimAdapter;

/** CLI arguments with which SIMONA is initiated */
private final String[] mainArgs;

// important trigger queue must be the same as held in actor
// to make it safer one might consider asking the actor for a reference on its trigger queue?!
public ExtSimAdapterData(ActorRef extSimAdapter, String[] mainArgs) {
public ExtSimAdapterData(
ActorRef<ControlResponseMessageFromExt> extSimAdapter, String[] mainArgs) {
this.extSimAdapter = extSimAdapter;
this.mainArgs = mainArgs;
}

public ActorRef getAdapter() {
public ActorRef<ControlResponseMessageFromExt> getAdapter() {
return extSimAdapter;
}

Expand All @@ -52,7 +53,7 @@ public void queueExtMsg(ControlMessageToExt msg) throws InterruptedException {
* @param msg the message to send
*/
public void send(ControlResponseMessageFromExt msg) {
extSimAdapter.tell(msg, ActorRef.noSender());
extSimAdapter.tell(msg);
}

public String[] getMainArgs() {
Expand Down
Loading