Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow usage of variable and context in answer expression #2039

Merged
merged 23 commits into from
Sep 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
ce5a189
Allow usage of variable and context in answer expression
maimoonak Jun 13, 2023
96e31f4
Add test
maimoonak Jun 13, 2023
ee9f18e
Merge branch 'master' into answer-exp-variable
maimoonak Jun 21, 2023
a55aca6
Merge branch 'master' into answer-exp-variable
maimoonak Jul 25, 2023
f1a20d9
resolve conflicts
maimoonak Jul 25, 2023
2a53d2d
Merge branch 'master' into answer-exp-variable
maimoonak Jul 31, 2023
d7d9c47
resolve conflicts and handle enabled items options
maimoonak Jul 31, 2023
e5daf70
Merge branch 'master' into answer-exp-variable
maimoonak Aug 8, 2023
6e55fe1
Merge branch 'master' into answer-exp-variable
f-odhiambo Aug 31, 2023
6ef89c0
Merge branch 'master' into answer-exp-variable
f-odhiambo Sep 4, 2023
36ecc87
Merge branch 'master' into answer-exp-variable
f-odhiambo Sep 7, 2023
dc963bd
Merge branch 'master' into answer-exp-variable
maimoonak Sep 11, 2023
24d4ab2
Extend variable context test
maimoonak Sep 11, 2023
fc5a31d
Merge branch 'answer-exp-variable' of https://github.com/opensrp/andr…
maimoonak Sep 11, 2023
6021cdd
Merge branch 'master' into answer-exp-variable
maimoonak Sep 12, 2023
3af708b
Resolve merge conflicts
maimoonak Sep 12, 2023
b373aa3
Merge branch 'master' into answer-exp-variable
maimoonak Sep 12, 2023
ccef36d
Breakdown fhirpath supplement context test
maimoonak Sep 12, 2023
fee87cb
Merge branch 'answer-exp-variable' of https://github.com/opensrp/andr…
maimoonak Sep 12, 2023
0b4c49f
Merge branch 'master' into answer-exp-variable
maimoonak Sep 12, 2023
44a92be
Merge branch 'master' into answer-exp-variable
maimoonak Sep 13, 2023
d2c5c85
Merge branch 'master' into answer-exp-variable
jingtang10 Sep 13, 2023
b2ff6b3
Run spotlessApply
jingtang10 Sep 13, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ import org.hl7.fhir.r4.model.ValueSet
* @param questionnaireResponse the [QuestionnaireResponse] related to the [Questionnaire]
* @param xFhirQueryResolver the [XFhirQueryResolver] to resolve resources based on the X-FHIR-Query
* @param externalValueSetResolver the [ExternalAnswerValueSetResolver] to resolve value sets
* externally/outside of the [Questionnaire]
* externally/outside of the [Questionnaire]
* @param questionnaireItemParentMap the [Map] of items parent
* @param questionnaireLaunchContextMap the [Map] of launchContext names to their resource values
*/
Expand All @@ -68,7 +68,7 @@ internal class EnabledAnswerOptionsEvaluator(
questionnaire,
questionnaireResponse,
questionnaireItemParentMap,
questionnaireLaunchContextMap
questionnaireLaunchContextMap,
)

private val answerValueSetMap =
Expand All @@ -94,13 +94,19 @@ internal class EnabledAnswerOptionsEvaluator(
questionnaireResponseItem: QuestionnaireResponseItemComponent,
): Pair<
List<Questionnaire.QuestionnaireItemAnswerOptionComponent>,
List<QuestionnaireResponse.QuestionnaireResponseItemAnswerComponent>
List<QuestionnaireResponse.QuestionnaireResponseItemAnswerComponent>,
> {
val resolvedAnswerOptions =
answerOptions(
questionnaireItem,
questionnaireResponseItem,
questionnaireResponse,
questionnaireItemParentMap,
)

val resolvedAnswerOptions = answerOptions(questionnaireItem)

if (questionnaireItem.answerOptionsToggleExpressions.isEmpty())
if (questionnaireItem.answerOptionsToggleExpressions.isEmpty()) {
return Pair(resolvedAnswerOptions, emptyList())
}

val enabledQuestionnaireAnswerOptions =
evaluateAnswerOptionsToggleExpressions(
Expand All @@ -121,29 +127,37 @@ internal class EnabledAnswerOptionsEvaluator(
/**
* In a `choice` or `open-choice` type question, the answer options are defined in one of the
* three elements in the questionnaire:
*
* - `Questionnaire.item.answerOption`: a list of permitted answers to the question
* - `Questionnaire.item.answerValueSet`: a reference to a value set containing a list of
* permitted answers to the question
* permitted answers to the question
* - `Extension answer-expression`: an expression based extension which defines the x-fhir-query
* or fhirpath to evaluate permitted answer options
* or fhirpath to evaluate permitted answer options
*
* Returns the answer options defined in one of the sources above. If the answer options are
* defined in `Questionnaire.item.answerValueSet`, the answer value set will be expanded.
*/
private suspend fun answerOptions(
questionnaireItem: QuestionnaireItemComponent,
questionnaireResponseItem: QuestionnaireResponseItemComponent,
questionnaireResponse: QuestionnaireResponse,
questionnaireItemParentMap: Map<QuestionnaireItemComponent, QuestionnaireItemComponent>,
): List<Questionnaire.QuestionnaireItemAnswerOptionComponent> =
when {
questionnaireItem.answerOption.isNotEmpty() -> questionnaireItem.answerOption
!questionnaireItem.answerValueSet.isNullOrEmpty() ->
resolveAnswerValueSet(questionnaireItem.answerValueSet)
questionnaireItem.answerExpression != null -> resolveAnswerExpression(questionnaireItem)
questionnaireItem.answerExpression != null ->
resolveAnswerExpression(
questionnaireItem,
questionnaireResponseItem,
questionnaireResponse,
questionnaireItemParentMap,
)
else -> emptyList()
}

private suspend fun resolveAnswerValueSet(
uri: String
uri: String,
): List<Questionnaire.QuestionnaireItemAnswerOptionComponent> {
// If cache hit, return it
if (answerValueSetMap.contains(uri)) {
Expand All @@ -164,7 +178,7 @@ internal class EnabledAnswerOptionsEvaluator(
.filterNot { it.abstract || it.inactive }
.map { component ->
Questionnaire.QuestionnaireItemAnswerOptionComponent(
Coding(component.system, component.code, component.display)
Coding(component.system, component.code, component.display),
)
}
}
Expand All @@ -185,6 +199,9 @@ internal class EnabledAnswerOptionsEvaluator(
// https://build.fhir.org/ig/HL7/sdc/expressions.html#x-fhir-query-enhancements
private suspend fun resolveAnswerExpression(
item: QuestionnaireItemComponent,
responseItem: QuestionnaireResponseItemComponent,
questionnaireResponse: QuestionnaireResponse,
questionnaireItemParentMap: Map<QuestionnaireItemComponent, QuestionnaireItemComponent>,
Comment on lines +203 to +204
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess these 2 were not needed in this API invocation

): List<Questionnaire.QuestionnaireItemAnswerOptionComponent> {
// Check cache first for database queries
val answerExpression = item.answerExpression ?: return emptyList()
Expand All @@ -208,16 +225,16 @@ internal class EnabledAnswerOptionsEvaluator(
options
}
?: error(
"XFhirQueryResolver cannot be null. Please provide the XFhirQueryResolver via DataCaptureConfig."
"XFhirQueryResolver cannot be null. Please provide the XFhirQueryResolver via DataCaptureConfig.",
)
}
answerExpression.isFhirPath -> {
val data = fhirPathEngine.evaluate(questionnaireResponse, answerExpression.expression)
val data = expressionEvaluator.evaluateExpression(item, responseItem, answerExpression)
item.extractAnswerOptions(data)
}
else ->
throw UnsupportedOperationException(
"${answerExpression.language} not supported for answer-expression yet"
"${answerExpression.language} not supported for answer-expression yet",
)
}
}
Expand All @@ -232,18 +249,19 @@ internal class EnabledAnswerOptionsEvaluator(
.map {
val (expression, toggleOptions) = it
val evaluationResult =
if (expression.isFhirPath)
if (expression.isFhirPath) {
fhirPathEngine.convertToBoolean(
expressionEvaluator.evaluateExpression(
item,
questionnaireResponseItem,
expression,
)
),
)
else
} else {
throw UnsupportedOperationException(
"${expression.language} not supported yet for answer-options-toggle-expression"
"${expression.language} not supported yet for answer-options-toggle-expression",
)
}
evaluationResult to toggleOptions
}
.partition { it.first }
Expand Down
Loading