如何将分支逻辑持久化到数据库中? [英] How to persist branching logic into database?

查看:90
本文介绍了如何将分支逻辑持久化到数据库中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在构建一个供内部使用的调查引擎.我想知道如何将问题分支逻辑持久化到数据库中?是否有任何机构在此之前完成过此工作,或者对数据库的架构有何想法?

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.

例如:

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?

有什么好主意吗?

推荐答案

您实质上是希望保留决策树到数据库中.您希望将每个问题存储为一个节点,并且为了规范化数据库的利益,将边存储在与其他依赖于其他问题的问题相关的单独表中(有向边),并适当走动.

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.

一个简单的设计可以是两个表:Questions和Edges.问题仅包含idquestion text.边可以是answered_question_idnext_question_idanswer.第一个表是不言自明的.第二张表列出了,如果问和回答answered_question_id且答案等于或匹配answer,则接下来继续问问题next_question_id.

A simple design can be two tables: Questions and Edges. Questions just has id and question text. Edges can be answered_question_id, next_question_id, and 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.

这篇关于如何将分支逻辑持久化到数据库中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆