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