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

Initial Choice State as Nested State of Data State #101

Open BeAddicted opened 1 month ago

BeAddicted commented 1 month ago

So there seems to be an issue with the combination of DataStates and Choice States.

I have a DataState that contains a nested choice state as initial state:

object MyDataState : DefaultDataState<Data>

addState(MyDataState) {
    initialChoiceState {
        if(MyDataState.data ...) {
             State1   
        }
        else {
             State2
        }
    }
}

I transition to MyDataState via a dataTransition. There seem to be two issues here. I cannot acces data from MyDataState here, since the transition to MyDataState seems not to be complete. (When accessing the active states inside the initialChoiceState-block I can see that the previous state is still active (not MyDataState)) Exception: java.lang.IllegalStateException: Data is not set. Is MyDataState($38827186) state active?

I can acces the data via the event that is an argument to the initialChoiceState, but there I need to assume the actual event type. Not really the best solution. But even if I do another problem occures. I then get the following exception:

java.lang.IllegalStateException: Last data is not available yet in MyDataState($38827186), and default data not provided.

Somehow as soon as I use an initialChoiceState it won't set the data of the dataState anymore? Not really sure what exactly is happening here. But If I remove the choice state and use another state the problem does not occure.

Any idea what is going wrong here?

nsk90 commented 1 month ago

Hi! The quick reply is: Here is a code which moves the data from DataEvent to a DataState https://github.com/KStateMachine/kstatemachine/blob/master/kstatemachine/src/commonMain/kotlin/ru/nsk/kstatemachine/state/DefaultDataState.kt#L32 It is triggered in a moment of DataState activation. In a sample that you have provided, the DataState contains initialChoiceState. This means that DataState is never activated. The ChoiceState is calculated before state activation, and the StateMachine goes directly to resulting state. Imagine there are many ChoiceState redirections one by one. The library calculates resulting state path passing through all of ChoiceStates and activates states only on the last resulting destination (path). All intermediate state paths that were calculated during redirections are not activated. So it looks that you cannot access data from DateState in such case and butter to look directly into Event property.

The long reply: I have to check this in runtime, this will take a while)

nsk90 commented 1 month ago

But even if I do another problem occures. I then get the following exception: java.lang.IllegalStateException: Last data is not available yet in MyDataState($38827186), and default data not provided.

I can not say why this happens, the code sample is provided for another case. You can provide another sample for this case, so I can see where the data is read from the DataState. Maybe If you read the data from Event there is no need to read it from the DataState? (and even use DataState api)

BeAddicted commented 1 month ago

Based in your first explanation I think the second problem seems to correlate. Both problems say that the data is not set even tough the state should be active. In the first case I activly try to get the data and run into an exception. In the second the statemachone itself seems to detect that there ist no data when transition into the state.

The code sample for this is pretty similar.

object MyDataState : DefaultDataState<Data>

addState(MyDataState) {
    initialChoiceState {
        if(true) {
             State1   
        }
        else {
             State2
        }
    }
}

Just dont use the Data in the code sample and make something like this (Code makes no sense but should trigger the problem, not at home to test this but pretty sure that was it). I belive what happens ist that the state ist still triggered after the choice state but without the data event? Not really sure about that.