fhir_r5_cql 0.7.0
fhir_r5_cql: ^0.7.0 copied to clipboard
FHIR R5 binding for the model-independent `cql` engine: the R5 ModelResolver and TerminologyProvider implementations that let the CQL engine execute against FHIR R5 data.
// Translate and execute CQL (Clinical Quality Language) with fhir_r5_cql.
//
// fhir_r5_cql is a thin R5 binding over the model-independent `cql` engine:
// `libraryFromCql` (CQL -> ELM) and `execute` come from `cql`, while
// `R5ModelResolver` supplies FHIR R5 data access for retrieves.
//
// Run: dart run example/main.dart
// ignore_for_file: avoid_print
import 'package:fhir_r5_cql/fhir_r5_cql.dart';
Future<void> main() async {
// A small library of pure expressions — no FHIR retrieves, so it runs with
// an empty execution context.
const source = '''
library Example version '1.0.0'
define "Greeting": 'Hello, CQL'
define "Sum": 1 + 2 + 3
define "IsAdult": 21 >= 18
define "Evens": (List{1, 2, 3, 4, 5, 6}) X where X mod 2 = 0
''';
// 1. Translate CQL source to an executable ELM library. Translation
// problems are recorded on `library.annotation` (mirroring the reference
// translator) rather than thrown — inspect it before executing if you
// need to surface errors.
final library = libraryFromCql(source);
// 2. Execute the library. The R5ModelResolver wires in FHIR R5 data access;
// expressions that retrieve (e.g. [Patient], [Observation]) resolve
// through it. Pure expressions ignore it. `execute` returns the run
// context, keyed by define name (alongside internal engine keys).
final results = await library.execute(
<String, dynamic>{},
const R5ModelResolver(),
) as Map<String, dynamic>;
// 3. Read each define's result by name.
for (final name in ['Greeting', 'Sum', 'IsAdult', 'Evens']) {
print('$name: ${results[name]}');
}
}