forked from cqframework/cql-execution
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexecutor.ts
More file actions
306 lines (296 loc) · 9.8 KB
/
Copy pathexecutor.ts
File metadata and controls
306 lines (296 loc) · 9.8 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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
import { MessageListener, NullMessageListener } from './messageListeners';
import { Results } from './results';
import { UnfilteredContext, PatientContext } from './context';
import { DateTime } from '../datatypes/datetime';
import { Parameter } from '../types/runtime.types';
import { DataProvider, LlmService, TerminologyProvider } from '../types';
export class Executor {
constructor(
public library: any,
public codeService?: TerminologyProvider,
public parameters?: Parameter,
public messageListener: MessageListener = new NullMessageListener(),
public llmService?: LlmService
) {}
withLibrary(lib: any) {
this.library = lib;
return this;
}
withParameters(params: Parameter) {
this.parameters = params != null ? params : {};
return this;
}
withCodeService(cs: TerminologyProvider) {
this.codeService = cs;
return this;
}
withMessageListener(ml: MessageListener) {
this.messageListener = ml;
return this;
}
async exec_expression(expression: any, patientSource: DataProvider, executionDateTime: DateTime) {
const r = new Results();
const expr = this.library.expressions[expression];
if (expr != null) {
// NOTE: Using await to support async data providers whose implementations return promise
// await has no effect on functions that don't return promises, so it is safe to use in all cases
let currentPatient = await patientSource.currentPatient();
while (currentPatient) {
const patient_ctx = new PatientContext(
this.library,
currentPatient,
this.codeService,
this.parameters,
executionDateTime,
this.messageListener
);
r.recordPatientResults(patient_ctx, { [expression]: await expr.execute(patient_ctx) });
currentPatient = await patientSource.nextPatient();
}
}
return r;
}
async exec(patientSource: DataProvider, executionDateTime?: DateTime) {
const r = await this.exec_patient_context(patientSource, executionDateTime);
const unfilteredContext = new UnfilteredContext(
this.library,
r,
this.codeService,
this.parameters,
executionDateTime,
this.messageListener
);
const resultMap: any = {};
for (const key in this.library.expressions) {
const expr = this.library.expressions[key];
if (expr.context === 'Unfiltered') {
resultMap[key] = await expr.exec(unfilteredContext);
}
}
r.recordUnfilteredResults(resultMap);
return r;
}
async exec_patient_context(patientSource: DataProvider, executionDateTime?: DateTime) {
const r = new Results();
// NOTE: Using await to support async data providers whose implementations return promise
// await has no effect on functions that don't return promises, so it is safe to use in all cases
let currentPatient = await patientSource.currentPatient();
while (currentPatient) {
const patient_ctx = new PatientContext(
this.library,
currentPatient,
this.codeService,
this.parameters,
executionDateTime,
this.messageListener
);
const resultMap: any = {};
for (const key in this.library.expressions) {
const expr = this.library.expressions[key];
if (expr.context === 'Patient') {
resultMap[key] = await expr.execute(patient_ctx);
}
//! START: Process with LLM if DocumentReference
/*
* expr
{
"name": "HasRecentVisualFootExam",
"context": "Patient",
"expression": {
"arg": {
"sources": [
{
"alias": "P",
"expression": {
"datatype": "{http://hl7.org/fhir}DocumentReference",
"templateId": "http://hl7.org/fhir/StructureDefinition/DocumentReference",
"codeProperty": "code",
"codes": {
"source": {
"name": "Diabetic foot examination (regime/therapy)"
},
"path": "codes"
}
}
}
],
"aliases": [
"P"
],
"letClauses": [],
"relationship": [],
"where": {
"args": [
{
"args": [
{
"source": {
"scope": "P",
"path": "status"
},
"path": "value"
},
{
"valueType": "{urn:hl7-org:elm-types:r1}String",
"value": "completed"
}
]
},
{
"args": [
{
"args": [
{
"source": {
"arg": {
"scope": "P",
"path": "performed"
},
"asTypeSpecifier": {
"name": "{http://hl7.org/fhir}dateTime",
"type": "NamedTypeSpecifier"
},
"strict": false
},
"path": "value"
},
{
"lowClosed": true,
"highClosed": true,
"low": {
"args": [
{},
{
"value": 1,
"unit": "year"
}
]
},
"high": {
"args": [
{},
{
"value": 1,
"unit": "year"
}
]
}
}
]
},
{
"arg": {
"arg": {}
}
}
]
}
]
},
"returnClause": null,
"aggregateClause": null,
"sortClause": null
}
}
}
* context
[
{
"clinicalStatus": {
"coding": [
{
"system": {
"value": "http://terminology.hl7.org/CodeSystem/condition-clinical"
},
"code": {
"value": "active"
},
"display": {
"value": "Active"
}
}
]
},
"verificationStatus": {
"coding": [
{
"system": {
"value": "http://terminology.hl7.org/CodeSystem/condition-ver-status"
},
"code": {
"value": "confirmed"
},
"display": {
"value": "Confirmed"
}
}
]
},
"code": {
"coding": [
{
"system": {
"value": "http://snomed.info/sct"
},
"code": {
"value": "44054006"
},
"display": {
"value": "Diabetes mellitus type 2 (disorder)"
}
}
]
},
"subject": {
"reference": {
"value": "Patient/Recent_Foot_Exam"
}
},
"onset": {
"value": "2015-01-30"
},
"id": {
"value": "2-1"
}
}
]
*/
try{
const expression = JSON.stringify(expr);
const context = JSON.stringify(await currentPatient.findRecords("DocumentReference"));
const source = expr.expression.arg.sources[0].expression.datatype;
if (source.includes('DocumentReference')) {
if (this.llmService) {
const result = await this.llmService.checkAssertion(expression, context);
resultMap[key] = result;
}
}
} catch (e) {
}
/*
{
"Patient": {
"gender": {
"value": "female"
},
"birthDate": {
"value": "1979-01-30"
},
"id": {
"value": "Recent_Foot_Exam"
}
},
"InDemographic": true,
"HasDiabetes": false,
"MeetsInclusionCriteria": false,
"HasRecentVisualFootExam": true
}
*/
//! END: Process with LLM if DocumentReference
}
r.recordPatientResults(patient_ctx, resultMap);
currentPatient = await patientSource.nextPatient();
}
return r;
}
}