Search in sources :

Example 6 with DataProvider

use of org.opencds.cqf.cql.engine.data.DataProvider 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 R4FhirTerminologyProvider(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) R4FhirTerminologyProvider(org.opencds.cqf.cql.engine.fhir.terminology.R4FhirTerminologyProvider) RestFhirRetrieveProvider(org.opencds.cqf.cql.engine.fhir.retrieve.RestFhirRetrieveProvider) PriorityRetrieveProvider(org.opencds.cqf.cql.evaluator.engine.retrieve.PriorityRetrieveProvider) CompositeDataProvider(org.opencds.cqf.cql.engine.data.CompositeDataProvider) R4FhirTerminologyProvider(org.opencds.cqf.cql.engine.fhir.terminology.R4FhirTerminologyProvider) 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 7 with DataProvider

use of org.opencds.cqf.cql.engine.data.DataProvider 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 8 with DataProvider

use of org.opencds.cqf.cql.engine.data.DataProvider in project cqf-ruler by DBCG.

the class ExpressionEvaluation method setupContext.

private Context setupContext(DomainResource instance, String patientId, LibraryLoader libraryLoader, RequestDetails theRequest) {
    // 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
    Context context = new Context(libraryLoader.load(new VersionedIdentifier().withId("LocalLibrary")));
    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;
}
Also used : FhirContext(ca.uhn.fhir.context.FhirContext) Context(org.opencds.cqf.cql.engine.execution.Context) DataProvider(org.opencds.cqf.cql.engine.data.DataProvider) VersionedIdentifier(org.cqframework.cql.elm.execution.VersionedIdentifier) TerminologyProvider(org.opencds.cqf.cql.engine.terminology.TerminologyProvider)

Example 9 with DataProvider

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

the class MeasureEvaluationSeeder method create.

public IMeasureEvaluationSeed create(Measure measure, String periodStart, String periodEnd, String productLine, Map<String, Parameter> parameters) {
    // Gather the primary library and all of its dependencies
    List<Library> fhirLibraries = libraryDependencyGatherer.gatherForMeasure(measure);
    if (CollectionUtils.isEmpty(fhirLibraries)) {
        throw new IllegalArgumentException(String.format("No libraries were able to be loaded for %s", measure.getId()));
    }
    // the "primary" library is always the first library loaded for the measure
    Library primaryFhirLibrary = fhirLibraries.get(0);
    VersionedIdentifier libraryIdentifier = new VersionedIdentifier().withId(primaryFhirLibrary.getName()).withVersion(primaryFhirLibrary.getVersion());
    LibraryLoader libraryLoader = new R4TranslatingLibraryLoader(libraryResolver, new CqlToElmTranslator());
    org.cqframework.cql.elm.execution.Library primaryLibrary = libraryLoader.load(libraryIdentifier);
    List<Triple<String, String, String>> usingDefs = UsingHelper.getUsingUrlAndVersion(primaryLibrary.getUsings());
    if (usingDefs.size() > 1) {
        throw new IllegalArgumentException("Evaluation of Measure using multiple Models is not supported at this time.");
    }
    // Per the above condition, we should only have one model per measure
    String lastModelUri = usingDefs.get(usingDefs.size() - 1).getRight();
    DataProvider dataProvider = dataProviders.get(lastModelUri);
    Context context = createContext(primaryLibrary, lastModelUri, dataProvider, productLine, libraryLoader);
    // fhir path: Measure.extension[measureParameter][].valueParameterDefinition.extension[defaultValue]
    measure.getExtension().stream().filter(MeasureEvaluationSeeder::isMeasureParameter).map(parameter -> dataProvider.resolvePath(parameter, "valueParameterDefinition")).map(ParameterDefinition.class::cast).forEach(parameterDefinition -> setDefaultValue(context, parameterDefinition));
    if (parameters != null) {
        parameters.entrySet().stream().forEach(e -> context.setParameter(null, e.getKey(), e.getValue().toCqlType()));
    }
    // Set measurement period last to make sure we respect periodStart
    // and periodEnd date boundaries for an execution.
    Interval measurementPeriod = createMeasurePeriod(periodStart, periodEnd);
    context.setParameter(null, MEASUREMENT_PERIOD, measurementPeriod);
    return new CustomMeasureEvaluationSeed(measure, context, measurementPeriod, dataProvider);
}
Also used : CDMContext(com.ibm.cohort.engine.cqfruler.CDMContext) Context(org.opencds.cqf.cql.engine.execution.Context) CqlToElmTranslator(com.ibm.cohort.cql.translation.CqlToElmTranslator) R4TranslatingLibraryLoader(com.ibm.cohort.cql.hapi.R4TranslatingLibraryLoader) LibraryLoader(org.opencds.cqf.cql.engine.execution.LibraryLoader) Triple(org.apache.commons.lang3.tuple.Triple) CqlDataProvider(com.ibm.cohort.cql.data.CqlDataProvider) DataProvider(org.opencds.cqf.cql.engine.data.DataProvider) VersionedIdentifier(org.cqframework.cql.elm.execution.VersionedIdentifier) Library(org.hl7.fhir.r4.model.Library) R4TranslatingLibraryLoader(com.ibm.cohort.cql.hapi.R4TranslatingLibraryLoader) Interval(org.opencds.cqf.cql.engine.runtime.Interval)

Aggregations

DataProvider (org.opencds.cqf.cql.engine.data.DataProvider)9 TerminologyProvider (org.opencds.cqf.cql.engine.terminology.TerminologyProvider)7 ArrayList (java.util.ArrayList)4 VersionedIdentifier (org.cqframework.cql.elm.execution.VersionedIdentifier)4 LibraryContentProvider (org.opencds.cqf.cql.evaluator.cql2elm.content.LibraryContentProvider)4 Operation (ca.uhn.fhir.rest.annotation.Operation)3 IGenericClient (ca.uhn.fhir.rest.client.api.IGenericClient)3 CompositeDataProvider (org.opencds.cqf.cql.engine.data.CompositeDataProvider)3 Context (org.opencds.cqf.cql.engine.execution.Context)3 CqlEngine (org.opencds.cqf.cql.engine.execution.CqlEngine)3 LibraryLoader (org.opencds.cqf.cql.engine.execution.LibraryLoader)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 CqlDataProvider (com.ibm.cohort.cql.data.CqlDataProvider)2 Library (org.hl7.fhir.r4.model.Library)2