如何将分支逻辑持久化到数据库中?

发布于 2024-10-22 02:38:33 字数 377 浏览 1 评论 0原文

我们正在构建一个供内部使用的调查引擎。我想知道如何将问题分支逻辑持久化到数据库中?任何机构之前做过这件事或者对数据库模式有什么想法吗?

如果用户给出答案,我们需要根据添加到问题的逻辑跳到下一个问题。每个问题可以添加多个逻辑。

例如:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

幸福%小乖 2024-10-29 02:38:33

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

编辑:一个简单的设计可以是两个表:问题和边缘。问题只有 id问题文本。边可以是 answered_question_idnext_question_idanswer。第一个表是不言自明的。第二个表列出,如果提出问题 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 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文