SimonGAndrews / xstate-fsmPlus-Espruino

Fork of the FSM finite state machine package in the XState library from STATELY Ai, to enable the FSM to be run as a module within the Espruino JavaScript Interpreter for Microcontrollers. Providing enhancements to enable some basic StateChart features to be supported (eg nested states).
MIT License
0 stars 0 forks source link

Hierarchical State Nodes - state.value to include substates #12

Open SimonGAndrews opened 2 years ago

SimonGAndrews commented 2 years ago

Within an enhancement to add Hierarchical State Nodes to xstate/fsm there is a need to represent the machine state 'value' property in a way than supports Hierarchical state definitions.

In xstate/fsm the 'value' property of a state is a simply string containing the name of the state. eg image

In order to support Hierarchical nodes the 'value' property will need to include substates from the machine root state to any given state / substate ie represent a statenode.

The 'value' property has at least two use cases in xstate/fsm:

The scope of this issue is to determine the format of 'value' , provide supporting handling functions and to implement it in the transition function.

SimonGAndrews commented 2 years ago

The format of state value in fsmPlus could be one of a number of options.

At the risk of not migrating xstate functionality to fsm-plus in line with the xstate conventions, the xstate full path id convention would seem to

Hence the initial version of fsmPlus will use delimited strings for the value property as in "chassie" , "chassie.axel' or 'chassie.engine.block' .

SimonGAndrews commented 2 years ago

the following code provide the means to return a state config from a state value reference in the full path id convention

// fsmPlus - returns the statenode config object (as defined in fsmConfig) from a given full state ID
// assumes a correctly formatted full state id which exists in the machine
function getStateFromID(id, fsmConfig) {
  if (id == null) return null
  path = 'states.' + id.slice(id.indexOf('.') + 1).split('.').join('.states.');
  return path.split('.').reduce((prev, curr) => prev && prev[curr], fsmConfig);
}

stackoverflow reference

SimonGAndrews commented 1 year ago

unit test stateMap.test.js is used to test this functionality.