Skip to content

Commit

Permalink
chore: use custommutablestateflow instead of flow in environment
Browse files Browse the repository at this point in the history
  • Loading branch information
giacomoaccursi committed Jan 27, 2024
1 parent c678b57 commit 6043148
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 44 deletions.
17 changes: 6 additions & 11 deletions src/main/kotlin/entity/Environment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

package entity

import kotlinx.coroutines.flow.Flow
import flow.CustomMutableStateFlow

/**
* The environment of the simulation.
Expand All @@ -17,29 +17,29 @@ interface Environment {
/**
* The list of the nodes in the environment.
*/
val nodes: Flow<List<Node>>
val nodes: CustomMutableStateFlow<List<Node>>

/**
* The list of neighborhoods.
*/
val neighborhoods: Flow<Map<Int, Neighborhood>>
val neighborhoods: CustomMutableStateFlow<Map<Int, Neighborhood>>

/**
* Maps every node with its position.
*/
val nodesToPosition: Flow<Map<Int, Position>>
val nodesToPosition: CustomMutableStateFlow<Map<Int, Position>>

/**
* Add a Node to the environment.
* @param node the node to add.
*/
fun addNode(node: Node, position: Position)
suspend fun addNode(node: Node, position: Position)

/**
* Remove node from the environment.
* @param node the node to remove
*/
fun removeNode(node: Node)
suspend fun removeNode(node: Node)

/**
* Moves node in a new position.
Expand Down Expand Up @@ -69,11 +69,6 @@ interface Environment {
*/
fun getAllNodes(): List<Node>

/**
* @return a flow for the neighbors of a node.
*/
fun neighbors(node: Node): Flow<Set<Node>>

/**
* Updates all neighborhoods.
* @param neighborhoods all new neighborhoods.
Expand Down
49 changes: 16 additions & 33 deletions src/main/kotlin/entity/EnvironmentImpl.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,58 +8,41 @@

package entity

import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.flow.Flow
import flow.CustomMutableStateFlow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.mapLatest

/**
* An implementation of the environment.
*/
class EnvironmentImpl :
Environment {

override val nodes: MutableStateFlow<List<Node>> = MutableStateFlow(emptyList())
override val neighborhoods: MutableStateFlow<Map<Int, Neighborhood>> = MutableStateFlow(emptyMap())
override val nodesToPosition: MutableStateFlow<Map<Int, Position>> = MutableStateFlow(emptyMap())
class EnvironmentImpl : Environment {
override val nodes: CustomMutableStateFlow<List<Node>> =
CustomMutableStateFlow(MutableStateFlow(emptyList()))
override val neighborhoods: CustomMutableStateFlow<Map<Int, Neighborhood>> =
CustomMutableStateFlow(MutableStateFlow(emptyMap()))
override val nodesToPosition: CustomMutableStateFlow<Map<Int, Position>> =
CustomMutableStateFlow(MutableStateFlow(emptyMap()))
private lateinit var linkingRule: LinkingRule

override fun addNode(node: Node, position: Position) {
nodes.value += node
nodesToPosition.value += Pair(node.id, position)
// updateNeighborhood(node)
}

@OptIn(ExperimentalCoroutinesApi::class)
override fun neighbors(node: Node): Flow<Set<Node>> {
return neighborhoods.mapLatest {
it[node.id]?.neighbors ?: emptySet()
}
override suspend fun addNode(node: Node, position: Position) {
nodes.emit(nodes.value + node)
nodesToPosition.emit(nodesToPosition.value + Pair(node.id, position))
}

override suspend fun updateNeighborhoods(neighborhoods: Map<Int, Neighborhood>) {
this.neighborhoods.value = neighborhoods
this.neighborhoods.emit(neighborhoods)
}

override fun setLinkingRule(linkingRule: LinkingRule) {
this.linkingRule = linkingRule
}

override fun removeNode(node: Node) {
nodes.value -= node
nodesToPosition.value = nodesToPosition.value.minus(node.id)
/*neighborhoods.value[node.id]?.neighbors?.forEach {
val nodeNeighborhood = getNeighborhood(it)
if (nodeNeighborhood != null) {
neighborhoods.value += Pair(it.id, nodeNeighborhood.removeNeighbor(node))
}
}
neighborhoods.value = neighborhoods.value.minus(node.id)*/
override suspend fun removeNode(node: Node) {
nodes.emit(nodes.value - node)
nodesToPosition.emit(nodesToPosition.value.minus(node.id))
}

override suspend fun moveNode(node: Node, position: Position) {
nodesToPosition.value += Pair(node.id, position)
// updateNeighborhood(node)
nodesToPosition.emit(nodesToPosition.value + Pair(node.id, position))
}

override fun getNodePosition(node: Node): Position {
Expand Down

0 comments on commit 6043148

Please sign in to comment.