KStateMachine / kstatemachine

KStateMachine is a Kotlin DSL library for creating state machines and statecharts.
https://kstatemachine.github.io/kstatemachine/
Boost Software License 1.0
339 stars 19 forks source link

State, transition names #89

Closed oyanyev closed 4 months ago

oyanyev commented 5 months ago

Hi again!

I created a lot of states as objects and there is a lot of transition between them. There are a lot of use cases and I prefer not to reuse states between them and create a dedicated states even if they mean same, but scope is different. The state machine is complicated and is split into parts, each of them can be exported into uml.

To have a beautiful descriptive diagram, I heavily use names for states and transitions. And these names cannot be very long to fit diagram.

And here comes a problem when names are same and state machine won't start because of that. The simplest workaround is to add spaces to names, each time different number of them ))

Can states, transitions be somehow compared not by names? May be with ===

nsk90 commented 5 months ago

I dont think it is safe to relax "unique name" restriction. APIs like findState(name: String, ...) if based on it.

But possibly we can upgrade exportToPlantUml() function to accept converters/beautifiers for names. Converters/beautifiers are functions that take original state/transition/machine and return printable name.

So if your state names follow some schema like Block1.Group1.State1 you can simply drop the path and leave only State1 as printable name.

What do you think about it?

oyanyev commented 5 months ago

I have another idea: displayName or umlName which can be defaulted to name, but can be overriden and will be used solely in umls

nsk90 commented 5 months ago

yes, this is also possible, but this requires to change many APIs that are not related to the export feature.

What about plantUml itself, how it handles non unique names? I suppose it can have a problems with transitions directions?

oyanyev commented 5 months ago

PlantUml accepts titles for entities: state InvalidBarcode as "Title"

Screenshot 2024-02-01 at 15 31 51

transition.displayName is converted to arrow description without any problem

oyanyev commented 5 months ago

I can create PR, if you do not see that it might have side effects

nsk90 commented 5 months ago

looks this issue is related to https://github.com/nsk90/kstatemachine/issues/82

Some kind of meta data objects should cover both cases. I do not see any side effects, yes you can open a PR, thank you.

oyanyev commented 5 months ago

Started with PR and already have one question.

internal class CheckUniqueNamesVisitor : RecursiveVisitor {
   ...
    private val transitionNames = mutableSetOf<String>()

   ...

    override fun <E : Event> visit(transition: Transition<E>) {
        transition.name?.let { check(transitionNames.add(it)) { "Transition name is not unique: $it" } }
    }
}

transition.name visitor has no sense so far. Name is used only there and in export to uml. Can this check be omitted? If check not applicable, then name can be not unique and can be safely used for uml labels

nsk90 commented 5 months ago

it is also used here, so the name behaviour should not change.

/**
 * Find transition by name. This might be used to start listening to transition after state machine setup.
 */
fun TransitionStateApi.findTransition(name: String) = transitions.find { it.name == name }
ayanyev commented 5 months ago

Added PR from my personal Github account. Should not break any existing code. https://github.com/nsk90/kstatemachine/pull/90

Published to local maven, added to my project - works.

nsk90 commented 4 months ago

Done in https://github.com/nsk90/kstatemachine/releases/tag/v0.27.0 Thanks, for the contribution!