-
-
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.
Add CheckMetaInfoStructureVisitor to verify correct MetaInfo structur…
…e on StateMachine startup
- Loading branch information
Showing
8 changed files
with
140 additions
and
30 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
50 changes: 50 additions & 0 deletions
50
...hine/src/commonMain/kotlin/ru/nsk/kstatemachine/visitors/CheckMetaInfoStructureVisitor.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,50 @@ | ||
/* | ||
* Author: Mikhail Fedotov | ||
* Github: https://github.com/KStateMachine | ||
* Copyright (c) 2024. | ||
* All rights reserved. | ||
*/ | ||
|
||
package ru.nsk.kstatemachine.visitors | ||
|
||
import ru.nsk.kstatemachine.event.Event | ||
import ru.nsk.kstatemachine.metainfo.CompositeMetaInfo | ||
import ru.nsk.kstatemachine.metainfo.MetaInfo | ||
import ru.nsk.kstatemachine.state.IState | ||
import ru.nsk.kstatemachine.statemachine.StateMachine | ||
import ru.nsk.kstatemachine.transition.Transition | ||
|
||
/** | ||
* Checks that [MetaInfo] is correctly structured. | ||
* [CompositeMetaInfo] is not nested into each other. | ||
* Certain [MetaInfo] subclasses are applied only once. | ||
*/ | ||
internal class CheckMetaInfoStructureVisitor : RecursiveVisitor { | ||
override fun visit(machine: StateMachine) { | ||
machine.metaInfo?.checkStructure() | ||
machine.visitChildren() | ||
} | ||
|
||
override fun visit(state: IState) { | ||
state.metaInfo?.checkStructure() | ||
|
||
if (state !is StateMachine) // do not check nested machines | ||
state.visitChildren() | ||
} | ||
|
||
override fun <E : Event> visit(transition: Transition<E>) { | ||
transition.metaInfo?.checkStructure() | ||
} | ||
|
||
private fun MetaInfo.checkStructure() { | ||
if (this is CompositeMetaInfo) { | ||
val groups = metaInfoSet.groupBy { | ||
check(it !is CompositeMetaInfo) { "CompositeMetaInfo cannot nest each other" } | ||
it::class | ||
} | ||
groups.entries.forEach { | ||
check(it.value.size == 1) { "MetaInfo ${it.key} is repeated more than once" } | ||
} | ||
} | ||
} | ||
} |
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