Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/dev' into jb/#671-externaldatase…
Browse files Browse the repository at this point in the history
…rvice

# Conflicts:
#	src/main/scala/edu/ie3/simona/event/listener/ResultEventListener.scala
#	src/main/scala/edu/ie3/simona/sim/SimonaSim.scala
#	src/main/scala/edu/ie3/simona/sim/setup/SimonaSetup.scala
#	src/main/scala/edu/ie3/simona/sim/setup/SimonaStandaloneSetup.scala
#	src/test/scala/edu/ie3/simona/sim/SimonaSimFailSpec.scala
#	src/test/scala/edu/ie3/simona/sim/setup/SimonaSetupSpec.scala
  • Loading branch information
jo-bao committed Feb 26, 2024
2 parents 94aba27 + eef9c8f commit 6b07e15
Show file tree
Hide file tree
Showing 38 changed files with 1,121 additions and 922 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Added capability of SystemParticipants to handle flexibility [#308](https://github.com/ie3-institute/simona/issues/308)
- Added smart charging logic [#31](https://github.com/ie3-institute/simona/issues/31) and flex calculation in `EvcsAgent` [#332](https://github.com/ie3-institute/simona/issues/332)
- Enhance output quotes of `RunSimona` [#743](https://github.com/ie3-institute/simona/issues/743)
- Printing logs of failed tests [#747](https://github.com/ie3-institute/simona/issues/747)

### Changed
- Adapted to changed data source in PSDM [#435](https://github.com/ie3-institute/simona/issues/435)
Expand All @@ -37,6 +38,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Enable windows path as config parameters [#549](https://github.com/ie3-institute/simona/issues/549)
- Unified consideration of scaling factor when simulating system participants [#81](https://github.com/ie3-institute/simona/issues/81)
- Small improvements in `ResultEventListener` [#738](https://github.com/ie3-institute/simona/issues/738)
- Converting `SimonaSim` to pekko typed/terminating SimonSim when initialization fails [#210](https://github.com/ie3-institute/simona/issues/210)

### Fixed
- Removed a repeated line in the documentation of vn_simona config [#658](https://github.com/ie3-institute/simona/issues/658)
Expand Down
14 changes: 8 additions & 6 deletions src/main/scala/edu/ie3/simona/agent/EnvironmentRefs.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@

package edu.ie3.simona.agent

import org.apache.pekko.actor.ActorRef
import edu.ie3.simona.ontology.messages.SchedulerMessage
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 @@ -23,9 +25,9 @@ import org.apache.pekko.actor.ActorRef
* Reference to the EV data service, if existing
*/
final case class EnvironmentRefs(
scheduler: ActorRef,
runtimeEventListener: ActorRef,
primaryServiceProxy: ActorRef,
weather: ActorRef,
evDataService: Option[ActorRef],
scheduler: ActorRef[SchedulerMessage],
runtimeEventListener: ClassicRef,
primaryServiceProxy: ClassicRef,
weather: ClassicRef,
evDataService: Option[ClassicRef],
)
10 changes: 1 addition & 9 deletions src/main/scala/edu/ie3/simona/agent/grid/GridAgent.scala
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import edu.ie3.simona.ontology.messages.SchedulerMessage.{
Completion,
ScheduleActivation,
}
import edu.ie3.simona.ontology.messages.{Activation, StopMessage}
import edu.ie3.simona.ontology.messages.Activation
import edu.ie3.simona.scheduler.ScheduleLock.ScheduleKey
import edu.ie3.simona.util.SimonaConstants.INIT_SIM_TICK
import edu.ie3.util.TimeUtil
Expand Down Expand Up @@ -257,14 +257,6 @@ class GridAgent(

goto(SimulateGrid) using gridAgentBaseData

case Event(StopMessage(_), data: GridAgentBaseData) =>
// shutdown children
data.gridEnv.nodeToAssetAgents.foreach { case (_, actors) =>
actors.foreach(context.stop)
}

// we are done
stop()
}

// everything else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@

package edu.ie3.simona.agent.grid

import org.apache.pekko.actor.typed.scaladsl.adapter.ClassicActorRefOps
import org.apache.pekko.actor.{ActorContext, ActorRef}
import org.apache.pekko.event.LoggingAdapter
import com.typesafe.scalalogging.LazyLogging
import edu.ie3.datamodel.models.input.container.{SubGridContainer, ThermalGrid}
import edu.ie3.datamodel.models.input.system._
Expand All @@ -33,6 +30,9 @@ import edu.ie3.simona.ontology.messages.SchedulerMessage.ScheduleActivation
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.scaladsl.adapter.{ClassicActorRefOps, _}
import org.apache.pekko.actor.{ActorContext, ActorRef}
import org.apache.pekko.event.LoggingAdapter

import java.time.ZonedDateTime
import java.util.UUID
Expand Down Expand Up @@ -339,7 +339,7 @@ class GridAgentController(
): ActorRef =
gridAgentContext.simonaActorOf(
FixedFeedInAgent.props(
environmentRefs.scheduler,
environmentRefs.scheduler.toClassic,
ParticipantInitializeStateData(
fixedFeedInInput,
modelConfiguration,
Expand Down Expand Up @@ -390,7 +390,7 @@ class GridAgentController(
): ActorRef =
gridAgentContext.simonaActorOf(
LoadAgent.props(
environmentRefs.scheduler,
environmentRefs.scheduler.toClassic,
ParticipantInitializeStateData(
loadInput,
modelConfiguration,
Expand Down Expand Up @@ -444,7 +444,7 @@ class GridAgentController(
): ActorRef =
gridAgentContext.simonaActorOf(
PvAgent.props(
environmentRefs.scheduler,
environmentRefs.scheduler.toClassic,
ParticipantInitializeStateData(
pvInput,
modelConfiguration,
Expand Down Expand Up @@ -498,7 +498,7 @@ class GridAgentController(
): ActorRef =
gridAgentContext.simonaActorOf(
EvcsAgent.props(
environmentRefs.scheduler,
environmentRefs.scheduler.toClassic,
ParticipantInitializeStateData(
evcsInput,
modelConfiguration,
Expand Down Expand Up @@ -547,7 +547,7 @@ class GridAgentController(
): ActorRef =
gridAgentContext.simonaActorOf(
HpAgent.props(
environmentRefs.scheduler,
environmentRefs.scheduler.toClassic,
ParticipantInitializeStateData(
hpInput,
thermalGrid,
Expand Down Expand Up @@ -602,7 +602,7 @@ class GridAgentController(
): ActorRef =
gridAgentContext.simonaActorOf(
WecAgent.props(
environmentRefs.scheduler,
environmentRefs.scheduler.toClassic,
ParticipantInitializeStateData(
wecInput,
modelConfiguration,
Expand Down
8 changes: 5 additions & 3 deletions src/main/scala/edu/ie3/simona/api/ExtSimAdapter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ package edu.ie3.simona.api

import org.apache.pekko.actor.typed.scaladsl.adapter.ClassicActorRefOps
import org.apache.pekko.actor.{Actor, ActorRef, PoisonPill, Props}
import edu.ie3.simona.api.ExtSimAdapter.{Create, ExtSimAdapterStateData}
import edu.ie3.simona.api.ExtSimAdapter.{Create, ExtSimAdapterStateData, Stop}
import edu.ie3.simona.api.data.ontology.ScheduleDataServiceMessage
import edu.ie3.simona.api.simulation.ExtSimAdapterData
import edu.ie3.simona.api.simulation.ontology.{
Expand All @@ -23,7 +23,7 @@ import edu.ie3.simona.ontology.messages.SchedulerMessage.{
ScheduleActivation,
}
import edu.ie3.simona.ontology.messages.services.ServiceMessage.RegistrationResponseMessage.ScheduleServiceActivation
import edu.ie3.simona.ontology.messages.{Activation, StopMessage}
import edu.ie3.simona.ontology.messages.Activation
import edu.ie3.simona.scheduler.ScheduleLock
import edu.ie3.simona.scheduler.ScheduleLock.ScheduleKey
import edu.ie3.simona.util.SimonaConstants.INIT_SIM_TICK
Expand All @@ -45,6 +45,8 @@ object ExtSimAdapter {
*/
final case class Create(extSimData: ExtSimAdapterData, unlockKey: ScheduleKey)

final case class Stop(simulationSuccessful: Boolean)

final case class ExtSimAdapterStateData(
extSimData: ExtSimAdapterData,
currentTick: Option[Long] = None,
Expand Down Expand Up @@ -107,7 +109,7 @@ final case class ExtSimAdapter(scheduler: ActorRef)
key,
)

case StopMessage(simulationSuccessful) =>
case Stop(simulationSuccessful) =>
// let external sim know that we have terminated
stateData.extSimData.queueExtMsg(
new TerminationMessage(simulationSuccessful)
Expand Down
4 changes: 2 additions & 2 deletions src/main/scala/edu/ie3/simona/event/ResultEvent.scala
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ import edu.ie3.datamodel.models.result.system.{
}
import edu.ie3.datamodel.models.result.thermal.ThermalUnitResult
import edu.ie3.simona.agent.grid.GridResultsSupport.PartialTransformer3wResult
import edu.ie3.simona.event.listener.ResultEventListener.ResultMessage
import edu.ie3.simona.event.listener.ResultEventListener

sealed trait ResultEvent extends ResultMessage
sealed trait ResultEvent extends Event with ResultEventListener.Request

/** Calculation result events
*/
Expand Down
4 changes: 3 additions & 1 deletion src/main/scala/edu/ie3/simona/event/RuntimeEvent.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@

package edu.ie3.simona.event

import edu.ie3.simona.event.listener.RuntimeEventListener.Request

/** Event type for simulation control */
sealed trait RuntimeEvent extends Event
sealed trait RuntimeEvent extends Event with Request

object RuntimeEvent {

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* © 2024. TU Dortmund University,
* Institute of Energy Systems, Energy Efficiency and Energy Economics,
* Research group Distribution grid planning and operation
*/

package edu.ie3.simona.event.listener

import org.apache.pekko.actor.typed.Behavior
import org.apache.pekko.actor.typed.scaladsl.{ActorContext, Behaviors}

import scala.concurrent.duration.DurationInt

/** Helper that provides functionality for delayed stopping of actors, i.e. upon
* receiving [[FlushAndStop]], the actor is stopped after a fixed amount of
* time after the last message has been received
*/
object DelayedStopHelper {

/** Note: Needs to extend be message traits for actors that want to use this
* functionality
*/
sealed trait StoppingMsg
extends ResultEventListener.Request
with RuntimeEventListener.Request

/** Message indicating that [[RuntimeEventListener]] should stop. Instead of
* using [[org.apache.pekko.actor.typed.scaladsl.ActorContext.stop]], this
* way of stopping allows the current mailbox to be processed, plus more
* messages that are pending to be received.
*/
case object FlushAndStop extends StoppingMsg

private case object StopTimeout extends StoppingMsg

def handleMsg[T >: StoppingMsg]
: PartialFunction[(ActorContext[T], StoppingMsg), Behavior[T]] = {

case (ctx, FlushAndStop) =>
ctx.log.debug(
s"Received FlushAndStop message, shutting down once no message has been received for 5 seconds."
)
ctx.setReceiveTimeout(5.seconds, StopTimeout)
Behaviors.same

case (ctx, StopTimeout) =>
// there have been no messages for 5 seconds, let's end this
ctx.log.debug(s"${getClass.getSimpleName} is now stopped.")
Behaviors.stopped
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import org.apache.pekko.actor.typed.{Behavior, PostStop}
import edu.ie3.datamodel.io.processor.result.ResultEntityProcessor
import edu.ie3.datamodel.models.result.{NodeResult, ResultEntity}
import edu.ie3.simona.agent.grid.GridResultsSupport.PartialTransformer3wResult
import edu.ie3.simona.event.Event
import edu.ie3.simona.event.ResultEvent.{
FlexOptionsResultEvent,
ParticipantResultEvent,
Expand All @@ -36,15 +35,13 @@ import scala.util.{Failure, Success, Try}

object ResultEventListener extends Transformer3wResultSupport {

trait ResultMessage extends Event
trait Request

private final case class SinkResponse(
response: Map[Class[_], ResultEntitySink]
) extends ResultMessage
) extends Request

private final case class Failed(ex: Exception) extends ResultMessage

private final case object StopTimeout extends ResultMessage
private final case class InitFailed(ex: Exception) extends Request

/** [[ResultEventListener]] base data containing all information the listener
* needs
Expand Down Expand Up @@ -256,7 +253,7 @@ object ResultEventListener extends Transformer3wResultSupport {
def apply(
resultFileHierarchy: ResultFileHierarchy,
extResultDataService: Option[ActorRef] = Option.empty[ActorRef],
): Behavior[ResultMessage] = Behaviors.setup[ResultMessage] { ctx =>
): Behavior[Request] = Behaviors.setup[Request] { ctx =>
ctx.log.debug("Starting initialization!")
resultFileHierarchy.resultSinkType match {
case _: ResultSinkType.Kafka =>
Expand All @@ -275,7 +272,7 @@ object ResultEventListener extends Transformer3wResultSupport {
ResultEventListener.initializeSinks(resultFileHierarchy)
)
) {
case Failure(exception: Exception) => Failed(exception)
case Failure(exception: Exception) => InitFailed(exception)
case Success(result) => SinkResponse(result.toMap)
}

Expand All @@ -284,13 +281,13 @@ object ResultEventListener extends Transformer3wResultSupport {

private def init(
extResultDataService: Option[ActorRef]
): Behavior[ResultMessage] = Behaviors.withStash(200) { buffer =>
Behaviors.receive[ResultMessage] {
): Behavior[Request] = Behaviors.withStash(200) { buffer =>
Behaviors.receive[Request] {
case (ctx, SinkResponse(response)) =>
ctx.log.debug("Initialization complete!")
buffer.unstashAll(idle(BaseData(response, extResultDataService)))

case (ctx, Failed(ex)) =>
case (ctx, InitFailed(ex)) =>
ctx.log.error("Unable to setup ResultEventListener.", ex)
Behaviors.stopped

Expand All @@ -301,8 +298,8 @@ object ResultEventListener extends Transformer3wResultSupport {
}
}

private def idle(baseData: BaseData): Behavior[ResultMessage] = Behaviors
.receive[ResultMessage] {
private def idle(baseData: BaseData): Behavior[Request] = Behaviors
.receivePartial[Request] {
case (ctx, ParticipantResultEvent(participantResult)) =>
val updatedBaseData = handleResult(participantResult, baseData, ctx.log)
idle(updatedBaseData)
Expand Down Expand Up @@ -342,17 +339,8 @@ object ResultEventListener extends Transformer3wResultSupport {
val updatedBaseData = handleResult(flexOptionsResult, baseData, ctx.log)
idle(updatedBaseData)

case (ctx, _: StopMessage) =>
ctx.log.debug(
s"${getClass.getSimpleName} received Stop message, shutting down when no message has been received in 5 seconds."
)
ctx.setReceiveTimeout(5.seconds, StopTimeout)
Behaviors.same

case (ctx, StopTimeout) =>
// there have been no messages for 5 seconds, let's end this
ctx.log.debug(s"${getClass.getSimpleName} is now stopped.")
Behaviors.stopped
case (ctx, msg: DelayedStopHelper.StoppingMsg) =>
DelayedStopHelper.handleMsg((ctx, msg))

}
.receiveSignal { case (ctx, PostStop) =>
Expand Down
Loading

0 comments on commit 6b07e15

Please sign in to comment.