I've been working on a dynamic question-and-answers system, but I'm having trouble creating a efficient AND flexible design for this system. I'd love to know if there's an established design pattern or any recommendations for designing this system.
What I'm trying to do
I have a set of questions. After answering them, another set of questions are shown, depending on the answers to the previous set. This repeats, until no more questions are needed.
The question answers are all boolean, multiple-choice, or numeric.
The important part is that most questions are only shown when a specific set of criteria is met, based on the previous answers.
I need the criteria to support mainly boolean logic, such as And, Or, Not, Equals, Greater Than, and Less Than.
For example, let's say I have already received answers to questions such as
One of the next questions is
In School?, but it should ONLY be displayed if:
Age < 30 AND Gender=Male AND (State = CA OR State = NY)
Has anyone heard of a similar design pattern? How would you approach this design?
I tried Database columns
At first, we only had 3 initial questions, so we just used 3 columns to filter the second set of questions.
However, our business needs grew and we started needing more initial questions, added more columns, and put more logic within those filters.
This quickly became too rigid and cumbersome.
I tried a Logic Interpreter
This worked pretty well for flexibility, but retrieving thousands of rows from the database and interpreting the scripts was extremely inefficient and performed too poorly for production.
I tried a Hybrid
We finally combined the two approaches, and came up with something feasable.
This hybrid system still has many drawbacks:
I'd really like to hear suggestions on how to improve this design.
We had to do something similar in the past for a medical system and due to its complexity, we resorted to reuse the rule engine that support multi-classification decision tree. I remember that I came across a nice simple design about this and managed to dig out the link.
The design is loosely coupled from the data storage, so making it easy to fit into your existing solution design.