use of org.hl7.fhir.r4b.model.DomainResource 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.hl7.fhir.r4b.model.DomainResource in project cqf-ruler by DBCG.
the class ExpressionEvaluation method getLibraryReferences.
private List<CanonicalType> getLibraryReferences(DomainResource instance, RequestDetails theRequest) {
List<CanonicalType> references = new ArrayList<>();
if (instance.hasContained()) {
for (Resource resource : instance.getContained()) {
if (resource instanceof Library) {
resource.setId(resource.getIdElement().getIdPart().replace("#", ""));
this.jpaFhirDalFactory.create(theRequest).update((Library) resource);
// getLibraryLoader().putLibrary(resource.getIdElement().getIdPart(),
// getLibraryLoader().toElmLibrary((Library) resource));
}
}
}
if (instance instanceof ActivityDefinition) {
references.addAll(((ActivityDefinition) instance).getLibrary());
} else if (instance instanceof PlanDefinition) {
references.addAll(((PlanDefinition) instance).getLibrary());
} else if (instance instanceof Measure) {
references.addAll(((Measure) instance).getLibrary());
}
for (Extension extension : instance.getExtensionsByUrl("http://hl7.org/fhir/StructureDefinition/cqif-library")) {
Type value = extension.getValue();
if (value instanceof CanonicalType) {
references.add((CanonicalType) value);
} else {
throw new RuntimeException("Library extension does not have a value of type reference");
}
}
return cleanReferences(references);
}
use of org.hl7.fhir.r4b.model.DomainResource in project beneficiary-fhir-data by CMSgov.
the class TransformerUtils method addExtensionCoding.
/**
* Adds an {@link Extension} to the specified {@link DomainResource}. {@link Extension#getValue()}
* will be set to a {@link CodeableConcept} containing a single {@link Coding}, with the specified
* system and code.
*
* <p>Data Architecture Note: The {@link CodeableConcept} might seem extraneous -- why not just
* add the {@link Coding} directly to the {@link Extension}? The main reason for doing it this way
* is consistency: this is what FHIR seems to do everywhere.
*
* @param fhirElement the FHIR element to add the {@link Extension} to
* @param extensionUrl the {@link Extension#getUrl()} to use
* @param codingSystem the {@link Coding#getSystem()} to use
* @param codingDisplay the {@link Coding#getDisplay()} to use
* @param codingCode the {@link Coding#getCode()} to use
*/
static void addExtensionCoding(IBaseHasExtensions fhirElement, String extensionUrl, String codingSystem, String codingDisplay, String codingCode) {
IBaseExtension<?, ?> extension = fhirElement.addExtension();
extension.setUrl(extensionUrl);
if (codingDisplay == null)
extension.setValue(new Coding().setSystem(codingSystem).setCode(codingCode));
else
extension.setValue(new Coding().setSystem(codingSystem).setCode(codingCode).setDisplay(codingDisplay));
}
use of org.hl7.fhir.r4b.model.DomainResource in project beneficiary-fhir-data by CMSgov.
the class TransformerUtils method addExtensionValueIdentifier.
/**
* Adds an {@link Extension} to the specified {@link DomainResource}. {@link Extension#getValue()}
* will be set to a {@link Identifier} with the specified url, system, and value.
*
* @param fhirElement the FHIR element to add the {@link Extension} to
* @param extensionUrl the {@link Extension#getUrl()} to use
* @param extensionSystem the {@link Identifier#getSystem()} to use
* @param extensionValue the {@link Identifier#getValue()} to use
*/
static void addExtensionValueIdentifier(IBaseHasExtensions fhirElement, String extensionUrl, String extensionSystem, String extensionValue) {
IBaseExtension<?, ?> extension = fhirElement.addExtension();
extension.setUrl(extensionUrl);
Identifier valueIdentifier = new Identifier();
valueIdentifier.setSystem(extensionSystem).setValue(extensionValue);
extension.setValue(valueIdentifier);
}
use of org.hl7.fhir.r4b.model.DomainResource in project odm2fhir by num-codex.
the class SymptomConditions method createConditions.
private Stream<DomainResource> createConditions(FormData formData, ItemData generalSymptomCoding, ItemData specificCoding) {
var condition = (Condition) new Condition().addIdentifier(createIdentifier(CONDITION, specificCoding)).setClinicalStatus(ACTIVE).setRecordedDateElement(// TODO Set actual DateTime value
UNKNOWN_DATE_TIME).addCategory(createCodeableConcept(generalSymptomCoding)).setSeverity(createCodeableConcept(formData.getItemData(specificCoding.getItemOID() + "_schweregrad"))).setMeta(createMeta(SYMPTOMS_COVID_19));
var specificCodings = createCodings(specificCoding);
var codeCodeableConcept = new CodeableConcept();
for (var coding : specificCodings) {
switch(coding.getCode()) {
case // PRESENT
"410605003":
condition.setClinicalStatus(ACTIVE).setVerificationStatus(CONFIRMED);
break;
case // ABSENT
"410594000":
condition.setVerificationStatus(REFUTED);
break;
case // UNKNOWN
"261665006":
condition.addModifierExtension(DATA_PRESENCE_UNKNOWN);
break;
case // Answer = Sonstige/Other
"385432009":
codeCodeableConcept.addCoding(coding.setDisplay("Not applicable (qualifier value)"));
if (!formData.getItemData("symptome_andere_symptome_textfeld").isEmpty()) {
codeCodeableConcept.setText(formData.getItemData("symptome_andere_symptome_textfeld").getValue());
}
break;
default:
// SYMPTOM CODING
if (!"symptome_geruchs_bzw_geschmacksstoerungen".equals(specificCoding.getItemOID())) {
codeCodeableConcept.addCoding(coding);
}
break;
}
}
if ("symptome_geruchs_bzw_geschmacksstoerungen".equals(specificCoding.getItemOID()) && specificCodings.size() == 2) {
var codeableConceptTASTE = new CodeableConcept().addCoding(specificCodings.get(0));
var codeableConceptSMELL = new CodeableConcept().addCoding(specificCodings.get(1));
var identifier = condition.getIdentifierFirstRep();
// TODO Check if really nothing should be returned in case only either taste or smell is empty
return codeableConceptTASTE.isEmpty() || codeableConceptSMELL.isEmpty() ? Stream.empty() : Stream.of(condition.copy().setCode(codeableConceptTASTE).setIdentifier(List.of(identifier.copy().setValue(identifier.getValue() + "_geschmack"))), condition.copy().setCode(codeableConceptSMELL).setIdentifier(List.of(identifier.copy().setValue(identifier.getValue() + "_geruch"))));
}
return codeCodeableConcept.isEmpty() ? Stream.empty() : Stream.of(condition.setCode(codeCodeableConcept));
}
Aggregations