Skip to content

Commit 2ffbf9e

Browse files
feat(profiles): generate from multiple igs
1 parent c4ff6cb commit 2ffbf9e

File tree

1 file changed

+37
-18
lines changed

1 file changed

+37
-18
lines changed

src/python-generator/second-try/main.clj

+37-18
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
[clojure.string :as str]
66
[clojure.set :as set]))
77

8+
(def constraint-count (atom 0))
9+
810
(defn compile-backbone [parent_name property_name definition]
911
(let [name (str parent_name "_" (help/uppercase-first-letter (name property_name)))
1012
data (help/get-typings-and-imports name (or (:required definition) []) (help/elements-to-vector definition))
@@ -58,10 +60,10 @@
5860
(safe-conj (hash-map :base (get schema :base) :url (get schema :url))))) schemas))
5961

6062
(defn combine-elements [schemas]
61-
(map (fn [schema]
63+
(map (fn [[name, schema]]
6264
(->> schema
6365
(mix-parents-elements-circular schemas)
64-
(mix-parents-backbones-circular schemas))) schemas))
66+
#_(mix-parents-backbones-circular schemas))) schemas))
6567

6668
(defn apply-excluded [excluded schema]
6769
(filter (fn [field-schema]
@@ -79,7 +81,6 @@
7981
(filter #(not (contains? choises-to-exclude (:name %))) schema)))))
8082

8183
(defn pattern-codeable-concept [name schema]
82-
(print)
8384
(->> (str "\tcoding: list[" (str/join ", " (map #(str "Coding" (str/join (str/split (:code %) #"-"))) (get-in schema [:pattern :coding] []))) "] = [" (str/join ", " (map #(str "Coding" (str/join (str/split (:code %) #"-")) "()") (get-in schema [:pattern :coding] []))) "]\n")
8485
(str "class " name "(CodeableConcept):\n")
8586
(str (when-let [coding (:coding (:pattern schema))]
@@ -89,9 +90,11 @@
8990
(str "\nclass Coding" (str/join (str/split (:code code) #"-")) "(Coding):\n"))) coding))) "\n")))
9091

9192
(defn create-single-pattern [constraint-name, [name, schema]]
92-
(case (help/get-resource-name (:type schema))
93-
"CodeableConcept" (pattern-codeable-concept (str (help/uppercase-first-letter (help/get-resource-name constraint-name)) (help/uppercase-first-letter (subs (str name) 1))) schema)
94-
"default" ""))
93+
(println constraint-name name schema)
94+
""
95+
#_(case (help/get-resource-name (:type schema))
96+
"CodeableConcept" (pattern-codeable-concept (str (help/uppercase-first-letter (help/get-resource-name constraint-name)) (help/uppercase-first-letter (subs (str name) 1))) schema)
97+
"default" ""))
9598

9699
(defn apply-patterns [constraint-name patterns schema]
97100
(->> (map (fn [item]
@@ -106,27 +109,33 @@
106109

107110

108111
(defn apply-single-constraint [constraint parent-schema]
109-
(println parent-schema)
112+
(println (:url constraint) (reset! constraint-count (+ 1 (deref constraint-count))))
110113
(->> (:elements parent-schema)
111114
(apply-required (:required constraint))
112115
(apply-excluded (:excluded constraint))
113116
(apply-choises (filter #(contains? (last %) :choices) (:elements constraint)))
114117
(hash-map :elements)
115118
(conj parent-schema)
116-
(apply-patterns (:url constraint) (filter #(contains? (last %) :pattern) (:elements constraint)))))
119+
#_(apply-patterns (:url constraint) (filter #(contains? (last %) :pattern) (:elements constraint)))))
120+
121+
#_(reduce (fn [_, constraint-schema]
122+
(when (not (get result (:url constraint-schema))) (reduced true))) false constraint-schemas)
117123

118124
(defn apply-constraints [constraint-schemas result base-schemas]
119-
(if (reduce (fn [_, constraint-schema]
120-
(when (not (get result (:url constraint-schema))) (reduced true))) false constraint-schemas)
125+
(println "run" (count result))
126+
(if (not (= (count constraint-schemas) (count result)))
121127
(apply-constraints
122128
constraint-schemas
123129
(reduce (fn [acc constraint-schema]
124-
(if (contains? result (:base constraint-schema))
130+
(if (and (contains? result (:base constraint-schema)) (not (contains? result (:url constraint-schema))))
125131
(conj acc (hash-map (:url constraint-schema) (apply-single-constraint constraint-schema (get result (:base constraint-schema)))))
126132

127-
(if (contains? base-schemas (:base constraint-schema))
133+
(if (and (contains? base-schemas (:base constraint-schema)) (not (contains? result (:url constraint-schema))))
128134
(conj acc (hash-map (:url constraint-schema) (apply-single-constraint constraint-schema (get base-schemas (:base constraint-schema))))) acc))) result constraint-schemas) base-schemas) result))
129135

136+
(defn get-class-name [profile-name]
137+
(str/join "" (map help/uppercase-first-letter (clojure.string/split (help/get-resource-name profile-name) #"-"))))
138+
130139
(defn combine-single-class [name elements]
131140
(->> (map (fn [item]
132141
(when (not (contains? item :choices))
@@ -138,7 +147,7 @@
138147
(str "\t" (:name item) ": ")
139148
(str "\n")))) elements)
140149
(str/join "")
141-
(str "\n\nclass " (help/uppercase-first-letter (help/get-resource-name name)) "(BaseModel):")))
150+
(str "\n\nclass " (get-class-name name) "(BaseModel):")))
142151

143152
(defn save-to-file [[name, definition]]
144153
(->> (str (combine-single-class name (:elements definition)))
@@ -153,18 +162,28 @@
153162

154163
(defn main []
155164
(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")
156-
base-schemas (->> schemas (filter #(= (:derivation %) "specialization")))
165+
base-schemas (->> schemas (filter #(or (= (:url %) "http://hl7.org/fhir/StructureDefinition/headcircum") (= (:derivation %) "specialization"))))
157166
constraint-schemas (->> schemas
158167
(filter #(= (:derivation %) "constraint"))
159-
(filter #(or (= (:url %) "http://hl7.org/fhir/StructureDefinition/vitalsigns") (= (:url %) "http://hl7.org/fhir/StructureDefinition/triglyceride") (= (:url %) "http://hl7.org/fhir/StructureDefinition/bmi"))))]
168+
(filter #(not (= (:url %) "http://fhir-registry.smarthealthit.org/StructureDefinition/oauth-uris"))))
169+
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")
170+
(filter #(= (:derivation %) "constraint")))
171+
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")
172+
(filter #(= (:derivation %) "constraint")))
173+
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")
174+
(filter #(= (:derivation %) "constraint")))]
160175

161176
(->> base-schemas
162177
(compile-elements)
178+
(filter #(not (nil? (:url %))))
179+
(map (fn [item] (hash-map (:url item) item)))
180+
(into {})
163181
(combine-elements)
164182
(filter #(not (nil? (:url %))))
165183
(map (fn [item] (hash-map (:url item) item)))
166184
(into {})
167-
(apply-constraints constraint-schemas {})
168-
(map save-to-file))))
185+
(apply-constraints (concat us-core constraint-schemas mcode codex) {})
186+
(doallmap))))
169187

170-
(main)
188+
(main)
189+
;; 479 -> 521

0 commit comments

Comments
 (0)