Example fhir-paths for extracting specific data

Home   »   Example fhir-paths for extracting specific data

/*
 * (C) Copyright IBM Corp. 2021
 *
 * SPDX-License-Identifier: Apache-2.0
 */

package demo;

import static com.ibm.fhir.model.type.String.string;

import java.time.LocalDate;
import java.time.ZoneOffset;
import java.util.Collection;
import java.util.Iterator;
import java.util.UUID;

import com.ibm.fhir.model.resource.Bundle;
import com.ibm.fhir.model.resource.Bundle.Entry.Request;
import com.ibm.fhir.model.resource.Observation;
import com.ibm.fhir.model.resource.Observation.Component;
import com.ibm.fhir.model.resource.Patient;
import com.ibm.fhir.model.type.Code;
import com.ibm.fhir.model.type.CodeableConcept;
import com.ibm.fhir.model.type.Coding;
import com.ibm.fhir.model.type.Date;
import com.ibm.fhir.model.type.Decimal;
import com.ibm.fhir.model.type.HumanName;
import com.ibm.fhir.model.type.Id;
import com.ibm.fhir.model.type.Instant;
import com.ibm.fhir.model.type.Meta;
import com.ibm.fhir.model.type.Narrative;
import com.ibm.fhir.model.type.Quantity;
import com.ibm.fhir.model.type.Reference;
import com.ibm.fhir.model.type.Uri;
import com.ibm.fhir.model.type.Xhtml;
import com.ibm.fhir.model.type.code.BundleType;
import com.ibm.fhir.model.type.code.HTTPVerb;
import com.ibm.fhir.model.type.code.NarrativeStatus;
import com.ibm.fhir.model.type.code.ObservationStatus;
import com.ibm.fhir.path.FHIRPathNode;
import com.ibm.fhir.path.evaluator.FHIRPathEvaluator;
import com.ibm.fhir.path.evaluator.FHIRPathEvaluator.EvaluationContext;
import com.ibm.fhir.path.exception.FHIRPathException;

/**
 * Using the IBM FHIR Server with FHIR Path - http://hl7.org/fhirpath/
 *
 */
public class Main {
    public Bundle sample() {
        var ptnt1 = buildTestPatient();
        Bundle.Entry patient1 = Bundle.Entry.builder()
                .resource(ptnt1)
                .request(Request.builder().method(HTTPVerb.POST)
                    .url(Uri.of("Patient")).build())
                .build();
        Bundle.Entry observation1 = Bundle.Entry.builder()
                .resource(buildObservation(ptnt1.getId(), 89.0f, 124.9f))
                .request(Request.builder().method(HTTPVerb.POST)
                    .url(Uri.of("Observation")).build())
                .build();
        Bundle.Entry observation2 = Bundle.Entry.builder()
                .resource(buildObservation(ptnt1.getId(), 89.0f, 124.9f))
                .request(Request.builder().method(HTTPVerb.POST)
                    .url(Uri.of("Observation")).build())
                .build();

        var ptnt2 = buildTestPatient();
        Bundle.Entry patient2 = Bundle.Entry.builder()
                .resource(ptnt2)
                .request(Request.builder().method(HTTPVerb.POST)
                    .url(Uri.of("Patient")).build())
                .build();
        Bundle.Entry observation3 = Bundle.Entry.builder()
                .resource(buildObservation(ptnt2.getId(), 90.0f, 132.9f))
                .request(Request.builder().method(HTTPVerb.POST)
                    .url(Uri.of("Observation")).build())
                .build();
        Bundle.Entry observation4 = Bundle.Entry.builder()
                .resource(buildObservation(ptnt2.getId(), 91.0f, 132.9f))
                .request(Request.builder().method(HTTPVerb.POST)
                    .url(Uri.of("Observation")).build())
                .build();

        return Bundle.builder()
                .type(BundleType.MESSAGE)
                .entry(patient1, observation1, observation2, patient2, observation3, observation4)
                .timestamp(com.ibm.fhir.model.type.Instant.now())
                .build();
    }

    private Patient buildTestPatient() {
        var id = UUID.randomUUID().toString();

        var meta = Meta.builder()
                    .versionId(Id.of("1"))
                    .lastUpdated(Instant.now(ZoneOffset.UTC))
                    .build();

        com.ibm.fhir.model.type.String given =
                com.ibm.fhir.model.type.String.builder()
                .value("John")
                .build();

        var name = HumanName.builder()
                    .id("someId")
                    .given(given)
                    .family(string("Doe")).build();

        var uUID = UUID.randomUUID().toString();

        Reference providerRef =
                Reference.builder().reference(string("urn:uuid:" + uUID)).build();

        return Patient.builder().id(id)
                .active(com.ibm.fhir.model.type.Boolean.TRUE)
                .multipleBirth(com.ibm.fhir.model.type.Integer.of(2))
                .meta(meta).name(name).birthDate(Date.of(LocalDate.now()))
                .generalPractitioner(providerRef)
                .text(
                    Narrative.builder()
                        .div(Xhtml.of("
Generated Sample Data
")) .status(NarrativeStatus.GENERATED).build()) .build(); } private Observation buildObservation(String patientId, float low, float high) { CodeableConcept code = CodeableConcept.builder().coding(Coding.builder().code(Code.of("55284-4")) .system(Uri.of("http://loinc.org")).build()) .text(string("Blood pressure systolic & diastolic")) .build(); return Observation.builder() .id(UUID.randomUUID().toString()) .status(ObservationStatus.FINAL) .bodySite( CodeableConcept.builder().coding(Coding.builder().code(Code.of("55284-4")) .system(Uri.of("http://loinc.org")).build()) .text(string("Blood pressure systolic & diastolic")).build()) .category(CodeableConcept.builder().coding(Coding.builder().code(Code.of("signs")) .system(Uri.of("http://hl7.org/fhir/observation-category")).build()) .text(string("Vital Signs")).build()) .code(code) .subject(Reference.builder().reference(string("Patient/" + patientId)).build()) .component(Component.builder().code(CodeableConcept.builder().coding(Coding.builder().code(Code.of("8480-6")) .system(Uri.of("http://loinc.org")).build()) .text(string("Systolic blood pressure")).build()) .value(Quantity.builder().value(Decimal.of(high)).unit(string("mmHg")).build()).build()) .component( Component.builder() .code(CodeableConcept.builder() .coding(Coding.builder() .code(Code.of("8462-4")) .system(Uri.of("http://loinc.org")).build()) .text(string("Diastolic blood pressure")).build()) .value(Quantity.builder().value(Decimal.of(low)).unit(string("mmHg")).build()).build()) .text( Narrative.builder() .div(Xhtml.of("
Generated Sample Data
")) .status(NarrativeStatus.GENERATED).build()) .build(); } public static void main(String[] args) throws FHIRPathException { var main = new Main(); var bundle = main.sample(); var evaluator = FHIRPathEvaluator.evaluator(); var evaluationContext = new EvaluationContext(bundle); System.out.println("# Extract the references from the first subject"); Collection result = evaluator.evaluate(evaluationContext, "Bundle.entry.resource.ofType(Observation).subject[0].reference.value"); Iterator iter = result.iterator(); while (iter.hasNext()) { System.out.println(iter.next()); } System.out.println(); System.out.println("# Extract the references from the first subject alternative"); result = evaluator.evaluate(evaluationContext, "Bundle.entry.resource.ofType(Observation).subject.first().reference.value"); iter = result.iterator(); while (iter.hasNext()) { System.out.println(iter.next()); } System.out.println(); System.out.println("# Select the first Patient reference where the Diastolic is over 90"); result = evaluator.evaluate(evaluationContext, "Bundle.entry.resource.ofType(Observation).where(component.where(code.coding.code = '8462-4').value.where( value > 90.0).exists()).subject.reference.value.first()"); iter = result.iterator(); var v = ""; while (iter.hasNext()) { v = iter.next().toString(); System.out.println(v); } System.out.println(); System.out.println("# Select the Patient's first name where the reference matches the logical id"); // Bundle.entry.resource.ofType(Observation).where(component.where(code.coding.code = '8462-4').value.where( value > 90.0).exists()).subject.reference.value.first() result = evaluator.evaluate(evaluationContext, "Bundle.entry.resource.ofType(Patient).where(id = '" + v.split("/")[1] + "').name.given.value"); iter = result.iterator(); while (iter.hasNext()) { System.out.println(iter.next()); } System.out.println(); } }

Leave a Reply

Your email address will not be published. Required fields are marked *