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

Fix issue with empty context with initial expression #1228

Merged
merged 36 commits into from
Apr 23, 2022
Merged
Show file tree
Hide file tree
Changes from 20 commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
01ba81c
- today() support added in initial expression
RaaziaTarique Mar 10, 2022
117f474
- spotless apply
RaaziaTarique Mar 10, 2022
cb89b67
- Enhance initialExpression support
RaaziaTarique Mar 10, 2022
9e4ca7f
Merge branch 'master' into 1033_advance_behaviour
RaaziaTarique Mar 10, 2022
2b96744
- Enhance initialExpression support modifications
RaaziaTarique Mar 11, 2022
512f760
Merge branch 'master' into 1033_advance_behaviour
RaaziaTarique Mar 16, 2022
03ac5e9
- Use ExpressionNode to evaluate expression
RaaziaTarique Mar 18, 2022
dffe99c
Merge remote-tracking branch 'origin/1033_advance_behaviour' into 103…
RaaziaTarique Mar 18, 2022
5b1d462
Merge branch 'master' into 1033_advance_behaviour
RaaziaTarique Mar 18, 2022
9a26aab
- Reduce step one of expression evaluation to use only one expression…
RaaziaTarique Mar 18, 2022
ed18ff5
Merge remote-tracking branch 'origin/1033_advance_behaviour' into 103…
RaaziaTarique Mar 18, 2022
a3deda3
- Simplify ResourceMapper.kt
RaaziaTarique Mar 21, 2022
2f004af
- Simply Code
RaaziaTarique Mar 22, 2022
5d12a9e
- Remove extract answers from expressions using definition code.
RaaziaTarique Mar 24, 2022
6197c6f
Merge branch 'master' into 1033_advance_behaviour
RaaziaTarique Mar 24, 2022
ffc9709
- Resolve requested changes.
RaaziaTarique Mar 24, 2022
dacf027
Merge branch 'master' into 1033_advance_behaviour
RaaziaTarique Mar 24, 2022
8d7ad14
- Resolve requested changes.
RaaziaTarique Mar 25, 2022
e048cb3
- Fix failing test cases.
RaaziaTarique Mar 25, 2022
b630b16
- Address requested changes
RaaziaTarique Mar 29, 2022
31d78d9
Merge branch 'master' into 1033_advance_behaviour
f-odhiambo Mar 30, 2022
5284d6c
- Code refactor
RaaziaTarique Apr 4, 2022
b2b6187
Merge branch 'master' into 1033_advance_behaviour
RaaziaTarique Apr 4, 2022
34e7e81
Merge branch 'master' into 1033_advance_behaviour
f-odhiambo Apr 5, 2022
5a32c3f
- Resolve requested changes.
RaaziaTarique Apr 5, 2022
c67a406
- Resolve requested changes.
RaaziaTarique Apr 5, 2022
82f7a23
Merge branch 'master' into 1033_advance_behaviour
RaaziaTarique Apr 5, 2022
2f27423
Refactor ResourceMapper.kt to make it more concise.
jingtang10 Apr 20, 2022
85398df
Add documentation.
jingtang10 Apr 20, 2022
8c02a1a
Make ITEM_INITIAL_EXPRESSION_URL internal
jingtang10 Apr 20, 2022
b978015
Merge branch 'master' into 1033_advance_behaviour
jingtang10 Apr 20, 2022
748fe39
Merge branch 'master' into 1033_advance_behaviour
jingtang10 Apr 21, 2022
61d59bd
Use Kotlin scope functions to replace if..else...
jingtang10 Apr 22, 2022
93e3b7a
Merge branch 'master' into 1033_advance_behaviour
jingtang10 Apr 22, 2022
957b991
Merge branch 'master' into 1033_advance_behaviour
jingtang10 Apr 22, 2022
59ce913
Merge branch 'master' into 1033_advance_behaviour
jingtang10 Apr 22, 2022
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 @@ -46,7 +46,6 @@ import org.hl7.fhir.r4.model.Questionnaire
import org.hl7.fhir.r4.model.QuestionnaireResponse
import org.hl7.fhir.r4.model.Resource
import org.hl7.fhir.r4.model.StringType
import org.hl7.fhir.r4.model.StructureMap
import org.hl7.fhir.r4.model.Type
import org.hl7.fhir.r4.model.UriType
import org.hl7.fhir.r4.utils.FHIRPathEngine
Expand Down Expand Up @@ -197,27 +196,54 @@ object ResourceMapper {
vararg resources: Resource
) {
if (questionnaireItem.type != Questionnaire.QuestionnaireItemType.GROUP) {
questionnaireItem.fetchExpression?.let { exp ->
val resourceType = exp.expression.substringBefore(".").removePrefix("%")

// Match the first resource of the same type
val contextResource =
resources.firstOrNull { it.resourceType.name.equals(resourceType) } ?: return

val answerExtracted = fhirPathEngine.evaluate(contextResource, exp.expression)
answerExtracted.firstOrNull()?.let { answer ->
questionnaireItem.initial =
mutableListOf(
Questionnaire.QuestionnaireItemInitialComponent().setValue(answer.asExpectedType())
)
}
questionnaireItem.initialExpression?.let { exp ->
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think this is much better as a extension function on questionnaireItem.

fun QuestionnaireItem.setInitialValueFromInitialExpression() seems appropriate.

you're passing the questionnaire item to the evaluateInitialExpressionAndSetInitialValue function which seems odd to me. All this happens within the context of the questionnaire item, so should probably be an extension function.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@jingtang10 I have created an extension function with slight modifications. Please re-review the PR.

evaluateInitialExpressionAndSetInitialValue(exp, resources, questionnaireItem)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
questionnaireItem.initialExpression?.let { exp ->
evaluateInitialExpressionAndSetInitialValue(exp, resources, questionnaireItem)
questionnaireItem.initialExpression?.let {
evaluateInitialExpressionAndSetInitialValue(it, resources, questionnaireItem)

}
}

populateInitialValues(questionnaireItem.item, *resources)
}

private val Questionnaire.QuestionnaireItemComponent.fetchExpression: Expression?
private fun evaluateInitialExpressionAndSetInitialValue(
exp: Expression,
Copy link
Collaborator

Choose a reason for hiding this comment

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

avoid using abbreviations

Suggested change
exp: Expression,
expression: Expression,

resources: Array<out Resource>,
Copy link
Collaborator

Choose a reason for hiding this comment

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

i don't feel like this out here is justified? I thought it's only useful for declarations on class/interface. can you elaborate why this is needed?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Actually vararg resources: Resource converts to Array<out Resource>, when being passed to another method.

questionnaireItem: Questionnaire.QuestionnaireItemComponent
) {
val resourceType = exp.expression.substringBefore(".").removePrefix("%")
val contextResource =
resources.firstOrNull { it.resourceType.name.lowercase().equals(resourceType.lowercase()) }

val extractedAnswers = getAnswers(contextResource, resources, exp)
extractedAnswers.firstOrNull()?.let { answer ->
// Resetting QuestionnaireItemInitialComponent value using provided initialExpression
questionnaireItem.initial =
mutableListOf(
Questionnaire.QuestionnaireItemInitialComponent().setValue(answer.asExpectedType())
)
}
}

private fun getAnswers(
contextResource: Resource?,
resources: Array<out Resource>,
Copy link
Collaborator

Choose a reason for hiding this comment

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

ditto

exp: Expression
): MutableList<Base> {
var extractedAnswers = mutableListOf<Base>()
if (contextResource == null) {
if (resources.isNotEmpty()) {
// Using the first provided resource as a base resource inorder to fetch answers from the
// functions
extractedAnswers = fhirPathEngine.evaluate(resources[0], exp.expression.removePrefix("%"))
}
} else {
// Using the resource extracted from provided expression as a base resource inorder to fetch
// answers from the functions
extractedAnswers = fhirPathEngine.evaluate(contextResource, exp.expression.removePrefix("%"))
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

this code seems duplicated to me. you're just saying if the context resource is empty just take the first resource as the context resource right?

i feel this code is actually harder to read and doesn't add any more functionality.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@jingtang10, this code added functionality to use FhirPath functions such as today(), now() and etc.
Previously these functions were not supported in SDK because it was only using the extracted resource from the FhirPath expression and in case of FhirPath functions, the FhirPath Expressions doesn't have any resource name in it.

Copy link
Collaborator

Choose a reason for hiding this comment

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

the only thing that's different in the code is that if the type doesn't match the expected expression the first resource is passed anyway as context.

return extractedAnswers
}

private val Questionnaire.QuestionnaireItemComponent.initialExpression: Expression?
get() {
return this.extension.firstOrNull { it.url == ITEM_INITIAL_EXPRESSION_URL }?.let {
it.value as Expression
Expand Down Expand Up @@ -514,7 +540,7 @@ private fun wrapAnswerInFieldType(answer: Base, fieldType: Field): Base {
return answer
}

private const val ITEM_INITIAL_EXPRESSION_URL: String =
const val ITEM_INITIAL_EXPRESSION_URL: String =
"http://hl7.org/fhir/uv/sdc/StructureDefinition/sdc-questionnaire-initialExpression"

private val Field.isList: Boolean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import android.os.Build
import androidx.test.core.app.ApplicationProvider
import ca.uhn.fhir.context.FhirContext
import ca.uhn.fhir.parser.IParser
import com.google.android.fhir.datacapture.views.localDate
import com.google.common.truth.Truth.assertThat
import java.math.BigDecimal
import java.text.SimpleDateFormat
Expand Down Expand Up @@ -490,6 +491,36 @@ class ResourceMapperTest {
assertThat(patient.telecom[0].value).isEqualTo("+254711001122")
}

@Test
fun `populate() should correctly populate current date from fhirpath expression in QuestionnaireResponse`() =
runBlocking {
val questionnaire =
Questionnaire()
.addItem(
Questionnaire.QuestionnaireItemComponent().apply {
linkId = "patient-dob"
type = Questionnaire.QuestionnaireItemType.TEXT
extension =
listOf(
Extension(
ITEM_INITIAL_EXPRESSION_URL,
Expression().apply {
language = "text/fhirpath"
expression = "today()"
}
)
)
}
)

val patientId = UUID.randomUUID().toString()
val patient = Patient().apply { id = "Patient/$patientId/_history/2" }
val questionnaireResponse = ResourceMapper.populate(questionnaire, patient)

assertThat((questionnaireResponse.item[0].answer[0].value as DateType).localDate)
.isEqualTo((DateType(Date())).localDate)
}

@Test
fun `extract() should perform definition-based extraction with unanswered questions`() =
runBlocking {
Expand Down Expand Up @@ -960,9 +991,6 @@ class ResourceMapperTest {
@Test
fun `populate() should fill QuestionnaireResponse with values when given multiple Resources`() =
runBlocking {
val ITEM_EXTRACTION_CONTEXT_EXTENSION_URL =
"http://hl7.org/fhir/uv/sdc/StructureDefinition/sdc-questionnaire-initialExpression"

val relatedPerson =
RelatedPerson().apply {
name =
Expand Down Expand Up @@ -990,7 +1018,7 @@ class ResourceMapperTest {
extension =
listOf(
Extension(
ITEM_EXTRACTION_CONTEXT_EXTENSION_URL,
ITEM_INITIAL_EXPRESSION_URL,
Expression().apply {
language = "text/fhirpath"
expression = "Patient.name.given"
Expand All @@ -1011,7 +1039,7 @@ class ResourceMapperTest {
extension =
listOf(
Extension(
ITEM_EXTRACTION_CONTEXT_EXTENSION_URL,
ITEM_INITIAL_EXPRESSION_URL,
Expression().apply {
language = "text/fhirpath"
expression = "RelatedPerson.name.family"
Expand All @@ -1029,7 +1057,7 @@ class ResourceMapperTest {
extension =
listOf(
Extension(
ITEM_EXTRACTION_CONTEXT_EXTENSION_URL,
ITEM_INITIAL_EXPRESSION_URL,
Expression().apply {
language = "text/fhirpath"
expression = "RelatedPerson.birthDate"
Expand All @@ -1045,7 +1073,7 @@ class ResourceMapperTest {
extension =
listOf(
Extension(
ITEM_EXTRACTION_CONTEXT_EXTENSION_URL,
ITEM_INITIAL_EXPRESSION_URL,
Expression().apply {
language = "text/fhirpath"
expression = "Observation.value"
Expand Down Expand Up @@ -1073,9 +1101,6 @@ class ResourceMapperTest {

@Test
fun `populate() should correctly populate IdType value in QuestionnaireResponse`() = runBlocking {
val ITEM_EXTRACTION_CONTEXT_EXTENSION_URL =
"http://hl7.org/fhir/uv/sdc/StructureDefinition/sdc-questionnaire-initialExpression"

val questionnaire =
Questionnaire()
.addItem(
Expand All @@ -1085,7 +1110,7 @@ class ResourceMapperTest {
extension =
listOf(
Extension(
ITEM_EXTRACTION_CONTEXT_EXTENSION_URL,
ITEM_INITIAL_EXPRESSION_URL,
Expression().apply {
language = "text/fhirpath"
expression = "Patient.id"
Expand All @@ -1106,9 +1131,6 @@ class ResourceMapperTest {
@Test
fun `populate() should correctly populate IdType value with history in QuestionnaireResponse`() =
runBlocking {
val ITEM_EXTRACTION_CONTEXT_EXTENSION_URL =
"http://hl7.org/fhir/uv/sdc/StructureDefinition/sdc-questionnaire-initialExpression"

val questionnaire =
Questionnaire()
.addItem(
Expand All @@ -1118,7 +1140,7 @@ class ResourceMapperTest {
extension =
listOf(
Extension(
ITEM_EXTRACTION_CONTEXT_EXTENSION_URL,
ITEM_INITIAL_EXPRESSION_URL,
Expression().apply {
language = "text/fhirpath"
expression = "Patient.id"
Expand All @@ -1139,9 +1161,6 @@ class ResourceMapperTest {
@Test
fun `populate() should correctly populate Enumeration value in QuestionnaireResponse`() =
runBlocking {
val ITEM_EXTRACTION_CONTEXT_EXTENSION_URL =
"http://hl7.org/fhir/uv/sdc/StructureDefinition/sdc-questionnaire-initialExpression"

val questionnaire =
Questionnaire()
.addItem(
Expand All @@ -1151,7 +1170,7 @@ class ResourceMapperTest {
extension =
listOf(
Extension(
ITEM_EXTRACTION_CONTEXT_EXTENSION_URL,
ITEM_INITIAL_EXPRESSION_URL,
Expression().apply {
language = "text/fhirpath"
expression = "Patient.gender"
Expand Down Expand Up @@ -1186,9 +1205,6 @@ class ResourceMapperTest {

@Test
fun `populate() should populate nested non-group questions`() = runBlocking {
val ITEM_EXTRACTION_CONTEXT_EXTENSION_URL =
"http://hl7.org/fhir/uv/sdc/StructureDefinition/sdc-questionnaire-initialExpression"

val questionnaire =
Questionnaire()
.addItem(
Expand All @@ -1198,7 +1214,7 @@ class ResourceMapperTest {
extension =
listOf(
Extension(
ITEM_EXTRACTION_CONTEXT_EXTENSION_URL,
ITEM_INITIAL_EXPRESSION_URL,
Expression().apply {
language = "text/fhirpath"
expression = "Patient.gender"
Expand Down Expand Up @@ -1228,7 +1244,7 @@ class ResourceMapperTest {
extension =
listOf(
Extension(
ITEM_EXTRACTION_CONTEXT_EXTENSION_URL,
ITEM_INITIAL_EXPRESSION_URL,
Expression().apply {
language = "text/fhirpath"
expression = "Patient.id"
Expand Down