-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathexecutor.ts
More file actions
107 lines (98 loc) · 3.44 KB
/
executor.ts
File metadata and controls
107 lines (98 loc) · 3.44 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
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, TerminologyProvider } from '../types';
export class Executor {
constructor(
public library: any,
public codeService?: TerminologyProvider,
public parameters?: Parameter,
public messageListener: MessageListener = new NullMessageListener()
) {}
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);
}
}
r.recordPatientResults(patient_ctx, resultMap);
currentPatient = await patientSource.nextPatient();
}
return r;
}
}