7

data flow example

I have been trying to find a proper way to represent the above data flow (this is just a small case), and I'm not sure if I found a good way to do it. Our current implementation just evaluate all the previous data like: (if user.isMarried && user.age > 23 & ....), so is very hard to maintain.

Basically, what I need is a way to calculate the next step based on current input and previous conditions. I've been thinking about a finite state machine, but my main issue is that it will not only depend on the input, but previous data already answered.

It also came to my mind using something like a decision tree, but I can't represent it properly. Any light on the subject would be really appreciated.

Note: Check that the blank questions doesn't depend on the input, but on previous answers (married or not).

Doc Brown
  • 199,015
  • 33
  • 367
  • 565
  • 1
    is the number of pre-conditions large? if there aren't too many, an FSM when you can mark subsets of those pre-conditions as required for a transition might not be so bad – GoatInTheMachine Feb 21 '17 at 16:54
  • @GoatInTheMachine, indeed, they should not be too large, but they will be repeated, I think the key should be more about grouping those conditions so they can be reused – Tomas Ramirez Sarduy Feb 21 '17 at 17:00
  • I think the key here is simply to use re-usable predicates to match the next question. Such predicates could take the form of simple functions that returns booleans, e.g. `boolean isMarried(survey);`. – plalx Feb 21 '17 at 18:43
  • Maybe the problem is not a good fit for an imperative language, this seems something you could better represent with something like [PDDL](http://www.cs.toronto.edu/~sheila/2542/s14/A1/introtopddl2.pdf) or maybe [PROLOG](http://www.cs.toronto.edu/~sheila/384/w11/simple-prolog-examples.html). – Sergio Basurco Feb 21 '17 at 21:30

1 Answers1

1

One solution is a state machine, because in a state machine even if you process one entry at a time, you are on a different state based on the previous set of actions. This is also especially useful if you have cycles. Actually, the flowchart diagram IS a state machine. Follow it with a pen, the box you are currently in is based on what happened before, the box in which you are going to get depends on the input AND on where you are coming from (i.e. what happened before).

The problem is managing the states and their transitions, which is hard to do in an imperative language.

Prolog has been suggested in comments, which can help a lot in your case, since it has been designed at least partially for handling complex rules. It's nice because it's efficient for this kind of task, can evaluate lots of rules very fast and you can reuse your rules. The downside is that, at least from my experience, it required quite a lot of brain rewiring to learn it if you're not used to the paradigm.

Paul92
  • 2,571
  • 15
  • 17
  • Thanks for your answer, the only problem I wanted to avoid is to have repeated states. Since they will depend sometimes on previous conditions, there can be previous paths very similar. Not sure if it's clear what I'm trying to say – Tomas Ramirez Sarduy Mar 01 '17 at 18:37
  • Well, you can always go back to the same state – Paul92 Mar 01 '17 at 19:24