Skip to content

FHIR Mapping Language

OctoFHIR runs FHIR Mapping Language maps on the fml-rs engine. The operations are on by default.

[fml]
enabled = true
debug_role = "admin" # role a user needs for debug=true
timeout_ms = 30000 # a transform running longer is cancelled
max_rules = 20000000 # rules one transform may run
max_trace_events = 10000 # trace events kept with debug=true
map_folders = [] # folders of .fml maps and StructureMap / ConceptMap JSON

A map or ConceptMap is looked up in the server’s own resources, then the installed packages, then map_folders. Stored StructureMaps are checked when they are validated; problems are warnings.

POST /fhir/StructureMap/$transform
POST /fhir/StructureMap/{id}/$transform

Run a map written in FML over a resource:

POST /fhir/StructureMap/$transform
Content-Type: application/fhir+json
{
"resourceType": "Parameters",
"parameter": [
{"name": "srcMap", "valueString": "/// url = 'http://example.org/StructureMap/PatientToBasic'\n/// name = 'PatientToBasic'\n\nuses \"http://hl7.org/fhir/StructureDefinition/Patient\" alias Patient as source\nuses \"http://hl7.org/fhir/StructureDefinition/Basic\" alias Basic as target\n\ngroup Main(source src : Patient, target tgt : Basic) {\n src.id as v -> tgt.id = v \"id\";\n}\n"},
{"name": "content", "resource": {"resourceType": "Patient", "id": "example"}}
]
}

The map is the instance, or one of source, sourceMap and srcMap.

Parameter Meaning
source Canonical URL of the map
sourceMap A StructureMap resource
srcMap FML text. Repeats: the first is the map to run, the others are available to it.
supportingMap Canonical URL of another map to import from. Repeats.
content The input: a resource, or a Binary holding JSON (a logical model, another FHIR version)
model StructureDefinitions and ConceptMaps for this run only, alone or in a Bundle, as a resource or as JSON text. Repeats.
debug In the query or the body: return the trace instead of the result

The result is the target resource. A target the server’s FHIR version does not define, such as a logical model or another release, comes back as a Binary. FML that does not compile is 422, with each problem at its line and column.

debug=true returns a Parameters with the trace. The trace holds every value the map reads and writes, so only a user with the role in fml.debug_role may ask for it.

Mapping expressions can use resolve() and %server.read(type, id) to read resources from this server. Reads call the internal OperationContext with the invocation’s identity, so the caller’s grants and resource policies still apply. There are no HTTP requests back to the server and no extra server URL to configure.

The FHIRPath engine and schemas are shared, but each map execution gets its own provider. Resource bodies and caller identities are not shared across invocations. Resolved resources retain their FHIR types and can be narrowed with as or ofType, or passed to typed groups.

Only individual resource reads are enabled. Other %server operations, including writes, search and calls to another server via %server.at(url), fail the operation. A missing resource returns an empty collection. A read error, including a denied read, stops the map and preserves the operation’s error response rather than returning a partial result or a debug trace.

Reference resolution follows the current FHIRPath implementation: local references are looked up in the evaluation root, and Type/id references can be read from the server. This integration does not add absolute, versioned or canonical reference resolution, or restore the original root when evaluating an isolated reference.

$convert converts a resource between FHIR versions with the HL7 cross-version maps. The input is in the version of Content-Type, the output in the version of Accept; either defaults to the server’s own.

POST /fhir/$convert
Content-Type: application/fhir+json; fhirVersion=4.0
Accept: application/fhir+json; fhirVersion=5.0
{"resourceType": "Patient", "id": "example", "gender": "male"}

The resource is the body, or the input parameter of a Parameters. The versions are 1.0, 3.0, 4.0, 4.3, 5.0 and 6.0; an unknown version is 406.

The map for a type is http://hl7.org/fhir/uv/xver/StructureMap/{Type}{from}to{to}, for example Patient4to5. No package publishes these maps, so point fml.map_folders at a checkout of HL7/fhir-cross-version:

[fml]
map_folders = ["../fhir-cross-version/input"]

Without a map for the type the request is 422. The response is the converted resource with Content-Type: application/fhir+json; fhirVersion=.... Values the maps could not carry over are listed as warnings in an OperationOutcome in the X-Conversion-Outcome header.

QuestionnaireResponse/$extract is the StructureMap-based form of SDC extraction. The Questionnaire names its map with the sdc-questionnaire-targetStructureMap extension, and the map runs over the response.

POST /fhir/QuestionnaireResponse/{id}/$extract

At type level, pass the response in questionnaire-response. questionnaire overrides the Questionnaire the response names, and debug works as for $transform. The result is what the map produces, usually a transaction Bundle. Definition-based and observation-based extraction are not implemented: a Questionnaire without a target map is 422.

The FML page (g k) of the web console is an editor for maps.

  • Write a map or open a stored one. It is checked as you type, with syntax, compile and type problems at their place in the source.
  • The editor knows the map through a language server:
    • completion: elements after . with their types, followed through the path; variables in scope; groups after then; transforms after =; types after :;
    • hover: types, group signatures, and transform descriptions;
    • go to definition (F12), find references (Shift+F12) and rename (F2) for variables and groups;
    • signature help for transforms and group calls;
    • the outline (Ctrl+Shift+O);
    • colours by meaning: variables, parameters, elements, groups, transforms and types each look different.
  • Run it over a resource with $transform, with models of its own: logical models and ConceptMaps for this run only.
  • Run it over a CDA document, read into the CDA model. See C-CDA.
  • Replay the debug trace against the source and the input. Without the debug role the map runs without a trace.
  • Format the map, Save it as a StructureMap, and Share a link. The map, the input and the models travel in the URL fragment, so nothing is stored on the server.

The editor works through POST /api/fml/check, /api/fml/format, /api/fml/compile (FML to a StructureMap in the server’s release) and /api/fml/print (a StructureMap to FML), each with a source field except print, which takes the StructureMap. None of them reads or writes data.

The language server is a WebSocket at /api/lsp/fml. It speaks the Language Server Protocol through the fml-lsp crate, and any user with a user/*, system/* or admin/* scope can connect. It checks maps against the core definitions of the server’s FHIR release. For any other structure a map’s uses names, it loads the StructureDefinition from the installed packages, along with the structures that one refers to; logical models and profiles are covered this way. Outside the console, fml lsp from fml-rs runs the same server over standard input and output, for desktop editors.