-
-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
142 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
samples/src/commonMain/kotlin/ru/nsk/samples/PlantUmlUnsafeExportWithExportMetaInfoSample.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/* | ||
* 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.metainfo.ExportMetaInfo | ||
import ru.nsk.kstatemachine.metainfo.buildExportMetaInfo | ||
import ru.nsk.kstatemachine.state.State | ||
import ru.nsk.kstatemachine.state.finalState | ||
import ru.nsk.kstatemachine.state.initialState | ||
import ru.nsk.kstatemachine.state.transitionOn | ||
import ru.nsk.kstatemachine.statemachine.createStateMachine | ||
import ru.nsk.kstatemachine.visitors.export.exportToPlantUml | ||
import ru.nsk.samples.PlantUmlUnsafeExportWithExportMetaInfoSample.FirstEvent | ||
import ru.nsk.samples.PlantUmlUnsafeExportWithExportMetaInfoSample.SecondEvent | ||
|
||
private object PlantUmlUnsafeExportWithExportMetaInfoSample { | ||
class FirstEvent(val data: Int) : Event | ||
class SecondEvent(val data: Int) : Event | ||
} | ||
|
||
/** | ||
* The sample shows how to use [ExportMetaInfo] to get complete export output even with conditional lambdas. | ||
* You have EventAndArgumentResolutionHint and EventAndArgumentResolutionHint alternative, you can choose one of them, | ||
* or use them together mixing in a single metaInfo if necessary. | ||
*/ | ||
fun main() = runBlocking { | ||
lateinit var state2: State | ||
lateinit var state3: State | ||
val machine = createStateMachine(this) { | ||
state2 = finalState("State2") | ||
state3 = finalState("State3") | ||
|
||
initialState("State1") { | ||
transitionOn<FirstEvent> { | ||
metaInfo = buildExportMetaInfo { | ||
// using EventAndArgumentResolutionHint calls targetState lambda with specified events | ||
addEventAndArgumentResolutionHint("data == 42", FirstEvent(42)) | ||
addEventAndArgumentResolutionHint("else", FirstEvent(42)) | ||
} | ||
targetState = { if (event.data == 42) state2 else state3 } | ||
} | ||
|
||
transitionOn<SecondEvent> { | ||
metaInfo = buildExportMetaInfo { | ||
// using StateResolutionHint does not require targetState lambda call | ||
addStateResolutionHint("data == 123", state2) | ||
addStateResolutionHint("else", state3) | ||
} | ||
targetState = { if (event.data == 123) state2 else state3 } | ||
} | ||
} | ||
} | ||
|
||
println(machine.exportToPlantUml(showEventLabels = true, unsafeCallConditionalLambdas = true)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters