Skip to content

Commit

Permalink
Interpolate conditionalFhirPathExpressions in listResource
Browse files Browse the repository at this point in the history
To pass in evaluated rule variables
  • Loading branch information
LZRS committed Oct 8, 2024
1 parent 2e8ef96 commit a30225b
Showing 1 changed file with 12 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import androidx.compose.runtime.mutableStateListOf
import androidx.compose.runtime.snapshots.SnapshotStateList
import androidx.compose.runtime.snapshots.SnapshotStateMap
import com.google.android.fhir.datacapture.extensions.logicalId
import javax.inject.Inject
import org.hl7.fhir.r4.model.Resource
import org.jeasy.rules.api.Facts
import org.smartregister.fhircore.engine.configuration.view.ListProperties
Expand All @@ -29,7 +30,7 @@ import org.smartregister.fhircore.engine.domain.model.ResourceData
import org.smartregister.fhircore.engine.domain.model.RuleConfig
import org.smartregister.fhircore.engine.domain.model.ViewType
import org.smartregister.fhircore.engine.util.extension.extractLogicalIdUuid
import javax.inject.Inject
import org.smartregister.fhircore.engine.util.extension.interpolate

/**
* This class is used to fire rules used to extract and manipulate data from FHIR resources.
Expand Down Expand Up @@ -73,7 +74,7 @@ class ResourceDataRulesExecutor @Inject constructor(val rulesFactory: RulesFacto
listResourceDataStateMap: SnapshotStateMap<String, SnapshotStateList<ResourceData>>,
) {
listProperties.resources.forEach { listResource ->
filteredListResources(relatedResourcesMap, listResource)
filteredListResources(relatedResourcesMap, listResource, computedValuesMap)
.mapToResourceData(
listResourceConfig = listResource,
relatedResourcesMap = relatedResourcesMap,
Expand Down Expand Up @@ -133,11 +134,14 @@ class ResourceDataRulesExecutor @Inject constructor(val rulesFactory: RulesFacto
relatedResourcesMap = relatedResourcesMap,
)
}

val interpolatedConditionalFhirPathExpression =
relatedListResourceConfig.conditionalFhirPathExpression?.interpolate(computedValuesMap)

rulesFactory.rulesEngineService
.filterResources(
resources = retrievedRelatedResources,
conditionalFhirPathExpression =
relatedListResourceConfig.conditionalFhirPathExpression,
conditionalFhirPathExpression = interpolatedConditionalFhirPathExpression,
)
.also { filteredResources ->
// Add to queue for processing
Expand Down Expand Up @@ -198,14 +202,17 @@ class ResourceDataRulesExecutor @Inject constructor(val rulesFactory: RulesFacto
private fun filteredListResources(
relatedResourceMap: Map<String, List<Resource>>,
listResource: ListResourceConfig,
computedValuesMap: Map<String, Any>,
): List<Resource> {
val relatedResourceKey = listResource.relatedResourceId ?: listResource.resourceType.name
val interpolatedConditionalFhirPathExpression =
listResource.conditionalFhirPathExpression?.interpolate(computedValuesMap)

// Filter by condition derived from fhirPathExpression otherwise return original or empty list
val resources =
rulesFactory.rulesEngineService.filterResources(
resources = relatedResourceMap[relatedResourceKey],
conditionalFhirPathExpression = listResource.conditionalFhirPathExpression,
conditionalFhirPathExpression = interpolatedConditionalFhirPathExpression,
)

// Sort resources if valid sort configuration is provided
Expand Down

0 comments on commit a30225b

Please sign in to comment.