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).

  • 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 – Tomás Ramírez 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 or maybe PROLOG. – Sergio Basurco Feb 21 '17 at 21:30
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.

|improve this answer|||||
  • 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 – Tomás Ramírez Mar 1 '17 at 18:37
  • Well, you can always go back to the same state – Paul92 Mar 1 '17 at 19:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.