1
1
package fr.sncf.osrd.sim.interlocking.impl
2
2
3
3
import fr.sncf.osrd.sim_infra.api.*
4
+ import fr.sncf.osrd.sim.interlocking.api.MovableElementInitPolicy
4
5
import fr.sncf.osrd.sim.interlocking.api.MovableElementSim
5
6
import fr.sncf.osrd.utils.indexing.get
6
7
import kotlinx.coroutines.delay
@@ -10,18 +11,21 @@ import kotlinx.coroutines.flow.update
10
11
import kotlinx.coroutines.sync.Mutex
11
12
12
13
13
- fun movableElementSim (infra : MovableElementsInfra ): MovableElementSim {
14
- return MovableElementSimImpl (infra)
14
+ fun movableElementSim (infra : MovableElementsInfra , initPolicy : MovableElementInitPolicy ): MovableElementSim {
15
+ return MovableElementSimImpl (infra, initPolicy )
15
16
}
16
17
17
- internal class MovableElementSimImpl (private val infra : MovableElementsInfra ) : MovableElementSim {
18
- private val states = infra.movableElements.map { id ->
19
- MutableStateFlow (infra.getMovableElementDefaultConfig(id))
18
+ internal class MovableElementSimImpl (
19
+ private val infra : MovableElementsInfra ,
20
+ private val initPolicy : MovableElementInitPolicy ,
21
+ ) : MovableElementSim {
22
+ private val states: List <MutableStateFlow <MovableElementConfigId ?>> = infra.movableElements.map { id ->
23
+ MutableStateFlow (null )
20
24
}
21
25
22
26
private val locks = infra.movableElements.map { Mutex () }
23
27
24
- override fun watchMovableElement (movable : MovableElementId ): StateFlow <MovableElementConfigId > {
28
+ override fun watchMovableElement (movable : MovableElementId ): StateFlow <MovableElementConfigId ? > {
25
29
return states[movable.index]
26
30
}
27
31
@@ -32,7 +36,10 @@ internal class MovableElementSimImpl(private val infra: MovableElementsInfra) :
32
36
override suspend fun move (movable : MovableElementId , config : MovableElementConfigId ) {
33
37
assert (locks[movable.index].isLocked) { " cannot move a non-locked movable element" }
34
38
states[movable.index].update { prevConfig ->
35
- if (prevConfig != config)
39
+ if (prevConfig == null ) {
40
+ if (initPolicy == MovableElementInitPolicy .PESSIMISTIC )
41
+ delay(infra.getMovableElementDelay(movable))
42
+ } else if (prevConfig != config)
36
43
delay(infra.getMovableElementDelay(movable))
37
44
config
38
45
}
0 commit comments