如何将分支逻辑持久化到数据库中?
我们正在构建一个供内部使用的调查引擎。我想知道如何将问题分支逻辑持久化到数据库中?任何机构之前做过这件事或者对数据库模式有什么想法吗?
如果用户给出答案,我们需要根据添加到问题的逻辑跳到下一个问题。每个问题可以添加多个逻辑。
例如:
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)
{
}
如何将上述内容持久保存到数据库并从那里开始?
有什么好主意吗?
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 ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您本质上是希望将决策树保存到数据库中。您希望将每个问题存储为一个节点,并且为了规范化数据库的利益,将边存储在与依赖于其他问题(有向边)的问题相关的单独表中,并根据需要行走。
编辑:一个简单的设计可以是两个表:问题和边缘。问题只有
id
和问题文本
。边可以是answered_question_id
、next_question_id
和answer
。第一个表是不言自明的。第二个表列出,如果提出问题answered_question_id
,并且回答的内容等于或匹配answer
,则转到下一个问题next_question_id
。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
andquestion text
. Edges can beanswered_question_id
,next_question_id
, andanswer
. The first table is self-explanatory. The second table lists that if a questionanswered_question_id
is asked and answered with something that either equals to or matchesanswer
, forward to questionnext_question_id
next.