Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is a statechart / finite state machine suitable for modelling a questionnaire

enter image description here

I'd like model the above questionnaire which I believe is a directed acyclic graph.

The two libraries I've been looking at are:

  • https://github.com/davidkpiano/xstate
  • https://github.com/jbeard4/SCION-CORE

A couple of the issues I have are:

  1. The questionnaire relies on previous states e.g. the answers to previous questions are used to transition to another state (question). Am I right in thinking that "external state" could solve this problem?

  2. If I'm at Q6 and I want to transition to the previous question, then depending on the previous answers, this could be either Q1, Q4, or Q5. I think I could use a stack to push each state as the questionnaire progresses and then pop to get back to a previous state.

Does this all sound feasible or is there a better way to model this problem?

like image 664
Stephen S Avatar asked Nov 08 '22 03:11

Stephen S


1 Answers

A solution to this problem can be modelized by an extended state machine. You don't necessarily need a hierarchical one. The states and transitions you show can be addressed with a regular state machine, but the 'memory' part is properly addressed with an extended state machine.

In short, your graph remains the same, with the addition of updates to the extended state on each transition. For instance your extended state could be {A1, A2. ..., A6, H}, where Ax stands for the answer to Qx, and H is the history of states the machine has gone through. When you transition from Q1 to Q2, you also update A1 to the answer, and H to [A1]. You do that for all Qs. Arriving on Q6 you have all the information you need to write a guard deciding where to transition based on the history of previous states, and answers.

So, to summarize an answer to your question : yes, external state can be used profitably, and in that case, a stack holding the state history would solve your problem, as you intuited. You can also use a statechart library, of course, as statecharts generalize extended state machines.

As a add-on, I include also my own extended state machine library to the excellent ones you already mentioned : https://github.com/brucou/state-transducer.

You will find there a demo which is very similar to the problem you just described. It is also about a multi-step questionnaire, with the complication that it included error paths.

like image 188
user3743222 Avatar answered Dec 07 '22 11:12

user3743222