|
60 | 60 | (safe-conj (hash-map :base (get schema :base) :url (get schema :url))))) schemas))
|
61 | 61 |
|
62 | 62 | (defn combine-elements [schemas]
|
63 |
| - (map (fn [[name, schema]] |
| 63 | + (map (fn [[_, schema]] |
64 | 64 | (->> schema
|
65 | 65 | (mix-parents-elements-circular schemas)
|
66 | 66 | (mix-parents-backbones-circular schemas))) schemas))
|
|
93 | 93 | (case (help/get-resource-name (some #(when (= (name key) (:name %)) (:value %)) elements))
|
94 | 94 | "CodeableConcept" (pattern-codeable-concept (str (help/uppercase-first-letter (help/get-resource-name constraint-name)) (help/uppercase-first-letter (subs (str key) 1))) schema) ""))
|
95 | 95 |
|
96 |
| - |
97 |
| - |
98 | 96 | (defn apply-patterns [constraint-name patterns schema]
|
99 | 97 | (->> (map (fn [item]
|
100 | 98 | (if-let [pattern (some #(when (= (name (first %)) (:name item)) (last %)) patterns)]
|
|
103 | 101 | "CodeableConcept" (conj item (hash-map :value (str (str/join (map help/uppercase-first-letter (str/split (help/get-resource-name constraint-name) #"-"))) (str/join (map help/uppercase-first-letter (str/split (:name item) #"-")))) :codeable-concept-pattern true))
|
104 | 102 | "Quantity" item item) item)) (:elements schema))
|
105 | 103 | (hash-map :elements)
|
106 |
| - (conj schema) |
107 |
| - (conj (hash-map :patterns (map (fn [item] (create-single-pattern constraint-name item (:elements schema))) patterns))))) |
| 104 | + (conj schema (hash-map :patterns (concat (get schema :patterns []) (map (fn [item] (create-single-pattern constraint-name item (:elements schema))) patterns)))))) |
108 | 105 |
|
| 106 | +(defn add-meta [constraint-name elements] |
| 107 | + (->> (filter #(not (= (:name %) "meta")) elements) |
| 108 | + (concat [{:name "meta" :required true :value (str "Meta = Meta(profile=[\"" constraint-name "\"])")}]))) |
109 | 109 |
|
110 | 110 | (defn apply-single-constraint [constraint parent-schema]
|
111 | 111 | (println (:url constraint) (reset! constraint-count (+ 1 (deref constraint-count))))
|
112 | 112 | (->> (:elements parent-schema)
|
113 | 113 | (apply-required (:required constraint))
|
114 | 114 | (apply-excluded (:excluded constraint))
|
115 | 115 | (apply-choises (filter #(contains? (last %) :choices) (:elements constraint)))
|
| 116 | + (add-meta (:url constraint)) |
116 | 117 | (hash-map :elements)
|
117 | 118 | (conj parent-schema)
|
118 | 119 | (apply-patterns (:url constraint) (filter #(contains? (last %) :pattern) (:elements constraint)))))
|
|
131 | 132 | (defn get-class-name [profile-name]
|
132 | 133 | (str/join "" (map help/uppercase-first-letter (clojure.string/split (help/get-resource-name profile-name) #"-"))))
|
133 | 134 |
|
134 |
| -(defn combine-single-class [name elements] |
| 135 | +(defn combine-single-class [name elements t] |
135 | 136 | (->> (map (fn [item]
|
136 | 137 | (when (not (contains? item :choices))
|
137 | 138 | (->> (:value item)
|
|
144 | 145 | (str "\t" (:name item) ": ")
|
145 | 146 | (str "\n")))) elements)
|
146 | 147 | (str/join "")
|
147 |
| - (str "\n\nclass " (get-class-name name) "(BaseModel):"))) |
| 148 | + (str "\n\nclass " (get-class-name name) "(" (case t "backbone" "BackboneElement" "BaseModel") "):"))) |
148 | 149 |
|
149 | 150 | (defn save-to-file [[name, definition]]
|
150 |
| - (->> (str (combine-single-class name (:elements definition))) |
151 |
| - (str (str/join (map (fn [definition] (combine-single-class (:name definition) (:elements definition))) (:backbone-elements definition)))) |
| 151 | + (->> (str (combine-single-class name (:elements definition) "default")) |
| 152 | + (str (str/join (map (fn [definition] (combine-single-class (:name definition) (:elements definition) "backbone")) (:backbone-elements definition)))) |
152 | 153 | (str (str/join (:patterns definition)))
|
153 | 154 | (str "from base import *\n")
|
154 | 155 | (str "from typing import Optional, List, Literal\n")
|
155 | 156 | (str "from pydantic import BaseModel\n")
|
156 |
| - (help/write-to-file "/Users/letzabelin/projects/health-samurai/aidbox-sdk-js/test_dir" (str/join "_" (str/split (help/get-resource-name name) #"-"))))) |
| 157 | + (help/write-to-file "/Users/gena.razmakhnin/Documents/aidbox-sdk-js/test_dir/constraint" (str/join "_" (str/split (help/get-resource-name name) #"-"))))) |
157 | 158 |
|
158 | 159 | (defn doallmap [elements] (doall (map save-to-file elements)))
|
159 | 160 |
|
160 | 161 | (defn flat-backbones [backbone-elements accumulator]
|
161 |
| - (reduce (fn [acc, item] (concat (flat-backbones (:backbone-elements item) acc) |
162 |
| - [(dissoc item :backbone-elements)]) ) |
163 |
| - accumulator |
| 162 | + (reduce (fn [acc, item] (concat (flat-backbones (:backbone-elements item) acc) |
| 163 | + [(dissoc item :backbone-elements)])) |
| 164 | + accumulator |
164 | 165 | backbone-elements))
|
165 | 166 |
|
166 | 167 | (defn main []
|
167 |
| - (let [schemas (help/parse-ndjsonk -gz "/Users/letzabelin/projects/health-samurai/aidbox-sdk-js/test_dir/1.0.0_hl7.fhir.r4.core#4.0.1_package.ndjson (1).gz") |
| 168 | + (let [schemas (help/parse-ndjson-gz "/Users/gena.razmakhnin/Documents/aidbox-python-tooklit/fhir-schema-2/1.0.0_hl7.fhir.r4.core#4.0.1_package.ndjson.gz") |
168 | 169 | base-schemas (->> schemas (filter #(or (= (:url %) "http://hl7.org/fhir/StructureDefinition/BackboneElement") (= (:url %) "http://hl7.org/fhir/StructureDefinition/Resource") (= (:derivation %) "specialization"))))
|
169 | 170 | constraint-schemas (->> schemas
|
170 | 171 | (filter #(= (:derivation %) "constraint"))
|
171 | 172 | (filter #(and (not (= (:type %) "Extension")) (not (= (:url %) "http://fhir-registry.smarthealthit.org/StructureDefinition/oauth-uris")))))
|
172 |
| - us-core (->> (help/parse-ndjson-gz "/Users/letzabelin/projects/health-samurai/aidbox-sdk-js/test_dir/1.0.0_hl7.fhir.us.core#4.0.0_package.ndjson.gz") |
| 173 | + us-core (->> (help/parse-ndjson-gz "/Users/gena.razmakhnin/Documents/aidbox-python-tooklit/fhir-schema-2/1.0.0_hl7.fhir.us.core#4.0.0_package.ndjson.gz") |
173 | 174 | (filter #(and (not (= (:type %) "Extension")) (= (:derivation %) "constraint"))))
|
174 |
| - mcode (->> (help/parse-ndjson-gz "/Users/letzabelin/projects/health-samurai/aidbox-sdk-js/test_dir/1.0.0_hl7.fhir.us.mcode#2.1.0_package.ndjson.gz") |
| 175 | + mcode (->> (help/parse-ndjson-gz "/Users/gena.razmakhnin/Documents/aidbox-python-tooklit/fhir-schema-2/1.0.0_hl7.fhir.us.mcode#2.1.0_package.ndjson.gz") |
175 | 176 | (filter #(and (not (= (:type %) "Extension")) (= (:derivation %) "constraint"))))
|
176 |
| - codex (->> (help/parse-ndjson-gz "/Users/letzabelin/projects/health-samurai/aidbox-sdk-js/test_dir/1.0.0_hl7.fhir.us.codex-radiation-therapy#1.0.0_package.ndjson.gz") |
| 177 | + codex (->> (help/parse-ndjson-gz "/Users/gena.razmakhnin/Documents/aidbox-python-tooklit/fhir-schema-2/1.0.0_hl7.fhir.us.codex-radiation-therapy#1.0.0_package.ndjson.gz") |
177 | 178 | (filter #(and (not (= (:type %) "Extension")) (= (:derivation %) "constraint")))
|
178 | 179 | #_(filter #(= (:url %) "http://hl7.org/fhir/us/codex-radiation-therapy/StructureDefinition/codexrt-radiotherapy-adverse-event")))]
|
179 | 180 |
|
|
0 commit comments