Search in sources :

Example 1 with InMemoryLibraryContentProvider

use of org.opencds.cqf.cql.evaluator.cql2elm.content.InMemoryLibraryContentProvider in project cqf-ruler by DBCG.

the class CqlExecutionProvider method setupEngine.

private CqlEngine setupEngine(VersionedIdentifier localLibraryIdentifier, String expression, List<LibraryParameter> library, String subject, Parameters parameters, Endpoint contentEndpoint, Endpoint dataEndpoint, Endpoint terminologyEndpoint, Bundle data, boolean useServerData, RequestDetails theRequestDetails) {
    JpaFhirDal jpaFhirDal = jpaFhirDalFactory.create(theRequestDetails);
    // temporary LibraryLoader to resolve library dependencies when building
    // includes
    List<LibraryContentProvider> libraryProviders = new ArrayList<>();
    libraryProviders.add(jpaLibraryContentProviderFactory.create(theRequestDetails));
    if (contentEndpoint != null) {
        libraryProviders.add(fhirRestLibraryContentProviderFactory.create(contentEndpoint.getAddress(), contentEndpoint.getHeader().stream().map(PrimitiveType::asStringValue).collect(Collectors.toList())));
    }
    LibraryLoader tempLibraryLoader = libraryLoaderFactory.create(new ArrayList<>(libraryProviders));
    String cql = buildCqlLibrary(library, jpaFhirDal, tempLibraryLoader, expression, parameters, theRequestDetails);
    libraryProviders.add(new InMemoryLibraryContentProvider(Arrays.asList(cql)));
    LibraryLoader libraryLoader = libraryLoaderFactory.create(new ArrayList<>(libraryProviders));
    return setupEngine(subject, parameters, dataEndpoint, terminologyEndpoint, data, useServerData, libraryLoader, localLibraryIdentifier, theRequestDetails);
}
Also used : 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) ArrayList(java.util.ArrayList) JpaFhirDal(org.opencds.cqf.ruler.cql.JpaFhirDal) LibraryLoader(org.opencds.cqf.cql.engine.execution.LibraryLoader)

Example 2 with InMemoryLibraryContentProvider

use of org.opencds.cqf.cql.evaluator.cql2elm.content.InMemoryLibraryContentProvider in project cqf-ruler by DBCG.

the class ExpressionEvaluation method setupContext.

private Context setupContext(DomainResource instance, String cql, String patientId, Boolean aliasedExpression, RequestDetails theRequest) {
    JpaFhirDal jpaFhirDal = jpaFhirDalFactory.create(theRequest);
    List<CanonicalType> libraries = getLibraryReferences(instance, theRequest);
    String fhirVersion = this.fhirContext.getVersion().getVersion().getFhirVersionString();
    // Remove LocalLibrary from cache first...
    VersionedIdentifier localLibraryIdentifier = new VersionedIdentifier().withId("LocalLibrary");
    globalLibraryCache.remove(localLibraryIdentifier);
    // 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 = getVersionedIdentifierFromCanonical(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", Canonicals.getIdPart(libraries.get(0))));
            vi.setId(library.getName());
            if (library.getVersion() != null) {
                vi.setVersion(library.getVersion());
            }
        }
        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, theRequest), instance.fhirType(), instance.fhirType(), instance.fhirType(), vi.getId() + ".\"" + cql + "\"");
    } else {
        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, theRequest), instance.fhirType(), instance.fhirType(), instance.fhirType(), cql);
    }
    LibraryLoader libraryLoader = libraryLoaderFactory.create(new ArrayList<LibraryContentProvider>(Arrays.asList(jpaLibraryContentProviderFactory.create(theRequest), new InMemoryLibraryContentProvider(Arrays.asList(source)))));
    // resolve execution context
    return setupContext(instance, patientId, libraryLoader, theRequest);
}
Also used : 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) JpaFhirDal(org.opencds.cqf.ruler.cql.JpaFhirDal) CanonicalType(org.hl7.fhir.r4.model.CanonicalType) LibraryLoader(org.opencds.cqf.cql.engine.execution.LibraryLoader) IdType(org.hl7.fhir.r4.model.IdType) VersionedIdentifier(org.cqframework.cql.elm.execution.VersionedIdentifier) Library(org.hl7.fhir.r4.model.Library)

Example 3 with InMemoryLibraryContentProvider

use of org.opencds.cqf.cql.evaluator.cql2elm.content.InMemoryLibraryContentProvider in project cqf-ruler by DBCG.

the class CqlExecutionProvider method setupEngine.

private CqlEngine setupEngine(VersionedIdentifier localLibraryIdentifier, String expression, List<LibraryParameter> library, String subject, Parameters parameters, Endpoint contentEndpoint, Endpoint dataEndpoint, Endpoint terminologyEndpoint, Bundle data, boolean useServerData, RequestDetails theRequestDetails) {
    JpaFhirDal jpaFhirDal = jpaFhirDalFactory.create(theRequestDetails);
    // temporary LibraryLoader to resolve library dependencies when building
    // includes
    List<LibraryContentProvider> libraryProviders = new ArrayList<>();
    libraryProviders.add(jpaLibraryContentProviderFactory.create(theRequestDetails));
    if (contentEndpoint != null) {
        libraryProviders.add(fhirRestLibraryContentProviderFactory.create(contentEndpoint.getAddress(), contentEndpoint.getHeader().stream().map(PrimitiveType::asStringValue).collect(Collectors.toList())));
    }
    LibraryLoader tempLibraryLoader = libraryLoaderFactory.create(new ArrayList<>(libraryProviders));
    String cql = buildCqlLibrary(library, jpaFhirDal, tempLibraryLoader, expression, parameters, theRequestDetails);
    libraryProviders.add(new InMemoryLibraryContentProvider(Arrays.asList(cql)));
    LibraryLoader libraryLoader = libraryLoaderFactory.create(new ArrayList<>(libraryProviders));
    return setupEngine(subject, parameters, dataEndpoint, terminologyEndpoint, data, useServerData, libraryLoader, localLibraryIdentifier, theRequestDetails);
}
Also used : 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) ArrayList(java.util.ArrayList) JpaFhirDal(org.opencds.cqf.ruler.cql.JpaFhirDal) LibraryLoader(org.opencds.cqf.cql.engine.execution.LibraryLoader)

Example 4 with InMemoryLibraryContentProvider

use of org.opencds.cqf.cql.evaluator.cql2elm.content.InMemoryLibraryContentProvider 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)

Aggregations

LibraryLoader (org.opencds.cqf.cql.engine.execution.LibraryLoader)4 InMemoryLibraryContentProvider (org.opencds.cqf.cql.evaluator.cql2elm.content.InMemoryLibraryContentProvider)4 LibraryContentProvider (org.opencds.cqf.cql.evaluator.cql2elm.content.LibraryContentProvider)4 JpaFhirDal (org.opencds.cqf.ruler.cql.JpaFhirDal)4 ArrayList (java.util.ArrayList)2 VersionedIdentifier (org.cqframework.cql.elm.execution.VersionedIdentifier)2 IdType (org.hl7.fhir.dstu3.model.IdType)1 Library (org.hl7.fhir.dstu3.model.Library)1 Reference (org.hl7.fhir.dstu3.model.Reference)1 CanonicalType (org.hl7.fhir.r4.model.CanonicalType)1 IdType (org.hl7.fhir.r4.model.IdType)1 Library (org.hl7.fhir.r4.model.Library)1 DataProvider (org.opencds.cqf.cql.engine.data.DataProvider)1 Context (org.opencds.cqf.cql.engine.execution.Context)1 TerminologyProvider (org.opencds.cqf.cql.engine.terminology.TerminologyProvider)1