Stream: hapi
Topic: Generate Snapshot from StructureDefinition
Sebastian Mate (Feb 17 2021 at 14:53):
Hello everybody,
I have a "differential" StructureDefinition (exported from Simplifier) and would like to derive a Snapshot from it using the HAPI library.
Unfortunately I cannot get the sample code from https://hapifhir.io/hapi-fhir/docs/validation/examples.html working, as it seems to be outdated. I've tried adopting it to the most recent version of the HAPI library (5.2.1) with no real success.
This is what I came up with (not really knowing what I am doing):
StructureDefinition differential = parser.parseResource(StructureDefinition.class, jsonData);
DefaultProfileValidationSupport defaultSupport = new DefaultProfileValidationSupport(ctx);
SnapshotGeneratingValidationSupport snapshotGenerator = new SnapshotGeneratingValidationSupport(ctx);
ValidationSupportChain chain = new ValidationSupportChain(defaultSupport, snapshotGenerator);
ValidationSupportContext theValidationSupportContext = new ValidationSupportContext(defaultSupport);
StructureDefinition snapshot = (StructureDefinition) chain.generateSnapshot(theValidationSupportContext, differential, "", "", "");
I then found the following code from Graham in this chat:
private static void makeSnapshot(StructureDefinition sd) throws DefinitionException, FHIRException {
StructureDefinition sdb = context.fetchResource(StructureDefinition.class, sd.getBaseDefinition());
if (sdb != null) {
makeSnapshot(sdb);
new ProfileUtilities(context, null, null).generateSnapshot(sdb, sd, sd.getUrl(), null, sd.getName());
}
}
But I could not get this working either. What is "context"? I've tried to initalize it with static FhirContext context = FhirContext.forR4();
but this does not seem to work.
I would be happy if someone has a tip for me on how to get ahead here.
Many thanks, Sebastian
Sebastian Mate (Feb 17 2021 at 16:02):
OK, thanks to https://github.com/hapifhir/hapi-fhir/blob/aa05667761bc41c12445400ef724f89e46dbf5a1/hapi-fhir-validation/src/test/java/org/hl7/fhir/r4/validation/FhirInstanceValidatorR4Test.java#L281 I'm making some progress:
StructureDefinition differential = parser.parseResource(StructureDefinition.class, jsonData);
DefaultProfileValidationSupport defaultSupport = new DefaultProfileValidationSupport(ctx);
StructureDefinition derived = differential;
StructureDefinition base = (StructureDefinition) defaultSupport.fetchStructureDefinition(derived.getBaseDefinition());
IWorkerContext worker = new HapiWorkerContext(ctx, defaultSupport);
List<ValidationMessage> issues = new ArrayList<>();
ProfileUtilities profileUtilities = new ProfileUtilities(worker, issues, null);
profileUtilities.generateSnapshot(base, derived, "", "", "");
It's still complaining "no base profile provided", but I will continue working on it tomorrow.
Sebastian
Grahame Grieve (Feb 17 2021 at 22:19):
looks like you haven't provided the base profile
Sebastian Mate (Feb 18 2021 at 08:41):
The trick was to include
<dependency>
<groupId>ca.uhn.hapi.fhir</groupId>
<artifactId>hapi-fhir-validation-resources-r4</artifactId>
<version>4.2.0</version>
</dependency>
into the pom.xml file. :sweat_smile:
Sebastian Mate (Feb 18 2021 at 12:35):
OK, I can now successfully generate snapshots based on the "official" FHIR profiles. I also figured out how to also load the other "custom" base profiles from my hard disk. But when trying to generate snapshots, I encounter errors of this type:
Found base profile: C:\Users\matesn\Documents\Development\Miracum GitLab\SimplifierParser\input\GeccoBaseCondition.json
Error in snapshot generation: Differential for https://www.netzwerk-universitaetsmedizin.de/fhir/StructureDefinition/chronic-lung-diseases with id: Condition.category has an element that is not marked with a snapshot match
Error in snapshot generation: Differential for https://www.netzwerk-universitaetsmedizin.de/fhir/StructureDefinition/chronic-lung-diseases with id: Condition.category.coding has an element that is not marked with a snapshot match
Error in snapshot generation: Differential for https://www.netzwerk-universitaetsmedizin.de/fhir/StructureDefinition/chronic-lung-diseases with id: Condition.category.coding:pulmonaryMedicine has an element that is not marked with a snapshot match
Error in snapshot generation: Differential for https://www.netzwerk-universitaetsmedizin.de/fhir/StructureDefinition/chronic-lung-diseases with id: Condition.category.coding:pulmonaryMedicine.system has an element that is not marked with a snapshot match
Error in snapshot generation: Differential for https://www.netzwerk-universitaetsmedizin.de/fhir/StructureDefinition/chronic-lung-diseases with id: Condition.category.coding:pulmonaryMedicine.code has an element that is not marked with a snapshot match
...
What's going on here? Any help/ideas highly appreciated.
Sebastian
Sebastian Mate (Feb 18 2021 at 12:38):
Ah OK, found this: https://github.com/HL7/cda-ccda-2.2/issues/16 and this: https://github.com/highmed/highmed-dsf/issues/45
Sebastian Mate (Feb 18 2021 at 12:41):
OK, I see -- my base is also a differential -- I need to resolve this recursively ...
Last updated: Apr 12 2022 at 19:14 UTC