Skip to content
Alpha: see what works today →
Menu

Matcher rule

same-code-instant-to-the-second-value-and-unit-others-by-code-and-instant

Two records of one kind match when their current versions give one test code, one instant, and one value of one kind in one unit. Two times with zones are compared as instants, so one instant written in two time zones is one; two times with no zone, as a C-CDA time may be written, are compared as written, and a time with a zone and one without never match, since nothing says where the second was taken. A day with no time, and a time its source wrote to the hour or the minute and the version holds padded (cascade:PaddedDateTime), which is its clinically relevant time, the one time a lab result holds, is never an instant: it is joined by its day, not here. A quantity's number is compared by value, never as a term, so 6.8 and 6.80 are one number, with the same comparator or none on both. A ratio compares its numerator and its denominator so, a range its low and its high bounds, each bound present on both or on neither; a coded value shares a code; text is never compared. Units are compared as UCUM defines one unit: each annotation in braces goes, a unit that was annotations alone is 1, the litre's two symbols, l and L, with any prefix, are one, and every other difference of case is kept, so a count per minute annotated as beats is one per minute and mmol/l is mmol/L, but Mmol/L is not. A UCUM unit is compared where a quantity has one, else its unit text. Each ordered pair is a row.

The query

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

SELECT DISTINCT ?record ?other
WHERE {
  { SELECT ?record ?version ?code ?instant ?valueKind ?firstNumber ?firstComparator ?firstUnit ?secondNumber ?secondComparator ?secondUnit
    # bound: ?record
    WHERE {
      ?record pav:hasCurrentVersion ?version .
      ?version cascade:testCode ?code ; cascade:resultClinicallyRelevantTime ?time ;
        cascade:resultValueKind ?valueKind .
      FILTER (DATATYPE(?time) = xsd:dateTime && ?valueKind != cascade:TextResultValue)
      MINUS { ?version cascade:paddedDateTime ?padded }
      BIND (COALESCE(IF(TZ(?time) != "", STR(?time - "1970-01-01T00:00:00Z"^^xsd:dateTime),
                        CONCAT("local ", STR(?time - "1970-01-01T00:00:00"^^xsd:dateTime))), CONCAT("written ", STR(?time))) AS ?instant)
      OPTIONAL { ?version cascade:resultQuantity ?firstQuantity . ?firstQuantity cascade:decimalValue ?firstQuantityNumber .
                 OPTIONAL { ?firstQuantity cascade:quantityComparator ?firstQuantityComparator }
                 OPTIONAL { ?firstQuantity cascade:ucumUnit ?firstQuantityUcum }
                 OPTIONAL { ?firstQuantity cascade:unitText ?firstQuantityText } }
      OPTIONAL { ?version cascade:resultRatioNumerator ?firstNumerator . ?firstNumerator cascade:decimalValue ?firstNumeratorNumber .
                 OPTIONAL { ?firstNumerator cascade:quantityComparator ?firstNumeratorComparator }
                 OPTIONAL { ?firstNumerator cascade:ucumUnit ?firstNumeratorUcum }
                 OPTIONAL { ?firstNumerator cascade:unitText ?firstNumeratorText } }
      OPTIONAL { ?version cascade:resultValueRangeLow ?firstLow . ?firstLow cascade:decimalValue ?firstLowNumber .
                 OPTIONAL { ?firstLow cascade:quantityComparator ?firstLowComparator }
                 OPTIONAL { ?firstLow cascade:ucumUnit ?firstLowUcum }
                 OPTIONAL { ?firstLow cascade:unitText ?firstLowText } }
      OPTIONAL { ?version cascade:resultRatioDenominator ?secondDenominator . ?secondDenominator cascade:decimalValue ?secondDenominatorNumber .
                 OPTIONAL { ?secondDenominator cascade:quantityComparator ?secondDenominatorComparator }
                 OPTIONAL { ?secondDenominator cascade:ucumUnit ?secondDenominatorUcum }
                 OPTIONAL { ?secondDenominator cascade:unitText ?secondDenominatorText } }
      OPTIONAL { ?version cascade:resultValueRangeHigh ?secondHigh . ?secondHigh cascade:decimalValue ?secondHighNumber .
                 OPTIONAL { ?secondHigh cascade:quantityComparator ?secondHighComparator }
                 OPTIONAL { ?secondHigh cascade:ucumUnit ?secondHighUcum }
                 OPTIONAL { ?secondHigh cascade:unitText ?secondHighText } }
      BIND (COALESCE(?firstQuantityNumber, ?firstNumeratorNumber, ?firstLowNumber) AS ?firstNumber)
      BIND (COALESCE(?firstQuantityComparator, ?firstNumeratorComparator, ?firstLowComparator) AS ?firstComparator)
      BIND (COALESCE(?firstQuantityUcum, ?firstNumeratorUcum, ?firstLowUcum) AS ?firstUcum)
      BIND (COALESCE(?firstQuantityText, ?firstNumeratorText, ?firstLowText) AS ?firstText)
      BIND (COALESCE(?firstUcum, ?firstText, "") AS ?firstUnitWritten)
      BIND (REPLACE(?firstUnitWritten, "\\{[^{}]*\\}", "") AS ?firstUnitBare)
      BIND (REPLACE(REPLACE(REPLACE(?firstUnitBare, "(^|[./(])(Y|Z|E|P|T|G|M|k|h|da|d|c|m|u|n|p|f|a|z|y)?l($|[./)0-9+-])", "$1$2\u0001$3"), "(^|[./(])(Y|Z|E|P|T|G|M|k|h|da|d|c|m|u|n|p|f|a|z|y)?l($|[./)0-9+-])", "$1$2\u0001$3"), "\u0001", "L")
            AS ?firstUnitFolded)
      BIND (IF(?firstUnitFolded = "" && ?firstUnitWritten != "", "1", ?firstUnitFolded) AS ?firstUnit)
      BIND (COALESCE(?secondDenominatorNumber, ?secondHighNumber) AS ?secondNumber)
      BIND (COALESCE(?secondDenominatorComparator, ?secondHighComparator) AS ?secondComparator)
      BIND (COALESCE(?secondDenominatorUcum, ?secondHighUcum) AS ?secondUcum)
      BIND (COALESCE(?secondDenominatorText, ?secondHighText) AS ?secondText)
      BIND (COALESCE(?secondUcum, ?secondText, "") AS ?secondUnitWritten)
      BIND (REPLACE(?secondUnitWritten, "\\{[^{}]*\\}", "") AS ?secondUnitBare)
      BIND (REPLACE(REPLACE(REPLACE(?secondUnitBare, "(^|[./(])(Y|Z|E|P|T|G|M|k|h|da|d|c|m|u|n|p|f|a|z|y)?l($|[./)0-9+-])", "$1$2\u0001$3"), "(^|[./(])(Y|Z|E|P|T|G|M|k|h|da|d|c|m|u|n|p|f|a|z|y)?l($|[./)0-9+-])", "$1$2\u0001$3"), "\u0001", "L")
            AS ?secondUnitFolded)
      BIND (IF(?secondUnitFolded = "" && ?secondUnitWritten != "", "1", ?secondUnitFolded) AS ?secondUnit)
    } }
  { SELECT ?other ?otherVersion ?code ?instant ?otherInstant ?valueKind ?otherFirstNumber ?otherFirstComparator ?otherFirstUnit ?otherSecondNumber ?otherSecondComparator ?otherSecondUnit
    WHERE {
      { SELECT DISTINCT ?code ?instant
        # bound: ?record
        WHERE { ?record pav:hasCurrentVersion ?version .
                ?version cascade:testCode ?code ; cascade:resultClinicallyRelevantTime ?time .
                FILTER (DATATYPE(?time) = xsd:dateTime)
                BIND (COALESCE(IF(TZ(?time) != "", STR(?time - "1970-01-01T00:00:00Z"^^xsd:dateTime),
                              CONCAT("local ", STR(?time - "1970-01-01T00:00:00"^^xsd:dateTime))), CONCAT("written ", STR(?time))) AS ?instant) } }
      { SELECT ?other ?otherVersion ?code ?instant ?otherTime ?valueKind
        WHERE {
          { SELECT DISTINCT ?code
            # bound: ?record
            WHERE { ?record pav:hasCurrentVersion ?version . ?version cascade:testCode ?code } }
          ?other pav:hasCurrentVersion ?otherVersion .
          ?otherVersion cascade:testCode ?code ; cascade:resultClinicallyRelevantTime ?otherTime ;
            cascade:resultValueKind ?valueKind .
          FILTER (DATATYPE(?otherTime) = xsd:dateTime && ?valueKind != cascade:TextResultValue)
          MINUS { ?otherVersion cascade:paddedDateTime ?otherPadded }
          BIND (COALESCE(IF(TZ(?otherTime) != "", STR(?otherTime - "1970-01-01T00:00:00Z"^^xsd:dateTime),
                                CONCAT("local ", STR(?otherTime - "1970-01-01T00:00:00"^^xsd:dateTime))), CONCAT("written ", STR(?otherTime))) AS ?instant) } }
      BIND (?instant AS ?otherInstant)
      OPTIONAL { ?otherVersion cascade:resultQuantity ?otherFirstQuantity . ?otherFirstQuantity cascade:decimalValue ?otherFirstQuantityNumber .
                 OPTIONAL { ?otherFirstQuantity cascade:quantityComparator ?otherFirstQuantityComparator }
                 OPTIONAL { ?otherFirstQuantity cascade:ucumUnit ?otherFirstQuantityUcum }
                 OPTIONAL { ?otherFirstQuantity cascade:unitText ?otherFirstQuantityText } }
      OPTIONAL { ?otherVersion cascade:resultRatioNumerator ?otherFirstNumerator . ?otherFirstNumerator cascade:decimalValue ?otherFirstNumeratorNumber .
                 OPTIONAL { ?otherFirstNumerator cascade:quantityComparator ?otherFirstNumeratorComparator }
                 OPTIONAL { ?otherFirstNumerator cascade:ucumUnit ?otherFirstNumeratorUcum }
                 OPTIONAL { ?otherFirstNumerator cascade:unitText ?otherFirstNumeratorText } }
      OPTIONAL { ?otherVersion cascade:resultValueRangeLow ?otherFirstLow . ?otherFirstLow cascade:decimalValue ?otherFirstLowNumber .
                 OPTIONAL { ?otherFirstLow cascade:quantityComparator ?otherFirstLowComparator }
                 OPTIONAL { ?otherFirstLow cascade:ucumUnit ?otherFirstLowUcum }
                 OPTIONAL { ?otherFirstLow cascade:unitText ?otherFirstLowText } }
      OPTIONAL { ?otherVersion cascade:resultRatioDenominator ?otherSecondDenominator . ?otherSecondDenominator cascade:decimalValue ?otherSecondDenominatorNumber .
                 OPTIONAL { ?otherSecondDenominator cascade:quantityComparator ?otherSecondDenominatorComparator }
                 OPTIONAL { ?otherSecondDenominator cascade:ucumUnit ?otherSecondDenominatorUcum }
                 OPTIONAL { ?otherSecondDenominator cascade:unitText ?otherSecondDenominatorText } }
      OPTIONAL { ?otherVersion cascade:resultValueRangeHigh ?otherSecondHigh . ?otherSecondHigh cascade:decimalValue ?otherSecondHighNumber .
                 OPTIONAL { ?otherSecondHigh cascade:quantityComparator ?otherSecondHighComparator }
                 OPTIONAL { ?otherSecondHigh cascade:ucumUnit ?otherSecondHighUcum }
                 OPTIONAL { ?otherSecondHigh cascade:unitText ?otherSecondHighText } }
      BIND (COALESCE(?otherFirstQuantityNumber, ?otherFirstNumeratorNumber, ?otherFirstLowNumber) AS ?otherFirstNumber)
      BIND (COALESCE(?otherFirstQuantityComparator, ?otherFirstNumeratorComparator, ?otherFirstLowComparator) AS ?otherFirstComparator)
      BIND (COALESCE(?otherFirstQuantityUcum, ?otherFirstNumeratorUcum, ?otherFirstLowUcum) AS ?otherFirstUcum)
      BIND (COALESCE(?otherFirstQuantityText, ?otherFirstNumeratorText, ?otherFirstLowText) AS ?otherFirstText)
      BIND (COALESCE(?otherFirstUcum, ?otherFirstText, "") AS ?otherFirstUnitWritten)
      BIND (REPLACE(?otherFirstUnitWritten, "\\{[^{}]*\\}", "") AS ?otherFirstUnitBare)
      BIND (REPLACE(REPLACE(REPLACE(?otherFirstUnitBare, "(^|[./(])(Y|Z|E|P|T|G|M|k|h|da|d|c|m|u|n|p|f|a|z|y)?l($|[./)0-9+-])", "$1$2\u0001$3"), "(^|[./(])(Y|Z|E|P|T|G|M|k|h|da|d|c|m|u|n|p|f|a|z|y)?l($|[./)0-9+-])", "$1$2\u0001$3"), "\u0001", "L")
            AS ?otherFirstUnitFolded)
      BIND (IF(?otherFirstUnitFolded = "" && ?otherFirstUnitWritten != "", "1", ?otherFirstUnitFolded) AS ?otherFirstUnit)
      BIND (COALESCE(?otherSecondDenominatorNumber, ?otherSecondHighNumber) AS ?otherSecondNumber)
      BIND (COALESCE(?otherSecondDenominatorComparator, ?otherSecondHighComparator) AS ?otherSecondComparator)
      BIND (COALESCE(?otherSecondDenominatorUcum, ?otherSecondHighUcum) AS ?otherSecondUcum)
      BIND (COALESCE(?otherSecondDenominatorText, ?otherSecondHighText) AS ?otherSecondText)
      BIND (COALESCE(?otherSecondUcum, ?otherSecondText, "") AS ?otherSecondUnitWritten)
      BIND (REPLACE(?otherSecondUnitWritten, "\\{[^{}]*\\}", "") AS ?otherSecondUnitBare)
      BIND (REPLACE(REPLACE(REPLACE(?otherSecondUnitBare, "(^|[./(])(Y|Z|E|P|T|G|M|k|h|da|d|c|m|u|n|p|f|a|z|y)?l($|[./)0-9+-])", "$1$2\u0001$3"), "(^|[./(])(Y|Z|E|P|T|G|M|k|h|da|d|c|m|u|n|p|f|a|z|y)?l($|[./)0-9+-])", "$1$2\u0001$3"), "\u0001", "L")
            AS ?otherSecondUnitFolded)
      BIND (IF(?otherSecondUnitFolded = "" && ?otherSecondUnitWritten != "", "1", ?otherSecondUnitFolded) AS ?otherSecondUnit)
    } }
  ?record pav:hasCurrentVersion ?version .
  FILTER (?record != ?other && ?instant = ?otherInstant)
  FILTER (IF(?valueKind = cascade:CodedResultValue,
                EXISTS { ?version cascade:resultValueCode ?sharedCode . ?otherVersion cascade:resultValueCode ?sharedCode },
                (BOUND(?firstNumber) || BOUND(?secondNumber))
                && BOUND(?firstNumber) = BOUND(?otherFirstNumber) && BOUND(?secondNumber) = BOUND(?otherSecondNumber)
                && (!BOUND(?firstNumber)
                    || (?firstNumber = ?otherFirstNumber && ?firstUnit = ?otherFirstUnit
                        && COALESCE(?firstComparator, "") = COALESCE(?otherFirstComparator, "")))
                && (!BOUND(?secondNumber)
                    || (?secondNumber = ?otherSecondNumber && ?secondUnit = ?otherSecondUnit
                        && COALESCE(?secondComparator, "") = COALESCE(?otherSecondComparator, "")))))
}

Terms it reads

cascade:CodedResultValue · cascade:TextResultValue · cascade:decimalValue · cascade:paddedDateTime · cascade:quantityComparator · cascade:resultClinicallyRelevantTime · cascade:resultQuantity · cascade:resultRatioDenominator · cascade:resultRatioNumerator · cascade:resultValueCode · cascade:resultValueKind · cascade:resultValueRangeHigh · cascade:resultValueRangeLow · cascade:testCode · cascade:ucumUnit · cascade:unitText · pav:hasCurrentVersion

Rules and decision notes that name it

queries/matcher/same-code-instant-to-the-second-value-and-unit-others-by-code-and-instant.rq on GitHub ↗