use of org.opencds.cqf.cql.engine.fhir.model.R4FhirModelResolver in project CRD by HL7-DaVinci.
the class CqlExecutionContextBuilder method getExecutionContext.
public static Context getExecutionContext(CqlRule cqlRule, HashMap<String, Resource> cqlParams, String baseUrl) {
ModelManager modelManager = new ModelManager();
LibraryManager libraryManager = new LibraryManager(modelManager);
libraryManager.getLibrarySourceLoader().clearProviders();
Library library = null;
LibraryLoader libraryLoader = null;
if (cqlRule.isPrecompiled()) {
// todo
} else {
libraryManager.getLibrarySourceLoader().registerProvider(cqlRule.getRawCqlLibrarySourceProvider(CQL_VERSION));
libraryLoader = new LocalLibraryLoader(libraryManager);
try {
library = CqlExecution.translate(cqlRule.getRawMainCqlLibrary(CQL_VERSION), libraryManager, modelManager);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
FhirContext fhirContext = FhirContext.forR4();
Context context = new Context(library);
context.registerLibraryLoader(libraryLoader);
context.setExpressionCaching(true);
R4FhirModelResolver modelResolver = new R4FhirModelResolver();
RestFhirRetrieveProvider retrieveProvider = new RestFhirRetrieveProvider(new SearchParameterResolver(fhirContext), fhirContext.newRestfulGenericClient("http://fhirtest.uhn.ca/baseR4"));
CompositeDataProvider provider = new CompositeDataProvider(modelResolver, retrieveProvider);
context.registerDataProvider("http://hl7.org/fhir", provider);
for (Map.Entry<String, org.hl7.fhir.r4.model.Resource> entry : cqlParams.entrySet()) {
context.setParameter(null, entry.getKey(), entry.getValue());
}
context.setParameter(null, "base_url", baseUrl);
return context;
}
use of org.opencds.cqf.cql.engine.fhir.model.R4FhirModelResolver in project cqf-ruler by DBCG.
the class R4HookEvaluator method resolveActions.
private void resolveActions(List<PlanDefinition.PlanDefinitionActionComponent> actions, Context context, String patientId, RequestGroupBuilder requestGroupBuilder, List<RequestGroup.RequestGroupActionComponent> actionComponents, IGenericClient applyClient) {
for (PlanDefinition.PlanDefinitionActionComponent action : actions) {
boolean conditionsMet = true;
for (PlanDefinition.PlanDefinitionActionConditionComponent condition : action.getCondition()) {
if (condition.getKind() == PlanDefinition.ActionConditionKind.APPLICABILITY) {
if (!condition.hasExpression()) {
continue;
}
Object result = context.resolveExpressionRef(condition.getExpression().getExpression()).getExpression().evaluate(context);
if (!(result instanceof Boolean)) {
continue;
}
if (!(Boolean) result) {
conditionsMet = false;
}
}
if (conditionsMet) {
RequestGroupActionBuilder actionBuilder = new RequestGroupActionBuilder();
if (action.hasTitle()) {
actionBuilder.buildTitle(action.getTitle());
}
if (action.hasDescription()) {
actionBuilder.buildDescripition(action.getDescription());
}
// source
if (action.hasDocumentation()) {
RelatedArtifact artifact = action.getDocumentationFirstRep();
RelatedArtifactBuilder artifactBuilder = new RelatedArtifactBuilder();
if (artifact.hasDisplay()) {
artifactBuilder.buildDisplay(artifact.getDisplay());
}
if (artifact.hasUrl()) {
artifactBuilder.buildUrl(artifact.getUrl());
}
if (artifact.hasDocument() && artifact.getDocument().hasUrl()) {
AttachmentBuilder attachmentBuilder = new AttachmentBuilder();
attachmentBuilder.buildUrl(artifact.getDocument().getUrl());
artifactBuilder.buildDocument(attachmentBuilder.build());
}
actionBuilder.buildDocumentation(Collections.singletonList(artifactBuilder.build()));
}
// TODO - uuid
if (action.hasPrefix()) {
actionBuilder.buildPrefix(action.getPrefix());
}
if (action.hasType()) {
actionBuilder.buildType(action.getType());
}
if (action.hasSelectionBehavior()) {
actionBuilder.buildSelectionBehavior(RequestGroup.ActionSelectionBehavior.fromCode(action.getSelectionBehavior().toCode()));
}
Resource resource = null;
if (action.hasDefinition()) {
if (action.getDefinitionCanonicalType().getValue().contains("ActivityDefinition")) {
Parameters inParams = new Parameters();
inParams.addParameter().setName("patient").setValue(new StringType(patientId));
Parameters outParams = applyClient.operation().onInstance(new IdDt(action.getDefinitionCanonicalType().getValue())).named("$apply").withParameters(inParams).useHttpGet().execute();
List<Parameters.ParametersParameterComponent> response = outParams.getParameter();
resource = response.get(0).getResource().setId(UUID.randomUUID().toString());
}
}
// on here...
if (action.hasDynamicValue()) {
for (PlanDefinition.PlanDefinitionActionDynamicValueComponent dynamicValue : action.getDynamicValue()) {
if (dynamicValue.hasPath() && dynamicValue.hasExpression()) {
if (dynamicValue.getPath().endsWith("title")) {
// summary
String title = (String) context.resolveExpressionRef(dynamicValue.getExpression().getExpression()).evaluate(context);
actionBuilder.buildTitle(title);
} else if (dynamicValue.getPath().endsWith("description")) {
// detail
String description = (String) context.resolveExpressionRef(dynamicValue.getExpression().getExpression()).evaluate(context);
actionBuilder.buildDescripition(description);
} else if (dynamicValue.getPath().endsWith("extension")) {
// indicator
String extension = (String) context.resolveExpressionRef(dynamicValue.getExpression().getExpression()).evaluate(context);
actionBuilder.buildExtension(extension);
} else {
if (resource != null) {
Object value = context.resolveExpressionRef(dynamicValue.getExpression().getExpression()).evaluate(context);
// TODO need to verify type... yay
if (value instanceof Boolean) {
value = new BooleanType((Boolean) value);
}
R4FhirModelResolver modelResolver = new R4FhirModelResolver();
modelResolver.setValue(resource, dynamicValue.getPath(), value);
actionBuilder.buildResourceTarget(resource);
actionBuilder.buildResource(new ReferenceBuilder().buildReference(resource.getId()).build());
}
}
}
}
}
if (!actionBuilder.build().isEmpty()) {
actionComponents.add(actionBuilder.build());
}
if (action.hasAction()) {
resolveActions(action.getAction(), context, patientId, requestGroupBuilder, actionComponents, applyClient);
}
}
}
}
requestGroupBuilder.buildAction(new ArrayList<>(actionComponents));
}
Aggregations