[英]How to persist branching logic into database?
We are building a survey engine for our internal use. 我们正在为内部使用构建调查引擎。 I would like to know how to persist the question branching logic into the database?
我想知道如何将问题分支逻辑保存到数据库中? Any body done this before or any ideas on the schema for the database?
以前做过这个的任何机构还是对数据库架构的任何想法?
If the user responses with an answer, we need to skip to the next questions based on the logic added to the questions Each question can have multiple logic added to it. 如果用户回答了答案,我们需要根据添加到问题中的逻辑跳过下一个问题。每个问题都可以添加多个逻辑。
For eg: 例如:
Question: Is it Sunny, Raining or Cloudy?
Answer: Raining.
The next question should be based on the previous answer.
if(Raining)
{
}
if(Sunny)
{
}
if(Cloudy)
{
}
how do I persist the above to the database and go from there? 我如何将上述内容保存到数据库并从那里开始?
Any bright ideas ? 有什么好主意吗?
You're essentially looking to persist a decision tree into a database. 您基本上希望将决策树保存到数据库中。 You'd want to store each question as a node and, in the interests of a normalized database, store the edges in a separate table relating questions that depend on other questions (directed edges), and walk as appropriate.
您希望将每个问题存储为一个节点,并且为了规范化数据库的利益,将边缘存储在一个单独的表中,该表关联依赖于其他问题(有向边)的问题,并在适当时走路。
Edit: A simple design can be two tables: Questions and Edges. 编辑:一个简单的设计可以是两个表:问题和边缘。 Questions just has
id
and question text
. 问题只有
id
和question text
。 Edges can be answered_question_id
, next_question_id
, and answer
. 边缘可以
answered_question_id
, next_question_id
和answer
。 The first table is self-explanatory. 第一个表格是不言自明的。 The second table lists that if a question
answered_question_id
is asked and answered with something that either equals to or matches answer
, forward to question next_question_id
next. 第二个表列出,如果一个问题
answered_question_id
被问及的东西,要么等于或匹配回答answer
,期待质疑next_question_id
未来。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.