it.unibo.alchemist.boundary.graphql.monitor.EnvironmentSubscriptionMonitor.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of alchemist-graphql-surrogates Show documentation
Show all versions of alchemist-graphql-surrogates Show documentation
GraphQL surrogates API for Alchemist
/*
* Copyright (C) 2010-2023, Danilo Pianini and contributors
* listed, for each module, in the respective subproject's build.gradle.kts file.
*
* This file is part of Alchemist, and is distributed under the terms of the
* GNU General Public License, with a linking exception,
* as described in the file LICENSE in the Alchemist distribution's top directory.
*/
package it.unibo.alchemist.boundary.graphql.monitor
import it.unibo.alchemist.boundary.OutputMonitor
import it.unibo.alchemist.boundary.graphql.schema.model.surrogates.EnvironmentSurrogate
import it.unibo.alchemist.boundary.graphql.schema.model.surrogates.toGraphQLEnvironmentSurrogate
import it.unibo.alchemist.model.Actionable
import it.unibo.alchemist.model.Environment
import it.unibo.alchemist.model.Position
import it.unibo.alchemist.model.Time
import kotlinx.coroutines.channels.BufferOverflow
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableSharedFlow
/**
* An [OutputMonitor] that emits a new [Environment] as a [EnvironmentSurrogate]
* each time the [OutputMonitor.stepDone] function is called.
*
* @param T the concentration type
* @param P the position
*/
class EnvironmentSubscriptionMonitor> : OutputMonitor {
private val internalFlow = MutableSharedFlow>(
replay = 1,
onBufferOverflow = BufferOverflow.DROP_OLDEST,
)
/**
* Returns a [Flow] that emits a new [EnvironmentSurrogate] each time the
* [OutputMonitor.stepDone] function is called.
*/
val eventFlow: Flow> get() = internalFlow
override fun stepDone(environment: Environment, reaction: Actionable?, time: Time, step: Long) {
internalFlow.tryEmit(environment.toGraphQLEnvironmentSurrogate())
}
override fun finished(environment: Environment, time: Time, step: Long) {
this.stepDone(environment, null, time, step)
}
override fun initialized(environment: Environment) {
this.stepDone(environment, null, Time.ZERO, 0L)
}
}