Search in sources :

Example 1 with CqlEngine

use of org.opencds.cqf.cql.engine.execution.CqlEngine in project cqf-ruler by DBCG.

the class CqlExecutionProvider method setupEngine.

private CqlEngine setupEngine(String subject, Parameters parameters, Endpoint dataEndpoint, Endpoint terminologyEndpoint, Bundle data, boolean useServerData, LibraryLoader libraryLoader, VersionedIdentifier libraryIdentifier, RequestDetails theRequestDetails) {
    TerminologyProvider terminologyProvider;
    if (terminologyEndpoint != null) {
        IGenericClient client = Clients.forEndpoint(getFhirContext(), terminologyEndpoint);
        terminologyProvider = new Dstu3FhirTerminologyProvider(client);
    } else {
        terminologyProvider = jpaTerminologyProviderFactory.create(theRequestDetails);
    }
    DataProvider dataProvider;
    List<RetrieveProvider> retrieveProviderList = new ArrayList<>();
    if (useServerData) {
        JpaFhirRetrieveProvider jpaRetriever = new JpaFhirRetrieveProvider(getDaoRegistry(), new SearchParameterResolver(getFhirContext()));
        jpaRetriever.setTerminologyProvider(terminologyProvider);
        // Assume it's a different server, therefore need to expand.
        if (terminologyEndpoint != null) {
            jpaRetriever.setExpandValueSets(true);
        }
        retrieveProviderList.add(jpaRetriever);
    }
    if (dataEndpoint != null) {
        IGenericClient client = Clients.forEndpoint(dataEndpoint);
        RestFhirRetrieveProvider restRetriever = new RestFhirRetrieveProvider(new SearchParameterResolver(getFhirContext()), client);
        restRetriever.setTerminologyProvider(terminologyProvider);
        if (terminologyEndpoint == null || (terminologyEndpoint != null && !terminologyEndpoint.getAddress().equals(dataEndpoint.getAddress()))) {
            restRetriever.setExpandValueSets(true);
        }
        retrieveProviderList.add(restRetriever);
    }
    if (data != null) {
        BundleRetrieveProvider bundleRetriever = new BundleRetrieveProvider(getFhirContext(), data);
        bundleRetriever.setTerminologyProvider(terminologyProvider);
        retrieveProviderList.add(bundleRetriever);
    }
    PriorityRetrieveProvider priorityProvider = new PriorityRetrieveProvider(retrieveProviderList);
    dataProvider = new CompositeDataProvider(myModelResolver, priorityProvider);
    return new CqlEngine(libraryLoader, Collections.singletonMap("http://hl7.org/fhir", dataProvider), terminologyProvider);
}
Also used : JpaFhirRetrieveProvider(org.opencds.cqf.ruler.cql.JpaFhirRetrieveProvider) IGenericClient(ca.uhn.fhir.rest.client.api.IGenericClient) ArrayList(java.util.ArrayList) CqlEngine(org.opencds.cqf.cql.engine.execution.CqlEngine) CompositeDataProvider(org.opencds.cqf.cql.engine.data.CompositeDataProvider) DataProvider(org.opencds.cqf.cql.engine.data.DataProvider) SearchParameterResolver(org.opencds.cqf.cql.engine.fhir.searchparam.SearchParameterResolver) BundleRetrieveProvider(org.opencds.cqf.cql.evaluator.engine.retrieve.BundleRetrieveProvider) TerminologyProvider(org.opencds.cqf.cql.engine.terminology.TerminologyProvider) Dstu3FhirTerminologyProvider(org.opencds.cqf.cql.engine.fhir.terminology.Dstu3FhirTerminologyProvider) RestFhirRetrieveProvider(org.opencds.cqf.cql.engine.fhir.retrieve.RestFhirRetrieveProvider) Dstu3FhirTerminologyProvider(org.opencds.cqf.cql.engine.fhir.terminology.Dstu3FhirTerminologyProvider) PriorityRetrieveProvider(org.opencds.cqf.cql.evaluator.engine.retrieve.PriorityRetrieveProvider) CompositeDataProvider(org.opencds.cqf.cql.engine.data.CompositeDataProvider) RestFhirRetrieveProvider(org.opencds.cqf.cql.engine.fhir.retrieve.RestFhirRetrieveProvider) JpaFhirRetrieveProvider(org.opencds.cqf.ruler.cql.JpaFhirRetrieveProvider) RetrieveProvider(org.opencds.cqf.cql.engine.retrieve.RetrieveProvider) BundleRetrieveProvider(org.opencds.cqf.cql.evaluator.engine.retrieve.BundleRetrieveProvider) PriorityRetrieveProvider(org.opencds.cqf.cql.evaluator.engine.retrieve.PriorityRetrieveProvider)

Example 2 with CqlEngine

use of org.opencds.cqf.cql.engine.execution.CqlEngine in project cqf-ruler by DBCG.

the class CqlExecutionProvider method evaluate.

/**
 * Evaluates a CQL expression and returns the results as a Parameters resource.
 *
 * @param theRequestDetails   the {@link RequestDetails RequestDetails}
 * @param subject             ***Only Patient is supported as of now*** Subject
 *                            for which the expression will be
 *                            evaluated. This corresponds to the context in
 *                            which the expression will be evaluated and is
 *                            represented as a relative FHIR id (e.g.
 *                            Patient/123), which establishes both the context
 *                            and context value for the evaluation
 * @param expression          Expression to be evaluated. Note that this is an
 *                            expression of CQL, not the text of a library with
 *                            definition statements.
 * @param parameters          Any input parameters for the expression.
 *                            {@link Parameters} Parameters defined in this
 *                            input will be made available by name to the CQL
 *                            expression. Parameter types are mapped to CQL as
 *                            specified in the Using CQL section of the CPG
 *                            Implementation guide. If a parameter appears more
 *                            than once in the input Parameters resource, it is
 *                            represented with a List in the input CQL. If a
 *                            parameter has parts, it is represented as a Tuple
 *                            in the input CQL.
 * @param library             A library to be included. The {@link Library}
 *                            library is resolved by url and made available by
 *                            name within the expression to be evaluated.
 * @param useServerData       Whether to use data from the server performing the
 *                            evaluation. If this parameter is true (the
 *                            default), then the operation will use data first
 *                            from any bundles provided as parameters (through
 *                            the data and prefetch parameters), second data
 *                            from the server performing the operation, and
 *                            third, data from the dataEndpoint parameter (if
 *                            provided). If this parameter is false, the
 *                            operation will use data first from the bundles
 *                            provided in the data or prefetch parameters, and
 *                            second from the dataEndpoint parameter (if
 *                            provided).
 * @param data                Data to be made available to the library
 *                            evaluation. This parameter is exclusive with the
 *                            prefetchData parameter (i.e. either provide all
 *                            data as a single bundle, or provide data using
 *                            multiple bundles with prefetch descriptions).
 * @param prefetchData        ***Not Yet Implemented***
 * @param dataEndpoint        An {@link Endpoint} endpoint to use to access data
 *                            referenced by retrieve operations in the library.
 *                            If provided, this endpoint is used after the data
 *                            or prefetchData bundles, and the server, if the
 *                            useServerData parameter is true.
 * @param contentEndpoint     An {@link Endpoint} endpoint to use to access
 *                            content (i.e. libraries) referenced by the
 *                            library. If no content endpoint is supplied, the
 *                            evaluation will attempt to retrieve content from
 *                            the server on which the operation is being
 *                            performed.
 * @param terminologyEndpoint An {@link Endpoint} endpoint to use to access
 *                            terminology (i.e. valuesets, codesystems, and
 *                            membership testing) referenced by the library. If
 *                            no terminology endpoint is supplied, the
 *                            evaluation will attempt to use the server on which
 *                            the operation is being performed as the
 *                            terminology server.
 * @return The result of evaluating the given expression, returned as a FHIR
 *         type, either a {@link Resource} resource, or a FHIR-defined type
 *         corresponding to the CQL return type, as defined in the Using CQL
 *         section of the CPG Implementation guide. If the result is a List of
 *         resources, the result will be a {@link Bundle} Bundle . If the result
 *         is a CQL system-defined or FHIR-defined type, the result is returned
 *         as a {@link Parameters} Parameters resource
 */
@Operation(name = "$cql")
@Description(shortDefinition = "$cql", value = "Evaluates a CQL expression and returns the results as a Parameters resource. Defined: http://build.fhir.org/ig/HL7/cqf-recommendations/OperationDefinition-cpg-cql.html", example = "$cql?expression=5*5")
public Parameters evaluate(RequestDetails theRequestDetails, @OperationParam(name = "subject", max = 1) String subject, @OperationParam(name = "expression", min = 1, max = 1) String expression, @OperationParam(name = "parameters", max = 1) Parameters parameters, @OperationParam(name = "library") List<Parameters> library, @OperationParam(name = "useServerData", max = 1) BooleanType useServerData, @OperationParam(name = "data", max = 1) Bundle data, @OperationParam(name = "prefetchData") List<Parameters> prefetchData, @OperationParam(name = "dataEndpoint", max = 1) Endpoint dataEndpoint, @OperationParam(name = "contentEndpoint", max = 1) Endpoint contentEndpoint, @OperationParam(name = "terminologyEndpoint", max = 1) Endpoint terminologyEndpoint) {
    if (prefetchData != null) {
        throw new NotImplementedException("prefetchData is not yet supported.");
    }
    if (useServerData == null) {
        useServerData = new BooleanType(true);
    }
    List<LibraryParameter> libraryParameters = new ArrayList<>();
    if (library != null) {
        for (Parameters libraryParameter : library) {
            String url = null;
            String name = null;
            for (ParametersParameterComponent param : libraryParameter.getParameter()) {
                switch(param.getName()) {
                    case "url":
                        url = ((StringType) param.getValue()).asStringValue();
                        break;
                    case "name":
                        name = ((StringType) param.getValue()).asStringValue();
                        break;
                    default:
                        throw new IllegalArgumentException("Only url and name parts are allowed for Parameter: library");
                }
            }
            if (url == null) {
                throw new IllegalArgumentException("If library parameter must provide a url parameter part.");
            }
            libraryParameters.add(new LibraryParameter().withUrl(url).withName(name));
        }
    // Remove LocalLibrary from cache first...
    }
    VersionedIdentifier localLibraryIdentifier = new VersionedIdentifier().withId("LocalLibrary").withVersion("1.0.0");
    globalLibraryCache.remove(localLibraryIdentifier);
    CqlEngine engine = setupEngine(localLibraryIdentifier, expression, libraryParameters, subject, parameters, contentEndpoint, dataEndpoint, terminologyEndpoint, data, useServerData.booleanValue(), theRequestDetails);
    Map<String, Object> resolvedParameters = new HashMap<String, Object>();
    if (parameters != null) {
        for (Parameters.ParametersParameterComponent pc : parameters.getParameter()) {
            resolvedParameters.put(pc.getName(), pc.getValue());
        }
    }
    String contextType = subject != null ? subject.substring(0, subject.lastIndexOf("/") - 1) : null;
    String subjectId = subject != null ? subject.substring(0, subject.lastIndexOf("/") - 1) : null;
    EvaluationResult evalResult = engine.evaluate(localLibraryIdentifier, null, Pair.of(contextType != null ? contextType : "Unspecified", subjectId == null ? "null" : subject), resolvedParameters, this.getDebugMap());
    if (evalResult != null && evalResult.expressionResults != null) {
        if (evalResult.expressionResults.size() > 1) {
            logger.debug("Evaluation resulted in more than one expression result.  ");
        }
        Parameters result = new Parameters();
        resolveResult(theRequestDetails, evalResult, result);
        return result;
    }
    return null;
}
Also used : Parameters(org.hl7.fhir.dstu3.model.Parameters) HashMap(java.util.HashMap) NotImplementedException(org.apache.commons.lang3.NotImplementedException) BooleanType(org.hl7.fhir.dstu3.model.BooleanType) ArrayList(java.util.ArrayList) CqlEngine(org.opencds.cqf.cql.engine.execution.CqlEngine) EvaluationResult(org.opencds.cqf.cql.engine.execution.EvaluationResult) VersionedIdentifier(org.cqframework.cql.elm.execution.VersionedIdentifier) ParametersParameterComponent(org.hl7.fhir.dstu3.model.Parameters.ParametersParameterComponent) ParametersParameterComponent(org.hl7.fhir.dstu3.model.Parameters.ParametersParameterComponent) Description(ca.uhn.fhir.model.api.annotation.Description) Operation(ca.uhn.fhir.rest.annotation.Operation)

Example 3 with CqlEngine

use of org.opencds.cqf.cql.engine.execution.CqlEngine in project cqf-ruler by DBCG.

the class CqlExecutionProvider method evaluate.

/**
 * Evaluates a CQL expression and returns the results as a Parameters resource.
 *
 * @param theRequestDetails   the {@link RequestDetails RequestDetails}
 * @param subject             Subject for which the expression will be
 *                            evaluated. This corresponds to the context in
 *                            which the expression will be evaluated and is
 *                            represented as a relative FHIR id (e.g.
 *                            Patient/123), which establishes both the context
 *                            and context value for the evaluation
 * @param expression          Expression to be evaluated. Note that this is an
 *                            expression of CQL, not the text of a library with
 *                            definition statements.
 * @param parameters          Any input parameters for the expression.
 *                            {@link Parameters} Parameters defined in this
 *                            input will be made available by name to the CQL
 *                            expression. Parameter types are mapped to CQL as
 *                            specified in the Using CQL section of the CPG
 *                            Implementation guide. If a parameter appears more
 *                            than once in the input Parameters resource, it is
 *                            represented with a List in the input CQL. If a
 *                            parameter has parts, it is represented as a Tuple
 *                            in the input CQL.
 * @param library             A library to be included. The {@link Library}
 *                            library is resolved by url and made available by
 *                            name within the expression to be evaluated.
 * @param useServerData       Whether to use data from the server performing the
 *                            evaluation. If this parameter is true (the
 *                            default), then the operation will use data first
 *                            from any bundles provided as parameters (through
 *                            the data and prefetch parameters), second data
 *                            from the server performing the operation, and
 *                            third, data from the dataEndpoint parameter (if
 *                            provided). If this parameter is false, the
 *                            operation will use data first from the bundles
 *                            provided in the data or prefetch parameters, and
 *                            second from the dataEndpoint parameter (if
 *                            provided).
 * @param data                Data to be made available to the library
 *                            evaluation. This parameter is exclusive with the
 *                            prefetchData parameter (i.e. either provide all
 *                            data as a single bundle, or provide data using
 *                            multiple bundles with prefetch descriptions).
 * @param prefetchData        ***Not Yet Implemented***
 * @param dataEndpoint        An {@link Endpoint} endpoint to use to access data
 *                            referenced by retrieve operations in the library.
 *                            If provided, this endpoint is used after the data
 *                            or prefetchData bundles, and the server, if the
 *                            useServerData parameter is true.
 * @param contentEndpoint     An {@link Endpoint} endpoint to use to access
 *                            content (i.e. libraries) referenced by the
 *                            library. If no content endpoint is supplied, the
 *                            evaluation will attempt to retrieve content from
 *                            the server on which the operation is being
 *                            performed.
 * @param terminologyEndpoint An {@link Endpoint} endpoint to use to access
 *                            terminology (i.e. valuesets, codesystems, and
 *                            membership testing) referenced by the library. If
 *                            no terminology endpoint is supplied, the
 *                            evaluation will attempt to use the server on which
 *                            the operation is being performed as the
 *                            terminology server.
 * @return The result of evaluating the given expression, returned as a FHIR
 *         type, either a {@link Resource} resource, or a FHIR-defined type
 *         corresponding to the CQL return type, as defined in the Using CQL
 *         section of the CPG Implementation guide. If the result is a List of
 *         resources, the result will be a {@link Bundle} Bundle . If the result
 *         is a CQL system-defined or FHIR-defined type, the result is returned
 *         as a {@link Parameters} Parameters resource
 */
@Operation(name = "$cql")
@Description(shortDefinition = "$cql", value = "Evaluates a CQL expression and returns the results as a Parameters resource. Defined: http://build.fhir.org/ig/HL7/cqf-recommendations/OperationDefinition-cpg-cql.html", example = "$cql?expression=5*5")
public Parameters evaluate(RequestDetails theRequestDetails, @OperationParam(name = "subject", max = 1) String subject, @OperationParam(name = "expression", min = 1, max = 1) String expression, @OperationParam(name = "parameters", max = 1) Parameters parameters, @OperationParam(name = "library") List<Parameters> library, @OperationParam(name = "useServerData", max = 1) BooleanType useServerData, @OperationParam(name = "data", max = 1) Bundle data, @OperationParam(name = "prefetchData") List<Parameters> prefetchData, @OperationParam(name = "dataEndpoint", max = 1) Endpoint dataEndpoint, @OperationParam(name = "contentEndpoint", max = 1) Endpoint contentEndpoint, @OperationParam(name = "terminologyEndpoint", max = 1) Endpoint terminologyEndpoint) {
    if (prefetchData != null) {
        throw new NotImplementedException("prefetchData is not yet supported.");
    }
    if (useServerData == null) {
        useServerData = new BooleanType(true);
    }
    List<LibraryParameter> libraryParameters = new ArrayList<>();
    if (library != null) {
        for (Parameters libraryParameter : library) {
            CanonicalType url = null;
            String name = null;
            for (ParametersParameterComponent param : libraryParameter.getParameter()) {
                switch(param.getName()) {
                    case "url":
                        url = ((CanonicalType) param.getValue());
                        break;
                    case "name":
                        name = ((StringType) param.getValue()).asStringValue();
                        break;
                    default:
                        throw new IllegalArgumentException("Only url and name parts are allowed for Parameter: library");
                }
            }
            if (url == null) {
                throw new IllegalArgumentException("If library parameter must provide a url parameter part.");
            }
            libraryParameters.add(new LibraryParameter().withUrl(url).withName(name));
        }
    // Remove LocalLibrary from cache first...
    }
    VersionedIdentifier localLibraryIdentifier = new VersionedIdentifier().withId("LocalLibrary").withVersion("1.0.0");
    globalLibraryCache.remove(localLibraryIdentifier);
    CqlEngine engine = setupEngine(localLibraryIdentifier, expression, libraryParameters, subject, parameters, contentEndpoint, dataEndpoint, terminologyEndpoint, data, useServerData.booleanValue(), theRequestDetails);
    Map<String, Object> resolvedParameters = new HashMap<>();
    if (parameters != null) {
        for (Parameters.ParametersParameterComponent pc : parameters.getParameter()) {
            resolvedParameters.put(pc.getName(), pc.getValue());
        }
    }
    String contextType = subject != null ? subject.substring(0, subject.lastIndexOf("/") - 1) : null;
    String subjectId = subject != null ? subject.substring(0, subject.lastIndexOf("/") - 1) : null;
    EvaluationResult evalResult = engine.evaluate(localLibraryIdentifier, null, Pair.of(contextType != null ? contextType : "Unspecified", subjectId == null ? "null" : subject), resolvedParameters, this.getDebugMap());
    if (evalResult != null && evalResult.expressionResults != null) {
        if (evalResult.expressionResults.size() > 1) {
            logger.debug("Evaluation resulted in more than one expression result.  ");
        }
        Parameters result = new Parameters();
        resolveResult(theRequestDetails, evalResult, result);
        return result;
    }
    return null;
}
Also used : Parameters(org.hl7.fhir.r4.model.Parameters) HashMap(java.util.HashMap) NotImplementedException(org.apache.commons.lang3.NotImplementedException) BooleanType(org.hl7.fhir.r4.model.BooleanType) ArrayList(java.util.ArrayList) CqlEngine(org.opencds.cqf.cql.engine.execution.CqlEngine) CanonicalType(org.hl7.fhir.r4.model.CanonicalType) EvaluationResult(org.opencds.cqf.cql.engine.execution.EvaluationResult) VersionedIdentifier(org.cqframework.cql.elm.execution.VersionedIdentifier) ParametersParameterComponent(org.hl7.fhir.r4.model.Parameters.ParametersParameterComponent) ParametersParameterComponent(org.hl7.fhir.r4.model.Parameters.ParametersParameterComponent) Description(ca.uhn.fhir.model.api.annotation.Description) Operation(ca.uhn.fhir.rest.annotation.Operation)

Example 4 with CqlEngine

use of org.opencds.cqf.cql.engine.execution.CqlEngine in project cqf-ruler by DBCG.

the class LibraryEvaluationProvider method evaluate.

@SuppressWarnings({ "unchecked" })
@Operation(name = "$evaluate", idempotent = true, type = Library.class)
public Bundle evaluate(@IdParam IdType theId, @OperationParam(name = "patientId") String patientId, @OperationParam(name = "periodStart") String periodStart, @OperationParam(name = "periodEnd") String periodEnd, @OperationParam(name = "productLine") String productLine, @OperationParam(name = "terminologyEndpoint") Endpoint terminologyEndpoint, @OperationParam(name = "dataEndpoint") Endpoint dataEndpoint, @OperationParam(name = "context") String contextParam, @OperationParam(name = "executionResults") String executionResults, @OperationParam(name = "parameters") Parameters parameters, @OperationParam(name = "additionalData") Bundle additionalData, RequestDetails theRequestDetails) {
    log.info("Library evaluation started..");
    if (patientId == null && contextParam != null && contextParam.equals("Patient")) {
        log.error("Patient id null");
        throw new IllegalArgumentException("Must specify a patientId when executing in Patient context.");
    }
    Bundle libraryBundle = new Bundle();
    Library theResource = null;
    if (additionalData != null) {
        for (Bundle.BundleEntryComponent entry : additionalData.getEntry()) {
            if (entry.getResource().fhirType().equals("Library")) {
                libraryBundle.addEntry(entry);
                if (entry.getResource().getIdElement().equals(theId)) {
                    theResource = (Library) entry.getResource();
                }
            }
        }
    }
    if (theResource == null) {
        theResource = read(theId, theRequestDetails);
    }
    VersionedIdentifier libraryIdentifier = new VersionedIdentifier().withId(theResource.getName()).withVersion(theResource.getVersion());
    TerminologyProvider terminologyProvider;
    if (terminologyEndpoint != null) {
        IGenericClient client = Clients.forEndpoint(getFhirContext(), terminologyEndpoint);
        terminologyProvider = new R4FhirTerminologyProvider(client);
    } else {
        terminologyProvider = myJpaTerminologyProviderFactory.create(new SystemRequestDetails());
    }
    DataProvider dataProvider;
    if (dataEndpoint != null) {
        List<RetrieveProvider> retrieveProviderList = new ArrayList<>();
        IGenericClient client = Clients.forEndpoint(dataEndpoint);
        RestFhirRetrieveProvider retriever = new RestFhirRetrieveProvider(new SearchParameterResolver(getFhirContext()), client);
        retriever.setTerminologyProvider(terminologyProvider);
        if (terminologyEndpoint == null || (terminologyEndpoint != null && !terminologyEndpoint.getAddress().equals(dataEndpoint.getAddress()))) {
            retriever.setExpandValueSets(true);
        }
        retrieveProviderList.add(retriever);
        if (additionalData != null) {
            BundleRetrieveProvider bundleProvider = new BundleRetrieveProvider(getFhirContext(), additionalData);
            bundleProvider.setTerminologyProvider(terminologyProvider);
            retrieveProviderList.add(bundleProvider);
            PriorityRetrieveProvider priorityProvider = new PriorityRetrieveProvider(retrieveProviderList);
            dataProvider = new CompositeDataProvider(myModelResolver, priorityProvider);
        } else {
            dataProvider = new CompositeDataProvider(myModelResolver, retriever);
        }
    } else {
        List<RetrieveProvider> retrieveProviderList = new ArrayList<>();
        JpaFhirRetrieveProvider retriever = new JpaFhirRetrieveProvider(getDaoRegistry(), new SearchParameterResolver(getFhirContext()));
        retriever.setTerminologyProvider(terminologyProvider);
        // Assume it's a different server, therefore need to expand.
        if (terminologyEndpoint != null) {
            retriever.setExpandValueSets(true);
        }
        retrieveProviderList.add(retriever);
        if (additionalData != null) {
            BundleRetrieveProvider bundleProvider = new BundleRetrieveProvider(getFhirContext(), additionalData);
            bundleProvider.setTerminologyProvider(terminologyProvider);
            retrieveProviderList.add(bundleProvider);
            PriorityRetrieveProvider priorityProvider = new PriorityRetrieveProvider(retrieveProviderList);
            dataProvider = new CompositeDataProvider(myModelResolver, priorityProvider);
        } else {
            dataProvider = new CompositeDataProvider(myModelResolver, retriever);
        }
    }
    LibraryContentProvider bundleLibraryProvider = new BundleFhirLibraryContentProvider(this.getFhirContext(), libraryBundle, adapterFactory, libraryVersionSelector);
    LibraryContentProvider jpaLibraryContentProvider = this.myJpaLibraryContentProviderFactory.create(theRequestDetails);
    List<LibraryContentProvider> sourceProviders = new ArrayList<LibraryContentProvider>(Arrays.asList(bundleLibraryProvider, jpaLibraryContentProvider));
    LibraryLoader libraryLoader = this.myLibraryLoaderFactory.create(sourceProviders);
    CqlEngine engine = new CqlEngine(libraryLoader, Collections.singletonMap("http://hl7.org/fhir", dataProvider), terminologyProvider);
    Map<String, Object> resolvedParameters = new HashMap<>();
    if (parameters != null) {
        for (Parameters.ParametersParameterComponent pc : parameters.getParameter()) {
            resolvedParameters.put(pc.getName(), pc.getValue());
        }
    }
    if (periodStart != null && periodEnd != null) {
        // resolve the measurement period
        Interval measurementPeriod = new Interval(Operations.resolveRequestDate(periodStart, true), true, Operations.resolveRequestDate(periodEnd, false), true);
        resolvedParameters.put("Measurement Period", new Interval(DateTime.fromJavaDate((Date) measurementPeriod.getStart()), true, DateTime.fromJavaDate((Date) measurementPeriod.getEnd()), true));
    }
    if (productLine != null) {
        resolvedParameters.put("Product Line", productLine);
    }
    EvaluationResult evalResult = engine.evaluate(libraryIdentifier, null, Pair.of(contextParam != null ? contextParam : "Unspecified", patientId == null ? "null" : patientId), resolvedParameters, this.getDebugMap());
    List<Resource> results = new ArrayList<>();
    FhirMeasureBundler bundler = new FhirMeasureBundler();
    if (evalResult != null && evalResult.expressionResults != null) {
        for (Map.Entry<String, Object> def : evalResult.expressionResults.entrySet()) {
            Parameters result = new Parameters();
            try {
                result.setId(def.getKey());
                Object res = def.getValue();
                if (res == null) {
                    result.addParameter().setName("value").setValue(new StringType("null"));
                } else if (res instanceof List<?>) {
                    if (((List<?>) res).size() > 0 && ((List<?>) res).get(0) instanceof Resource) {
                        if (executionResults != null && executionResults.equals("Summary")) {
                            result.addParameter().setName("value").setValue(new StringType(((Resource) ((List<?>) res).get(0)).getIdElement().getResourceType() + "/" + ((Resource) ((List<?>) res).get(0)).getIdElement().getIdPart()));
                        } else {
                            result.addParameter().setName("value").setResource(bundler.bundle((Iterable<Resource>) res, theRequestDetails.getFhirServerBase()));
                        }
                    } else {
                        result.addParameter().setName("value").setValue(new StringType(res.toString()));
                    }
                } else if (res instanceof Iterable) {
                    result.addParameter().setName("value").setResource(bundler.bundle((Iterable<Resource>) res, theRequestDetails.getFhirServerBase()));
                } else if (res instanceof Resource) {
                    if (executionResults != null && executionResults.equals("Summary")) {
                        result.addParameter().setName("value").setValue(new StringType(((Resource) res).getIdElement().getResourceType() + "/" + ((Resource) res).getIdElement().getIdPart()));
                    } else {
                        result.addParameter().setName("value").setResource((Resource) res);
                    }
                } else if (res instanceof Type) {
                    result.addParameter().setName("value").setValue((Type) res);
                } else {
                    result.addParameter().setName("value").setValue(new StringType(res.toString()));
                }
                result.addParameter().setName("resultType").setValue(new StringType(resolveType(res)));
            } catch (RuntimeException re) {
                re.printStackTrace();
                String message = re.getMessage() != null ? re.getMessage() : re.getClass().getName();
                result.addParameter().setName("error").setValue(new StringType(message));
            }
            results.add(result);
        }
    }
    return bundler.bundle(results, theRequestDetails.getFhirServerBase());
}
Also used : HashMap(java.util.HashMap) StringType(org.hl7.fhir.r4.model.StringType) IGenericClient(ca.uhn.fhir.rest.client.api.IGenericClient) ArrayList(java.util.ArrayList) LibraryLoader(org.opencds.cqf.cql.engine.execution.LibraryLoader) CompositeDataProvider(org.opencds.cqf.cql.engine.data.CompositeDataProvider) DataProvider(org.opencds.cqf.cql.engine.data.DataProvider) VersionedIdentifier(org.cqframework.cql.elm.execution.VersionedIdentifier) BundleRetrieveProvider(org.opencds.cqf.cql.evaluator.engine.retrieve.BundleRetrieveProvider) RestFhirRetrieveProvider(org.opencds.cqf.cql.engine.fhir.retrieve.RestFhirRetrieveProvider) SystemRequestDetails(ca.uhn.fhir.jpa.partition.SystemRequestDetails) PriorityRetrieveProvider(org.opencds.cqf.cql.evaluator.engine.retrieve.PriorityRetrieveProvider) List(java.util.List) ArrayList(java.util.ArrayList) R4FhirTerminologyProvider(org.opencds.cqf.cql.engine.fhir.terminology.R4FhirTerminologyProvider) FhirMeasureBundler(org.opencds.cqf.ruler.cpg.r4.util.FhirMeasureBundler) JpaFhirRetrieveProvider(org.opencds.cqf.ruler.cql.JpaFhirRetrieveProvider) LibraryContentProvider(org.opencds.cqf.cql.evaluator.cql2elm.content.LibraryContentProvider) BundleFhirLibraryContentProvider(org.opencds.cqf.cql.evaluator.cql2elm.content.fhir.BundleFhirLibraryContentProvider) Parameters(org.hl7.fhir.r4.model.Parameters) BundleFhirLibraryContentProvider(org.opencds.cqf.cql.evaluator.cql2elm.content.fhir.BundleFhirLibraryContentProvider) Bundle(org.hl7.fhir.r4.model.Bundle) Resource(org.hl7.fhir.r4.model.Resource) CqlEngine(org.opencds.cqf.cql.engine.execution.CqlEngine) EvaluationResult(org.opencds.cqf.cql.engine.execution.EvaluationResult) StringType(org.hl7.fhir.r4.model.StringType) Type(org.hl7.fhir.r4.model.Type) IdType(org.hl7.fhir.r4.model.IdType) SearchParameterResolver(org.opencds.cqf.cql.engine.fhir.searchparam.SearchParameterResolver) TerminologyProvider(org.opencds.cqf.cql.engine.terminology.TerminologyProvider) R4FhirTerminologyProvider(org.opencds.cqf.cql.engine.fhir.terminology.R4FhirTerminologyProvider) CompositeDataProvider(org.opencds.cqf.cql.engine.data.CompositeDataProvider) Library(org.hl7.fhir.r4.model.Library) RestFhirRetrieveProvider(org.opencds.cqf.cql.engine.fhir.retrieve.RestFhirRetrieveProvider) JpaFhirRetrieveProvider(org.opencds.cqf.ruler.cql.JpaFhirRetrieveProvider) RetrieveProvider(org.opencds.cqf.cql.engine.retrieve.RetrieveProvider) BundleRetrieveProvider(org.opencds.cqf.cql.evaluator.engine.retrieve.BundleRetrieveProvider) PriorityRetrieveProvider(org.opencds.cqf.cql.evaluator.engine.retrieve.PriorityRetrieveProvider) Map(java.util.Map) DebugMap(org.opencds.cqf.cql.engine.debug.DebugMap) HashMap(java.util.HashMap) Interval(org.opencds.cqf.cql.engine.runtime.Interval) Operation(ca.uhn.fhir.rest.annotation.Operation)

Example 5 with CqlEngine

use of org.opencds.cqf.cql.engine.execution.CqlEngine in project quality-measure-and-cohort-service by Alvearie.

the class CQLParameterTests method runTest.

public void runTest(String type, Object expectedOutput, String defaultValue) throws Exception {
    String cql = "library \"Test\" version '1.0.0'\n";
    cql += "codesystem \"DUMMY\": 'urn:mysystem'\n";
    cql += "parameter Input " + type;
    if (defaultValue != null) {
        cql += " default ";
        cql += defaultValue;
    }
    cql += "\n";
    cql += "define Output:\n\tInput\n";
    Map<String, Object> parameters = null;
    if (defaultValue == null) {
        parameters = new HashMap<>();
        parameters.put("Input", expectedOutput);
    }
    TerminologyProvider tp = new TerminologyProvider() {

        @Override
        public boolean in(Code code, ValueSetInfo valueSet) {
            return false;
        }

        @Override
        public Iterable<Code> expand(ValueSetInfo valueSet) {
            return null;
        }

        @Override
        public Code lookup(Code code, CodeSystemInfo codeSystem) {
            return code;
        }
    };
    LibraryLoader ll = new InMemoryLibraryLoader(Arrays.asList(toLibrary(cql)));
    CqlEngine engine = new CqlEngine(ll, Collections.<String, DataProvider>emptyMap(), tp);
    EvaluationResult result = engine.evaluate("Test", parameters);
    assertEquals(1, result.expressionResults.size());
    Object actual = result.forExpression("Output");
    assertEquals(expectedOutput.toString(), actual.toString());
}
Also used : CodeSystemInfo(org.opencds.cqf.cql.engine.terminology.CodeSystemInfo) TerminologyProvider(org.opencds.cqf.cql.engine.terminology.TerminologyProvider) CqlEngine(org.opencds.cqf.cql.engine.execution.CqlEngine) Code(org.opencds.cqf.cql.engine.runtime.Code) ValueSetInfo(org.opencds.cqf.cql.engine.terminology.ValueSetInfo) LibraryLoader(org.opencds.cqf.cql.engine.execution.LibraryLoader) InMemoryLibraryLoader(org.opencds.cqf.cql.engine.execution.InMemoryLibraryLoader) InMemoryLibraryLoader(org.opencds.cqf.cql.engine.execution.InMemoryLibraryLoader) EvaluationResult(org.opencds.cqf.cql.engine.execution.EvaluationResult)

Aggregations

CqlEngine (org.opencds.cqf.cql.engine.execution.CqlEngine)6 ArrayList (java.util.ArrayList)5 EvaluationResult (org.opencds.cqf.cql.engine.execution.EvaluationResult)4 TerminologyProvider (org.opencds.cqf.cql.engine.terminology.TerminologyProvider)4 Operation (ca.uhn.fhir.rest.annotation.Operation)3 IGenericClient (ca.uhn.fhir.rest.client.api.IGenericClient)3 HashMap (java.util.HashMap)3 VersionedIdentifier (org.cqframework.cql.elm.execution.VersionedIdentifier)3 CompositeDataProvider (org.opencds.cqf.cql.engine.data.CompositeDataProvider)3 DataProvider (org.opencds.cqf.cql.engine.data.DataProvider)3 RestFhirRetrieveProvider (org.opencds.cqf.cql.engine.fhir.retrieve.RestFhirRetrieveProvider)3 SearchParameterResolver (org.opencds.cqf.cql.engine.fhir.searchparam.SearchParameterResolver)3 RetrieveProvider (org.opencds.cqf.cql.engine.retrieve.RetrieveProvider)3 BundleRetrieveProvider (org.opencds.cqf.cql.evaluator.engine.retrieve.BundleRetrieveProvider)3 PriorityRetrieveProvider (org.opencds.cqf.cql.evaluator.engine.retrieve.PriorityRetrieveProvider)3 JpaFhirRetrieveProvider (org.opencds.cqf.ruler.cql.JpaFhirRetrieveProvider)3 Description (ca.uhn.fhir.model.api.annotation.Description)2 NotImplementedException (org.apache.commons.lang3.NotImplementedException)2 Parameters (org.hl7.fhir.r4.model.Parameters)2 LibraryLoader (org.opencds.cqf.cql.engine.execution.LibraryLoader)2