Title: WHOCommon Id: WHOCommon Version: 0.2.0 Url: WHOCommon Status: draft Experimental: true Type: system: http://terminology.hl7.org/CodeSystem/library-type code: logic-library Date: 2025-07-02 07:24:46+0000 Publisher: WHO Description: This library defines common terminologies and functions used throughout WHO SMART Guidelines content Related Artifacts: Dependencies Condition Clinical Status Codes Parameters: Name Type Min Max In/Out Patient Patient 0 1 Out Content: text/cql library WHOCommon#xD; #xD; using FHIR version '4.0.1'#xD; #xD; include FHIRHelpers version '4.0.1'#xD; #xD; codesystem "ConditionClinicalStatusCodes": 'http://terminology.hl7.org/CodeSystem/condition-clinical'#xD; #xD; // Condition Clinical Status Codes - Consider value sets for these#xD; code "active": 'active' from "ConditionClinicalStatusCodes"#xD; code "recurrence": 'recurrence' from "ConditionClinicalStatusCodes"#xD; code "relapse": 'relapse' from "ConditionClinicalStatusCodes"#xD; code "inactive": 'inactive' from "ConditionClinicalStatusCodes"#xD; code "remission": 'remission' from "ConditionClinicalStatusCodes"#xD; code "resolved": 'resolved' from "ConditionClinicalStatusCodes"#xD; #xD; context Patient#xD; #xD; define fluent function official(identifiers List<Identifier>):#xD; singleton from (identifiers I where I.use = 'official')#xD; #xD; define fluent function official(addresses List<Address>):#xD; singleton from (addresses A where A.use = 'official')#xD; #xD; define fluent function official(names List<HumanName>):#xD; singleton from (names N where N.use = 'official')#xD; #xD; define fluent function mobile(contactPoints List<ContactPoint>):#xD; singleton from (contactPoints P where P.use = 'mobile')#xD; #xD; define fluent function only(allergies List<AllergyIntolerance>):#xD; singleton from allergies#xD; #xD; define fluent function only(appointments List<Appointment>):#xD; singleton from appointments#xD; #xD; define fluent function only(careplans List<CarePlan>):#xD; singleton from careplans#xD; #xD; define fluent function only(conditions List<Condition>):#xD; singleton from conditions#xD; #xD; define fluent function only(encounters List<Encounter>):#xD; singleton from encounters#xD; #xD; define fluent function only(immunizations List<Immunization>):#xD; singleton from immunizations#xD; #xD; define fluent function only(locations List<Location>):#xD; singleton from locations#xD; #xD; define fluent function only(medicationrequests List<MedicationRequest>):#xD; singleton from medicationrequests#xD; #xD; define fluent function only(observations List<Observation>):#xD; singleton from observations#xD; #xD; define fluent function only(procedures List<Procedure>):#xD; singleton from procedures#xD; #xD; define fluent function only(serviceRequests List<ServiceRequest>):#xD; singleton from serviceRequests#xD; #xD; define fluent function only(dosages List<Dosage>):#xD; singleton from dosages#xD; #xD; define fluent function only(doses List<FHIR.Dosage.DoseAndRate>):#xD; singleton from doses#xD; #xD; define fluent function earliest(observations List<Observation>):#xD; First(#xD; observations O#xD; sort by issued#xD; )#xD; #xD; define fluent function earliest(immunizations List<Immunization>):#xD; First(#xD; immunizations I#xD; sort by start of occurrence.toInterval()#xD; )#xD; #xD; #xD; define fluent function latest(observations List<Observation>):#xD; Last(#xD; observations O#xD; sort by issued#xD; )#xD; #xD; define fluent function mostRecent(observations List<Observation>):#xD; Last(#xD; observations O#xD; sort by issued#xD; )#xD; #xD; define fluent function mostRecent(procedures List<Procedure>):#xD; Last(#xD; procedures P#xD; sort by start of performed.toInterval()#xD; )#xD; #xD; define fluent function mostRecent(immunizations List<Immunization>):#xD; Last(#xD; immunizations I#xD; sort by start of occurrence.toInterval()#xD; )#xD; #xD; define fluent function mostRecent(medicationRequests List<MedicationRequest>):#xD; Last(#xD; medicationRequests MR#xD; sort by authoredOn#xD; )#xD; #xD; define fluent function lowest(observations List<Observation>):#xD; First(#xD; observations O#xD; sort by FHIRHelpers.ToQuantity(value as FHIR.Quantity)#xD; )#xD; #xD; define fluent function highest(observations List<Observation>):#xD; Last(#xD; observations O#xD; sort by FHIRHelpers.ToQuantity(value as FHIR.Quantity)#xD; )#xD; #xD; define fluent function complete(observations List<Observation>):#xD; observations O where O.status in { 'final', 'amended', 'corrected' }#xD; #xD; define function EarliestOf(dates List<Date>):#xD; Min(dates)#xD; #xD; define function LatestOf(dates List<Date>):#xD; Max(dates)#xD; #xD; /*#xD; From FHIRCommon 4.1.0:#xD; */#xD; #xD; /*#xD; @description: Normalizes a value that is a choice of timing-valued types to an equivalent interval#xD; @comment: Normalizes a choice type of FHIR.dateTime, FHIR.Period, FHIR.Timing, FHIR.instance, FHIR.string, FHIR.Age, or FHIR.Range types#xD; to an equivalent interval. This selection of choice types is a superset of the majority of choice types that are used as possible#xD; representations for timing-valued elements in FHIR, allowing this function to be used across any resource.#xD; #xD; The input can be provided as a dateTime, Period, Timing, instant, string, Age, or Range.#xD; The intent of this function is to provide a clear and concise mechanism to treat single#xD; elements that have multiple possible representations as intervals so that logic doesn't have to account#xD; for the variability. More complex calculations (such as medication request period or dispense period#xD; calculation) need specific guidance and consideration. That guidance may make use of this function, but#xD; the focus of this function is on single element calculations where the semantics are unambiguous.#xD; If the input is a dateTime, the result a DateTime Interval beginning and ending on that dateTime.#xD; If the input is a Period, the result is a DateTime Interval.#xD; If the input is a Timing, an error is raised indicating a single interval cannot be computed from a Timing.#xD; If the input is an instant, the result is a DateTime Interval beginning and ending on that instant.#xD; If the input is a string, an error is raised indicating a single interval cannot be computed from a string.#xD; If the input is an Age, the result is a DateTime Interval beginning when the patient was the given Age,#xD; and ending immediately prior to when the patient was the given Age plus one year.#xD; If the input is a Range, the result is a DateTime Interval beginning when the patient was the Age given#xD; by the low end of the Range, and ending immediately prior to when the patient was the Age given by the#xD; high end of the Range plus one year.#xD; #xD; NOTE: Due to the#xD; complexity of determining a single interval from a Timing or String type, this function will throw a run-time exception if it is used#xD; with a Timing or String.#xD; */#xD; define fluent function toInterval(choice Choice<FHIR.dateTime, FHIR.Period, FHIR.Timing, FHIR.instant, FHIR.string, FHIR.Age, FHIR.Range>):#xD; case#xD; when choice is FHIR.dateTime then#xD; Interval[FHIRHelpers.ToDateTime(choice as FHIR.dateTime), FHIRHelpers.ToDateTime(choice as FHIR.dateTime)]#xD; when choice is FHIR.Period then#xD; FHIRHelpers.ToInterval(choice as FHIR.Period)#xD; when choice is FHIR.instant then#xD; Interval[FHIRHelpers.ToDateTime(choice as FHIR.instant), FHIRHelpers.ToDateTime(choice as FHIR.instant)]#xD; when choice is FHIR.Age then#xD; Interval[FHIRHelpers.ToDate(Patient.birthDate) + FHIRHelpers.ToQuantity(choice as FHIR.Age),#xD; FHIRHelpers.ToDate(Patient.birthDate) + FHIRHelpers.ToQuantity(choice as FHIR.Age) + 1 year)#xD; when choice is FHIR.Range then#xD; Interval[FHIRHelpers.ToDate(Patient.birthDate) + FHIRHelpers.ToQuantity((choice as FHIR.Range).low),#xD; FHIRHelpers.ToDate(Patient.birthDate) + FHIRHelpers.ToQuantity((choice as FHIR.Range).high) + 1 year)#xD; when choice is FHIR.Timing then#xD; Message(null as Interval<DateTime>, true, 'NOT_IMPLEMENTED', 'Error', 'Calculation of an interval from a Timing value is not supported')#xD; when choice is FHIR.string then#xD; Message(null as Interval<DateTime>, true, 'NOT_IMPLEMENTED', 'Error', 'Calculation of an interval from a String value is not supported')#xD; else#xD; null as Interval<DateTime>#xD; end#xD; #xD; // Was getting error on this for this particular choice set not being found#xD; #xD; #xD; /*#xD; @description: Returns an interval representing the normalized Abatement of a given Condition resource.#xD; @comment: NOTE: Due to the complexity of determining an interval from a String, this function will throw#xD; a run-time exception if used with a Condition instance that has a String as the abatement value.#xD; */#xD; define fluent function abatementInterval(condition Condition):#xD; if condition.abatement is FHIR.dateTime then#xD; Interval[FHIRHelpers.ToDateTime(condition.abatement as FHIR.dateTime), FHIRHelpers.ToDateTime(condition.abatement as FHIR.dateTime)]#xD; else if condition.abatement is FHIR.Period then#xD; FHIRHelpers.ToInterval(condition.abatement as FHIR.Period)#xD; else if condition.abatement is FHIR.string then#xD; Message(null as Interval<DateTime>, true, 'NOT_IMPLEMENTED', 'Error', 'Calculation of an interval from a String value is not supported')#xD; else if condition.abatement is FHIR.Age then#xD; Interval[FHIRHelpers.ToDate(Patient.birthDate) + FHIRHelpers.ToQuantity(condition.abatement as FHIR.Age),#xD; FHIRHelpers.ToDate(Patient.birthDate) + FHIRHelpers.ToQuantity(condition.abatement as FHIR.Age) + 1 year)#xD; else if condition.abatement is FHIR.Range then#xD; Interval[FHIRHelpers.ToDate(Patient.birthDate) + FHIRHelpers.ToQuantity((condition.abatement as FHIR.Range).low),#xD; FHIRHelpers.ToDate(Patient.birthDate) + FHIRHelpers.ToQuantity((condition.abatement as FHIR.Range).high) + 1 year)#xD; else if condition.abatement is FHIR.boolean then#xD; Interval[end of condition.onset.toInterval(), condition.recordedDate)#xD; else #xD; null#xD; #xD; /*#xD; @description: Returns an interval representing the normalized prevalence period of a given Condition resource.#xD; @comment: Uses the toInterval and toAbatementInterval functions to determine the widest potential interval from#xD; onset to abatement as specified in the given Condition.#xD; */#xD; define fluent function prevalenceInterval(condition Condition):#xD; if condition.clinicalStatus ~ "active"#xD; or condition.clinicalStatus ~ "recurrence"#xD; or condition.clinicalStatus ~ "relapse" then#xD; Interval[start of condition.onset.toInterval(), end of condition.abatementInterval()]#xD; else#xD; (end of condition.abatementInterval()) abatementDate#xD; return #xD; if abatementDate is null then#xD; Interval[start of condition.onset.toInterval(), abatementDate)#xD; else#xD; Interval[start of condition.onset.toInterval(), abatementDate]#xD; #xD; /*#xD; @description: Returns true if the given reference is to the given resource#xD; @comment: Returns true if the `id` element of the given resource exactly equals the tail of the given reference.#xD; NOTE: This function assumes resources from the same source server.#xD; */#xD; define fluent function references(reference FHIR.Reference, resource FHIR.Resource):#xD; resource.id = Last(Split(reference.reference, '/')) #xD; #xD; /*#xD; @description: Returns true if the given reference is to the given resourceId#xD; @comment: Returns true if the `resourceId` parameter exactly equals the tail of the given reference.#xD; NOTE: This function assumes resources from the same source server.#xD; */#xD; define fluent function references(reference FHIR.Reference, resourceId String):#xD; resourceId = Last(Split(reference.reference, '/'))#xD; #xD; /*#xD; @description: Returns true if any of the given references are to the given resource#xD; @comment: Returns true if the `id` element of the given resource exactly equals the tail of any of the given references.#xD; NOTE: This function assumes resources from the same source server.#xD; */#xD; define fluent function references(references List<FHIR.Reference>, resource FHIR.Resource):#xD; exists (references R where R.references(resource))#xD; #xD; /*#xD; @description: Returns true if any of the given references are to the given resourceId#xD; @comment: Returns true if the `resourceId` parameter exactly equals the tail of any of the given references.#xD; NOTE: This function assumes resources from the same source server.#xD; */#xD; define fluent function references(references List<FHIR.Reference>, resourceId String):#xD; exists (references R where R.references(resourceId))#xD; Content: application/elm+xml Encoded data (327416 characters)
This library defines common terminologies and functions used throughout WHO SMART Guidelines content
Code System ConditionClinicalStatusCodes