This commit is contained in:
parent
67345e2089
commit
42d392ddba
|
@ -39,7 +39,7 @@ class Questionnaire(
|
||||||
// Identify target questions influenced by the source question
|
// Identify target questions influenced by the source question
|
||||||
val influencedQuestionIds = getInfluencedQuestionIdsInGroup(questionId)
|
val influencedQuestionIds = getInfluencedQuestionIdsInGroup(questionId)
|
||||||
for (targetQuestionId in influencedQuestionIds) {
|
for (targetQuestionId in influencedQuestionIds) {
|
||||||
addDependenciesAfterAnswer(this, questionId, targetQuestionId)
|
addDependenciesAfterAnswer(this, questionId, optionId,targetQuestionId)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Recalculate weights for all options that might be affected
|
// Recalculate weights for all options that might be affected
|
||||||
|
@ -60,40 +60,39 @@ class Questionnaire(
|
||||||
fun addDependenciesAfterAnswer(
|
fun addDependenciesAfterAnswer(
|
||||||
questionnaire: Questionnaire,
|
questionnaire: Questionnaire,
|
||||||
sourceQuestionId: Long,
|
sourceQuestionId: Long,
|
||||||
|
sourceOptionId: Long,
|
||||||
targetQuestionId: Long
|
targetQuestionId: Long
|
||||||
) {
|
) {
|
||||||
val sourceOptionSet = questionnaire.getSelectedOption(sourceQuestionId)
|
val influencedOptionSet = questionnaire.getSelectedOption(sourceQuestionId)
|
||||||
val targetQuestion = questionnaire.questions.map { it.question }.find { it.id == targetQuestionId }
|
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 ->
|
targetQuestion.optionList.forEach { targetOption ->
|
||||||
// Remove existing dependencies from the same source question
|
// Remove existing dependencies from the same source question
|
||||||
targetOption.dependencies.removeAll { it.sourceQuestionId == sourceQuestionId }
|
|
||||||
|
|
||||||
val dependency = OptionDependency(
|
val dependency = OptionDependency(
|
||||||
sourceQuestionId = sourceQuestionId,
|
sourceQuestionId = sourceQuestionId,
|
||||||
sourceOptionId = sourceOption.id,
|
sourceOptionId = influencedOption.id,
|
||||||
targetOption = targetOption,
|
targetOptionId = targetOption.id,
|
||||||
condition = { q ->
|
condition = { q ->
|
||||||
q.isOptionSelected(sourceQuestionId, sourceOption.id)
|
q.isOptionSelected(sourceQuestionId, sourceOptionId)
|
||||||
},
|
},
|
||||||
effect = { _ ->
|
effect = { _ ->
|
||||||
// Define your effect based on source and target options
|
// Define your effect based on source and target options
|
||||||
calculateEffectBasedOnOrder(sourceOption, targetOption)
|
calculateEffectBasedOnOrder(influencedOption, targetOption)
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
targetOption.dependencies.add(dependency)
|
targetOption.addDependency(dependency)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun calculateEffectBasedOnOrder(
|
fun calculateEffectBasedOnOrder(
|
||||||
sourceOption: QuestionOption,
|
influencedOption: QuestionOption,
|
||||||
targetOption: QuestionOption
|
targetOption: QuestionOption
|
||||||
): Double {
|
): Double {
|
||||||
val orderDifference = abs(sourceOption.markOrder - targetOption.markOrder)
|
val orderDifference = abs(influencedOption.markOrder - targetOption.markOrder)
|
||||||
return when (orderDifference) {
|
return when (orderDifference) {
|
||||||
0 -> 1.0
|
0 -> 1.0
|
||||||
1 -> 0.7
|
1 -> 0.7
|
||||||
|
@ -193,6 +192,13 @@ class QuestionOption(
|
||||||
fun calculateAndMapToWeight(questionnaire: Questionnaire): QuestionOptionWeight {
|
fun calculateAndMapToWeight(questionnaire: Questionnaire): QuestionOptionWeight {
|
||||||
return QuestionOptionWeight(id, calculateWeightWithDependencies(questionnaire))
|
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(
|
class QuestionOptionWeight(
|
||||||
|
@ -203,7 +209,7 @@ class QuestionOptionWeight(
|
||||||
class OptionDependency(
|
class OptionDependency(
|
||||||
val sourceQuestionId: Long,
|
val sourceQuestionId: Long,
|
||||||
val sourceOptionId: Long,
|
val sourceOptionId: Long,
|
||||||
val targetOption: QuestionOption,
|
val targetOptionId: Long,
|
||||||
val condition: (Questionnaire) -> Boolean,
|
val condition: (Questionnaire) -> Boolean,
|
||||||
val effect: (Questionnaire) -> Double
|
val effect: (Questionnaire) -> Double
|
||||||
)
|
)
|
||||||
|
@ -245,10 +251,11 @@ class SurveyBackground(
|
||||||
|
|
||||||
class SurveyProcedure(
|
class SurveyProcedure(
|
||||||
val id: Long,
|
val id: Long,
|
||||||
|
val sourceQuestionId: Long,
|
||||||
val sourceOptionId: Long,
|
val sourceOptionId: Long,
|
||||||
val targetQuestionId: Long,
|
val influencedQuestionId: Long,
|
||||||
val targetOptionId: Long,
|
val influencedOptionId: Long,
|
||||||
val computeMethod: ComputeMethod
|
val computeMethod: ComputeMethod = ComputeMethod.BaseComputeMethod
|
||||||
) {
|
) {
|
||||||
|
|
||||||
fun ifProcess(sourceOptionalId: Long, questionnaire: Questionnaire) {
|
fun ifProcess(sourceOptionalId: Long, questionnaire: Questionnaire) {
|
||||||
|
@ -256,6 +263,17 @@ class SurveyProcedure(
|
||||||
}
|
}
|
||||||
|
|
||||||
fun generateOptionDependency(questionnaire: Questionnaire): OptionDependency? {
|
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
|
return null
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue