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 services to pekko typed. #1076

Draft
wants to merge 3 commits into
base: dev
Choose a base branch
from
Draft
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Removed Deployment stage from Jenkinsfile [#1063](https://github.com/ie3-institute/simona/issues/1063)
- Prepare 'ChpModelSpec' and 'CylindricalThermalStorageSpec' for Storage without storageVolumeLvlMin [#1012](https://github.com/ie3-institute/simona/issues/1012)
- Fixed SonarQube quality gate using the correct parameter '-Dsonar.qualitygate.wait=true' [#1072](https://github.com/ie3-institute/simona/issues/1072)
- Converted services to pekko typed [#1069](https://github.com/ie3-institute/simona/issues/1069)

### Fixed
- Fix rendering of references in documentation [#505](https://github.com/ie3-institute/simona/issues/505)
Expand Down
12 changes: 8 additions & 4 deletions src/main/scala/edu/ie3/simona/agent/EnvironmentRefs.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,12 @@ package edu.ie3.simona.agent

import edu.ie3.simona.event.RuntimeEvent
import edu.ie3.simona.ontology.messages.SchedulerMessage
import edu.ie3.simona.ontology.messages.services.{
EvMessage,
PrimaryDataMessage,
WeatherMessage,
}
import org.apache.pekko.actor.typed.ActorRef
import org.apache.pekko.actor.{ActorRef => ClassicRef}

/** Container class, that gather together reference to relevant entities, that
* represent the environment in the simulation
Expand All @@ -28,7 +32,7 @@ import org.apache.pekko.actor.{ActorRef => ClassicRef}
final case class EnvironmentRefs(
scheduler: ActorRef[SchedulerMessage],
runtimeEventListener: ActorRef[RuntimeEvent],
primaryServiceProxy: ClassicRef,
weather: ClassicRef,
evDataService: Option[ClassicRef],
primaryServiceProxy: ActorRef[PrimaryDataMessage],
weather: ActorRef[WeatherMessage],
evDataService: Option[ActorRef[EvMessage]],
)
28 changes: 16 additions & 12 deletions src/main/scala/edu/ie3/simona/agent/grid/GridAgentController.scala
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,17 @@ import edu.ie3.simona.exceptions.CriticalFailureException
import edu.ie3.simona.exceptions.agent.GridAgentInitializationException
import edu.ie3.simona.ontology.messages.SchedulerMessage.ScheduleActivation
import edu.ie3.simona.ontology.messages.flex.FlexibilityMessage.FlexResponse
import edu.ie3.simona.ontology.messages.services.{
EvMessage,
PrimaryDataMessage,
WeatherMessage,
}
import edu.ie3.simona.util.ConfigUtil
import edu.ie3.simona.util.ConfigUtil._
import edu.ie3.simona.util.SimonaConstants.INIT_SIM_TICK
import org.apache.pekko.actor.typed.ActorRef
import org.apache.pekko.actor.typed.scaladsl.ActorContext
import org.apache.pekko.actor.typed.scaladsl.adapter._
import org.apache.pekko.actor.{ActorRef => ClassicRef}
import org.slf4j.Logger

import java.time.ZonedDateTime
Expand Down Expand Up @@ -472,7 +476,7 @@ class GridAgentController(
private def buildFixedFeedIn(
fixedFeedInInput: FixedFeedInInput,
modelConfiguration: FixedFeedInRuntimeConfig,
primaryServiceProxy: ClassicRef,
primaryServiceProxy: ActorRef[PrimaryDataMessage],
simulationStartDate: ZonedDateTime,
simulationEndDate: ZonedDateTime,
resolution: Long,
Expand Down Expand Up @@ -529,7 +533,7 @@ class GridAgentController(
private def buildLoad(
loadInput: LoadInput,
modelConfiguration: LoadRuntimeConfig,
primaryServiceProxy: ClassicRef,
primaryServiceProxy: ActorRef[PrimaryDataMessage],
simulationStartDate: ZonedDateTime,
simulationEndDate: ZonedDateTime,
resolution: Long,
Expand Down Expand Up @@ -588,8 +592,8 @@ class GridAgentController(
private def buildPv(
pvInput: PvInput,
modelConfiguration: PvRuntimeConfig,
primaryServiceProxy: ClassicRef,
weatherService: ClassicRef,
primaryServiceProxy: ActorRef[PrimaryDataMessage],
weatherService: ActorRef[WeatherMessage],
simulationStartDate: ZonedDateTime,
simulationEndDate: ZonedDateTime,
resolution: Long,
Expand Down Expand Up @@ -648,8 +652,8 @@ class GridAgentController(
private def buildEvcs(
evcsInput: EvcsInput,
modelConfiguration: EvcsRuntimeConfig,
primaryServiceProxy: ClassicRef,
evMovementsService: ClassicRef,
primaryServiceProxy: ActorRef[PrimaryDataMessage],
evMovementsService: ActorRef[EvMessage],
simulationStartDate: ZonedDateTime,
simulationEndDate: ZonedDateTime,
resolution: Long,
Expand Down Expand Up @@ -708,8 +712,8 @@ class GridAgentController(
hpInput: HpInput,
thermalGrid: ThermalGrid,
modelConfiguration: HpRuntimeConfig,
primaryServiceProxy: ClassicRef,
weatherService: ClassicRef,
primaryServiceProxy: ActorRef[PrimaryDataMessage],
weatherService: ActorRef[WeatherMessage],
requestVoltageDeviationThreshold: Double,
outputConfig: NotifierConfig,
maybeControllingEm: Option[ActorRef[FlexResponse]],
Expand Down Expand Up @@ -766,8 +770,8 @@ class GridAgentController(
private def buildWec(
wecInput: WecInput,
modelConfiguration: WecRuntimeConfig,
primaryServiceProxy: ClassicRef,
weatherService: ClassicRef,
primaryServiceProxy: ActorRef[PrimaryDataMessage],
weatherService: ActorRef[WeatherMessage],
simulationStartDate: ZonedDateTime,
simulationEndDate: ZonedDateTime,
resolution: Long,
Expand Down Expand Up @@ -824,7 +828,7 @@ class GridAgentController(
private def buildStorage(
storageInput: StorageInput,
modelConfiguration: SimonaConfig.StorageRuntimeConfig,
primaryServiceProxy: ClassicRef,
primaryServiceProxy: ActorRef[PrimaryDataMessage],
simulationStartDate: ZonedDateTime,
simulationEndDate: ZonedDateTime,
resolution: Long,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import edu.ie3.simona.agent.participant.ParticipantAgent.{
import edu.ie3.simona.agent.participant.data.Data
import edu.ie3.simona.agent.participant.data.Data.PrimaryData.PrimaryDataWithApparentPower
import edu.ie3.simona.agent.participant.data.Data.{PrimaryData, SecondaryData}
import edu.ie3.simona.agent.participant.data.secondary.SecondaryDataService
import edu.ie3.simona.agent.participant.data.secondary.SecondaryDataService.SecondaryServiceType
import edu.ie3.simona.agent.participant.statedata.BaseStateData.{
FromOutsideBaseStateData,
ParticipantModelBaseStateData,
Expand Down Expand Up @@ -47,18 +47,17 @@ import edu.ie3.simona.model.participant.{
}
import edu.ie3.simona.ontology.messages.Activation
import edu.ie3.simona.ontology.messages.flex.FlexibilityMessage.{
FlexActivation,
FlexResponse,
IssueFlexControl,
FlexActivation,
}
import edu.ie3.simona.ontology.messages.services.ServiceMessage.RegistrationResponseMessage.RegistrationSuccessfulMessage
import edu.ie3.simona.ontology.messages.services.ServiceMessage.{
PrimaryServiceRegistrationMessage,
ProvisionMessage,
RegistrationResponseMessage,
}
import edu.ie3.simona.ontology.messages.services.PrimaryDataMessage.PrimaryServiceRegistrationMessage
import edu.ie3.simona.ontology.messages.services.ServiceMessage.ProvisionMessage
import edu.ie3.simona.ontology.messages.services.ServiceMessageUniversal.RegistrationResponseMessage
import edu.ie3.simona.ontology.messages.services.ServiceMessageUniversal.RegistrationResponseMessage.RegistrationSuccessfulMessage
import edu.ie3.simona.util.SimonaConstants.INIT_SIM_TICK
import edu.ie3.util.scala.quantities.ReactivePower
import org.apache.pekko.actor.typed.scaladsl.adapter.TypedActorRefOps
import org.apache.pekko.actor.typed.{ActorRef => TypedActorRef}
import org.apache.pekko.actor.{ActorRef, FSM}
import squants.{Dimensionless, Power}
Expand Down Expand Up @@ -129,7 +128,8 @@ abstract class ParticipantAgent[
* that will confirm, otherwise, a failed registration is announced. */
holdTick(INIT_SIM_TICK)
initStateData.primaryServiceProxy ! PrimaryServiceRegistrationMessage(
initStateData.inputModel.electricalInputModel.getUuid
self,
initStateData.inputModel.electricalInputModel.getUuid,
)
goto(HandleInformation) using ParticipantInitializingStateData(
initStateData.inputModel,
Expand Down Expand Up @@ -442,7 +442,7 @@ abstract class ParticipantAgent[
participantStateData.receivedSecondaryDataStore,
currentTick,
nonEmptyData.collect { case (actorRef, Some(data: SecondaryData)) =>
actorRef -> data
actorRef.toClassic -> data
},
)
case _ => participantStateData.receivedSecondaryDataStore
Expand Down Expand Up @@ -515,7 +515,7 @@ abstract class ParticipantAgent[
resolution: Long,
requestVoltageDeviationThreshold: Double,
outputConfig: NotifierConfig,
senderToMaybeTick: (ActorRef, Option[Long]),
senderToMaybeTick: (TypedActorRef[_], Option[Long]),
scheduler: ActorRef,
): FSM.State[AgentState, ParticipantStateData[PD]]

Expand Down Expand Up @@ -547,7 +547,7 @@ abstract class ParticipantAgent[
def initializeParticipantForModelCalculation(
inputModel: InputModelContainer[I],
modelConfig: MC,
services: Iterable[SecondaryDataService[_ <: SecondaryData]],
services: Iterable[SecondaryServiceType],
simulationStartDate: ZonedDateTime,
simulationEndDate: ZonedDateTime,
resolution: Long,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import edu.ie3.simona.agent.participant.data.Data.PrimaryData.{
}
import edu.ie3.simona.agent.participant.data.Data.{PrimaryData, SecondaryData}
import edu.ie3.simona.agent.participant.data.secondary.SecondaryDataService
import edu.ie3.simona.agent.participant.data.secondary.SecondaryDataService.SecondaryServiceType
import edu.ie3.simona.agent.participant.statedata.BaseStateData.{
FromOutsideBaseStateData,
ParticipantModelBaseStateData,
Expand Down Expand Up @@ -75,16 +76,18 @@ import edu.ie3.simona.ontology.messages.Activation
import edu.ie3.simona.ontology.messages.SchedulerMessage.Completion
import edu.ie3.simona.ontology.messages.flex.FlexibilityMessage._
import edu.ie3.simona.ontology.messages.flex.MinMaxFlexibilityMessage.ProvideMinMaxFlexOptions
import edu.ie3.simona.ontology.messages.services.ServiceMessage.{
ProvisionMessage,
RegistrationResponseMessage,
}
import edu.ie3.simona.ontology.messages.services.ServiceMessage
import edu.ie3.simona.ontology.messages.services.ServiceMessage.ProvisionMessage
import edu.ie3.simona.ontology.messages.services.ServiceMessageUniversal.RegistrationResponseMessage
import edu.ie3.simona.util.TickUtil._
import edu.ie3.util.quantities.PowerSystemUnits._
import edu.ie3.util.quantities.QuantityUtils.RichQuantityDouble
import edu.ie3.util.scala.quantities.DefaultQuantities._
import edu.ie3.util.scala.quantities.{Megavars, QuantityUtil, ReactivePower}
import org.apache.pekko.actor.typed.scaladsl.adapter.ClassicActorRefOps
import org.apache.pekko.actor.typed.scaladsl.adapter.{
ClassicActorRefOps,
TypedActorRefOps,
}
import org.apache.pekko.actor.typed.{ActorRef => TypedActorRef}
import org.apache.pekko.actor.{ActorRef, FSM, PoisonPill}
import org.apache.pekko.event.LoggingAdapter
Expand Down Expand Up @@ -123,7 +126,7 @@ protected trait ParticipantAgentFundamentals[
resolution: Long,
requestVoltageDeviationThreshold: Double,
outputConfig: NotifierConfig,
senderToMaybeTick: (ActorRef, Option[Long]),
senderToMaybeTick: (TypedActorRef[_], Option[Long]),
scheduler: ActorRef,
): FSM.State[AgentState, ParticipantStateData[PD]] = {
val stateData = determineFromOutsideBaseStateData(
Expand Down Expand Up @@ -175,7 +178,7 @@ protected trait ParticipantAgentFundamentals[
resolution: Long,
requestVoltageDeviationThreshold: Double,
outputConfig: NotifierConfig,
senderToMaybeTick: (ActorRef, Option[Long]),
senderToMaybeTick: (TypedActorRef[_], Option[Long]),
): FromOutsideBaseStateData[M, PD] = {
val model = buildModel(
inputModel,
Expand Down Expand Up @@ -257,7 +260,7 @@ protected trait ParticipantAgentFundamentals[
override def initializeParticipantForModelCalculation(
inputModel: InputModelContainer[I],
modelConfig: MC,
services: Iterable[SecondaryDataService[_ <: SecondaryData]],
services: Iterable[SecondaryServiceType],
simulationStartDate: ZonedDateTime,
simulationEndDate: ZonedDateTime,
resolution: Long,
Expand Down Expand Up @@ -337,7 +340,7 @@ protected trait ParticipantAgentFundamentals[
def determineModelBaseStateData(
inputModel: InputModelContainer[I],
modelConfig: MC,
services: Iterable[SecondaryDataService[_ <: SecondaryData]],
services: Iterable[SecondaryServiceType],
simulationStartDate: ZonedDateTime,
simulationEndDate: ZonedDateTime,
resolution: Long,
Expand Down Expand Up @@ -622,7 +625,7 @@ protected trait ParticipantAgentFundamentals[
modelStateData.receivedSecondaryDataStore,
tick,
stateData.data.map { case (actorRef, Some(data: SecondaryData)) =>
actorRef -> data
actorRef.toClassic -> data
},
)

Expand Down Expand Up @@ -987,7 +990,7 @@ protected trait ParticipantAgentFundamentals[
* A trial to get and process the needed data
*/
def prepareData(
data: Map[ActorRef, Option[_ <: Data]],
data: Map[TypedActorRef[_], Option[_ <: Data]],
reactivePowerFunction: Power => ReactivePower,
): Try[PD] =
data.headOption
Expand Down Expand Up @@ -1943,27 +1946,17 @@ protected trait ParticipantAgentFundamentals[
* @param services
* the services used in
* [[edu.ie3.simona.agent.participant.statedata.BaseStateData.ModelBaseStateData]]
* @param tag
* ClassTag of T
* @tparam T
* the type of secondary service to return
* @tparam S
* the type of the messages of the secondary service to return
* @return
* secondary service of given type
*/
protected def getService[T <: SecondaryDataService[_]](
services: Iterable[SecondaryDataService[_ <: SecondaryData]]
)(implicit tag: ClassTag[T]): ActorRef =
services
.find {
case _: T => true
case _ => false
}
.getOrElse(
throw new InconsistentStateException(
s"No $tag provided by ParticipantModelBaseStateData."
)
)
.actorRef
protected def getService[S <: ServiceMessage](
services: Iterable[SecondaryServiceType]
): Option[TypedActorRef[S]] =
services.collectFirst { case service: SecondaryDataService[_, S] =>
service.actorRef
}
}

object ParticipantAgentFundamentals {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

package edu.ie3.simona.agent.participant

import org.apache.pekko.actor.ActorRef
import edu.ie3.datamodel.models.input.system.{EvcsInput, SystemParticipantInput}
import edu.ie3.simona.agent.participant.data.Data.PrimaryData.PrimaryDataWithApparentPower
import edu.ie3.simona.agent.participant.data.Data.SecondaryData
Expand All @@ -26,6 +25,12 @@ import edu.ie3.simona.model.participant.{
}
import edu.ie3.simona.ontology.messages.services.EvMessage.RegisterForEvDataMessage
import edu.ie3.simona.ontology.messages.services.WeatherMessage.RegisterForWeatherMessage
import edu.ie3.simona.ontology.messages.services.{
EvMessage,
ServiceMessage,
WeatherMessage,
}
import org.apache.pekko.actor.typed.ActorRef

trait ServiceRegistration[
PD <: PrimaryDataWithApparentPower[PD],
Expand All @@ -50,8 +55,10 @@ trait ServiceRegistration[
*/
def registerForServices(
inputModel: I,
services: Iterable[SecondaryDataService[_ <: SecondaryData]],
): Iterable[ActorRef] =
services: Iterable[
SecondaryDataService[_ <: SecondaryData, _ <: ServiceMessage]
],
): Iterable[ActorRef[_]] =
services.flatMap(service =>
registerForSecondaryService(service, inputModel)
)
Expand All @@ -69,11 +76,12 @@ trait ServiceRegistration[
* supported at the moment
*/
private def registerForSecondaryService[
S <: SecondaryData
S <: SecondaryData,
M <: ServiceMessage,
](
serviceDefinition: SecondaryDataService[S],
serviceDefinition: SecondaryDataService[S, M],
inputModel: I,
): Option[ActorRef] = serviceDefinition match {
): Option[ActorRef[_]] = serviceDefinition match {
case SecondaryDataService.ActorPriceService(_) =>
log.debug(
s"Attempt to register for {}. This is currently not supported.",
Expand All @@ -97,7 +105,7 @@ trait ServiceRegistration[
* @return
*/
private def registerForWeather(
actorRef: ActorRef,
actorRef: ActorRef[WeatherMessage],
inputModel: I,
): Unit = {
/* If we are asked to register for weather, determine the proper geo position */
Expand All @@ -113,7 +121,7 @@ trait ServiceRegistration[
s"is invalid."
)
}
actorRef ! RegisterForWeatherMessage(lat, lon)
actorRef ! RegisterForWeatherMessage(self, lat, lon)
}

/** Register for the EV movement service
Expand All @@ -125,12 +133,12 @@ trait ServiceRegistration[
* @return
*/
private def registerForEvData(
serviceRef: ActorRef,
serviceRef: ActorRef[EvMessage],
inputModel: I,
): Unit = {
inputModel match {
case evcsInput: EvcsInput =>
serviceRef ! RegisterForEvDataMessage(evcsInput.getUuid)
serviceRef ! RegisterForEvDataMessage(self, evcsInput.getUuid)
case _ =>
throw new ServiceRegistrationException(
s"Cannot register for EV movements information at node ${inputModel.getNode.getId} " +
Expand Down
Loading
Loading