Search in sources :

Example 26 with PlanDefinition

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

the class ExpressionEvaluationIT method testExpressionEvaluationANCDT01PlanDefinitionDomain.

@Test
public void testExpressionEvaluationANCDT01PlanDefinitionDomain() throws Exception {
    DomainResource planDefinition = (DomainResource) planDefinitions.get("lcs-cds-patient-view");
    // Patient First
    uploadTests("test/plandefinition/LungCancerScreening/Former-Smoker/Patient");
    Map<String, IBaseResource> resources = uploadTests("test/plandefinition/LungCancerScreening/Former-Smoker");
    IBaseResource patient = resources.get("Former-Smoker");
    Object isFormerSmoker = expressionEvaluation.evaluateInContext(planDefinition, "Is former smoker who quit within past 15 years", patient.getIdElement().getIdPart(), true, new SystemRequestDetails());
    assertTrue(isFormerSmoker instanceof Boolean);
    assertTrue(((Boolean) isFormerSmoker).booleanValue());
    Object isCurrentSmoker = expressionEvaluation.evaluateInContext(planDefinition, "Is current smoker", patient.getIdElement().getIdPart(), true, new SystemRequestDetails());
    assertTrue(isCurrentSmoker instanceof Boolean);
    assertTrue((!(Boolean) isCurrentSmoker));
}
Also used : DomainResource(org.hl7.fhir.r4.model.DomainResource) SystemRequestDetails(ca.uhn.fhir.jpa.partition.SystemRequestDetails) IBaseResource(org.hl7.fhir.instance.model.api.IBaseResource) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest) RestIntegrationTest(org.opencds.cqf.ruler.test.RestIntegrationTest)

Example 27 with PlanDefinition

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

the class Session method resolveActions.

private void resolveActions(RequestDetails theRequest, PlanDefinition planDefinition, List<PlanDefinition.PlanDefinitionActionComponent> actions, Context context, String patientId, RequestGroupBuilder requestGroupBuilder, List<RequestGroup.RequestGroupActionComponent> actionComponents) {
    for (PlanDefinition.PlanDefinitionActionComponent action : actions) {
        boolean conditionsMet = true;
        for (PlanDefinition.PlanDefinitionActionConditionComponent condition : action.getCondition()) {
            if (condition.getKind() == PlanDefinition.ActionConditionKind.APPLICABILITY) {
                if (!condition.hasExpression()) {
                    continue;
                }
                Boolean aliasedExpression = null;
                if (condition.hasLanguage()) {
                    String language = condition.getLanguage();
                    if (language.equals("text/cql.identifier") || language.equals("text/cql-identifier") || language.equals("text/cql.name") || language.equals("text/cql-name")) {
                        aliasedExpression = true;
                    } else {
                        aliasedExpression = false;
                    }
                } else {
                    aliasedExpression = false;
                }
                Object result = expressionEvaluation.evaluateInContext(planDefinition, condition.getExpression(), aliasedExpression, patientId, theRequest);
                if (!(result instanceof Boolean)) {
                    continue;
                }
                if (!(Boolean) result) {
                    conditionsMet = false;
                }
            }
            if (conditionsMet) {
                RequestGroupActionBuilder actionBuilder = new RequestGroupActionBuilder();
                if (action.hasTitle()) {
                    actionBuilder.buildTitle(action.getTitle());
                }
                if (action.hasDescription()) {
                    actionBuilder.buildDescripition(action.getDescription());
                }
                // source
                if (action.hasDocumentation()) {
                    RelatedArtifact artifact = action.getDocumentationFirstRep();
                    RelatedArtifactBuilder artifactBuilder = new RelatedArtifactBuilder();
                    if (artifact.hasDisplay()) {
                        artifactBuilder.buildDisplay(artifact.getDisplay());
                    }
                    if (artifact.hasUrl()) {
                        artifactBuilder.buildUrl(artifact.getUrl());
                    }
                    if (artifact.hasDocument() && artifact.getDocument().hasUrl()) {
                        AttachmentBuilder attachmentBuilder = new AttachmentBuilder();
                        attachmentBuilder.buildUrl(artifact.getDocument().getUrl());
                        artifactBuilder.buildDocument(attachmentBuilder.build());
                    }
                    actionBuilder.buildDocumentation(Collections.singletonList(artifactBuilder.build()));
                }
                // TODO - uuid
                if (action.hasLabel()) {
                    actionBuilder.buildLabel(action.getLabel());
                }
                if (action.hasType()) {
                    actionBuilder.buildType(action.getType());
                }
                if (action.hasDefinition()) {
                    if (action.getDefinition().getReferenceElement().getResourceType().equals("ActivityDefinition")) {
                        if (action.getDefinition().getResource() != null) {
                            ActivityDefinition activityDefinition = (ActivityDefinition) action.getDefinition().getResource();
                            ReferenceBuilder referenceBuilder = new ReferenceBuilder();
                            referenceBuilder.buildDisplay(activityDefinition.getDescription());
                            actionBuilder.buildResource(referenceBuilder.build());
                            if (activityDefinition.hasDescription()) {
                                actionBuilder.buildDescripition(activityDefinition.getDescription());
                            }
                        }
                        ActivityDefinition activityDefinition = this.activityDefinitionDao.read(action.getDefinition().getReferenceElement());
                        if (activityDefinition.hasDescription()) {
                            actionBuilder.buildDescripition(activityDefinition.getDescription());
                        }
                        try {
                            this.activityDefinitionApplyProvider.apply(theRequest, new IdType(action.getDefinition().getReferenceElement().getIdPart()), patientId, null, null, null, null, null, null, null, null).setId(UUID.randomUUID().toString());
                        } catch (FHIRException e) {
                            throw new RuntimeException("Error applying ActivityDefinition " + e.getMessage());
                        }
                        Parameters inParams = new Parameters();
                        inParams.addParameter().setName("patient").setValue(new StringType(patientId));
                        Parameters outParams = this.fhirContext.newRestfulGenericClient(theRequest.getFhirServerBase()).operation().onInstance(new IdDt("ActivityDefinition", action.getDefinition().getId())).named("$apply").withParameters(inParams).useHttpGet().execute();
                        List<Parameters.ParametersParameterComponent> response = outParams.getParameter();
                        Resource resource = response.get(0).getResource().setId(UUID.randomUUID().toString());
                        actionBuilder.buildResourceTarget(resource);
                        actionBuilder.buildResource(new ReferenceBuilder().buildReference(resource.getId()).build());
                    }
                }
                // on here...
                if (action.hasDynamicValue()) {
                    for (PlanDefinition.PlanDefinitionActionDynamicValueComponent dynamicValue : action.getDynamicValue()) {
                        if (dynamicValue.hasPath() && dynamicValue.hasExpression()) {
                            if (dynamicValue.getPath().endsWith("title")) {
                                // summary
                                String title = (String) context.resolveExpressionRef(dynamicValue.getExpression()).evaluate(context);
                                actionBuilder.buildTitle(title);
                            } else if (dynamicValue.getPath().endsWith("description")) {
                                // detail
                                String description = (String) context.resolveExpressionRef(dynamicValue.getExpression()).evaluate(context);
                                actionBuilder.buildDescripition(description);
                            } else if (dynamicValue.getPath().endsWith("extension")) {
                                // indicator
                                String extension = (String) context.resolveExpressionRef(dynamicValue.getExpression()).evaluate(context);
                                actionBuilder.buildExtension(extension);
                            }
                        }
                    }
                }
                if (!actionBuilder.build().isEmpty()) {
                    actionComponents.add(actionBuilder.build());
                }
                if (action.hasAction()) {
                    resolveActions(theRequest, planDefinition, action.getAction(), context, patientId, requestGroupBuilder, actionComponents);
                }
            }
        }
    }
    requestGroupBuilder.buildAction(new ArrayList<>(actionComponents));
}
Also used : Parameters(org.hl7.fhir.dstu3.model.Parameters) StringType(org.hl7.fhir.dstu3.model.StringType) DomainResource(org.hl7.fhir.dstu3.model.DomainResource) Resource(org.hl7.fhir.dstu3.model.Resource) IdDt(ca.uhn.fhir.model.primitive.IdDt) RelatedArtifact(org.hl7.fhir.dstu3.model.RelatedArtifact) FHIRException(org.hl7.fhir.exceptions.FHIRException) IdType(org.hl7.fhir.dstu3.model.IdType) RequestGroupActionBuilder(org.opencds.cqf.ruler.cr.dstu3.builder.RequestGroupActionBuilder) AttachmentBuilder(org.opencds.cqf.ruler.cr.dstu3.builder.AttachmentBuilder) ReferenceBuilder(org.opencds.cqf.ruler.cr.dstu3.builder.ReferenceBuilder) PlanDefinition(org.hl7.fhir.dstu3.model.PlanDefinition) RelatedArtifactBuilder(org.opencds.cqf.ruler.cr.dstu3.builder.RelatedArtifactBuilder) ActivityDefinition(org.hl7.fhir.dstu3.model.ActivityDefinition)

Example 28 with PlanDefinition

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

the class Session method resolveCdsHooksPlanDefinition.

// For library use
public CarePlan resolveCdsHooksPlanDefinition(Context context, PlanDefinition planDefinition, String patientId, RequestDetails theRequest) {
    CarePlanBuilder carePlanBuilder = new CarePlanBuilder();
    RequestGroupBuilder requestGroupBuilder = new RequestGroupBuilder().buildStatus().buildIntent();
    // links
    if (planDefinition.hasRelatedArtifact()) {
        List<Extension> extensions = new ArrayList<>();
        for (RelatedArtifact relatedArtifact : planDefinition.getRelatedArtifact()) {
            AttachmentBuilder attachmentBuilder = new AttachmentBuilder();
            ExtensionBuilder extensionBuilder = new ExtensionBuilder();
            if (relatedArtifact.hasDisplay()) {
                // label
                attachmentBuilder.buildTitle(relatedArtifact.getDisplay());
            }
            if (relatedArtifact.hasUrl()) {
                // url
                attachmentBuilder.buildUrl(relatedArtifact.getUrl());
            }
            if (relatedArtifact.hasExtension()) {
                // type
                attachmentBuilder.buildExtension(relatedArtifact.getExtension());
            }
            extensionBuilder.buildUrl("http://example.org");
            extensionBuilder.buildValue(attachmentBuilder.build());
            extensions.add(extensionBuilder.build());
        }
        requestGroupBuilder.buildExtension(extensions);
    }
    resolveActions(planDefinition.getAction(), context, patientId, requestGroupBuilder, new ArrayList<>(), theRequest);
    CarePlanActivityBuilder carePlanActivityBuilder = new CarePlanActivityBuilder();
    carePlanActivityBuilder.buildReferenceTarget(requestGroupBuilder.build());
    carePlanBuilder.buildActivity(carePlanActivityBuilder.build());
    return carePlanBuilder.build();
}
Also used : Extension(org.hl7.fhir.r4.model.Extension) AttachmentBuilder(org.opencds.cqf.ruler.cr.r4.builder.AttachmentBuilder) CarePlanBuilder(org.opencds.cqf.ruler.cr.r4.builder.CarePlanBuilder) RequestGroupBuilder(org.opencds.cqf.ruler.cr.r4.builder.RequestGroupBuilder) ArrayList(java.util.ArrayList) ExtensionBuilder(org.opencds.cqf.ruler.cr.r4.builder.ExtensionBuilder) RelatedArtifact(org.hl7.fhir.r4.model.RelatedArtifact) CarePlanActivityBuilder(org.opencds.cqf.ruler.cr.r4.builder.CarePlanActivityBuilder)

Example 29 with PlanDefinition

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

the class ExpressionEvaluation method getLibraryReferences.

private List<Reference> getLibraryReferences(DomainResource instance, JpaFhirDal jpaFhirDal, RequestDetails theRequest) {
    List<Reference> references = new ArrayList<>();
    if (instance.hasContained()) {
        for (Resource resource : instance.getContained()) {
            if (resource instanceof Library) {
                resource.setId(resource.getIdElement().getIdPart().replace("#", ""));
                jpaFhirDal.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 Reference) {
            references.add((Reference) value);
        } else {
            throw new RuntimeException("Library extension does not have a value of type reference");
        }
    }
    return cleanReferences(references);
}
Also used : Extension(org.hl7.fhir.dstu3.model.Extension) Type(org.hl7.fhir.dstu3.model.Type) IdType(org.hl7.fhir.dstu3.model.IdType) Reference(org.hl7.fhir.dstu3.model.Reference) ArrayList(java.util.ArrayList) DomainResource(org.hl7.fhir.dstu3.model.DomainResource) Resource(org.hl7.fhir.dstu3.model.Resource) Measure(org.hl7.fhir.dstu3.model.Measure) PlanDefinition(org.hl7.fhir.dstu3.model.PlanDefinition) Library(org.hl7.fhir.dstu3.model.Library) ActivityDefinition(org.hl7.fhir.dstu3.model.ActivityDefinition)

Example 30 with PlanDefinition

use of org.hl7.fhir.r4.model.PlanDefinition 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)

Aggregations

Complex (org.hl7.fhir.dstu3.utils.formats.Turtle.Complex)8 Complex (org.hl7.fhir.r4.utils.formats.Turtle.Complex)8 PlanDefinition (org.hl7.fhir.dstu3.model.PlanDefinition)7 PlanDefinition (org.hl7.fhir.r4.model.PlanDefinition)7 SystemRequestDetails (ca.uhn.fhir.jpa.partition.SystemRequestDetails)6 Test (org.junit.jupiter.api.Test)6 RestIntegrationTest (org.opencds.cqf.ruler.test.RestIntegrationTest)6 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)6 ArrayList (java.util.ArrayList)5 DefinitionException (org.hl7.fhir.exceptions.DefinitionException)5 JsonObject (com.google.gson.JsonObject)4 DomainResource (org.hl7.fhir.dstu3.model.DomainResource)4 IBaseResource (org.hl7.fhir.instance.model.api.IBaseResource)3 Library (org.hl7.fhir.r4.model.Library)3 StructureDefinition (org.hl7.fhir.r4b.model.StructureDefinition)3 CanonicalResourceProxy (org.hl7.fhir.r5.context.CanonicalResourceManager.CanonicalResourceProxy)3 StructureDefinition (org.hl7.fhir.r5.model.StructureDefinition)3 FhirContext (ca.uhn.fhir.context.FhirContext)2 Operation (ca.uhn.fhir.rest.annotation.Operation)2 RequestDetails (ca.uhn.fhir.rest.api.server.RequestDetails)2