Skip to content

InteropEngine

The InteropEngine is the core component of the HealthChain interoperability module. It provides a unified interface for converting between different healthcare data formats.

Basic Usage

from healthchain.interop import create_engine, FormatType

# Create an interoperability engine
engine = create_engine()

# Convert CDA XML to FHIR resources
fhir_resources = engine.to_fhir(cda_xml, src_format=FormatType.CDA)

# Convert FHIR resources to CDA XML
cda_xml = engine.from_fhir(fhir_resources, dest_format=FormatType.CDA)

# Convert HL7v2 message to FHIR resources
fhir_resources = engine.to_fhir(hl7v2_message, src_format=FormatType.HL7V2)

Creating an Engine

The create_engine() function is the recommended way to create an engine instance:

from healthchain.interop import create_engine

# Create with default configuration
engine = create_engine()

# Create with custom configuration directory
from pathlib import Path
config_dir = Path("/path/to/configs")
engine = create_engine(config_dir=config_dir)

# Create with custom validation level
engine = create_engine(validation_level="warn")

Conversion Methods

All conversions convert to and from FHIR.

Method Description
to_fhir(data, src_format) Convert from source format to FHIR resources
from_fhir(resources, dest_format) Convert from FHIR resources to destination format

Converting to FHIR

# From CDA
fhir_resources = engine.to_fhir(cda_xml, src_format=FormatType.CDA)

# From HL7v2
fhir_resources = engine.to_fhir(hl7v2_message, src_format=FormatType.HL7V2)

Converting from FHIR

# To CDA
cda_xml = engine.from_fhir(fhir_resources, dest_format=FormatType.CDA)

# To HL7v2
hl7v2_message = engine.from_fhir(fhir_resources, dest_format=FormatType.HL7V2)

Accessing Configuration

The engine provides direct access to the underlying configuration manager:

# Access configuration directly
engine.config.set_environment("production")
engine.config.set_validation_level("warn")
value = engine.config.get_config_value("cda.sections.problems.resource")

Custom Components

You can register custom parsers and generators to extend the engine's capabilities. Note that registering a custom parser / generator for an existing format type will replace the default.

from healthchain.interop import FormatType

# Register a custom parser
engine.register_parser(FormatType.CDA, custom_parser)

# Register a custom generator
engine.register_generator(FormatType.FHIR, custom_generator)

Advanced Configuration

For more information on the configuration options, see the Configuration page.