Skip to content

Commit

Permalink
Support extra conditions on map resources to comma separated value la… (
Browse files Browse the repository at this point in the history
#3021)

* Support extra conditions on map resources to comma separated value labels

Signed-off-by: Elly Kitoto <[email protected]>

* Fix extraconditions not showing

---------

Signed-off-by: Elly Kitoto <[email protected]>
Co-authored-by: Sharon Akinyi <[email protected]>
Co-authored-by: sharon2719 <[email protected]>
  • Loading branch information
3 people authored Feb 1, 2024
1 parent ce2fc78 commit cdc6003
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -205,10 +205,10 @@ constructor(

/**
* This function returns a true or false value if any ( [matchAll]= false) or all ( [matchAll]=
* true) of the [resources] satisfy the [fhirPathExpression] provided
* true) of the [resources] satisfy the [conditionalFhirPathExpression] provided
*
* [resources] List of resources the expressions are run against [fhirPathExpression] An
* expression to run against the provided resources [matchAll] When true the function checks
* [resources] List of resources the expressions are run against [conditionalFhirPathExpression]
* An expression to run against the provided resources [matchAll] When true the function checks
* whether all of the resources fulfill the expression provided
*
* ```
Expand All @@ -218,19 +218,19 @@ constructor(
@JvmOverloads
fun evaluateToBoolean(
resources: List<Resource>?,
fhirPathExpression: String,
conditionalFhirPathExpression: String,
matchAll: Boolean = false,
): Boolean =
if (matchAll) {
resources?.all { base ->
fhirPathDataExtractor.extractData(base, fhirPathExpression).any {
fhirPathDataExtractor.extractData(base, conditionalFhirPathExpression).any {
it.isBooleanPrimitive && it.primitiveValue().toBoolean()
}
}
?: false
} else {
resources?.any { base ->
fhirPathDataExtractor.extractData(base, fhirPathExpression).any {
fhirPathDataExtractor.extractData(base, conditionalFhirPathExpression).any {
it.isBooleanPrimitive && it.primitiveValue().toBoolean()
}
}
Expand All @@ -245,17 +245,27 @@ constructor(
* return Comma Separated Values of 'CHILD' (to be serialized into [ServiceMemberIcon]) for
* each.
*/
@JvmOverloads
fun mapResourcesToLabeledCSV(
resources: List<Resource>?,
fhirPathExpression: String,
label: String,
matchAllExtraConditions: Boolean? = false,
vararg extraConditions: Any? = emptyArray(),
): String =
resources
?.mapNotNull {
?.mapNotNull { resource ->
if (
fhirPathDataExtractor.extractData(it, fhirPathExpression).any { base ->
fhirPathDataExtractor.extractData(resource, fhirPathExpression).any { base ->
base.isBooleanPrimitive && base.primitiveValue().toBoolean()
}
} &&
if (matchAllExtraConditions == true && extraConditions.isNotEmpty()) {
extraConditions.all { it is Boolean && it == true }
} else if (matchAllExtraConditions == false && extraConditions.isNotEmpty()) {
extraConditions.any { it is Boolean && it == true }
} else {
true
}
) {
label
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,51 @@ class RulesFactoryTest : RobolectricTest() {
Assert.assertEquals("CHILD", result)
}

@Test
fun `test mapResourcesToLabeledCSV with no resources`() {
val resources = null
val fhirPathExpression = "Patient.active and (Patient.birthDate <= today() - 60 'years')"
val label = "ELDERLY"
val matchAllExtraConditions = false
val extraConditions = emptyArray<Any>()

val result =
rulesEngineService.mapResourcesToLabeledCSV(
resources,
fhirPathExpression,
label,
matchAllExtraConditions,
*extraConditions,
)

Assert.assertEquals("", result)
}

@Test
fun `test mapResourcesToLabeledCSV with no matching resources`() {
val resources =
listOf(
Patient().setBirthDate(LocalDate.parse("2015-10-03").toDate()),
Patient().setActive(true).setBirthDate(LocalDate.parse("1954-10-03").toDate()),
Patient().setActive(true).setBirthDate(LocalDate.parse("1944-10-03").toDate()),
)
val fhirPathExpression = "Patient.active and (Patient.birthDate <= today() - 60 'years')"
val label = "ELDERLY"
val matchAllExtraConditions = false
val extraConditions = emptyArray<Any>()

val result =
rulesEngineService.mapResourcesToLabeledCSV(
resources,
fhirPathExpression,
label,
matchAllExtraConditions,
*extraConditions,
)

Assert.assertEquals("ELDERLY", result)
}

@Test
fun filterResourceList() {
val fhirPathExpression =
Expand Down

0 comments on commit cdc6003

Please sign in to comment.