google / android-fhir

The Android FHIR SDK is a set of Kotlin libraries for building offline-capable, mobile-first healthcare applications using the HL7® FHIR® standard on Android.
https://google.github.io/android-fhir/
Apache License 2.0
492 stars 293 forks source link

Compile CQL to ELM in the SDK #1365

Closed vitorpamplona closed 2 years ago

vitorpamplona commented 2 years ago

Is your feature request related to a problem? Please describe.

PlanDefinitions can include CQL expressions directly. Those expressions need to be compiled to ELM before running on the device.

Here's a valid Action (subcomponent of PlanDefinition) extracted from EmCare as an example:

{
    "id": "EmCareDT02",
    "description": "Register the child in the encounter",
    "trigger": [
        {
            "type": "named-event",
            "name": "q.EmCareB.Registration.P"
        }
    ],
    "condition": [
        {
            "kind": "applicability",
            "expression": {
                "language": "text/cql-expression",
                "expression": "now() -  5 years > %subject.BirthDate"
            }
        }
    ]
}

The SDK must be able to solve now() - 5 years > %subject.BirthDate.

As another example, the current RuleFilters test case includes a PlanDefinition with a CQL function call:

"condition": [ {
  "kind": "applicability",
  "expression": {
    "description": "Filterable",
    "language": "text/cql.name",
    "expression": "IsReportable"
  }
} ],

The text on expression must be placed inside a CQL library, compiled, and run to execute the IsReportable function.

As a third example, the current RuleFilters test case includes start and stop conditions as CQL:

"condition": [ {
  "kind": "start",
  "expression": {
    "description": "Encounter is active.",
    "language": "text/cql",
    "expression": "encounter.period.start != null and encounter.period.end = null"
  }
}, {
  "kind": "stop",
  "expression": {
    "description": "Encounter is closed.",
    "language": "text/cql",
    "expression": "encounter.period.end != null"
  }
} ]

Again, the text on expression must be placed inside a CQL library, compiled, and run to execute the start and stop conditions.

Describe the solution you'd like

The ideal solution would extract the existing compilation technique from the CQL Translator (PR 748) and expose it to the SDK, through the CQL Evaluator. The solution must also be able to read ModelInfos with Jackson's XML Mapper as opposed to the JAXB unmarshalling, which is awaiting a solution for issue #525

Describe alternatives you've considered

Until now, the SDK was expected to deal with already compiled expressions only, represented as ELM/JSON libraries. This requires the SDK to use a server to compile ELM when uncompiled CQL is present in PlanDefinitions. I don't think that solution is feasible in practice.

Additional context This is related to issues: #1334, #915, #1055, #656 and #718.

Would you like to work on the issue? Yes

vitorpamplona commented 2 years ago

Fixed on https://github.com/google/android-fhir/pull/1603