-
-
Notifications
You must be signed in to change notification settings - Fork 22
/
GuardedTransitionSample.kt
45 lines (36 loc) · 1.19 KB
/
GuardedTransitionSample.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
/*
* Author: Mikhail Fedotov
* Github: https://github.com/KStateMachine
* Copyright (c) 2024.
* All rights reserved.
*/
package ru.nsk.samples
import kotlinx.coroutines.runBlocking
import ru.nsk.kstatemachine.event.Event
import ru.nsk.kstatemachine.state.*
import ru.nsk.kstatemachine.statemachine.StateMachine
import ru.nsk.kstatemachine.statemachine.createStateMachine
import ru.nsk.samples.GuardedTransitionSample.States.State1
import ru.nsk.samples.GuardedTransitionSample.States.State2
import ru.nsk.samples.GuardedTransitionSample.SwitchEvent
private object GuardedTransitionSample {
object SwitchEvent : Event
sealed class States : DefaultState() {
class State1(val value: Int) : States()
object State2 : States(), FinalState
}
}
fun main() = runBlocking {
val machine = createStateMachine(this) {
logger = StateMachine.Logger { println(it()) }
addInitialState(State1(42)) {
transition<SwitchEvent> {
guard = { [email protected] > 10 }
targetState = State2
}
}
addFinalState(State2)
}
machine.processEvent(SwitchEvent)
check(State2 in machine.activeStates())
}