Search in sources :

Example 71 with DomainResource

use of org.hl7.fhir.r4.model.DomainResource in project cqf-ruler by DBCG.

the class ExpressionEvaluation method evaluateInContext.

/* Evaluates the given CQL expression in the context of the given resource */
/*
	 * If the resource has a library extension, or a library element, that library
	 * is loaded into the context for the expression
	 */
public Object evaluateInContext(DomainResource instance, String cql, Boolean aliasedExpression, String patientId, RequestDetails theRequest) {
    JpaFhirDal jpaFhirDal = jpaFhirDalFactory.create(theRequest);
    List<Reference> libraries = getLibraryReferences(instance, jpaFhirDal, theRequest);
    // String fhirVersion =
    // this.context.getVersion().getVersion().getFhirVersionString();
    String fhirVersion = "3.0.0";
    // temporary LibraryLoader to resolve library dependencies when building
    // includes
    LibraryLoader tempLibraryLoader = libraryLoaderFactory.create(new ArrayList<LibraryContentProvider>(Arrays.asList(jpaLibraryContentProviderFactory.create(theRequest))));
    String source = "";
    if (aliasedExpression) {
        if (libraries.size() != 1) {
            throw new RuntimeException("If an aliased expression is provided, there must be exactly one primary Library");
        }
        VersionedIdentifier vi = getVersionedIdentifierFromReference(libraries.get(0));
        // Still not the best way to build include, but at least checks dal for an
        // existing library
        // Check if id works for LibraryRetrieval
        org.cqframework.cql.elm.execution.Library executionLibrary = null;
        try {
            executionLibrary = tempLibraryLoader.load(vi);
        } catch (Exception e) {
        // log error
        }
        if (executionLibrary == null) {
            Library library = (Library) jpaFhirDal.read(new IdType("Library", vi.getId()));
            vi.setId(library.getName());
            if (library.getVersion() != null) {
                vi.setVersion(library.getVersion());
            }
        }
        // Provide the instance as the value of the '%context' parameter, as well as the
        // value of a parameter named the same as the resource
        // This enables expressions to access the resource by root, as well as through
        // the %context attribute
        source = String.format("library LocalLibrary using FHIR version '" + fhirVersion + "' include FHIRHelpers version '" + fhirVersion + "' called FHIRHelpers %s parameter %s %s parameter \"%%context\" %s define Expression: %s", buildIncludes(tempLibraryLoader, jpaFhirDal, libraries), instance.fhirType(), instance.fhirType(), instance.fhirType(), vi.getId() + ".\"" + cql + "\"");
    // String source = String.format("library LocalLibrary using FHIR version '1.8'
    // include FHIRHelpers version '1.8' called FHIRHelpers %s parameter %s %s
    // parameter \"%%context\" %s define Expression: %s",
    // buildIncludes(libraries), instance.fhirType(), instance.fhirType(),
    // instance.fhirType(), cql);
    } else {
        // Provide the instance as the value of the '%context' parameter, as well as the
        // value of a parameter named the same as the resource
        // This enables expressions to access the resource by root, as well as through
        // the %context attribute
        source = String.format("library LocalLibrary using FHIR version '" + fhirVersion + "' include FHIRHelpers version '" + fhirVersion + "' called FHIRHelpers %s parameter %s %s parameter \"%%context\" %s define Expression: %s", buildIncludes(tempLibraryLoader, jpaFhirDal, libraries), instance.fhirType(), instance.fhirType(), instance.fhirType(), cql);
    }
    LibraryLoader libraryLoader = libraryLoaderFactory.create(new ArrayList<LibraryContentProvider>(Arrays.asList(jpaLibraryContentProviderFactory.create(theRequest), new InMemoryLibraryContentProvider(Arrays.asList(source)))));
    // Remove LocalLibrary from cache first...
    VersionedIdentifier localLibraryIdentifier = new VersionedIdentifier().withId("LocalLibrary");
    globalLibraryCache.remove(localLibraryIdentifier);
    Context context = new Context(libraryLoader.load(localLibraryIdentifier));
    context.setDebugMap(getDebugMap());
    context.setParameter(null, instance.fhirType(), instance);
    context.setParameter(null, "%context", instance);
    context.setExpressionCaching(true);
    context.registerLibraryLoader(libraryLoader);
    context.setContextValue("Patient", patientId);
    TerminologyProvider terminologyProvider = jpaTerminologyProviderFactory.create(theRequest);
    context.registerTerminologyProvider(terminologyProvider);
    DataProvider dataProvider = jpaDataProviderFactory.create(theRequest, terminologyProvider);
    context.registerDataProvider("http://hl7.org/fhir", dataProvider);
    return context.resolveExpressionRef("Expression").evaluate(context);
}
Also used : Context(org.opencds.cqf.cql.engine.execution.Context) LibraryContentProvider(org.opencds.cqf.cql.evaluator.cql2elm.content.LibraryContentProvider) InMemoryLibraryContentProvider(org.opencds.cqf.cql.evaluator.cql2elm.content.InMemoryLibraryContentProvider) InMemoryLibraryContentProvider(org.opencds.cqf.cql.evaluator.cql2elm.content.InMemoryLibraryContentProvider) Reference(org.hl7.fhir.dstu3.model.Reference) JpaFhirDal(org.opencds.cqf.ruler.cql.JpaFhirDal) LibraryLoader(org.opencds.cqf.cql.engine.execution.LibraryLoader) IdType(org.hl7.fhir.dstu3.model.IdType) DataProvider(org.opencds.cqf.cql.engine.data.DataProvider) VersionedIdentifier(org.cqframework.cql.elm.execution.VersionedIdentifier) TerminologyProvider(org.opencds.cqf.cql.engine.terminology.TerminologyProvider) Library(org.hl7.fhir.dstu3.model.Library)

Example 72 with DomainResource

use of org.hl7.fhir.r4.model.DomainResource in project cqf-ruler by DBCG.

the class ExpressionEvaluation method getLibraryReferences.

private List<CanonicalType> getLibraryReferences(DomainResource instance, RequestDetails theRequest) {
    List<CanonicalType> references = new ArrayList<>();
    if (instance.hasContained()) {
        for (Resource resource : instance.getContained()) {
            if (resource instanceof Library) {
                resource.setId(resource.getIdElement().getIdPart().replace("#", ""));
                this.jpaFhirDalFactory.create(theRequest).update((Library) resource);
            // getLibraryLoader().putLibrary(resource.getIdElement().getIdPart(),
            // getLibraryLoader().toElmLibrary((Library) resource));
            }
        }
    }
    if (instance instanceof ActivityDefinition) {
        references.addAll(((ActivityDefinition) instance).getLibrary());
    } else if (instance instanceof PlanDefinition) {
        references.addAll(((PlanDefinition) instance).getLibrary());
    } else if (instance instanceof Measure) {
        references.addAll(((Measure) instance).getLibrary());
    }
    for (Extension extension : instance.getExtensionsByUrl("http://hl7.org/fhir/StructureDefinition/cqif-library")) {
        Type value = extension.getValue();
        if (value instanceof CanonicalType) {
            references.add((CanonicalType) value);
        } else {
            throw new RuntimeException("Library extension does not have a value of type reference");
        }
    }
    return cleanReferences(references);
}
Also used : Extension(org.hl7.fhir.r4.model.Extension) Type(org.hl7.fhir.r4.model.Type) CanonicalType(org.hl7.fhir.r4.model.CanonicalType) IdType(org.hl7.fhir.r4.model.IdType) ArrayList(java.util.ArrayList) Resource(org.hl7.fhir.r4.model.Resource) DomainResource(org.hl7.fhir.r4.model.DomainResource) Measure(org.hl7.fhir.r4.model.Measure) PlanDefinition(org.hl7.fhir.r4.model.PlanDefinition) Library(org.hl7.fhir.r4.model.Library) CanonicalType(org.hl7.fhir.r4.model.CanonicalType) ActivityDefinition(org.hl7.fhir.r4.model.ActivityDefinition)

Example 73 with DomainResource

use of org.hl7.fhir.r4.model.DomainResource in project beneficiary-fhir-data by CMSgov.

the class TransformerUtils method addExtensionCoding.

/**
 * Adds an {@link Extension} to the specified {@link DomainResource}. {@link Extension#getValue()}
 * will be set to a {@link CodeableConcept} containing a single {@link Coding}, with the specified
 * system and code.
 *
 * <p>Data Architecture Note: The {@link CodeableConcept} might seem extraneous -- why not just
 * add the {@link Coding} directly to the {@link Extension}? The main reason for doing it this way
 * is consistency: this is what FHIR seems to do everywhere.
 *
 * @param fhirElement the FHIR element to add the {@link Extension} to
 * @param extensionUrl the {@link Extension#getUrl()} to use
 * @param codingSystem the {@link Coding#getSystem()} to use
 * @param codingDisplay the {@link Coding#getDisplay()} to use
 * @param codingCode the {@link Coding#getCode()} to use
 */
static void addExtensionCoding(IBaseHasExtensions fhirElement, String extensionUrl, String codingSystem, String codingDisplay, String codingCode) {
    IBaseExtension<?, ?> extension = fhirElement.addExtension();
    extension.setUrl(extensionUrl);
    if (codingDisplay == null)
        extension.setValue(new Coding().setSystem(codingSystem).setCode(codingCode));
    else
        extension.setValue(new Coding().setSystem(codingSystem).setCode(codingCode).setDisplay(codingDisplay));
}
Also used : Coding(org.hl7.fhir.dstu3.model.Coding)

Example 74 with DomainResource

use of org.hl7.fhir.r4.model.DomainResource in project beneficiary-fhir-data by CMSgov.

the class TransformerUtils method addExtensionValueIdentifier.

/**
 * Adds an {@link Extension} to the specified {@link DomainResource}. {@link Extension#getValue()}
 * will be set to a {@link Identifier} with the specified url, system, and value.
 *
 * @param fhirElement the FHIR element to add the {@link Extension} to
 * @param extensionUrl the {@link Extension#getUrl()} to use
 * @param extensionSystem the {@link Identifier#getSystem()} to use
 * @param extensionValue the {@link Identifier#getValue()} to use
 */
static void addExtensionValueIdentifier(IBaseHasExtensions fhirElement, String extensionUrl, String extensionSystem, String extensionValue) {
    IBaseExtension<?, ?> extension = fhirElement.addExtension();
    extension.setUrl(extensionUrl);
    Identifier valueIdentifier = new Identifier();
    valueIdentifier.setSystem(extensionSystem).setValue(extensionValue);
    extension.setValue(valueIdentifier);
}
Also used : Identifier(org.hl7.fhir.dstu3.model.Identifier) CurrencyIdentifier(gov.cms.bfd.server.war.stu3.providers.BeneficiaryTransformer.CurrencyIdentifier)

Example 75 with DomainResource

use of org.hl7.fhir.r4.model.DomainResource in project odm2fhir by num-codex.

the class SymptomConditions method createConditions.

private Stream<DomainResource> createConditions(FormData formData, ItemData generalSymptomCoding, ItemData specificCoding) {
    var condition = (Condition) new Condition().addIdentifier(createIdentifier(CONDITION, specificCoding)).setClinicalStatus(ACTIVE).setRecordedDateElement(// TODO Set actual DateTime value
    UNKNOWN_DATE_TIME).addCategory(createCodeableConcept(generalSymptomCoding)).setSeverity(createCodeableConcept(formData.getItemData(specificCoding.getItemOID() + "_schweregrad"))).setMeta(createMeta(SYMPTOMS_COVID_19));
    var specificCodings = createCodings(specificCoding);
    var codeCodeableConcept = new CodeableConcept();
    for (var coding : specificCodings) {
        switch(coding.getCode()) {
            case // PRESENT
            "410605003":
                condition.setClinicalStatus(ACTIVE).setVerificationStatus(CONFIRMED);
                break;
            case // ABSENT
            "410594000":
                condition.setVerificationStatus(REFUTED);
                break;
            case // UNKNOWN
            "261665006":
                condition.addModifierExtension(DATA_PRESENCE_UNKNOWN);
                break;
            case // Answer = Sonstige/Other
            "385432009":
                codeCodeableConcept.addCoding(coding.setDisplay("Not applicable (qualifier value)"));
                if (!formData.getItemData("symptome_andere_symptome_textfeld").isEmpty()) {
                    codeCodeableConcept.setText(formData.getItemData("symptome_andere_symptome_textfeld").getValue());
                }
                break;
            default:
                // SYMPTOM CODING
                if (!"symptome_geruchs_bzw_geschmacksstoerungen".equals(specificCoding.getItemOID())) {
                    codeCodeableConcept.addCoding(coding);
                }
                break;
        }
    }
    if ("symptome_geruchs_bzw_geschmacksstoerungen".equals(specificCoding.getItemOID()) && specificCodings.size() == 2) {
        var codeableConceptTASTE = new CodeableConcept().addCoding(specificCodings.get(0));
        var codeableConceptSMELL = new CodeableConcept().addCoding(specificCodings.get(1));
        var identifier = condition.getIdentifierFirstRep();
        // TODO Check if really nothing should be returned in case only either taste or smell is empty
        return codeableConceptTASTE.isEmpty() || codeableConceptSMELL.isEmpty() ? Stream.empty() : Stream.of(condition.copy().setCode(codeableConceptTASTE).setIdentifier(List.of(identifier.copy().setValue(identifier.getValue() + "_geschmack"))), condition.copy().setCode(codeableConceptSMELL).setIdentifier(List.of(identifier.copy().setValue(identifier.getValue() + "_geruch"))));
    }
    return codeCodeableConcept.isEmpty() ? Stream.empty() : Stream.of(condition.setCode(codeCodeableConcept));
}
Also used : Condition(org.hl7.fhir.r4.model.Condition) CodeableConcept(org.hl7.fhir.r4.model.CodeableConcept)

Aggregations

XhtmlNode (org.hl7.fhir.utilities.xhtml.XhtmlNode)24 FHIRException (org.hl7.fhir.exceptions.FHIRException)22 DomainResource (org.hl7.fhir.r4.model.DomainResource)21 DefinitionException (org.hl7.fhir.exceptions.DefinitionException)16 IOException (java.io.IOException)15 DomainResource (org.hl7.fhir.dstu3.model.DomainResource)15 FileOutputStream (java.io.FileOutputStream)12 ArrayList (java.util.ArrayList)11 CommaSeparatedStringBuilder (org.hl7.fhir.utilities.CommaSeparatedStringBuilder)11 Test (org.junit.jupiter.api.Test)11 Resource (org.hl7.fhir.r4.model.Resource)10 IBaseResource (org.hl7.fhir.instance.model.api.IBaseResource)9 FileNotFoundException (java.io.FileNotFoundException)8 List (java.util.List)8 NotImplementedException (org.apache.commons.lang3.NotImplementedException)8 StructureDefinition (org.hl7.fhir.r5.model.StructureDefinition)8 SystemRequestDetails (ca.uhn.fhir.jpa.partition.SystemRequestDetails)7 File (java.io.File)7 ElementDefn (org.hl7.fhir.definitions.model.ElementDefn)7 RestIntegrationTest (org.opencds.cqf.ruler.test.RestIntegrationTest)7