-
Notifications
You must be signed in to change notification settings - Fork 0
/
generateFSH.ts
220 lines (190 loc) · 7.15 KB
/
generateFSH.ts
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
import fhir4 from 'fhir/r4';
import { logger } from '../common/logger';
import { RuntimeSettings } from '../config/settings';
import * as fs from 'fs';
import { ElementDetail, ProfileDetail } from '../analysis/elementDetails';
import { getMeasureListString } from '../common/util';
export function generateFSH(profileDetails: ProfileDetail[], settings: RuntimeSettings): string {
let fsh: string = fs.readFileSync('src/fixtures/extension.fsh', { encoding: 'utf8', flag: 'r' });
const stats: { nProfiles: number } = {
nProfiles: 0
};
profileDetails.forEach(oneProfile => {
stats.nProfiles++;
fsh += generateFSHProfile(oneProfile, settings);
});
return fsh;
}
function generateFSHProfile(oneProfile: ProfileDetail, settings: RuntimeSettings): string {
let fsh = '';
logger.info('');
logger.info('** Extending profile ' + oneProfile.getParentProfileDefinition().name);
// header
fsh += generateFSHProfileHeader(oneProfile, settings);
// items
oneProfile.getElements().forEach(oneElement => {
if (oneElement.isMeasureRequirement()) {
fsh += generateFSHProfileElement(oneProfile, oneElement, settings);
}
});
fsh += '\n';
return fsh;
}
function generateFSHProfileHeader(oneProfile: ProfileDetail, settings: RuntimeSettings): string {
let fsh = '';
const sd: fhir4.StructureDefinition = oneProfile.getParentProfileDefinition();
fsh += 'Profile: ' + settings.projectName + sd.name + '\n';
fsh += 'Parent: ' + sd.name + '\n';
fsh += 'Id: ' + oneProfile.getStructureDefId() + '\n';
fsh += 'Title: "' + settings.projectName + ' ' + sd.name + '"\n';
fsh += `Description: "This profile defines implementation-time requirements for data used in the calculation of ${
settings.projectMarkdownNameAndLink ? settings.projectMarkdownNameAndLink : settings.projectName
} quality measures."\n`;
// measures using this profile
oneProfile.getMeasureList().forEach(measure => {
fsh += fshUsedByMeasureExtension(measure);
});
return fsh;
}
function generateFSHProfileElement(
oneProfile: ProfileDetail,
oneElement: ElementDetail,
settings: RuntimeSettings
): string {
let fsh = '';
// take off the <ResouceType>. prefix for use in FSH
let elementPath = oneElement.getElementId().split(`${oneProfile.getResourceType()}.`)[1];
if (elementPath.includes('extension:')) {
const elementDef = oneElement.getElementDefinition();
const basePath = elementDef.path.split(`${oneProfile.getResourceType()}.`)[1];
if (basePath.includes('.')) {
logger.error('sub elements of extension not implemented yet!');
// can't determine the element path, so return nothing for this element
return '';
}
const extensionUrl = elementDef.type!.at(0)!.profile!.at(0)!;
elementPath = `${basePath}[${extensionUrl}]`;
}
// add must support
fsh += fshMustSupportElement(elementPath);
// prefix short with measure indicator
fsh += fshPrefixedShort(elementPath, '(Measure)', oneElement.getElementDefinition().short ?? '');
// add measure list to end of comment
const additionalComment =
'The following measures refrence this element in their calculation logic: ' +
getMeasureListString(oneElement.getMeasureList(), settings, ', ', '', true) +
'.';
fsh += fshSuffixedComment(
elementPath,
additionalComment,
oneElement.getElementDefinition().comment ?? ''
);
// add measure extensions
oneElement.getMeasureList().forEach(measure => {
fsh += fshUsedByMeasureExtension(measure, elementPath);
});
// add filter details only if this is a primary filter element
if (oneProfile.getPrimaryCodeFiltersElement() === oneElement.getElementId()) {
oneElement.getFilters().forEach(oneCodeOrValueSetFilter => {
if (oneCodeOrValueSetFilter.code) {
let codeString = '';
if (oneCodeOrValueSetFilter.code.system != undefined) {
codeString = `${oneCodeOrValueSetFilter.code.system}#${oneCodeOrValueSetFilter.code.code} "${oneCodeOrValueSetFilter.code.display}"`;
} else {
codeString = `#${oneCodeOrValueSetFilter.code.code ?? ''}`;
}
fsh += fshAssociatedWithValueSetComplexExtension(
elementPath,
oneCodeOrValueSetFilter.measureList,
codeString
);
} else if (oneCodeOrValueSetFilter.valueSet) {
fsh += fshAssociatedWithValueSetComplexExtension(
elementPath,
oneCodeOrValueSetFilter.measureList,
undefined,
oneCodeOrValueSetFilter.valueSet
);
} else {
logger.warn(`Missing code filter detail for element ${oneElement.getElementId()}`);
}
});
}
return fsh;
}
function fshMustSupportElement(elementPath: string): string {
return `* ${elementPath} MS\n`;
}
function fshPrefixedShort(elementPath: string, shortPrefix: string, originalShort: string): string {
const shortString = `${shortPrefix}${originalShort.length > 0 ? ` ${originalShort}` : ''}`;
return `* ${elementPath} ^short = "${fshifyString(shortString)}"\n`;
}
function fshSuffixedComment(
elementPath: string,
commentSuffix: string,
originalComment: string
): string {
const commentString = `${
originalComment.length > 0 ? `${originalComment}\r\r` : ''
}${commentSuffix}`;
return `* ${elementPath} ^comment = "${fshifyString(commentString)}"\n`;
}
function fshUsedByMeasureExtension(measure: string, elementPath?: string): string {
// allow for 3 cases:
// 1. profile level: no element path
// 2. element level: element path
// 3. within an element-level extension: element path with the start of a ^ rule
let pathAndPrefix = '^'; // profile-level default
if (elementPath) {
if (elementPath.includes(' ')) {
// 3. inside an element-level extension
pathAndPrefix = elementPath;
} else {
//2. element level
pathAndPrefix = `${elementPath} ^`;
}
}
return `* ${pathAndPrefix}extension[UsedByMeasure][+].valueString = "${measure}" \n`;
}
function fshAssociatedWithValueSetComplexExtension(
elementPath: string,
measureList: string[],
codeString?: string,
valueSetString?: string
): string {
if (!codeString && !valueSetString) {
logger.error(
`value set association extension generation failed for element ${elementPath}: no code or value set`
);
return '';
}
if (codeString && valueSetString) {
logger.error(
`value set association extension generation failed for element ${elementPath}: both code and value set`
);
return '';
}
let fsh = '';
if (codeString) {
fsh += `* ${elementPath} ^extension[AssociatedWithValueSet][+].extension[codeTarget][+].valueCoding = ${codeString}\n`;
}
if (valueSetString) {
fsh += `* ${elementPath} ^extension[AssociatedWithValueSet][+].extension[valueSetTarget][+].valueString = "${valueSetString}"\n`;
}
measureList.forEach(measure => {
fsh += fshUsedByMeasureExtension(
measure,
`${elementPath} ^extension[AssociatedWithValueSet][=].`
);
});
return fsh;
}
// from GoFSH - the order seems to matter!
function fshifyString(theString: string): string {
return theString
.replace(/\\/g, '\\\\')
.replace(/"/g, '\\"')
.replace(/\n/g, '\\n')
.replace(/\r/g, '\\r')
.replace(/\t/g, '\\t');
}