cqframework / cql-execution

A JavaScript framework for executing CQL
Apache License 2.0
65 stars 30 forks source link

Sharing Common Logic expressions returning null #227

Open hmayenve opened 3 years ago

hmayenve commented 3 years ago

Hello, First of all, thank you for working on this repo and taking the time to look into this issue.

For context: I'm using V2 (the JS version of cql-execution) along with cql-exec-fhir to execute the JSON-ELM produced.

We currently have a couple of .cql files with very similar logic expressions. We want to make a library of the common logic expressions to share among the files. It seem as though the common expressions being called from the files are returning null.

For example: If for CMS153_Common.cql we have

library CMS153_Common version '2'

using QUICK

valueset "Female Administrative Sex": 'urn:oid:2.16.840.1.113883.3.560.100.2'
...

context Patient

define ConditionsIndicatingSexualActivity:
  [Condition: "Other Female Reproductive Conditions"]
    union [Condition: "Genital Herpes"]
    union ...

define LaboratoryTestsIndicatingSexualActivity:
  [DiagnosticOrder: "Pregnancy Test"]
    union [DiagnosticOrder: "Pap"]
    union ...

define ResultsPresentForChlamydiaScreening:
  [DiagnosticReport: "Chlamydia Screening"] R where R.result is not null

and we want to use the expressions in CMS153_Common.cql for CMS153.cql:

library CMS153 version '2'

using QUICK

include CMS153_Common version '2' called Common

parameter MeasurementPeriod default Interval[
  @2013-01-01T00:00:00.0,
  @2014-01-01T00:00:00.0
)

context Patient

define Patient16To23AndFemale:
  AgeInYearsAt(start of MeasurementPeriod) >= 16
    and AgeInYearsAt(start of MeasurementPeriod) < 24
    and Patient.gender in Common."Female Administrative Sex"

define SexuallyActive:
  exists (Common.ConditionsIndicatingSexualActivity C
    where Interval[C.onsetDateTime, C.abatementDate] overlaps MeasurementPeriod)
  or exists (Common.LaboratoryTestsIndicatingSexualActivity R
    where R.issued during MeasurementPeriod)

define InInitialPopulation:
  Patient16To23AndFemale and SexuallyActive

define InDenominator:
  true

define InNumerator:
  exists (Common.ResultsPresentForChlamydiaScreening S
    where S.issued during MeasurementPeriod)

The issue is that Common.ResultsPresentForChlamydiaScreening or Common.LaboratoryTestsIndicatingSexualActivity for example would return null whenever used in the CMS153.cql file. It might be the case that somehow the expressions in the CMS153_Common.cql file are not visible in the CMS153.cql file.

For the JS portion: We follow the same process for CMS153_Common.cql as you said in this issue #154 about providing the ELM JSON for FHIRHelpers and then include it as a dependency when you execute the CQL as well as adding it to your json-elm folder then I adding the require statement at the top.

const cms153_common = require('../json-elm/CMS153_Common.json');
const fhirhelpers = require('../json-elm/FHIRHelpers.json');
...
const includedLibs = {
  FHIRHelpers: fhirhelpers,
  CMS153_Common: cms153_common 

};
const lib = new cql.Library(measure, new cql.Repository(includedLibs));

Do you have any ideas why this might be happening?

Thank you and I hope to hear back from you soon.

cmoesel commented 3 years ago

I think that the primary issue is likely that you're using cql-exec-fhir, which supports the FHIR data model, but the CQL is written using the QUICK data model. To get this working, you would either need to provide a different implementation of a PatientSource that supports QUICK or you would need to convert your libraries to use the standard FHIR data model.

It looks like maybe you are just testing with an example from the spec? If so, and if this is for example purposes only, then it's probably best just to convert the logic to the FHIR data model. It looks like it is already pretty close and the main differences will be how you reference the choice elements (e.g., onsetDateTime vs onset as dateTime) -- which changed from FHIR 1.0.2 to FHIR 3.x and 4.x.