Matcher rule
same-code-instant-to-the-second-different-units-others-by-code-and-instant
Two records of one kind give a finding when their current versions give one test code, one instant, and values of one kind other than text and coded, both holding a number in one place, the first (a quantity, a numerator, a low bound) or the second (a denominator, a high bound), in units that differ: mmol/L and mg/dL may measure one draw, and no rule converts one into the other. Two times with zones are one instant when they name one moment, two with none are one when written alike, and one of each never; a quantity's number is compared by value, so 6.8 and 6.80 are one; units are compared with their UCUM annotations dropped, a unit of annotations alone being 1, the litre's l and L one, and other case kept. A pair whose units are equal so is no pair here, and one this query gives is joined by no rule. 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 (?valueKind != cascade:CodedResultValue
&& ((BOUND(?firstNumber) && BOUND(?otherFirstNumber) && ?firstUnit != ?otherFirstUnit)
|| (BOUND(?secondNumber) && BOUND(?otherSecondNumber) && ?secondUnit != ?otherSecondUnit)))
}
Terms it reads
cascade:CodedResultValue · cascade:TextResultValue · cascade:decimalValue · cascade:paddedDateTime · cascade:quantityComparator · cascade:resultClinicallyRelevantTime · cascade:resultQuantity · cascade:resultRatioDenominator · cascade:resultRatioNumerator · cascade:resultValueKind · cascade:resultValueRangeHigh · cascade:resultValueRangeLow · cascade:testCode · cascade:ucumUnit · cascade:unitText · pav:hasCurrentVersion