This repository has been archived by the owner on Oct 17, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
filter.js
245 lines (209 loc) · 8.4 KB
/
filter.js
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
const { Specifications } = require('shr-models');
class SpecificationsFilter {
constructor(specifications, expSpecifications, configSpecifications) {
this._specs = specifications;
this._expSpecs = expSpecifications;
this._config = configSpecifications;
this._deDependencies = new IdentifierSet();
this._processedDataElements = [];
this._vsDependencies = new Set();
this._csDependencies = new Set();
this._filteredSpecs = new Specifications();
this._filteredExpSpecs = new Specifications();
}
filter() {
// get filter config data
let strategy = '';
let target = [];
if (this._config.filterStrategy != null) {
strategy = this._config.filterStrategy.strategy;
target = this._config.filterStrategy.target;
}
// Account for any filtering done in ContentProfile
// Only happens if no filtering done in config file
if (this._expSpecs.contentProfiles.all.length > 0 && strategy === '' && target.length === 0) {
// Just override strategy to hybrid since it can do both namespace and element
strategy = 'hybrid';
for (const cp of this._expSpecs.contentProfiles.all) {
for (const cpr of cp.rules) {
if (cpr.primaryProfile && !target.includes(cp.identifier.fqn)) {
target.push(cp.identifier.fqn);
}
}
}
}
// recursively find dependencies for each data element in specifications
// if element matches filter criteria
for (const element of this._expSpecs.dataElements.all) {
if (this.meetsFilterCriteria(strategy, target, element)) {
this.findDataElementDependenciesRecursive(element.identifier);
}
}
// filter data elements in specifications by data element dependencies
for (const element of this._expSpecs.dataElements.all) {
if (this._deDependencies.has(element.identifier)) {
this._filteredExpSpecs.dataElements.add(element);
}
}
// filter value sets in specifications by value set dependencies
for (const valueSet of this._expSpecs.valueSets.all) {
if (this._vsDependencies.has(valueSet.url)) {
this._filteredExpSpecs.valueSets.add(valueSet);
}
}
// find additional code system dependencies by filtered value sets
for (const valueSet of this._filteredExpSpecs.valueSets.all) {
valueSet.concepts.forEach(concept => {
this._csDependencies.add(concept.system);
});
valueSet.rules.forEach(rule => {
if (rule.system) {
this._csDependencies.add(rule.system);
} else {
this._csDependencies.add(rule.code.system);
}
});
}
// filter code systems in specifications by code system dependencies
for (const codeSystem of this._expSpecs.codeSystems.all) {
if (this._csDependencies.has(codeSystem.url)) {
this._filteredExpSpecs.codeSystems.add(codeSystem);
}
}
// filter namespaces in specifications by filtered data elements, value sets, and code systems
for (const namespace of this._expSpecs.namespaces.all) {
if (this._filteredExpSpecs.dataElements.namespaces.some(ns => ns === namespace.namespace)) {
this._filteredExpSpecs.namespaces.add(namespace);
} else if (this._filteredExpSpecs.valueSets.namespaces.some(ns => ns === namespace.namespace)) {
this._filteredExpSpecs.namespaces.add(namespace);
} else if (this._filteredExpSpecs.codeSystems.namespaces.some(ns => ns === namespace.namespace)) {
this._filteredExpSpecs.namespaces.add(namespace);
}
}
for (const target of this._expSpecs.maps.targets) {
for (const map of this._expSpecs.maps.byTarget(target)) {
if (this._deDependencies.has(map.identifier)) {
this._filteredExpSpecs.maps.add(map);
}
}
}
// Just copy over the content profiles untouched
for (const cp of this._expSpecs.contentProfiles.all) {
this._filteredExpSpecs.contentProfiles.add(cp.clone());
}
// In some cases (like cimcore import, this._specs may be null)
if (this._specs != null) {
// filter specifications based on filtered expanded specifications
for (const namespace of this._specs.namespaces.all) {
if (this._filteredExpSpecs.namespaces.all.includes(namespace)) {
this._filteredSpecs.namespaces.add(namespace);
}
}
for (const dataElement of this._specs.dataElements.all) {
if (this._filteredExpSpecs.dataElements.all.includes(dataElement)) {
this._filteredSpecs.dataElements.add(dataElement);
}
}
for (const valueSet of this._specs.valueSets.all) {
if (this._filteredExpSpecs.valueSets.all.includes(valueSet)) {
this._filteredSpecs.valueSets.add(valueSet);
}
}
for (const codeSystem of this._specs.codeSystems.all) {
if (this._filteredExpSpecs.codeSystems.all.includes(codeSystem)) {
this._filteredSpecs.codeSystems.add(codeSystem);
}
}
for (const target of this._specs.maps.targets) {
const filteredMaps = this._filteredExpSpecs.maps.byTarget(target);
for (const map of this._specs.maps.byTarget(target)) {
if (filteredMaps.includes(map)) {
this._filteredSpecs.maps.add(map);
}
}
}
// Just copy over the content profiles untouched
for (const cp of this._specs.contentProfiles.all) {
this._filteredSpecs.contentProfiles.add(cp.clone());
}
}
return [this._filteredSpecs, this._filteredExpSpecs];
}
meetsFilterCriteria(strategy, target, element) {
switch(strategy) {
case 'element':
return target.includes(element.identifier.name) || target.includes(element.identifier.fqn);
case 'namespace':
return target.includes(element.identifier.namespace);
case 'hybrid':
return target.includes(element.identifier.name) || target.includes(element.identifier.namespace) || target.includes(element.identifier.fqn);
default:
return false;
}
}
findDataElementDependenciesRecursive(identifier) {
// If it's primitive or we've already processed this one, don't go further
// (avoid circular dependencies)
if (this._processedDataElements.some(id => id.equals(identifier))) {
return;
} else if (identifier.isPrimitive) {
this._processedDataElements.push(identifier);
return;
}
this._deDependencies.add(identifier);
const element = this._expSpecs.dataElements.findByIdentifier(identifier);
element.basedOn.forEach(b => this._deDependencies.add(b));
element.concepts.forEach(concept => this._csDependencies.add(concept.system));
[element.value, ...element.fields].forEach(field => {
if (!field) {
return;
}
if (field.effectiveIdentifier) { // is IdentifiableValue
this._deDependencies.add(field.effectiveIdentifier);
} else if (field.options) { // is ChoiceValue
field.aggregateOptions.filter(opt => opt.effectiveIdentifier).forEach(option => {
this._deDependencies.add(option.effectiveIdentifier);
});
}
field.constraintsFilter.type.constraints.forEach(constraint => {
this._deDependencies.add(constraint.isA);
});
field.constraintsFilter.includesType.constraints.forEach(constraint => {
this._deDependencies.add(constraint.isA);
});
field.constraintsFilter.valueSet.constraints.forEach(constraint => {
this._vsDependencies.add(constraint.valueSet);
});
field.constraintsFilter.code.constraints.forEach(constraint => {
this._csDependencies.add(constraint.code.system);
});
field.constraintsFilter.includesCode.constraints.forEach(constraint => {
this._csDependencies.add(constraint.code.system);
});
});
this._processedDataElements.push(identifier);
this._deDependencies.forEach(de => {
this.findDataElementDependenciesRecursive(de);
});
}
}
/**
* IdentifierSet contains a set of Identifiers with the guarantee that there are no duplicates.
* This class mimics a few of the useful functions found on JavaScript's Set class.
*/
class IdentifierSet {
constructor() {
this._map = new Map();
}
add(identifier) {
this._map.set(identifier.fqn, identifier);
return this;
}
has(identifier) {
return this._map.has(identifier.fqn);
}
forEach(callback) {
this._map.forEach(v => callback(v, v, this));
}
}
module.exports = SpecificationsFilter;