diff --git a/src/main/kotlin/questionnaire/Questionnaire.kt b/src/main/kotlin/questionnaire/Questionnaire.kt index 776a065..77fcaa5 100644 --- a/src/main/kotlin/questionnaire/Questionnaire.kt +++ b/src/main/kotlin/questionnaire/Questionnaire.kt @@ -39,7 +39,7 @@ class Questionnaire( // Identify target questions influenced by the source question val influencedQuestionIds = getInfluencedQuestionIdsInGroup(questionId) for (targetQuestionId in influencedQuestionIds) { - addDependenciesAfterAnswer(this, questionId, targetQuestionId) + addDependenciesAfterAnswer(this, questionId, optionId,targetQuestionId) } // Recalculate weights for all options that might be affected @@ -60,40 +60,39 @@ class Questionnaire( fun addDependenciesAfterAnswer( questionnaire: Questionnaire, sourceQuestionId: Long, + sourceOptionId: Long, targetQuestionId: Long ) { - val sourceOptionSet = questionnaire.getSelectedOption(sourceQuestionId) + val influencedOptionSet = questionnaire.getSelectedOption(sourceQuestionId) val targetQuestion = questionnaire.questions.map { it.question }.find { it.id == targetQuestionId } - if (sourceOptionSet == null || targetQuestion == null) return + if (influencedOptionSet == null || targetQuestion == null) return - for (sourceOption in sourceOptionSet) { + for (influencedOption in influencedOptionSet) { targetQuestion.optionList.forEach { targetOption -> // Remove existing dependencies from the same source question - targetOption.dependencies.removeAll { it.sourceQuestionId == sourceQuestionId } - val dependency = OptionDependency( sourceQuestionId = sourceQuestionId, - sourceOptionId = sourceOption.id, - targetOption = targetOption, + sourceOptionId = influencedOption.id, + targetOptionId = targetOption.id, condition = { q -> - q.isOptionSelected(sourceQuestionId, sourceOption.id) + q.isOptionSelected(sourceQuestionId, sourceOptionId) }, effect = { _ -> // Define your effect based on source and target options - calculateEffectBasedOnOrder(sourceOption, targetOption) + calculateEffectBasedOnOrder(influencedOption, targetOption) } ) - targetOption.dependencies.add(dependency) + targetOption.addDependency(dependency) } } } fun calculateEffectBasedOnOrder( - sourceOption: QuestionOption, + influencedOption: QuestionOption, targetOption: QuestionOption ): Double { - val orderDifference = abs(sourceOption.markOrder - targetOption.markOrder) + val orderDifference = abs(influencedOption.markOrder - targetOption.markOrder) return when (orderDifference) { 0 -> 1.0 1 -> 0.7 @@ -193,6 +192,13 @@ class QuestionOption( fun calculateAndMapToWeight(questionnaire: Questionnaire): QuestionOptionWeight { return QuestionOptionWeight(id, calculateWeightWithDependencies(questionnaire)) } + + fun addDependency(dependency: OptionDependency) { + //添加选项依赖,添加前会删除所有之前 问题的产生的依赖项,然后再进行添加 + val sourceQuestionId = dependency.sourceQuestionId + this.dependencies.removeAll { it.sourceQuestionId == sourceQuestionId } + this.dependencies.add(dependency) + } } class QuestionOptionWeight( @@ -203,7 +209,7 @@ class QuestionOptionWeight( class OptionDependency( val sourceQuestionId: Long, val sourceOptionId: Long, - val targetOption: QuestionOption, + val targetOptionId: Long, val condition: (Questionnaire) -> Boolean, val effect: (Questionnaire) -> Double ) @@ -245,10 +251,11 @@ class SurveyBackground( class SurveyProcedure( val id: Long, + val sourceQuestionId: Long, val sourceOptionId: Long, - val targetQuestionId: Long, - val targetOptionId: Long, - val computeMethod: ComputeMethod + val influencedQuestionId: Long, + val influencedOptionId: Long, + val computeMethod: ComputeMethod = ComputeMethod.BaseComputeMethod ) { fun ifProcess(sourceOptionalId: Long, questionnaire: Questionnaire) { @@ -256,6 +263,17 @@ class SurveyProcedure( } fun generateOptionDependency(questionnaire: Questionnaire): OptionDependency? { + val influencedQuestionOptions = questionnaire.questions + .filter { it.questionId == influencedQuestionId } + .map { it.question } + .flatMap { it.optionList } + val influencedOption = influencedQuestionOptions + .filter { option -> option.id == influencedOptionId } + + influencedQuestionOptions.forEach { + //遍历所有的选项,向其中加入选项依赖 + OptionDependency(sourceOptionId,sourceOptionId,) + } return null }