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);
}
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);
}
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);
}
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);
}
Aggregations