/
executor.ts
105 lines (97 loc) · 3.05 KB
/
executor.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
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()
) {
this.library = library;
this.codeService = codeService;
this.parameters = parameters;
this.messageListener = messageListener;
}
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;
}
exec_expression(expression: any, patientSource: DataProvider, executionDateTime: DateTime) {
const r = new Results();
const expr = this.library.expressions[expression];
if (expr != null) {
while (patientSource.currentPatient()) {
const patient_ctx = new PatientContext(
this.library,
patientSource.currentPatient(),
this.codeService,
this.parameters,
executionDateTime,
this.messageListener
);
r.recordPatientResults(patient_ctx, { [expression]: expr.execute(patient_ctx) });
patientSource.nextPatient();
}
}
return r;
}
exec(patientSource: DataProvider, executionDateTime?: DateTime) {
const r = 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] = expr.exec(unfilteredContext);
}
}
r.recordUnfilteredResults(resultMap);
return r;
}
exec_patient_context(patientSource: DataProvider, executionDateTime?: DateTime) {
const r = new Results();
while (patientSource.currentPatient()) {
const patient_ctx = new PatientContext(
this.library,
patientSource.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] = expr.execute(patient_ctx);
}
}
r.recordPatientResults(patient_ctx, resultMap);
patientSource.nextPatient();
}
return r;
}
}