Skip to content

Commit

Permalink
Implement the ToConcept Function and Equivalence on Concept
Browse files Browse the repository at this point in the history
Closes: #1872
  • Loading branch information
alexanderkiel committed Jul 5, 2024
1 parent a5e23d3 commit ee79cbb
Show file tree
Hide file tree
Showing 23 changed files with 433 additions and 350 deletions.
6 changes: 3 additions & 3 deletions modules/cql/src/blaze/elm/code.clj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"Implementation of the code type."
(:require
[blaze.elm.compiler.core :as core]
[blaze.elm.concept :as concept]
[blaze.elm.concept :refer [concept]]
[blaze.elm.protocols :as p]))

(defrecord Code [system version code]
Expand Down Expand Up @@ -39,7 +39,7 @@
(-form [_]
`(~'code ~system ~version ~code)))

(defn to-code
(defn code
"Returns a CQL code with isn't the same as a FHIR code from the database."
[system version code]
(->Code system version code))
Expand All @@ -48,4 +48,4 @@
(extend-protocol p/ToConcept
Code
(to-concept [x]
(concept/to-concept [x])))
(concept [x])))
23 changes: 10 additions & 13 deletions modules/cql/src/blaze/elm/compiler/clinical_values.clj
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
[blaze.anomaly :as ba :refer [throw-anom]]
[blaze.elm.code :as code]
[blaze.elm.compiler.core :as core]
[blaze.elm.concept :as concept]
[blaze.elm.concept :refer [concept]]
[blaze.elm.date-time :as date-time]
[blaze.elm.quantity :as quantity]
[blaze.elm.ratio :as ratio]))
[blaze.elm.quantity :refer [quantity]]
[blaze.elm.ratio :refer [ratio]]))

(defn- find-code-system-def
"Returns the code-system-def with `name` from `library` or nil if not found."
Expand All @@ -30,7 +30,7 @@
{{system-name :name} :system :keys [code] :as expression}]
;; TODO: look into other libraries (:libraryName)
(if-let [{system :id :keys [version]} (find-code-system-def library system-name)]
(code/to-code system version code)
(code/code system version code)
(throw-anom (code-system-not-found-anom system-name context expression))))

;; 3.2. CodeDef
Expand All @@ -51,7 +51,7 @@
(find-code-def library name)]
(when code-system-ref
(when-let [{system :id :keys [version]} (core/compile* context (assoc code-system-ref :type "CodeSystemRef"))]
(code/to-code system version code)))))
(code/code system version code)))))

;; 3.4. CodeSystemDef
;;
Expand All @@ -69,7 +69,7 @@

(defmethod core/compile* :elm.compiler.type/concept
[context {:keys [codes]}]
(concept/to-concept (compile-codes context codes)))
(concept (compile-codes context codes)))

;; 3.7. ConceptDef
;;
Expand All @@ -85,8 +85,7 @@
(defmethod core/compile* :elm.compiler.type/concept-ref
[{:keys [library] :as context} {:keys [name]}]
(when-let [{codes-refs :code} (find-concept-def library name)]
(->> (map #(core/compile* context (assoc % :type "CodeRef")) codes-refs)
(concept/to-concept))))
(concept (mapv #(core/compile* context (assoc % :type "CodeRef")) codes-refs))))

;; 3.9. Quantity
(defmethod core/compile* :elm.compiler.type/quantity
Expand All @@ -101,15 +100,13 @@
("minute" "minutes") (date-time/period 0 0 (* value 60000))
("second" "seconds") (date-time/period 0 0 (* value 1000))
("millisecond" "milliseconds") (date-time/period 0 0 value)
(quantity/quantity value unit))))
(quantity value unit))))

;; 3.10. Ratio
(defmethod core/compile* :elm.compiler.type/ratio
[_ {:keys [numerator denominator]}]
(ratio/ratio (quantity/quantity (:value numerator) (or (:unit numerator)
"1"))
(quantity/quantity (:value denominator) (or (:unit denominator)
"1"))))
(ratio (quantity (:value numerator) (or (:unit numerator) "1"))
(quantity (:value denominator) (or (:unit denominator) "1"))))

;; 3.11. ValueSetDef
;;
Expand Down
24 changes: 22 additions & 2 deletions modules/cql/src/blaze/elm/compiler/reusing_logic.clj
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
[blaze.elm.code :as code]
[blaze.elm.compiler.core :as core]
[blaze.elm.compiler.macros :refer [reify-expr]]
[blaze.elm.concept :refer [concept]]
[blaze.elm.interval :as interval]
[blaze.elm.protocols :as p]
[blaze.elm.quantity :as quantity]
Expand Down Expand Up @@ -108,6 +109,9 @@
(-form [_]
`(~'call "ToQuantity" ~(core/-form operand)))))

(defn- to-code [{:keys [system version code]}]
(code/code (type/value system) (type/value version) (type/value code)))

(defn- to-code-function-expr [operand]
(reify-expr core/Expression
(-attach-cache [_ cache]
Expand All @@ -117,8 +121,7 @@
(-resolve-params [_ parameters]
(core/resolve-params-helper to-code-function-expr parameters operand))
(-eval [_ context resource scope]
(let [{:keys [system version code]} (core/-eval operand context resource scope)]
(code/to-code (type/value system) (type/value version) (type/value code))))
(some-> (core/-eval operand context resource scope) to-code))
(-form [_]
`(~'call "ToCode" ~(core/-form operand)))))

Expand Down Expand Up @@ -187,6 +190,20 @@
(-form [_]
`(~'call "ToInterval" ~(core/-form operand)))))

(defn- to-concept-function-expr [operand]
(reify-expr core/Expression
(-attach-cache [_ cache]
(core/attach-cache-helper to-concept-function-expr cache operand))
(-resolve-refs [_ expression-defs]
(to-concept-function-expr (core/-resolve-refs operand expression-defs)))
(-resolve-params [_ parameters]
(to-concept-function-expr (core/-resolve-params operand parameters)))
(-eval [_ context resource scope]
(when-let [{:keys [coding]} (core/-eval operand context resource scope)]
(concept (mapv to-code coding))))
(-form [_]
`(~'call "ToConcept" ~(core/-form operand)))))

(defn- function-def-not-found-anom [context name]
(ba/incorrect
(format "Function definition `%s` not found." name)
Expand Down Expand Up @@ -224,6 +241,9 @@
"ToInterval"
(to-interval-function-expr (first operands))

"ToConcept"
(to-concept-function-expr (first operands))

(compile-function context name operands))))

;; 9.5 OperandRef
Expand Down
2 changes: 1 addition & 1 deletion modules/cql/src/blaze/elm/compiler/structured_values.clj
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@
(case type
"{urn:hl7-org:elm-types:r1}Code"
(let [{:keys [system version code]} elements]
(code/to-code system version code))))))
(code/code system version code))))))

;; 2.3. Property
(defrecord SourcePropertyExpression [source key]
Expand Down
30 changes: 27 additions & 3 deletions modules/cql/src/blaze/elm/concept.clj
Original file line number Diff line number Diff line change
@@ -1,13 +1,37 @@
(ns blaze.elm.concept
"Implementation of the concept type."
(:require
[blaze.elm.compiler.core :as core]
[blaze.elm.protocols :as p]))

(defrecord Concept [codes]
p/Equivalent
; todo
)
(defn to-concept
(equivalent [_ other]
(reduce
(fn [_ code]
(if (some (partial p/equivalent code) (:codes other))
(reduced true)
false))
false
codes))

core/Expression
(-static [_]
true)
(-attach-cache [expr _]
[(fn [] [expr])])
(-patient-count [_]
nil)
(-resolve-refs [expr _]
expr)
(-resolve-params [expr _]
expr)
(-eval [this _ _ _]
this)
(-form [_]
`(~'concept ~@(map core/-form codes))))

(defn concept
"Returns a CQL concept"
[codes]
(->Concept codes))
4 changes: 2 additions & 2 deletions modules/cql/src/blaze/elm/list.clj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"Implementation of the list type."
(:require
[blaze.anomaly :as ba :refer [throw-anom]]
[blaze.elm.concept :as concept]
[blaze.elm.concept :refer [concept]]
[blaze.elm.protocols :as p])
(:import
[clojure.lang IReduceInit PersistentVector]))
Expand Down Expand Up @@ -175,4 +175,4 @@
(extend-protocol p/ToConcept
PersistentVector
(to-concept [x]
(concept/to-concept x)))
(concept x)))
2 changes: 1 addition & 1 deletion modules/cql/src/blaze/elm/quantity.clj
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
(-static [_]
true)
(-attach-cache [quantity _]
[quantity])
[(fn [] [quantity])])
(-patient-count [_]
nil)
(-resolve-refs [quantity _]
Expand Down
2 changes: 1 addition & 1 deletion modules/cql/test/blaze/elm/code_spec.clj
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@
(defn code? [x]
(instance? Code x))

(s/fdef code/to-code
(s/fdef code/code
:args (s/cat :system string? :version (s/nilable string?) :code string?)
:ret code?)
16 changes: 0 additions & 16 deletions modules/cql/test/blaze/elm/code_test.clj

This file was deleted.

Loading

0 comments on commit ee79cbb

Please sign in to comment.