Using CQL With FHIR
3.0.0-ballot - STU 3 Ballot

Using CQL With FHIR - Downloaded Version null See the Directory of published versions

## Library: FHIR Common

Official URL: http://hl7.org/fhir/uv/cql/Library/FHIRCommon | Version: 3.0.0-ballot | Highlight text changes?
Standards status: Informative | Maturity Level: 4 | Computable Name: FHIRCommon
Other Identifiers: OID:2.16.840.1.113883.4.642.40.37.28.10

This library defines terminologies and functions commonly used in FHIR-based CQL artifacts.

Metadata
Title | FHIR Common
Version | 3.0.0-ballot
Identifier | urn:oid:2.16.840.1.113883.4.642.40.37.28.10
Approval Date | 2025-05-27
Last Review Date | 2025-05-27
Jurisdiction | World
Topic | FHIR
Topic | CQL
Steward (Publisher) | HL7 International / Clinical Decision Support
Description | This library defines terminologies and functions commonly used in FHIR-based CQL artifacts.
Type | logic-library from http://terminology.hl7.org/CodeSystem/library-type
Dependency | Description: Library FHIRHelpers Resource: FHIR Helpers version: 4.0.2-ballot Canonical URL: `http://hl7.org/fhir/uv/cql/Library/FHIRHelpers|4.0.2-ballot`
Dependency | Description: Code System LOINC Resource: LOINC Canonical URL: `http://loinc.org`
Dependency | Description: Code System SNOMEDCT Resource: SNOMED CT (all versions) Canonical URL: `http://snomed.info/sct`
Dependency | Description: Code System ICD10CM Resource: International Classification of Diseases, Tenth Revision, Clinical Modification (ICD-10-CM) Canonical URL: `http://hl7.org/fhir/sid/icd-10-cm`
Dependency | Description: Code System RoleCode Resource: RoleCode Canonical URL: `http://terminology.hl7.org/CodeSystem/v3-RoleCode`
Dependency | Description: Code System Diagnosis Role Resource: Diagnosis Role Canonical URL: `http://terminology.hl7.org/CodeSystem/diagnosis-role`
Dependency | Description: Code System RequestIntent Resource: RequestIntent Canonical URL: `http://hl7.org/fhir/request-intent`
Dependency | Description: Code System MedicationRequestCategory Resource: MedicationRequest Category Codes Canonical URL: `http://terminology.hl7.org/CodeSystem/medicationrequest-category`
Dependency | Description: Code System ConditionClinicalStatusCodes Resource: Condition Clinical Status Codes Canonical URL: `http://terminology.hl7.org/CodeSystem/condition-clinical`
Dependency | Description: Code System ConditionVerificationStatusCodes Resource: ConditionVerificationStatus Canonical URL: `http://terminology.hl7.org/CodeSystem/condition-ver-status`
Dependency | Description: Code System AllergyIntoleranceClinicalStatusCodes Resource: AllergyIntolerance Clinical Status Codes Canonical URL: `http://terminology.hl7.org/CodeSystem/allergyintolerance-clinical`
Dependency | Description: Code System AllergyIntoleranceVerificationStatusCodes Resource: AllergyIntolerance Verification Status Canonical URL: `http://terminology.hl7.org/CodeSystem/allergyintolerance-verification`
Dependency | Description: Code System ConditionCategoryCodes Resource: Condition Category Codes Canonical URL: `http://terminology.hl7.org/CodeSystem/condition-category`
Dependency | Description: Code System ObservationStatusCodes Resource: ObservationStatus Canonical URL: `http://hl7.org/fhir/observation-status`
Dependency | Description: Code System ObservationCategoryCodes Resource: Observation Category Codes Canonical URL: `http://terminology.hl7.org/CodeSystem/observation-category`
Dependency | Description: Code System TaskCodeSystem Resource: Task Codes Canonical URL: `http://hl7.org/fhir/CodeSystem/task-code`
Parameters
Name | Use | Card. | Type | Documentation
Patient | Out | 0..1 | Patient |
Library Content
CQL Content |
    /*
    @description: Common terminologies and functions used in FHIR-based CQL artifacts
    */
    library FHIRCommon version '3.0.0-ballot'

    using FHIR version '4.0.1'

    include FHIRHelpers version '4.0.2-ballot'

    codesystem "LOINC": 'http://loinc.org'
    codesystem "SNOMEDCT": 'http://snomed.info/sct'
    codesystem "ICD10CM": 'http://hl7.org/fhir/sid/icd-10-cm'
    codesystem "RoleCode": 'http://terminology.hl7.org/CodeSystem/v3-RoleCode'
    codesystem "Diagnosis Role": 'http://terminology.hl7.org/CodeSystem/diagnosis-role'
    codesystem "RequestIntent": 'http://hl7.org/fhir/request-intent'
    codesystem "MedicationRequestCategory": 'http://terminology.hl7.org/CodeSystem/medicationrequest-category'
    codesystem "ConditionClinicalStatusCodes": 'http://terminology.hl7.org/CodeSystem/condition-clinical'
    codesystem "ConditionVerificationStatusCodes": 'http://terminology.hl7.org/CodeSystem/condition-ver-status'
    codesystem "AllergyIntoleranceClinicalStatusCodes": 'http://terminology.hl7.org/CodeSystem/allergyintolerance-clinical'
    codesystem "AllergyIntoleranceVerificationStatusCodes": 'http://terminology.hl7.org/CodeSystem/allergyintolerance-verification'
    codesystem "ConditionCategoryCodes": 'http://terminology.hl7.org/CodeSystem/condition-category'
    codesystem "ObservationStatusCodes": 'http://hl7.org/fhir/observation-status'
    codesystem "ObservationCategoryCodes": 'http://terminology.hl7.org/CodeSystem/observation-category'
    codesystem "TaskCodeSystem": 'http://hl7.org/fhir/CodeSystem/task-code'

    code "Birthdate": '21112-8' from "LOINC" display 'Birth date'
    code "Dead": '419099009' from "SNOMEDCT" display 'Dead'
    code "ER": 'ER' from "RoleCode" display 'Emergency room'
    code "ICU": 'ICU' from "RoleCode" display 'Intensive care unit'
    code "Billing": 'billing' from "Diagnosis Role" display 'Billing'

    // Condition Clinical Status Codes - Consider value sets for these
    code "active": 'active' from "ConditionClinicalStatusCodes"
    code "recurrence": 'recurrence' from "ConditionClinicalStatusCodes"
    code "relapse": 'relapse' from "ConditionClinicalStatusCodes"
    code "inactive": 'inactive' from "ConditionClinicalStatusCodes"
    code "remission": 'remission' from "ConditionClinicalStatusCodes"
    code "resolved": 'resolved' from "ConditionClinicalStatusCodes"

    // Condition Verification Status Codes - Consider value sets for these
    code "unconfirmed": 'unconfirmed' from ConditionVerificationStatusCodes
    code "provisional": 'provisional' from ConditionVerificationStatusCodes
    code "differential": 'differential' from ConditionVerificationStatusCodes
    code "confirmed": 'confirmed' from ConditionVerificationStatusCodes
    code "refuted": 'refuted' from ConditionVerificationStatusCodes
    code "entered-in-error": 'entered-in-error' from ConditionVerificationStatusCodes

    code "allergy-active": 'active' from "AllergyIntoleranceClinicalStatusCodes"
    code "allergy-inactive": 'inactive' from "AllergyIntoleranceClinicalStatusCodes"
    code "allergy-resolved": 'resolved' from "AllergyIntoleranceClinicalStatusCodes"

    // Allergy/Intolerance Verification Status Codes - Consider value sets for these
    code "allergy-unconfirmed": 'unconfirmed' from AllergyIntoleranceVerificationStatusCodes
    code "allergy-confirmed": 'confirmed' from AllergyIntoleranceVerificationStatusCodes
    code "allergy-refuted": 'refuted' from AllergyIntoleranceVerificationStatusCodes

    // MedicationRequest Category Codes
    code "Inpatient": 'inpatient' from "MedicationRequestCategory" display 'Inpatient'
    code "Outpatient": 'outpatient' from "MedicationRequestCategory" display 'Outpatient'
    code "Community": 'community' from "MedicationRequestCategory" display 'Community'
    code "Discharge": 'discharge' from "MedicationRequestCategory" display 'Discharge'

    // Diagnosis Role Codes
    code "AD": 'AD' from "Diagnosis Role" display 'Admission diagnosis'
    code "DD": 'DD' from "Diagnosis Role" display 'Discharge diagnosis'
    code "CC": 'CC' from "Diagnosis Role" display 'Chief complaint'
    code "CM": 'CM' from "Diagnosis Role" display 'Comorbidity diagnosis'
    code "pre-op": 'pre-op' from "Diagnosis Role" display 'pre-op diagnosis'
    code "post-op": 'post-op' from "Diagnosis Role" display 'post-op diagnosis'
    code "billing": 'billing' from "Diagnosis Role" display 'billing diagnosis'

    // Observation Status Codes
    code "observation-registered": 'registered' from "ObservationStatusCodes" display 'Registered'
    code "observation-preliminary": 'preliminary' from "ObservationStatusCodes" display 'Preliminary'
    code "observation-final": 'final' from "ObservationStatusCodes" display 'Final'
    code "observation-amended": 'amended' from "ObservationStatusCodes" display 'Amended'
    code "observation-corrected": 'corrected' from "ObservationStatusCodes" display 'Corrected'
    code "observation-cancelled": 'cancelled' from "ObservationStatusCodes" display 'Cancelled'
    code "observation-entered-in-error": 'entered-in-error' from "ObservationStatusCodes" display 'Entered in Error'
    code "observation-unknown": 'unknown' from "ObservationStatusCodes" display 'Unknown'

    // Observation Category Codes
    code "social-history": 'social-history' from "ObservationCategoryCodes" display 'Social History'
    code "vital-signs": 'vital-signs' from "ObservationCategoryCodes" display 'Vital Signs'
    code "imaging": 'imaging' from "ObservationCategoryCodes" display 'Imaging'
    code "laboratory": 'laboratory' from "ObservationCategoryCodes" display 'Laboratory'
    code "procedure": 'procedure' from "ObservationCategoryCodes" display 'Procedure'
    code "survey": 'survey' from "ObservationCategoryCodes" display 'Survey'
    code "exam": 'exam' from "ObservationCategoryCodes" display 'Exam'
    code "therapy": 'therapy' from "ObservationCategoryCodes" display 'Therapy'
    code "activity": 'activity' from "ObservationCategoryCodes" display 'Activity'

    // Condition Category Codes
    code "problem-list-item": 'problem-list-item' from "ConditionCategoryCodes" display 'Problem List Item'
    code "encounter-diagnosis": 'encounter-diagnosis' from "ConditionCategoryCodes" display 'Encounter Diagnosis'

    //Task codes
    code "Fulfill": 'fulfill' from "TaskCodeSystem" display 'Fulfill'

    context Patient

    /*
    @description: Returns the age in days of the patient as of the given date, using birth time if known
    @comment: This function returns the number of whole days between the patient birth date
    and the asOf date. If the patient has a birthTime, the calculation is performed using the
    birthDateTime, and the time component of the asOf parameter is considered. Note that when
    the birth time is known, timezone offset normalization will be used.
    */
    define fluent function ageInDaysAt(patient FHIR.Patient, asOf System.DateTime):
      if patient.birthTime() is not null then
        CalculateAgeInDaysAt(patient.birthDateTime(), asOf)
      else
        CalculateAgeInDaysAt(patient.birthDate, date from asOf)

    /*
    @description: Returns the current age in days of the patient, using birth time if known
    @comment: This function returns the number of whole days between the patient birth date
    and the current date. If the patient has a birthTime, the calculation is performed using the
    birthDateTime and Now(), otherwise the calculation is performed using Today(). Note that when
    the birth time is known, timezone offset normalization will be used.
    */
    define fluent function ageInDays(patient FHIR.Patient):
      if patient.birthTime() is not null then
        CalculateAgeInDaysAt(patient.birthDateTime(), Now())
      else
        CalculateAgeInDaysAt(patient.birthDate, Today())

    /*
    @description: Returns the age in months of the patient as of the given date, using birth time if known
    @comment: This function returns the number of whole calendar months between the patient birth date
    and the asOf date. If the patient has a birthTime, the calculation is performed using the
    birthDateTime, and the time component of the asOf parameter is considered. Note that when
    the birth time is known, timezone offset normalization will be used.
    */
    define fluent function ageInMonthsAt(patient FHIR.Patient, asOf System.DateTime):
      if patient.birthTime() is not null then
        CalculateAgeInMonthsAt(patient.birthDateTime(), asOf)
      else
        CalculateAgeInMonthsAt(patient.birthDate, date from asOf)

    /*
    @description: Returns the current age in months of the patient, using birth time if known
    @comment: This function returns the number of whole calendar months between the patient birth date
    and the current date. If the patient has a birthTime, the calculation is performed using the
    birthDateTime and Now(), otherwise the calculation is performed using Today(). Note that when
    the birth time is known, timezone offset normalization will be used.
    */
    define fluent function ageInMonths(patient FHIR.Patient):
      if patient.birthTime() is not null then
        CalculateAgeInMonthsAt(patient.birthDateTime(), Now())
      else
        CalculateAgeInMonthsAt(patient.birthDate, Today())

    /*
    @description: Returns the age in years of the patient, as of the given date
    @comment: This function returns the number of whole calendar years between the patient birth
    date and the given date. Regardless of whether the patient has a birthTime, the calculation is
    performed using only the birth date. If the given date has a time component, it is ignored, on
    the grounds that birth time is almost universally not considered when determining age in years.
    */
    define fluent function ageInYearsAt(patient FHIR.Patient, asOf System.DateTime):
      CalculateAgeInYearsAt(patient.birthDate, date from asOf)

    /*
    @description: Returns the current age in years of the patient
    @comment: This function returns the number of whole calendar years between the patient birth
    date and the current date. Regardless of whether the patient has a birthTime, the calculation is
    performed using only the birth date and Today(), on the grounds that birth time is almost universally
    not considered when determining age in years.
    */
    define fluent function ageInYears(patient FHIR.Patient):
      CalculateAgeInYearsAt(patient.birthDate, Today())

    /*
    @description: Returns the date when the given patient turned the given age
    */
    define fluent function whenTurned(patient FHIR.Patient, age System.Quantity):
      case
        when age.unit in { 'year', 'years' } then ToDateTime(patient.birthDate) + age
        else patient.birthDateTime() + age
      end

    /*
    @description: Returns the interval of time during which the patient was the given age
    */
    define fluent function whenAged(patient FHIR.Patient, age System.Quantity):
      case
        when age.unit in { 'year', 'years' } then Interval[ToDateTime(patient.birthDate) + age, ToDateTime(patient.birthDate) + age + 1 year)
        when age.unit in { 'month', 'months' } then Interval[patient.birthDateTime() + age, patient.birthDateTime() + age + 1 month)
        when age.unit in { 'week', 'weeks' } then Interval[patient.birthDateTime() + age, patient.birthDateTime() + age + 1 week)
        when age.unit in { 'day', 'days' } then Interval[patient.birthDateTime() + age, patient.birthDateTime() + age + 1 day)
        else null
      end

    /*
    @description: Returns the interval of time during which the patient was aged in the given range
    */
    define fluent function whenAged(patient FHIR.Patient, ageRange Interval<System.Quantity>):
      Interval[patient.whenAged(ageRange.low).low, patient.whenAged(ageRange.high).high)

    /*
    @description: Normalizes a value that is a choice of timing-valued types to an equivalent interval
    @comment: Normalizes a choice type of FHIR.date, FHIR.dateTime, FHIR.Period, FHIR.Timing, FHIR.instance, FHIR.string, FHIR.Age, or FHIR.Range types
    to an equivalent interval. This selection of choice types is a superset of the majority of choice types that are used as possible
    representations for timing-valued elements in FHIR, allowing this function to be used across any resource.

    The input can be provided as a date, dateTime, Period, Timing, instant, string, Age, or Range.
    The intent of this function is to provide a clear and concise mechanism to treat single
    elements that have multiple possible representations as intervals so that logic doesn't have to account
    for the variability. More complex calculations (such as medication request period or dispense period
    calculation) need specific guidance and consideration. That guidance may make use of this function, but
    the focus of this function is on single element calculations where the semantics are unambiguous.
    If the input is a date, the result is a DateTime Interval beinning and ending on that date, converted to a DateTime.
    If the input is a dateTime, the result is a DateTime Interval beginning and ending on that dateTime.
    If the input is a Period, the result is a DateTime Interval.
    If the input is a Timing, an error is raised indicating a single interval cannot be computed from a Timing.
    If the input is an instant, the result is a DateTime Interval beginning and ending on that instant.
    If the input is a string, an error is raised indicating a single interval cannot be computed from a string.
    If the input is an Age, the result is the interval during which the patient was that age.
    If the input is a Range, the result is the interval during which the patient was the low age to the high age in the range.

    NOTE: Due to the
    complexity of determining a single interval from a Timing or String type, this function will throw a run-time exception if it is used
    with a Timing or String.
    */
    define fluent function toInterval(choice Choice<FHIR.date, FHIR.dateTime, FHIR.Period, FHIR.Timing, FHIR.instant, FHIR.string, FHIR.Age, FHIR.Range>):
      case
        when choice is FHIR.date then
          Interval[ToDateTime(FHIRHelpers.ToDate(choice as FHIR.date)), ToDateTime(FHIRHelpers.ToDate(choice as FHIR.date))]
        when choice is FHIR.dateTime then
          Interval[FHIRHelpers.ToDateTime(choice as FHIR.dateTime), FHIRHelpers.ToDateTime(choice as FHIR.dateTime)]
        when choice is FHIR.Period then
          FHIRHelpers.ToInterval(choice as FHIR.Period)
        when choice is FHIR.instant then
          Interval[FHIRHelpers.ToDateTime(choice as FHIR.instant), FHIRHelpers.ToDateTime(choice as FHIR.instant)]
        when choice is FHIR.Age then
          Patient.whenAged(FHIRHelpers.ToQuantity(choice as FHIR.Age))
        when choice is FHIR.Range then
          Patient.whenAged(FHIRHelpers.ToInterval(choice as FHIR.Range))
        when choice is FHIR.Timing then
          Message(null as Interval<DateTime>, true, 'NOT_IMPLEMENTED', 'Error', 'Calculation of an interval from a Timing value is not supported')
        when choice is FHIR.string then
          Message(null as Interval<DateTime>, true, 'NOT_IMPLEMENTED', 'Error', 'Calculation of an interval from a String value is not supported')
        else
          null as Interval<DateTime>
      end

    /*
    @description: Given an interval, return true if the interval has a starting boundary specified
    (i.e. the start of the interval is not null and not the minimum DateTime value)
    */
    define fluent function hasStart(period Interval<DateTime>):
      not ( start of period is null
          or start of period = minimum DateTime
      )

    /*
    @description: Given an interval, returns true if the interval has an ending boundary specified
    (i.e. the end of the interval is not null and not the maximum DateTime value)
    */
    define fluent function hasEnd(period Interval<DateTime>):
      not (
        end of period is null
          or end of period = maximum DateTime
      )

    /*
    @description: Given an interval, returns the ending point if the interval has an ending boundary specified,
    otherwise, returns the starting point
    */
    define fluent function latest(choice Choice<FHIR.dateTime, FHIR.Period, FHIR.Age, FHIR.Range>):
      (choice.toInterval()) period
        return
          if (period.hasEnd()) then end of period
          else start of period

    /*
    @description: Given an interval, return the starting point if the interval has a starting boundary specified,
    otherwise, return the ending point
    */
    define fluent function earliest(choice Choice<FHIR.dateTime, FHIR.Period, FHIR.Age, FHIR.Range>):
      (choice.toInterval()) period
        return
          if (period.hasStart()) then start of period
          else end of period

    /*
    @description: Returns true if the given reference is to the given resource
    @comment: Returns true if the `id` element of the given resource exactly equals the tail of the given reference.
    NOTE: This function assumes resources from the same source server.
    */
    define fluent function references(reference FHIR.Reference, resource FHIR.Resource):
      resource.id = Last(Split(reference.reference, '/'))

    /*
    @description: Returns true if the given reference is to the given resourceId
    @comment: Returns true if the `resourceId` parameter exactly equals the tail of the given reference.
    NOTE: This function assumes resources from the same source server.
    */
    define fluent function references(reference FHIR.Reference, resourceId String):
      resourceId = Last(Split(reference.reference, '/'))

    /*
    @description: Returns true if any of the given references are to the given resource
    @comment: Returns true if the `id` element of the given resource exactly equals the tail of any of the given references.
    NOTE: This function assumes resources from the same source server.
    */
    define fluent function references(references List<FHIR.Reference>, resource FHIR.Resource):
      exists (references R where R.references(resource))

    /*
    @description: Returns true if any of the given references are to the given resourceId
    @comment: Returns true if the `resourceId` parameter exactly equals the tail of any of the given references.
    NOTE: This function assumes resources from the same source server.
    */
    define fluent function references(references List<FHIR.Reference>, resourceId String):
      exists (references R where R.references(resourceId))

    /*
    @description: Returns true if the given code is in the given codeList
    @comment: Returns true if the `code` is equivalent to any of the codes in the given `codeList`, false otherwise.
    */
    define fluent function includesCode(codeList List<CodeableConcept>, code Concept):
      exists (codeList C where C ~ code)

    /*
    @description: Returns the canonical reference to the ValueSet referred to by the codeOptions extension, if present
    @comment: This function is usable on any CodeableConcept, and looks for the [codeOptions](https://hl7.org/fhir/extensions/StructureDefinition-codeOptions.html)
    extension, returning the canonical reference to the ValueSet identifying the set of possible codes for the activity.
    @seealso: [Activity Extent](https://build.fhir.org/ig/HL7/cql-ig/patterns.html#activity-extent)
    */
    define fluent function codeOptions(code CodeableConcept):
      (code.ext('http://hl7.org/fhir/StructureDefinition/codeOptions').value as canonical)

    /*
    @description: Returns the url portion of the given canonical reference (as a string)
    @comment: This function returns the url portion of the given canonical reference (i.e. everything up to but not including the version pipe (`|`) and version).
    If the canonical reference does not have a version, this function returns the canonical reference.
    For example, given the canonical reference `http://example.org/ValueSet/ABC|123`, this function returns `http://example.org/ValueSet/ABC`.
    */
    define fluent function url(canonical FHIR.canonical):
      Split(canonical, '|').first()

    /*
    @description: Returns the version portion of the given canonical reference (as a string)
    @comment: This function returns the version portion of the given canonical reference (i.e. everything after the version pipe (`|`), if present).
    If the canonical reference does not have a version, this function returns `null`.
    For example, given the canonical reference `http://example.org/ValueSet/ABC|123`, this function returns `123`.
    */
    define fluent function version(canonical FHIR.canonical):
      Split(canonical, '|').skip(1).first()

    /*
    @description: Returns any extensions defined on the given resource with the specified url
    */
    define fluent function exts(domainResource DomainResource, url String):
      domainResource.extension E
        where E.url = url
        return E

    /*
    @description: Returns the single extension (if present) on the given resource with the specified url.
    @comment: This function uses singleton from to ensure that a run-time exception is thrown if there
    is more than one extension on the given resource with the specified url.
    */
    define fluent function ext(domainResource DomainResource, url String):
      singleton from domainResource.exts(url)

    /*
    @description: Returns any extensions defined on the given element with the specified url.
    */
    define fluent function exts(element Element, url String):
      element.extension E
        where E.url = url
        return E

    /*
    @description: Returns the single extension (if present) on the given element with the specified url.
    @comment: This function uses singleton from to ensure that a run-time exception is thrown if there
    is more than one extension on the given element with the specified url.
    */
    define fluent function ext(element Element, url String):
      singleton from element.exts(url)

    /*
    @description: Returns any modifier extensions defined on the given resource with the specified url.
    */
    define fluent function modifierExts(domainResource DomainResource, url String):
      domainResource.modifierExtension E
        where E.url = url
        return E

    /*
    @description: Returns the single modifier extension (if present) on the given resource with the specified url.
    @comment: This function uses singleton from to ensure that a run-time exception is thrown if there
    is more than one extension on the given resource with the specified url.
    */
    define fluent function modifierExt(domainResource DomainResource, url String):
      singleton from domainResource.modifierExts(url)

    /*
    @description: Returns any modifier extensions defined on the given element with the specified url.
    */
    define fluent function modifierExts(element BackboneElement, url String):
      element.modifierExtension E
        where E.url = url
        return E

    /*
    @description: Returns the single modifier extension (if present) on the given element with the specified url.
    @comment: This function uses singleton from to ensure that a run-time exception is thrown if there
    is more than one extension on the given element with the specified url.
    */
    define fluent function modifierExt(element BackboneElement, url String):
      singleton from element.modifierExts(url)

    /*
    @description: Returns the birthTime of the patient, if present
    @comment: This function returns the value of the birthTime extension, if present, null otherwise.
    */
    define fluent function birthTime(patient Patient):
      patient.birthDate.ext('http://hl7.org/fhir/StructureDefinition/patient-birthTime').value as dateTime

    /*
    @description: Returns the birth date (and time if present) (as a DateTime)
    @comment: This function returns the birthTime of the patient, if present, else the birthDate of the patient as a DateTime
    */
    define fluent function birthDateTime(patient Patient):
      Coalesce(patient.birthTime().value, patient.birthDate.toDateTime())

    /*
    @description: Returns all [sexParameterForClinicalUse](https://hl7.org/fhir/extensions/StructureDefinition-patient-sexParameterForClinicalUse.html) elements for the patient
    */
    define fluent function sexParameterForClinicalUse(patient FHIR.Patient):
      (patient.exts('http://hl7.org/fhir/StructureDefinition/patient-sexParameterForClinicalUse')) E
        return {
          value: E.ext('value').value as FHIR.CodeableConcept,
          period: E.ext('period').value as FHIR.Period,
          comment: E.ext('comment').value as FHIR.string,
          supportingInfo: (E.exts('supportingInfo')) SI return SI.value as Choice<FHIR.CodeableConcept, FHIR.Reference>,
          intendedClinicalUse: (E.exts('intendedClinicalUse')) CU return CU.value as Choice<FHIR.CodeableConcept, FHIR.Reference>
        }

    /*
    @description: Returns all [sexParameterForClinicalUse](https://hl7.org/fhir/extensions/StructureDefinition-patient-sexParameterForClinicalUse.html)
    elements for the patient applicable as of the given date and time
    @comment: Returns sexParameterForClinicalUse elements defined for the patient that either have no period specified, or have a period that includes
    the specified asOf. Elements are returned in descending chronological order by the start of the period element (so the most recent element is first).
    */
    define fluent function sexParameterForClinicalUse(patient FHIR.Patient, asOf System.DateTime):
      (patient.sexParameterForClinicalUse()) SPCU
        where asOf is null or SPCU.period is null or asOf during SPCU.period
        sort by start of period desc

    /*
    @description: Returns true if the given condition has the given category
    */
    define fluent function hasCategory(condition FHIR.Condition, category Code):
      exists (condition.category C
        where C ~ category
      )

    /*
    @description: Returns true if the given condition is a problem list item.
    */
    define fluent function isProblemListItem(condition FHIR.Condition):
      exists (condition.category C
        where C ~ "problem-list-item"
      )

    /*
    @description: Returns true if the given condition is an encounter diagnosis
    */
    define fluent function isEncounterDiagnosis(condition FHIR.Condition):
      exists (condition.category C
        where C ~ "encounter-diagnosis"
      )

    /*
    @description: Returns the assertedDate for a given condition, i.e. the date on which the existence of the condition was first asserted or acknowledged
    */
    define fluent function assertedDate(condition FHIR.Condition):
      condition.ext('http://hl7.org/fhir/StructureDefinition/condition-assertedDate').value as FHIR.dateTime

    /*
    @description: Returns an interval representing the normalized Abatement of a given Condition resource.
    @comment: @comment: If the abatement element of the Condition is represented as a DateTime, the result
    is an interval beginning and ending on that DateTime.
    If the abatement is represented as a Quantity, the quantity is expected to be a calendar-duration and is interpreted as the age of the patient. The
    result is the interval during which the patient was that age.
    If the abatement is represented as a Quantity Interval, the quantities are expected to be calendar-durations and are interpreted as an age range during
    which the abatement occurred. The result is the interval during which the patient was the ages in the range.

    NOTE: Due to the complexity of determining an interval from a String, this function will throw
    a run-time exception if used with a Condition instance that has a String as the abatement value.
    */
    define fluent function abatementInterval(condition Condition):
      if condition.abatement is FHIR.dateTime then
        Interval[FHIRHelpers.ToDateTime(condition.abatement as FHIR.dateTime), FHIRHelpers.ToDateTime(condition.abatement as FHIR.dateTime)]
      else if condition.abatement is FHIR.Period then
        FHIRHelpers.ToInterval(condition.abatement as FHIR.Period)
      else if condition.abatement is FHIR.string then
        Message(null as Interval<DateTime>, true, 'NOT_IMPLEMENTED', 'Error', 'Calculation of an interval from a String value is not supported')
      else if condition.abatement is FHIR.Age then
        Patient.whenAged(FHIRHelpers.ToQuantity(condition.abatement as FHIR.Age))
      else if condition.abatement is FHIR.Range then
        Patient.whenAged(FHIRHelpers.ToInterval(condition.abatement as FHIR.Range))
      else if condition.abatement is FHIR.boolean then
        Interval[end of condition.onset.toInterval(), condition.recordedDate)
      else
        null

    /*
    @description: Returns an interval representing the normalized prevalence period of a given Condition resource.
    @comment: Uses the toInterval and toAbatementInterval functions to determine the widest potential interval from
    onset to abatement as specified in the given Condition.
    */
    define fluent function prevalenceInterval(condition Condition):
    if condition.clinicalStatus ~ "active"
      or condition.clinicalStatus ~ "recurrence"
      or condition.clinicalStatus ~ "relapse" then
      Interval[start of condition.onset.toInterval(), end of condition.abatementInterval()]
    else
      (end of condition.abatementInterval()) abatementDate
        return
    	  if abatementDate is null then
            Interval[start of condition.onset.toInterval(), abatementDate)
          else
            Interval[start of condition.onset.toInterval(), abatementDate]

    /*
    @description: Returns true if the given condition has a clinical status of active, recurrence, or relapse
    */
    define fluent function isActive(condition FHIR.Condition):
      condition.clinicalStatus ~ "active"
        or condition.clinicalStatus ~ "recurrence"
        or condition.clinicalStatus ~ "relapse"

    /*
    @description: Returns conditions in the given list that have a clinical status of active, recurrence, or relapse
    */
    define fluent function active(conditions List<FHIR.Condition>):
      conditions C
        where C.clinicalStatus ~ "active"
          or C.clinicalStatus ~ "recurrence"
          or C.clinicalStatus ~ "relapse"

    /*
    @description: Returns true if the given condition either has no verification status or has a verification status of confirmed, unconfirmed, provisional, or differential
    */
    define fluent function isVerified(condition FHIR.Condition):
      condition.verificationStatus is not null implies
        (condition.verificationStatus ~ "confirmed"
          or condition.verificationStatus ~ "unconfirmed"
          or condition.verificationStatus ~ "provisional"
          or condition.verificationStatus ~ "differential"
        )

    /*
    @description: Returns conditions in the given list that either have no verification status or have a verification status of confirmed, unconfirmed, provisional, or differential
    */
    define fluent function verified(conditions List<FHIR.Condition>):
      conditions C
        where C.verificationStatus is not null implies
          (C.verificationStatus ~ "confirmed"
            or C.verificationStatus ~ "unconfirmed"
            or C.verificationStatus ~ "provisional"
            or C.verificationStatus ~ "differential"
          )

    /*
    @description: Returns true if the given condition has a verification status of unconfirmed
    */
    define fluent function isUnconfirmed(condition FHIR.Condition):
      condition.verificationStatus ~ "unconfirmed"

    /*
    @description: Returns conditions in the given list that have a verification status of unconfirmed
    */
    define fluent function unconfirmed(conditions List<FHIR.Condition>):
      conditions C
        where C.verificationStatus ~ "unconfirmed"

    /*
    @description: Returns true if the given condition has a verification status of provisional
    */
    define fluent function isProvisional(condition FHIR.Condition):
      condition.verificationStatus ~ "provisional"

    /*
    @description: Returns conditions in the given list that have a verification status of provisional
    */
    define fluent function provisional(conditions List<FHIR.Condition>):
      conditions C
        where C.verificationStatus ~ "provisional"

    /*
    @description: Returns true if the given condition has a verification status of differential
    */
    define fluent function isDifferential(condition FHIR.Condition):
      condition.verificationStatus ~ "differential"

    /*
    @description: Returns conditions in the given list that have a verification status of differential
    */
    define fluent function differential(conditions List<FHIR.Condition>):
      conditions C
        where C.verificationStatus ~ "differential"

    /*
    @description: Returns true if the given condition has a verification status of confirmed
    */
    define fluent function isConfirmed(condition FHIR.Condition):
      condition.verificationStatus ~ "confirmed"

    /*
    @description: Returns conditions in the given list that have a verification status of confirmed
    */
    define fluent function confirmed(conditions List<FHIR.Condition>):
      conditions C
        where C.verificationStatus ~ "confirmed"

    /*
    @description: Returns true if the given condition has a verification status of refuted
    */
    define fluent function isRefuted(condition FHIR.Condition):
      condition.verificationStatus ~ "refuted"

    /*
    @description: Returns conditions in the given list that have a verification status of refuted
    */
    define fluent function refuted(conditions List<FHIR.Condition>):
      conditions C
        where C.verificationStatus ~ "refuted"

    /*
    @description: Returns the abatement of the given AllergyIntolerance, if present
    */
    define fluent function abatement(allergyIntolerance AllergyIntolerance):
      allergyIntolerance.ext('http://hl7.org/fhir/StructureDefinition/allergyintolerance-abatement').value
        as Choice<dateTime, Age, Period, Range, string>

    /*
    @description: Returns the resolutionAge of the given AllergyIntolerance, if present
    */
    define fluent function resolutionAge(allergyIntolerance AllergyIntolerance):
      allergyIntolerance.ext('http://hl7.org/fhir/StructureDefinition/allergyintolerance-resolutionAge').value
        as Age

    /*
    @description: Returns the assertedDate for a given allergy/intolerance, i.e. the date on which the existence of the allergy/intolerance was first asserted or acknowledged
    */
    define fluent function assertedDate(allergyIntolerance FHIR.AllergyIntolerance):
      allergyIntolerance.ext('http://hl7.org/fhir/StructureDefinition/condition-assertedDate').value as FHIR.dateTime

    /*
    @description: Returns an interval representing the normalized abatement of a given AllergyIntolerance resource.
    @comment: This function uses the abatement extension, if present, otherwise the resolutionAge.
    If the abatement of the AllergyIntolerance is a DateTime, the result is an interval beginning and ending on that DateTime.
    If the abatement is a Period, the result is the Period converted to an Interval of DateTime.
    If the abatement is an Age, the result is the interval during which the patient was the abatement age.
    If the abatement is a Range, the quantities are expected to be calendar-durations and are interpreted as an age range during
    which the abatement occurred. The result is the interval during which the patient was the ages in the abatement range.
    If the resolutionAge is present, the result is the interval during which the patient was the resolution age.

    NOTE: Due to the complexity of determining an interval from a String, this function will throw
    a run-time exception if used with a Condition instance that has a String as the abatement value.
    */
    define fluent function abatementInterval(allergyIntolerance AllergyIntolerance):
      allergyIntolerance A
        let
          abatement: A.abatement(),
          resolutionAge: A.resolutionAge()
        return
          if abatement is FHIR.dateTime then
            Interval[FHIRHelpers.ToDateTime(abatement as FHIR.dateTime), FHIRHelpers.ToDateTime(abatement as FHIR.dateTime)]
          else if abatement is FHIR.Period then
            FHIRHelpers.ToInterval(abatement as FHIR.Period)
          else if abatement is FHIR.string then
            Message(null as Interval<DateTime>, true, 'NOT_IMPLEMENTED', 'Error', 'Calculation of an interval from a String value is not supported')
          else if abatement is FHIR.Age then
            Patient.whenAged(FHIRHelpers.ToQuantity(abatement as FHIR.Age))
          else if abatement is FHIR.Range then
            Patient.whenAged(FHIRHelpers.ToInterval(abatement as FHIR.Range))
          else if abatement is FHIR.boolean then
            Interval[end of A.onset.toInterval(), A.recordedDate)
          else if resolutionAge is not null then
            Patient.whenAged(FHIRHelpers.ToQuantity(resolutionAge))
          else
            null

    /*
    @description: Returns an interval representing the normalized prevalence period of a given Condition resource.
    @comment: Uses the toInterval and toAbatementInterval functions to determine the widest potential interval from
    onset to abatement as specified in the given Condition.
    */
    define fluent function prevalenceInterval(allergyIntolerance AllergyIntolerance):
    if allergyIntolerance.clinicalStatus ~ "allergy-active" then
      Interval[start of allergyIntolerance.onset.toInterval(), end of allergyIntolerance.abatementInterval()]
    else
      (end of allergyIntolerance.abatementInterval()) abatementDate
        return
          if abatementDate is null then
            Interval[start of allergyIntolerance.onset.toInterval(), abatementDate)
          else
            Interval[start of allergyIntolerance.onset.toInterval(), abatementDate]

    /*
    @description: Returns true if the given allergy has a clinical status of active
    */
    define fluent function isActive(allergyIntolerance FHIR.AllergyIntolerance):
      allergyIntolerance.clinicalStatus ~ "allergy-active"

    /*
    @description: Returns allergies in the given list that have a clinical status of active
    */
    define fluent function active(allergyIntolerances List<FHIR.AllergyIntolerance>):
      allergyIntolerances A
        where A.clinicalStatus ~ "allergy-active"

    /*
    @description: Returns true if the given allergy/intolerance has a clinical status of inactive
    */
    define fluent function isInactive(allergyIntolerance FHIR.AllergyIntolerance):
      allergyIntolerance.clinicalStatus ~ "allergy-inactive"

    /*
    @description: Returns allergies in the given list that have a clinical status of inactive
    */
    define fluent function inactive(allergyIntolerances List<FHIR.AllergyIntolerance>):
      allergyIntolerances A
        where A.clinicalStatus ~ "allergy-inactive"

    /*
    @description: Returns true if the given allergy/intolerance has a clinical status of resolved
    */
    define fluent function isResolved(allergyIntolerance FHIR.AllergyIntolerance):
      allergyIntolerance.clinicalStatus ~ "allergy-resolved"

    /*
    @description: Returns allergies in the given list that have a clinical status of resolved
    */
    define fluent function resolved(allergyIntolerances List<FHIR.AllergyIntolerance>):
      allergyIntolerances A
        where A.clinicalStatus ~ "allergy-resolved"

    /*
    @description: Returns true if the given allergy/intolerance either has no verification status or has a verification status of confirmed or unconfirmed
    */
    define fluent function isVerified(allergyIntolerance FHIR.AllergyIntolerance):
      allergyIntolerance.verificationStatus is not null implies
        (allergyIntolerance.verificationStatus ~ "allergy-confirmed"
          or allergyIntolerance.verificationStatus ~ "allergy-unconfirmed"
        )

    /*
    @description: Returns allergies/intolerances in the given list that either have no verification status or have a verification status of confirmed or unconfirmed
    */
    define fluent function verified(allergyIntolerances List<FHIR.AllergyIntolerance>):
      allergyIntolerances A
        where A.verificationStatus is not null implies
          (A.verificationStatus ~ "allergy-confirmed"
            or A.verificationStatus ~ "allergy-unconfirmed"
          )

    /*
    @description: Returns true if the given allergy/intolerance has a verification status of confirmed
    */
    define fluent function isConfirmed(allergyIntolerance FHIR.AllergyIntolerance):
      allergyIntolerance.verificationStatus ~ "allergy-confirmed"

    /*
    @description: Returns allergies/intolerances in the given list that have a verification status of confirmed
    */
    define fluent function confirmed(allergyIntolerances List<FHIR.AllergyIntolerance>):
      allergyIntolerances A
        where A.verificationStatus ~ "allergy-confirmed"

    /*
    @description: Returns true if the given allergy/intolerance has a verification status of unconfirmed
    */
    define fluent function isUnconfirmed(allergyIntolerance FHIR.AllergyIntolerance):
      allergyIntolerance.verificationStatus ~ "allergy-unconfirmed"

    /*
    @description: Returns allergies/intolerances in the given list that have a verification status of unconfirmed
    */
    define fluent function unconfirmed(allergyIntolerances List<FHIR.AllergyIntolerance>):
      allergyIntolerances A
        where A.verificationStatus ~ "allergy-unconfirmed"

    /*
    @description: Returns true if the given allergy/intolerance has a verification status of refuted
    */
    define fluent function isRefuted(allergyIntolerance FHIR.AllergyIntolerance):
      allergyIntolerance.verificationStatus ~ "allergy-refuted"

    /*
    @description: Returns allergies/intolerances in the given list that have a verification status of refuted
    */
    define fluent function refuted(allergyIntolerances List<FHIR.AllergyIntolerance>):
      allergyIntolerances A
        where A.verificationStatus ~ "allergy-refuted"

    /*
    @description: Returns true if the given observation has the given category
    */
    define fluent function hasCategory(observation FHIR.Observation, category Code):
      exists (observation.category C
        where C ~ category
      )

    /*
    @description: Returns true if the given observation is a social history observation
    */
    define fluent function isSocialHistory(observation FHIR.Observation):
      exists (observation.category C
        where C ~ "social-history"
      )

    /*
    @description: Returns true if the given observation is a vital sign
    */
    define fluent function isVitalSign(observation FHIR.Observation):
      exists (observation.category C
        where C ~ "vital-signs"
      )

    /*
    @description: Returns true if the given observation is an imaging observation
    */
    define fluent function isImaging(observation FHIR.Observation):
      exists (observation.category C
        where C ~ "imaging"
      )

    /*
    @description: Returns true if the given observation is a laboratory observation
    */
    define fluent function isLaboratory(observation FHIR.Observation):
      exists (observation.category C
        where C ~ "laboratory"
      )

    /*
    @description: Returns true if the given observation is a procedure observation
    */
    define fluent function isProcedure(observation FHIR.Observation):
      exists (observation.category C
        where C ~ "procedure"
      )

    /*
    @description: Returns true if the given observation is a survey observation
    */
    define fluent function isSurvey(observation FHIR.Observation):
      exists (observation.category C
        where C ~ "survey"
      )

    /*
    @description: Returns true if the given observation is an exam observation
    */
    define fluent function isExam(observation FHIR.Observation):
      exists (observation.category C
        where C ~ "exam"
      )

    /*
    @description: Returns true if the given observation is a therapy observation
    */
    define fluent function isTherapy(observation FHIR.Observation):
      exists (observation.category C
        where C ~ "therapy"
      )

    /*
    @description: Returns true if the given observation is an activity observation
    */
    define fluent function isActivity(observation FHIR.Observation):
      exists (observation.category C
        where C ~ "activity"
      )

    /*
    @description: Returns true if the given observation has status final, amended, or corrected
    */
    define fluent function isResulted(observation FHIR.Observation):
      observation.status ~ "observation-final".code
        or observation.status ~ "observation-amended".code
        or observation.status ~ "observation-corrected".code

    /*
    @description: Returns observations in the given list with a status of final, amended, or corrected
    */
    define fluent function resulted(observations List<FHIR.Observation>):
      observations observation
        where observation.status ~ "observation-final".code
          or observation.status ~ "observation-amended".code
          or observation.status ~ "observation-corrected".code

    /*
    @description: Returns true if the given observation has a status of final
    */
    define fluent function isFinal(observation FHIR.Observation):
      observation.status ~ "observation-final".code

    /*
    @description: Returns observations in the given list with a status of final
    */
    define fluent function final(observations List<FHIR.Observation>):
      observations observation
        where observation.status ~ "observation-final".code

    /*
    @description: Returns true if the given observation has a status of amended
    */
    define fluent function isAmended(observation FHIR.Observation):
      observation.status ~ "observation-amended".code

    /*
    @description: Returns observations in the given list with a status of amended
    */
    define fluent function amended(observations List<FHIR.Observation>):
      observations observation
        where observation.status ~ "observation-amended".code

    /*
    @description: Returns true if the given observation has a status of corrected
    */
    define fluent function isCorrected(observation FHIR.Observation):
      observation.status ~ "observation-corrected".code

    /*
    @description: Returns observations in the given list with a status of corrected
    */
    define fluent function corrected(observations List<FHIR.Observation>):
      observations observation
        where observation.status ~ "observation-corrected".code

    /*
    @description: Returns true if the given MedicationRequest has a category of Community
    */
    define fluent function isCommunity(medicationRequest MedicationRequest):
      exists (medicationRequest.category C
        where C ~ Community
      )

    /*
    @description: Returns true if the given MedicationRequest has a category of Discharge
    */
    define fluent function isDischarge(medicationRequest MedicationRequest):
      exists (medicationRequest.category C
        where C ~ Discharge
      )

    /*
    @description: Returns true if the given MedicationRequest has the given category
    */
    define fluent function hasCategory(medicationRequest MedicationRequest, category Code):
      exists (medicationRequest.category C
        where C ~ category
      )

    /*
    @description: Returns the most recent Condition by recordedDate
    */
    define fluent function mostRecent(conditions List<Condition>):
      Last(
        conditions C
          sort by recordedDate.value
      )

    /*
    @description: Returns the most recent Observation by issued
    */
    define fluent function mostRecent(observations List<FHIR.Observation>):
      Last(
        observations O
          sort by issued.value
      )

    /*
    @description: Returns the most recent Procedure by the start of performed.toInterval()
    */
    define fluent function mostRecent(procedures List<FHIR.Procedure>):
      Last(
        procedures P
          sort by start of performed.toInterval()
      )

    /*
    @description: Returns the most recent MedicationRequest by authoredOn
    */
    define fluent function mostRecent(medicationRequests List<"MedicationRequest">):
      Last(
        medicationRequests MR
          sort by authoredOn.value
      )

    /*
    @description: Returns the most recent ServiceRequest by authoredOn
    */
    define fluent function mostRecent(serviceRequests List<FHIR.ServiceRequest>):
      Last(
        serviceRequests SR
          sort by authoredOn.value
      )

    /*
    @description: Creates a list of integers from 1 to how many days are in the interval. Note, this wont create an index for
    the final day if it is less than 24 hours. This also includes the first 24 hour period.
    */
    define fluent function toDayNumbers(period Interval<DateTime>):
      ( expand { Interval[1, duration in days between start of period and end of period]} ) DayNumber
        return end of DayNumber

    /*
    @description: Creates a list of 24 hour long intervals in an interval paired with the index (1 indexed) to which 24 hour interval it is.
    Note that the result will include intervals that are closed at the beginning and open at the end
    */
    define fluent function daysInPeriod(period Interval<DateTime>):
      ( period.toDayNumbers() ) DayIndex
        let
          startPeriod: start of period + (24 hours * (DayIndex - 1)),
          endPeriod:
            if (hours between startPeriod and end of period < 24) then
              startPeriod
            else
              start of period + (24 hours * DayIndex)
        return Tuple {
          dayIndex: DayIndex,
          dayPeriod: Interval[startPeriod, endPeriod)
        }
ELM JSON Content |
    Encoded data
Generated using version 0.5.6-cibuild of the sample-content-ig Liquid templates