Skip to content

Commit

Permalink
Resolving comments by some refactoring and adding suggested changes.
Browse files Browse the repository at this point in the history
  • Loading branch information
MJ1998 committed Jul 20, 2023
1 parent 2f50763 commit 5846162
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ import kotlinx.coroutines.flow.update
import org.hl7.fhir.r4.model.Base
import org.hl7.fhir.r4.model.Coding
import org.hl7.fhir.r4.model.Element
import org.hl7.fhir.r4.model.Expression
import org.hl7.fhir.r4.model.Questionnaire
import org.hl7.fhir.r4.model.Questionnaire.QuestionnaireItemComponent
import org.hl7.fhir.r4.model.QuestionnaireResponse
Expand Down Expand Up @@ -578,7 +577,41 @@ internal class QuestionnaireViewModel(application: Application, state: SavedStat
// Check cache first for database queries
val answerExpression = item.answerExpression ?: return emptyList()

return loadAnswerExpressionOptions(item, answerExpression)
return when {
answerExpression.isXFhirQuery -> {
xFhirQueryResolver?.let { xFhirQueryResolver ->
val xFhirExpressionString =
ExpressionEvaluator.createXFhirQueryFromExpression(
questionnaire,
questionnaireResponse,
item,
answerExpression,
questionnaireItemParentMap,
questionnaireLaunchContextMap
)
if (answerExpressionMap.containsKey(xFhirExpressionString)) {
answerExpressionMap[xFhirExpressionString]
}

val data = xFhirQueryResolver.resolve(xFhirExpressionString)
val options = item.extractAnswerOptions(data)

answerExpressionMap[xFhirExpressionString] = options
options
}
?: error(
"XFhirQueryResolver cannot be null. Please provide the XFhirQueryResolver via DataCaptureConfig."
)
}
answerExpression.isFhirPath -> {
val data = fhirPathEngine.evaluate(questionnaireResponse, answerExpression.expression)
item.extractAnswerOptions(data)
}
else ->
throw UnsupportedOperationException(
"${answerExpression.language} not supported for answer-expression yet"
)
}
}

private fun resolveCqfExpression(
Expand All @@ -601,42 +634,6 @@ internal class QuestionnaireViewModel(application: Application, state: SavedStat
)
}

private suspend fun loadAnswerExpressionOptions(
item: QuestionnaireItemComponent,
expression: Expression,
): List<Questionnaire.QuestionnaireItemAnswerOptionComponent> {
var xFhirExpressionString = ""
val data =
if (expression.isXFhirQuery) {
checkNotNull(xFhirQueryResolver) {
"XFhirQueryResolver cannot be null. Please provide the XFhirQueryResolver via DataCaptureConfig."
}

xFhirExpressionString =
ExpressionEvaluator.createXFhirQueryFromExpression(
questionnaire,
questionnaireResponse,
item,
expression,
questionnaireItemParentMap,
questionnaireLaunchContextMap
)
if (answerExpressionMap.contains(xFhirExpressionString)) {
return answerExpressionMap[xFhirExpressionString]!!
}
xFhirQueryResolver!!.resolve(xFhirExpressionString)
} else if (expression.isFhirPath) {
fhirPathEngine.evaluate(questionnaireResponse, expression.expression)
} else {
throw UnsupportedOperationException(
"${expression.language} not supported for answer-expression yet"
)
}
val options = item.extractAnswerOptions(data)
if (expression.isXFhirQuery) answerExpressionMap[xFhirExpressionString] = options
return options
}

/**
* Traverses through the list of questionnaire items, the list of questionnaire response items and
* the list of items in the questionnaire response answer list and populates
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,8 @@ object ExpressionEvaluator {
}

/**
* Creates an x-fhir-query string for evaluation
* Creates an x-fhir-query string for evaluation. For this, it evaluates both variables and
* fhir-paths in the expression.
*
* @param expression x-fhir-query expression
* @param launchContextMap if passed, the launch context to evaluate the expression against
Expand Down Expand Up @@ -308,10 +309,8 @@ object ExpressionEvaluator {
.filterKeys { expression.expression.contains("{{%$it}}") }
.map { Pair("{{%${it.key}}}", it.value!!.primitiveValue()) }

var fhirPathsEvaluatedPairs = emptySequence<Pair<String, String>>()
if (launchContextMap != null) {
fhirPathsEvaluatedPairs = evaluateXFhirEnhancement(expression, launchContextMap)
}
val fhirPathsEvaluatedPairs =
launchContextMap?.let { evaluateXFhirEnhancement(expression, it) } ?: emptySequence()
return (fhirPathsEvaluatedPairs + variablesEvaluatedPairs).fold(expression.expression) {
acc: String,
pair: Pair<String, String> ->
Expand Down

0 comments on commit 5846162

Please sign in to comment.