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

Add support for multiple transition targets when using parallel states #53

Closed dimsuz closed 4 months ago

dimsuz commented 1 year ago

If I'm not mistaken, this feature is currently missing from kstatemachine.

I'll use existing statechart implementations as an example (again).

  1. In xstate.js having this state machine:

    {
    id: 'file',
    type: 'parallel',
    states: {
    upload: {
      initial: 'idle',
      states: {
        idle: {
          on: {
            INIT_UPLOAD: { target: 'pending' }
          }
        },
        pending: {
          on: {
            // NOTE multiple targets here
            UPLOAD_COMPLETE: { target: { upload: 'idle', download: 'pending' } }
          }
        },
        success: {}
      }
    },
    download: {
      initial: 'idle',
      states: {
        idle: {
          on: {
            INIT_DOWNLOAD: { target: 'pending' }
          }
        },
        pending: {
          on: {
            DOWNLOAD_COMPLETE: { target: 'success' }
          }
        },
        success: {}
      }
    }
    }
    }

    (sample transition above which has multiple targets might have a slight syntax or target specification errors, but it should work something like that, I tried before :))

  2. In SCXML:

    <scxml>
    <parallel id="ButtonActivity">
    <state id="Button">
      <state id="BtnOff">
        <!-- NOTE here multiple targets states are specified separated by space -->
        <transition cond="_event.data == 1" event="click" target="Button.BtnOn StateShape2"/>
      </state>
      <state id="BtnOn">
      </state>
    </state>
    <state id="StateShape1"/>
    <state id="StateShape2">
      <transition event="on.released" target="StateShape3"/>
    </state>
    <state id="StateShape3">
      <transition event="on.released" target="StateShape4"/>
    </state>
    </parallel>
    </scxml>

    It would be very nice to have something like this in kstatemachine.

nsk90 commented 4 months ago

Done in https://github.com/nsk90/kstatemachine/releases/tag/v0.26.0