-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexpand-valuesets.js
More file actions
226 lines (185 loc) · 7.84 KB
/
Copy pathexpand-valuesets.js
File metadata and controls
226 lines (185 loc) · 7.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
#!/usr/bin/env node
/**
* expand-valuesets.js
* Post-SUSHI ValueSet expansion using local CodeSystem resources.
*
* Usage: node expand-valuesets.js
* Run after: sushi .
*
* Expands:
* - Standalone ValueSet-*.json in fsh-generated/resources/
* - Contained ValueSets inside Questionnaire-*.json
*
* Preserves: designations (de/en), ordinalValue extensions, all other extensions
*/
const fs = require('fs');
const path = require('path');
const RESOURCES_DIR = path.join(__dirname, 'fsh-generated', 'resources');
const TIMESTAMP = new Date().toISOString();
const PKG_VERSION = JSON.parse(fs.readFileSync(path.join(__dirname, 'package.json'), 'utf8')).version;
// ── CodeSystem index ────────────────────────────────────────────────────────
function buildCodeSystemIndex(extraCS = []) {
const index = {}; // keyed by URL
const idIndex = {}; // keyed by "#id" for fragment references
const files = fs.readdirSync(RESOURCES_DIR)
.filter(f => f.startsWith('CodeSystem-') && f.endsWith('.json'));
for (const file of files) {
const cs = JSON.parse(fs.readFileSync(path.join(RESOURCES_DIR, file), 'utf8'));
if (cs.url) index[cs.url] = cs;
if (cs.id) idIndex[`#${cs.id}`] = cs;
}
// Merge contained CodeSystems — indexed by both URL and fragment id
for (const cs of extraCS) {
if (cs.url) index[cs.url] = cs;
if (cs.id) idIndex[`#${cs.id}`] = cs;
}
index._byId = idIndex;
return index;
}
// Flatten nested concepts into { code → concept } map
function flattenConcepts(concepts = {}) {
const map = {};
function walk(items) {
for (const item of (items || [])) {
map[item.code] = item;
if (item.concept) walk(item.concept);
}
}
walk(concepts);
return map;
}
// ── Expansion builder ───────────────────────────────────────────────────────
function buildContainsEntry(systemUrl, csConcept, composeConcept) {
const entry = {
system: systemUrl,
code: csConcept.code,
display: (composeConcept && composeConcept.display) || csConcept.display,
};
// Designations from CodeSystem concept
if (csConcept.designation && csConcept.designation.length > 0) {
entry.designation = csConcept.designation;
}
// Extensions: merge from compose concept first, then CS concept properties
const extensions = [
...((composeConcept && composeConcept.extension) || []),
...((csConcept && csConcept.extension) || []),
];
if (extensions.length > 0) {
entry.extension = extensions;
}
return entry;
}
function expandValueSet(vs, csIndex) {
if (vs.expansion) return false; // already expanded
const contains = [];
for (const include of (vs.compose && vs.compose.include) || []) {
// Support both URL lookup and fragment-id lookup (#contained-cs-id)
const cs = csIndex[include.system] || (csIndex._byId && csIndex._byId[include.system]);
if (!cs) {
if (include.concept && include.concept.length > 0) {
// No local CS, but compose has explicit concepts with full data (e.g. LOINC answer lists)
// → use compose concepts directly, they already carry display/designation/extensions
for (const composeConcept of include.concept) {
const entry = {
system: include.system,
code: composeConcept.code,
display: composeConcept.display,
};
if (composeConcept.designation) entry.designation = composeConcept.designation;
if (composeConcept.extension) entry.extension = composeConcept.extension;
contains.push(entry);
}
} else {
console.warn(` ⚠️ CodeSystem not found locally: ${include.system}`);
console.warn(` → Skipping (use Tx server for external CodeSystems)`);
}
continue;
}
const csConcepts = flattenConcepts(cs.concept);
if (include.concept && include.concept.length > 0) {
// Explicit concept list — enrich with CS designations/extensions
for (const composeConcept of include.concept) {
const csConcept = csConcepts[composeConcept.code];
contains.push(buildContainsEntry(include.system, csConcept || composeConcept, composeConcept));
}
} else {
// No filter → include all CS concepts
for (const csConcept of Object.values(csConcepts)) {
contains.push(buildContainsEntry(include.system, csConcept, null));
}
}
}
if (contains.length === 0) return false; // nothing resolved, skip
vs.expansion = {
identifier: `urn:uuid:mii-pro-expansion-${Date.now()}`,
timestamp: TIMESTAMP,
total: contains.length,
parameter: [
{ name: "mii-pro-version", valueString: PKG_VERSION },
{ name: "expandedAt", valueDateTime: TIMESTAMP },
],
contains,
};
return true;
}
// ── Standalone ValueSets ────────────────────────────────────────────────────
function processStandaloneValueSets(csIndex) {
const files = fs.readdirSync(RESOURCES_DIR)
.filter(f => f.startsWith('ValueSet-') && f.endsWith('.json'));
let count = 0;
for (const file of files) {
const filePath = path.join(RESOURCES_DIR, file);
const vs = JSON.parse(fs.readFileSync(filePath, 'utf8'));
if (vs.expansion) {
console.log(` ⏭️ ${file} (already expanded)`);
continue;
}
console.log(` 📦 ${file}`);
if (expandValueSet(vs, csIndex)) {
fs.writeFileSync(filePath, JSON.stringify(vs, null, 2));
console.log(` → ${vs.expansion.total} concepts`);
count++;
}
}
return count;
}
// ── Questionnaires with contained ValueSets ─────────────────────────────────
function processQuestionnaires(csIndex) {
const files = fs.readdirSync(RESOURCES_DIR)
.filter(f => f.startsWith('Questionnaire-') && f.endsWith('.json'));
let count = 0;
for (const file of files) {
const filePath = path.join(RESOURCES_DIR, file);
const qst = JSON.parse(fs.readFileSync(filePath, 'utf8'));
const contained = qst.contained || [];
const containedVS = contained.filter(r => r.resourceType === 'ValueSet');
if (containedVS.length === 0) continue;
// Contained CodeSystems override standalone (same URL, more specific context)
const containedCS = contained.filter(r => r.resourceType === 'CodeSystem');
const localIndex = buildCodeSystemIndex(containedCS);
let modified = false;
for (const vs of containedVS) {
if (vs.expansion) continue;
console.log(` 📦 ${file} → contained VS "${vs.id}"`);
if (expandValueSet(vs, localIndex)) {
console.log(` → ${vs.expansion.total} concepts`);
modified = true;
}
}
if (modified) {
fs.writeFileSync(filePath, JSON.stringify(qst, null, 2));
count++;
}
}
return count;
}
// ── Main ────────────────────────────────────────────────────────────────────
console.log('🔧 ValueSet Expansion');
console.log(`📁 ${RESOURCES_DIR}\n`);
const csIndex = buildCodeSystemIndex();
console.log(`✅ ${Object.keys(csIndex).length} CodeSystems geladen\n`);
console.log('── Standalone ValueSets ──────────────────────────');
const vsCount = processStandaloneValueSets(csIndex);
console.log('\n── Questionnaires (contained ValueSets) ──────────');
const qstCount = processQuestionnaires(csIndex);
console.log(`\n✅ Fertig: ${vsCount} ValueSet-Dateien, ${qstCount} Questionnaire-Dateien expandiert`);