Use matchbox-engine in a Java application
This guide shows how to use matchbox-engine as a library: add it to your project, create engines, load implementation guides, validate and transform. If you have never used it, start with the tutorial. The API is described in the matchbox-engine reference and the Javadoc.
Add the dependency
<dependency>
<groupId>health.matchbox</groupId>
<artifactId>matchbox-engine</artifactId>
<version>5.0.0</version>
</dependency>
Use the latest version from Maven Central.
Create an engine
The engine is configured with FHIR R4 and the terminology and extension packages bundled with matchbox-engine. Use
getEngineR4B() or getEngineR5() for the other FHIR versions.
An engine can only contain one version of an implementation guide. To validate against several versions, or to change
conformance resources without affecting other users of an engine, create several engines; you can create a new engine
from an existing one with new MatchboxEngine(existingEngine).
Validate a resource
import org.hl7.fhir.model.utilities.formats.FhirFormat;
import org.hl7.fhir.r4.model.OperationOutcome;
InputStream in = getClass().getResourceAsStream("/pat.json");
OperationOutcome outcome = engine.validate(in, FhirFormat.JSON, "http://hl7.org/fhir/StructureDefinition/Patient");
Load an implementation guide
From the local package cache or the FHIR package servers:
From the content of an NPM package (its dependencies aren't fetched automatically: load them first):
Choose where the packages are cached
Downloaded packages are stored in a package cache on the filesystem. Choose its location with the builder, before creating the engine:
MatchboxEngine.MatchboxEngineBuilder builder = new MatchboxEngine.MatchboxEngineBuilder();
builder.setPackageCachePath("/var/cache/fhir-packages");
// or: builder.setPackageCacheMode(FilesystemPackageCacheMode.SYSTEM);
MatchboxEngine engine = builder.getEngineR4();
The predefined modes are listed in the reference.
Use a terminology server
Without a terminology server, codes of code systems that aren't loaded in the engine aren't checked; see Terminology in validation.
The answers of the terminology server are cached in memory. To keep them between runs, store the cache in a folder; to always ask the server, disable the cache:
Transform with the FHIR mapping language
If the engine already contains the StructureMap (e.g. from an implementation guide), call it by its canonical URL:
Resource res = engine.transformToFhir(getFileAsStringFromResources("/qr.json"), true, "http://ahdis.ch/matchbox/fml/qr2patgender");
To use a map written in the FHIR mapping language, parse it and add it to the engine first:
StructureMap sm = engine.parseMap(getFileAsStringFromResources("/qr2patgender.map"));
engine.addCanonicalResource(sm);
Transform CDA to FHIR
Use the CdaMappingEngine, which is configured with the CDA logical model, and load
the implementation guide that contains your maps:
CdaMappingEngine engine = new CdaMappingEngine.CdaMappingEngineBuilder().getCdaEngineR4();
engine.loadPackage(getClass().getResourceAsStream("/cda-fhir-maps.tgz"));
String result = engine.transform(cda, false, "http://fhir.ch/ig/cda-fhir-maps/StructureMap/CdaToBundle", true);
Examples
The tests of matchbox-engine show the main features: