提交代码
This commit is contained in:
parent
ee23da58dd
commit
2d3036ca6c
|
@ -6,7 +6,7 @@
|
||||||
<GradleProjectSettings>
|
<GradleProjectSettings>
|
||||||
<option name="distributionType" value="LOCAL" />
|
<option name="distributionType" value="LOCAL" />
|
||||||
<option name="externalProjectPath" value="$PROJECT_DIR$" />
|
<option name="externalProjectPath" value="$PROJECT_DIR$" />
|
||||||
<option name="gradleHome" value="C:/develop/gradle-8.8" />
|
<option name="gradleHome" value="$PROJECT_DIR$/../../develop/gradle-8.5-bin/gradle-8.5" />
|
||||||
<option name="modules">
|
<option name="modules">
|
||||||
<set>
|
<set>
|
||||||
<option value="$PROJECT_DIR$" />
|
<option value="$PROJECT_DIR$" />
|
||||||
|
|
|
@ -1,13 +1,128 @@
|
||||||
package org.echo.questionnaire
|
package org.echo.questionnaire
|
||||||
|
|
||||||
|
import kotlin.math.abs
|
||||||
|
|
||||||
class Questionnaire(
|
class Questionnaire(
|
||||||
val title: String, // 问卷的标题
|
val title: String, // 问卷的标题
|
||||||
val description: String, // 问卷的描述或说明
|
val description: String, // 问卷的描述或说明
|
||||||
|
val questions: List<SurveyQuestion>,
|
||||||
val questions: List<SurveyQuestion>, // 问卷中的所有问题
|
val sample: SurveyBackgroundSample// 问卷中的所有问题
|
||||||
val surveySample: SurveySample //调查样本的背景信息
|
|
||||||
)
|
)
|
||||||
|
|
||||||
class SurveyQuestion(
|
open class Question(
|
||||||
|
val questionId: String,
|
||||||
|
val title: String,
|
||||||
|
val content: String,
|
||||||
|
val optionList: List<QuestionOption>,
|
||||||
|
val questionType: QuestionType, // 表示问题类型:单选、多选、开放式等
|
||||||
|
val order: Int, // 表示问题顺序
|
||||||
|
val isRequired: Boolean = true // 是否必答,默认为必答
|
||||||
|
) {
|
||||||
|
fun getOptionList(): List<QuestionOption> {
|
||||||
|
return optionList
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class QuestionGroup(
|
||||||
|
val groupName: String,
|
||||||
|
val surveyQuestionList: List<SurveyQuestion>,
|
||||||
|
var answeredQuestions: MutableMap<SurveyQuestion, QuestionOption> = mutableMapOf() // 已回答的问题及其分数
|
||||||
|
) {
|
||||||
|
// 当组内的问题被回答时,更新其他问题的依赖关系
|
||||||
|
fun updateDependenciesOnAnswer(answeredQuestion: SurveyQuestion, selectedOption: QuestionOption) {
|
||||||
|
// 将已回答的问题和选项添加到记录中
|
||||||
|
answeredQuestions[answeredQuestion] = selectedOption
|
||||||
|
|
||||||
|
// 遍历组内未回答的问题,并为每个未回答问题的选项更新依赖关系
|
||||||
|
surveyQuestionList
|
||||||
|
.filter { it != answeredQuestion && !answeredQuestions.containsKey(it) } // 过滤未回答的问题
|
||||||
|
.flatMap { it.getQuestion().getOptionList() } // 将每个问题的选项展开成一个列表
|
||||||
|
.forEach { option ->
|
||||||
|
// 创建一个新的 OptionDependency,根据已回答的选项影响当前选项
|
||||||
|
val dependency = OptionDependency(
|
||||||
|
sourceOption = selectedOption,
|
||||||
|
targetOption = option,
|
||||||
|
condition = { _ ->
|
||||||
|
// 定义条件,sourceOption 和 targetOption 必须同一问题组,且影响关系基于 markOrder 差值
|
||||||
|
true
|
||||||
|
},
|
||||||
|
effect = { targetOpt ->
|
||||||
|
val orderDifference = abs(selectedOption.markOrder - targetOpt.markOrder)
|
||||||
|
when (orderDifference) {
|
||||||
|
0 -> 1.0 // 相同 markOrder,权重最大
|
||||||
|
1 -> 0.7 // 相邻 markOrder,权重较高
|
||||||
|
2 -> 0.4 // 相差2,权重中等
|
||||||
|
else -> 0.1 // 更远的,权重最低
|
||||||
|
}
|
||||||
|
}
|
||||||
|
)
|
||||||
|
// 将新的依赖关系添加到选项的 dependencies 列表中
|
||||||
|
option.dependencies += dependency
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class QuestionOption(
|
||||||
|
val label: String,
|
||||||
|
val content: String,
|
||||||
|
val mark: String,
|
||||||
|
val markOrder: Int,
|
||||||
|
val score: Int? = null, // 可选的分数字段
|
||||||
|
var dependencies: MutableList<OptionDependency> = mutableListOf() // 与其他选项的依赖关系
|
||||||
|
) {
|
||||||
|
// 计算依赖关系对权重的影响
|
||||||
|
fun calculateWeightWithDependencies(): Double {
|
||||||
|
var baseWeight = 1.0 // 基础权重
|
||||||
|
dependencies.forEach { dependency ->
|
||||||
|
if (dependency.condition(dependency.sourceOption)) {
|
||||||
|
baseWeight += dependency.effect(dependency.sourceOption)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return baseWeight.coerceAtLeast(0.0) // 确保权重不小于0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class OptionDependency(
|
||||||
|
val sourceOption: QuestionOption, // 依赖的来源选项
|
||||||
|
val targetOption: QuestionOption, // 目标选项,受影响的选项
|
||||||
|
val condition: (QuestionOption) -> Boolean, // 判断条件,当 sourceOption 满足某个条件时影响 targetOption
|
||||||
|
val effect: (QuestionOption) -> Double // 定义当条件满足时对目标选项的权重影响
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
enum class QuestionType {
|
||||||
|
SINGLE_CHOICE, // 单选
|
||||||
|
MULTIPLE_CHOICE, // 多选
|
||||||
|
OPEN_ENDED // 开放式问题
|
||||||
|
}
|
||||||
|
|
||||||
|
enum class Gender {
|
||||||
|
MALE, FEMALE
|
||||||
|
}
|
||||||
|
|
||||||
|
class SurveyQuestion(
|
||||||
|
val title: String,
|
||||||
|
val content: String,
|
||||||
|
val question: Question,
|
||||||
|
) {
|
||||||
|
fun getQuestion(): Question {
|
||||||
|
return this.question
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class SurveyBackgroundSample(
|
||||||
|
val surveyRespondent: SurveyRespondent,
|
||||||
|
val surveyBackgroundList: List<SurveyBackground>
|
||||||
|
)
|
||||||
|
|
||||||
|
class SurveyRespondent(
|
||||||
|
val name: String,
|
||||||
|
val age: Int,
|
||||||
|
val gender: Gender
|
||||||
|
)
|
||||||
|
|
||||||
|
class SurveyBackground(
|
||||||
|
val title: String,
|
||||||
|
val backgroundQuestion: Question
|
||||||
|
)
|
||||||
|
|
||||||
)
|
|
|
@ -1,25 +0,0 @@
|
||||||
package org.echo.questionnaire
|
|
||||||
|
|
||||||
class SurveySample(val surveyBackgroundSet: Set<SurveyBackgroundOption>) {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
class SurveySampleBackground(
|
|
||||||
//关联的背景选题
|
|
||||||
val surveyBackground: SurveyBackground,
|
|
||||||
//选项
|
|
||||||
val chooseOption: SurveyBackgroundOption
|
|
||||||
)
|
|
||||||
|
|
||||||
class SurveyBackground(
|
|
||||||
val backgroundFactor: String,
|
|
||||||
val option: Set<SurveyBackgroundOption> = mutableSetOf()
|
|
||||||
) {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
class SurveyBackgroundOption(
|
|
||||||
val optionName: String,
|
|
||||||
val optionMark: String,
|
|
||||||
val optionDes: String
|
|
||||||
)
|
|
Loading…
Reference in New Issue