From 2347120cd3b733875c7de75ae83520b3e84293ac Mon Sep 17 00:00:00 2001 From: dandan <1033719135@qq.com> Date: Sun, 22 Sep 2024 22:26:20 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8F=90=E4=BA=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .idea/misc.xml | 1 - .../kotlin/questionnaire/Questionnaire.kt | 68 ++++++++++++------- 2 files changed, 43 insertions(+), 26 deletions(-) diff --git a/.idea/misc.xml b/.idea/misc.xml index fe0b0da..f937d01 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,4 +1,3 @@ - diff --git a/src/main/kotlin/questionnaire/Questionnaire.kt b/src/main/kotlin/questionnaire/Questionnaire.kt index 77fcaa5..7435450 100644 --- a/src/main/kotlin/questionnaire/Questionnaire.kt +++ b/src/main/kotlin/questionnaire/Questionnaire.kt @@ -3,6 +3,8 @@ package org.echo.questionnaire import kotlin.math.abs import kotlin.random.Random + + class Questionnaire( val id: Long, val title: String, // 问卷的标题 @@ -10,7 +12,7 @@ class Questionnaire( val questions: List, val questionGroup: List, val sample: SurveyBackgroundSample, // 问卷中的所有问题 - val procedures: List + val procedures: List, ) { private val selectedOptions: MutableMap> = mutableMapOf()// Map of questionId to optionId @@ -39,7 +41,7 @@ class Questionnaire( // Identify target questions influenced by the source question val influencedQuestionIds = getInfluencedQuestionIdsInGroup(questionId) for (targetQuestionId in influencedQuestionIds) { - addDependenciesAfterAnswer(this, questionId, optionId,targetQuestionId) + addDependenciesAfterAnswer(this, questionId, optionId, targetQuestionId) } // Recalculate weights for all options that might be affected @@ -61,7 +63,7 @@ class Questionnaire( questionnaire: Questionnaire, sourceQuestionId: Long, sourceOptionId: Long, - targetQuestionId: Long + targetQuestionId: Long, ) { val influencedOptionSet = questionnaire.getSelectedOption(sourceQuestionId) val targetQuestion = questionnaire.questions.map { it.question }.find { it.id == targetQuestionId } @@ -73,14 +75,19 @@ class Questionnaire( // Remove existing dependencies from the same source question val dependency = OptionDependency( sourceQuestionId = sourceQuestionId, - sourceOptionId = influencedOption.id, - targetOptionId = targetOption.id, + sourceOptionId = sourceOptionId, + influencedOptionId = influencedOption.id, + targetOptionId = targetQuestion.id, condition = { q -> q.isOptionSelected(sourceQuestionId, sourceOptionId) }, effect = { _ -> - // Define your effect based on source and target options - calculateEffectBasedOnOrder(influencedOption, targetOption) + // 使用枚举类的 calculateEffectBasedOnOrder 方法 + val effectValue = ComputeMethod.BaseComputeMethod + .calculateEffectBasedOnOrder(influencedOption, targetOption) + // 根据计算结果进行相应处理 + println("Effect calculated: $effectValue") + effectValue // 返回 effect 值,这个可以用于后续的逻辑 } ) targetOption.addDependency(dependency) @@ -90,7 +97,7 @@ class Questionnaire( fun calculateEffectBasedOnOrder( influencedOption: QuestionOption, - targetOption: QuestionOption + targetOption: QuestionOption, //是他自己 ): Double { val orderDifference = abs(influencedOption.markOrder - targetOption.markOrder) return when (orderDifference) { @@ -148,7 +155,7 @@ open class Question( val optionList: List, val questionType: QuestionType, // 表示问题类型:单选、多选、开放式等 val order: Int, // 表示问题顺序 - val isRequired: Boolean = true // 是否必答,默认为必答 + val isRequired: Boolean = true, // 是否必答,默认为必答 ) { fun getOptionListData(): List { return optionList @@ -162,7 +169,7 @@ class QuestionGroup( val parentId: Long, val groupName: String, val surveyQuestionList: List, - var answeredQuestions: MutableMap = mutableMapOf() // 已回答的问题及其分数 + var answeredQuestions: MutableMap = mutableMapOf(), // 已回答的问题及其分数 ) { fun checkIfInGroup(questionId: Long): Boolean { return surveyQuestionList.map { it.question }.any { it.id == questionId } @@ -176,7 +183,7 @@ class QuestionOption( val mark: String, val markOrder: Int, val score: Int? = null, // 可选的分数字段 - var dependencies: MutableList = mutableListOf() // 与其他选项的依赖关系 + var dependencies: MutableList = mutableListOf(), // 与其他选项的依赖关系 ) { // 计算依赖关系对权重的影响 fun calculateWeightWithDependencies(questionnaire: Questionnaire): Double { @@ -209,9 +216,10 @@ class QuestionOptionWeight( class OptionDependency( val sourceQuestionId: Long, val sourceOptionId: Long, + val influencedOptionId: Long, val targetOptionId: Long, val condition: (Questionnaire) -> Boolean, - val effect: (Questionnaire) -> Double + val effect: (Questionnaire) -> Double, ) @@ -236,17 +244,17 @@ class SurveyQuestion( class SurveyBackgroundSample( val surveyRespondent: SurveyRespondent, - val surveyBackgroundList: List + val surveyBackgroundList: List, ) class SurveyRespondent( val name: String, - val age: Int + val age: Int, ) class SurveyBackground( val title: String, - val backgroundQuestion: Question + val backgroundQuestion: Question, ) class SurveyProcedure( @@ -255,37 +263,47 @@ class SurveyProcedure( val sourceOptionId: Long, val influencedQuestionId: Long, val influencedOptionId: Long, - val computeMethod: ComputeMethod = ComputeMethod.BaseComputeMethod + val computeMethod: ComputeMethod = ComputeMethod.BaseComputeMethod, ) { fun ifProcess(sourceOptionalId: Long, questionnaire: Questionnaire) { if (sourceOptionalId == sourceOptionId) generateOptionDependency(questionnaire) } - fun generateOptionDependency(questionnaire: Questionnaire): OptionDependency? { - val influencedQuestionOptions = questionnaire.questions + fun generateOptionDependency(questionnaire: Questionnaire) { + val influencedQuestionOptions = questionnaire.questions .filter { it.questionId == influencedQuestionId } .map { it.question } .flatMap { it.optionList } val influencedOption = influencedQuestionOptions .filter { option -> option.id == influencedOptionId } - + .last() influencedQuestionOptions.forEach { //遍历所有的选项,向其中加入选项依赖 - OptionDependency(sourceOptionId,sourceOptionId,) + val optionDependency = OptionDependency( + sourceOptionId, sourceOptionId, + influencedOption.id, + it.id, + condition = { q -> + q.isOptionSelected(sourceQuestionId, sourceOptionId) + }, + effect = { _ -> + // Define your effect based on source and target options + computeMethod.calculateEffectBasedOnOrder(influencedOption, it) + } + ) + it.addDependency(optionDependency) } - - return null } } enum class ComputeMethod( - val computeMethodName: String + val computeMethodName: String, ) { BaseComputeMethod("Default") { override fun calculateEffectBasedOnOrder( influencedOption: QuestionOption, - targetOption: QuestionOption + targetOption: QuestionOption, ): Double { val orderDifference = abs(influencedOption.markOrder - targetOption.markOrder) return when (orderDifference) { @@ -300,6 +318,6 @@ enum class ComputeMethod( // 定义抽象方法,子类必须实现 abstract fun calculateEffectBasedOnOrder( sourceOption: QuestionOption, - targetOption: QuestionOption + targetOption: QuestionOption, ): Double } \ No newline at end of file