Search in sources :

Example 1 with R4FhirModelResolver

use of org.opencds.cqf.cql.engine.fhir.model.R4FhirModelResolver in project CRD by HL7-DaVinci.

the class CqlExecutionContextBuilder method getExecutionContext.

public static Context getExecutionContext(CqlRule cqlRule, HashMap<String, Resource> cqlParams, String baseUrl) {
    ModelManager modelManager = new ModelManager();
    LibraryManager libraryManager = new LibraryManager(modelManager);
    libraryManager.getLibrarySourceLoader().clearProviders();
    Library library = null;
    LibraryLoader libraryLoader = null;
    if (cqlRule.isPrecompiled()) {
    // todo
    } else {
        libraryManager.getLibrarySourceLoader().registerProvider(cqlRule.getRawCqlLibrarySourceProvider(CQL_VERSION));
        libraryLoader = new LocalLibraryLoader(libraryManager);
        try {
            library = CqlExecution.translate(cqlRule.getRawMainCqlLibrary(CQL_VERSION), libraryManager, modelManager);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
    FhirContext fhirContext = FhirContext.forR4();
    Context context = new Context(library);
    context.registerLibraryLoader(libraryLoader);
    context.setExpressionCaching(true);
    R4FhirModelResolver modelResolver = new R4FhirModelResolver();
    RestFhirRetrieveProvider retrieveProvider = new RestFhirRetrieveProvider(new SearchParameterResolver(fhirContext), fhirContext.newRestfulGenericClient("http://fhirtest.uhn.ca/baseR4"));
    CompositeDataProvider provider = new CompositeDataProvider(modelResolver, retrieveProvider);
    context.registerDataProvider("http://hl7.org/fhir", provider);
    for (Map.Entry<String, org.hl7.fhir.r4.model.Resource> entry : cqlParams.entrySet()) {
        context.setParameter(null, entry.getKey(), entry.getValue());
    }
    context.setParameter(null, "base_url", baseUrl);
    return context;
}
Also used : Context(org.opencds.cqf.cql.engine.execution.Context) FhirContext(ca.uhn.fhir.context.FhirContext) FhirContext(ca.uhn.fhir.context.FhirContext) Resource(org.hl7.fhir.r4.model.Resource) ModelManager(org.cqframework.cql.cql2elm.ModelManager) LibraryLoader(org.opencds.cqf.cql.engine.execution.LibraryLoader) LocalLibraryLoader(org.hl7.davinci.endpoint.cql.LocalLibraryLoader) R4FhirModelResolver(org.opencds.cqf.cql.engine.fhir.model.R4FhirModelResolver) SearchParameterResolver(org.opencds.cqf.cql.engine.fhir.searchparam.SearchParameterResolver) LocalLibraryLoader(org.hl7.davinci.endpoint.cql.LocalLibraryLoader) RestFhirRetrieveProvider(org.opencds.cqf.cql.engine.fhir.retrieve.RestFhirRetrieveProvider) LibraryManager(org.cqframework.cql.cql2elm.LibraryManager) CompositeDataProvider(org.opencds.cqf.cql.engine.data.CompositeDataProvider) Library(org.cqframework.cql.elm.execution.Library) HashMap(java.util.HashMap) Map(java.util.Map)

Example 2 with R4FhirModelResolver

use of org.opencds.cqf.cql.engine.fhir.model.R4FhirModelResolver in project cqf-ruler by DBCG.

the class R4HookEvaluator method resolveActions.

private void resolveActions(List<PlanDefinition.PlanDefinitionActionComponent> actions, Context context, String patientId, RequestGroupBuilder requestGroupBuilder, List<RequestGroup.RequestGroupActionComponent> actionComponents, IGenericClient applyClient) {
    for (PlanDefinition.PlanDefinitionActionComponent action : actions) {
        boolean conditionsMet = true;
        for (PlanDefinition.PlanDefinitionActionConditionComponent condition : action.getCondition()) {
            if (condition.getKind() == PlanDefinition.ActionConditionKind.APPLICABILITY) {
                if (!condition.hasExpression()) {
                    continue;
                }
                Object result = context.resolveExpressionRef(condition.getExpression().getExpression()).getExpression().evaluate(context);
                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.hasPrefix()) {
                    actionBuilder.buildPrefix(action.getPrefix());
                }
                if (action.hasType()) {
                    actionBuilder.buildType(action.getType());
                }
                if (action.hasSelectionBehavior()) {
                    actionBuilder.buildSelectionBehavior(RequestGroup.ActionSelectionBehavior.fromCode(action.getSelectionBehavior().toCode()));
                }
                Resource resource = null;
                if (action.hasDefinition()) {
                    if (action.getDefinitionCanonicalType().getValue().contains("ActivityDefinition")) {
                        Parameters inParams = new Parameters();
                        inParams.addParameter().setName("patient").setValue(new StringType(patientId));
                        Parameters outParams = applyClient.operation().onInstance(new IdDt(action.getDefinitionCanonicalType().getValue())).named("$apply").withParameters(inParams).useHttpGet().execute();
                        List<Parameters.ParametersParameterComponent> response = outParams.getParameter();
                        resource = response.get(0).getResource().setId(UUID.randomUUID().toString());
                    }
                }
                // 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().getExpression()).evaluate(context);
                                actionBuilder.buildTitle(title);
                            } else if (dynamicValue.getPath().endsWith("description")) {
                                // detail
                                String description = (String) context.resolveExpressionRef(dynamicValue.getExpression().getExpression()).evaluate(context);
                                actionBuilder.buildDescripition(description);
                            } else if (dynamicValue.getPath().endsWith("extension")) {
                                // indicator
                                String extension = (String) context.resolveExpressionRef(dynamicValue.getExpression().getExpression()).evaluate(context);
                                actionBuilder.buildExtension(extension);
                            } else {
                                if (resource != null) {
                                    Object value = context.resolveExpressionRef(dynamicValue.getExpression().getExpression()).evaluate(context);
                                    // TODO need to verify type... yay
                                    if (value instanceof Boolean) {
                                        value = new BooleanType((Boolean) value);
                                    }
                                    R4FhirModelResolver modelResolver = new R4FhirModelResolver();
                                    modelResolver.setValue(resource, dynamicValue.getPath(), value);
                                    actionBuilder.buildResourceTarget(resource);
                                    actionBuilder.buildResource(new ReferenceBuilder().buildReference(resource.getId()).build());
                                }
                            }
                        }
                    }
                }
                if (!actionBuilder.build().isEmpty()) {
                    actionComponents.add(actionBuilder.build());
                }
                if (action.hasAction()) {
                    resolveActions(action.getAction(), context, patientId, requestGroupBuilder, actionComponents, applyClient);
                }
            }
        }
    }
    requestGroupBuilder.buildAction(new ArrayList<>(actionComponents));
}
Also used : IdDt(ca.uhn.fhir.model.primitive.IdDt) R4FhirModelResolver(org.opencds.cqf.cql.engine.fhir.model.R4FhirModelResolver)

Aggregations

R4FhirModelResolver (org.opencds.cqf.cql.engine.fhir.model.R4FhirModelResolver)2 FhirContext (ca.uhn.fhir.context.FhirContext)1 IdDt (ca.uhn.fhir.model.primitive.IdDt)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 LibraryManager (org.cqframework.cql.cql2elm.LibraryManager)1 ModelManager (org.cqframework.cql.cql2elm.ModelManager)1 Library (org.cqframework.cql.elm.execution.Library)1 LocalLibraryLoader (org.hl7.davinci.endpoint.cql.LocalLibraryLoader)1 Resource (org.hl7.fhir.r4.model.Resource)1 CompositeDataProvider (org.opencds.cqf.cql.engine.data.CompositeDataProvider)1 Context (org.opencds.cqf.cql.engine.execution.Context)1 LibraryLoader (org.opencds.cqf.cql.engine.execution.LibraryLoader)1 RestFhirRetrieveProvider (org.opencds.cqf.cql.engine.fhir.retrieve.RestFhirRetrieveProvider)1 SearchParameterResolver (org.opencds.cqf.cql.engine.fhir.searchparam.SearchParameterResolver)1