Skip to content
Alpha: see what works today →
Menu

Derivation

medication-lifecycles

The status of each medication record's current version, however its source wrote it, is classed by the first step of runtime rule L1 that holds, and the step is recorded; an entry has the class and step of the record it takes its status from, and one no member gives a status to is unknown at step 2. The key is the status lower-cased, each run of other characters than a-z and 0-9 made one space, trimmed. A step-2 to step-4 row holds when it equals the key, or equals it with the spaces of both removed ('d/c' is dc). A step-5 row, a fragment, holds when the compact key contains the fragment with its spaces removed (so NotActive is read as not active), and the class among those that do is the most conservative: the lowest precedence. Step 6 holds always, so every record has a class and 'unmatched' is step 6. The FHIR rows are the codes of R4's MedicationRequest.status and MedicationStatement.status (a code in both has one row), 'aborted' and 'nullified' are HL7 v3 ActStatus codes mapped by the C-CDA on FHIR ConceptMap CF-MedicationStatus 2.0.0, and the rest are cascade-knowledge's medication-status-synonym rows. A negated hold ('no longer on hold') is a reader's job, not a row's.

The query

PREFIX cascade: <https://ns.cascadeprotocol.org/cascade#>
PREFIX pav: <http://purl.org/pav/>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>

CONSTRUCT {
  ?record cascade:currentVersionStatusLifecycle ?class ; cascade:currentVersionStatusLifecycleStep ?step .
  ?shown cascade:statusLifecycle ?shownClass ; cascade:statusLifecycleStep ?shownStep .
}
WHERE {
  { { SELECT ?record (MIN(?score) AS ?best)
    WHERE {
      ?record a cascade:PrescribedOrReportedMedication ; cascade:inEntry [] .
      OPTIONAL { ?record pav:hasCurrentVersion/cascade:workflowStatus ?stated }
      BIND (REPLACE(REPLACE(LCASE(STR(COALESCE(?stated, ""))), "[^a-z0-9]+", " "), "^ +| +$", "") AS ?key)
      BIND (REPLACE(?key, " ", "") AS ?compact)
      VALUES (?by ?text ?kind) {
        (2 "" cascade:MedicationUnknown)
        (3 "active" cascade:MedicationActive)
        (3 "cancelled" cascade:MedicationStopped)
        (3 "completed" cascade:MedicationStopped)
        (3 "draft" cascade:MedicationUnknown)
        (3 "entered in error" cascade:MedicationEnteredInError)
        (3 "intended" cascade:MedicationUnknown)
        (3 "not taken" cascade:MedicationStopped)
        (3 "on hold" cascade:MedicationPaused)
        (3 "stopped" cascade:MedicationStopped)
        (3 "unknown" cascade:MedicationUnknown)
        (4 "aborted" cascade:MedicationStopped)
        (4 "canceled" cascade:MedicationStopped)
        (4 "cont" cascade:MedicationActive)
        (4 "continue" cascade:MedicationActive)
        (4 "continued" cascade:MedicationActive)
        (4 "continuing" cascade:MedicationActive)
        (4 "current" cascade:MedicationActive)
        (4 "dc" cascade:MedicationStopped)
        (4 "dcd" cascade:MedicationStopped)
        (4 "discontinued" cascade:MedicationStopped)
        (4 "held" cascade:MedicationPaused)
        (4 "hold" cascade:MedicationPaused)
        (4 "inactive" cascade:MedicationStopped)
        (4 "nullified" cascade:MedicationEnteredInError)
        (4 "off" cascade:MedicationStopped)
        (4 "on" cascade:MedicationActive)
        (4 "ongoing" cascade:MedicationActive)
        (4 "stop" cascade:MedicationStopped)
        (4 "suspended" cascade:MedicationPaused)
        (4 "taking" cascade:MedicationActive)
        (5 "active" cascade:MedicationActive)
        (5 "cancel" cascade:MedicationStopped)
        (5 "entered in error" cascade:MedicationEnteredInError)
        (5 "ceased" cascade:MedicationStopped)
        (5 "completed" cascade:MedicationStopped)
        (5 "continu" cascade:MedicationActive)
        (5 "continued off" cascade:MedicationStopped)
        (5 "current" cascade:MedicationActive)
        (5 "did not start" cascade:MedicationStopped)
        (5 "discontinu" cascade:MedicationStopped)
        (5 "expired" cascade:MedicationStopped)
        (5 "finished" cascade:MedicationStopped)
        (5 "former" cascade:MedicationStopped)
        (5 "historical" cascade:MedicationStopped)
        (5 "history of" cascade:MedicationStopped)
        (5 "hx of" cascade:MedicationStopped)
        (5 "in the past" cascade:MedicationStopped)
        (5 "inactive" cascade:MedicationStopped)
        (5 "never started" cascade:MedicationStopped)
        (5 "never taken" cascade:MedicationStopped)
        (5 "non active" cascade:MedicationStopped)
        (5 "no longer" cascade:MedicationStopped)
        (5 "not active" cascade:MedicationStopped)
        (5 "not current" cascade:MedicationStopped)
        (5 "not continu" cascade:MedicationStopped)
        (5 "not ongoing" cascade:MedicationStopped)
        (5 "not started" cascade:MedicationStopped)
        (5 "not taking" cascade:MedicationStopped)
        (5 "on hold" cascade:MedicationPaused)
        (5 "ongoing" cascade:MedicationActive)
        (5 "past med" cascade:MedicationStopped)
        (5 "presently" cascade:MedicationActive)
        (5 "previously" cascade:MedicationStopped)
        (5 "prior med" cascade:MedicationStopped)
        (5 "started" cascade:MedicationActive)
        (5 "stopped" cascade:MedicationStopped)
        (5 "stop taking" cascade:MedicationStopped)
        (5 "suspended" cascade:MedicationPaused)
        (5 "taking" cascade:MedicationActive)
        (6 "" cascade:MedicationUnknown)
      }
      VALUES (?kind ?precedence) {
        (cascade:MedicationEnteredInError 0) (cascade:MedicationStopped 1) (cascade:MedicationPaused 2)
        (cascade:MedicationUnknown 3) (cascade:MedicationActive 4)
      }
      FILTER (?by = 6 || IF(?by = 5, CONTAINS(?compact, REPLACE(?text, " ", "")), ?compact = REPLACE(?text, " ", "")))
      BIND (?by * 10 + ?precedence AS ?score)
    }
    GROUP BY ?record }
  BIND (xsd:integer(FLOOR(?best / 10)) AS ?step)
  VALUES (?class ?held) {
    (cascade:MedicationEnteredInError 0) (cascade:MedicationStopped 1) (cascade:MedicationPaused 2)
    (cascade:MedicationUnknown 3) (cascade:MedicationActive 4)
  }
  FILTER (?held = ?best - 10 * ?step)
  OPTIONAL { ?record cascade:inEntry ?shown . ?shown cascade:statusFrom ?record }
  BIND (?class AS ?shownClass)
  BIND (?step AS ?shownStep) }
  UNION
  { ?record a cascade:PrescribedOrReportedMedication ; cascade:inEntry ?shown .
    FILTER NOT EXISTS { ?shown cascade:statusFrom [] }
    BIND (cascade:MedicationUnknown AS ?shownClass)
    BIND (2 AS ?shownStep) }
}

Terms it reads

cascade:MedicationActive · cascade:MedicationEnteredInError · cascade:MedicationPaused · cascade:MedicationStopped · cascade:MedicationUnknown · cascade:PrescribedOrReportedMedication · cascade:currentVersionStatusLifecycle · cascade:currentVersionStatusLifecycleStep · cascade:inEntry · cascade:statusFrom · cascade:statusLifecycle · cascade:statusLifecycleStep · pav:hasCurrentVersion

Rules and decision notes that name it

queries/derivations/medication-lifecycles.rq on GitHub ↗