I'm looking for a bit of help/inspiration on how I would better go about traversing a json decision tree while attempting to force a specific path.
Here's an example of the decision tree in which you can see that there are flags for a given answer which is what i would use to force a path.
{
"questions": {
"1": {
"question_code": "1",
"answers": {
"1": {
"next_question": "2",
"flags": "highest priority flag"
},
"2": {
"next_question": "3",
"flags": "medium priority flag"
}
}
},
"2": {
"question_code": "2",
"answers": {
"3": {
"next_question": "4",
"flags": "highest priority flag"
},
"4": {
"next_question": "4",
"flags": "second priority flag"
}
}
},
"3": {
"question_code": "3",
"answers": {
"3": {
"next_question": "4",
"flags": "lowest priority flag"
},
"4": {
"next_question": "4",
"flags": "no flag"
}
}
},
"4": {
"question_code": "4",
"answers": {
"3": {
"next_question": "END",
"flags": "medium priority flag"
},
"4": {
"next_question": "END",
"flags": "highest priority flag"
}
}
}
}
}
Here's an example of what the flag order is:
export const flag_priority_order = [
'highest priority flag',
'second priority flag',
'medium priority flag',
'lowest priority flag',
'no flag',
]
The flags are used to trigger certain events that occur at the end of the decision tree. If I wanted to trigger the 'medium' priority flag at the end of the decision tree I would only be able to select answers that have that flag or a lower priority.
This is because at the end of the decision tree the highest flag triggered is used to determine the final trigger.
What Ive tried so far with limited success is a series of loops and it doesn't feel like the most efficient way of doing this at all.
Essentially the first loop looks at the flags of the current question and checks which answers have the target flag or lower. It then passes those to a second loop which runs the same check again on the next set of potential answers.
If it finds a valid path (2 levels deep) it then selects the appropriate answers and continues in this vain.
The trouble with this solution is it only works part of the time, as going only 2 levels deep means it can end up in a situation where the target path is not reachable.
Bearing in mind the decision tree is well over 200 questions long or more.
The decision tree design or priority order cannot be changed and is set in stone so I just have to work with what I have.
What I'm looking for is some help in spidering through this decision tree in a better and deeper way to find the path that i'm trying to force or to see if that would lead to a dead end.