Skip to content

Commit

Permalink
#377: Added STU3 plandefinition refresh operation and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
c-schuler committed Aug 5, 2022
1 parent ecf6568 commit 4041700
Show file tree
Hide file tree
Showing 45 changed files with 11,832 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package org.opencds.cqf.tooling.plandefinition.stu3;

import org.cqframework.cql.cql2elm.CqlTranslatorOptions;
import org.cqframework.cql.cql2elm.LibraryManager;
import org.cqframework.cql.cql2elm.model.TranslatedLibrary;
import org.cqframework.cql.elm.requirements.fhir.DataRequirementsProcessor;
import org.hl7.fhir.convertors.advisors.impl.BaseAdvisor_30_50;
import org.hl7.fhir.convertors.conv30_50.VersionConvertor_30_50;
import org.hl7.fhir.dstu3.model.PlanDefinition;
import org.hl7.fhir.r5.model.Extension;
import org.hl7.fhir.r5.model.Library;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class PlanDefinitionRefreshProcessor {

public PlanDefinition refreshPlanDefinition(PlanDefinition planToUse, LibraryManager libraryManager, TranslatedLibrary translatedLibrary, CqlTranslatorOptions options) {
Set<String> expressions = new HashSet<>();
if (planToUse.hasAction()) {
getExpressions(planToUse.getAction(), expressions);
}
DataRequirementsProcessor dqReqTrans = new DataRequirementsProcessor();
Library moduleDefinitionLibrary = dqReqTrans.gatherDataRequirements(libraryManager, translatedLibrary, options, expressions, true);

planToUse.getExtension().removeAll(planToUse.getExtensionsByUrl("http://hl7.org/fhir/us/cqfmeasures/StructureDefinition/cqfm-parameter"));
planToUse.getExtension().removeAll(planToUse.getExtensionsByUrl("http://hl7.org/fhir/us/cqfmeasures/StructureDefinition/cqfm-dataRequirement"));
planToUse.getExtension().removeAll(planToUse.getExtensionsByUrl("http://hl7.org/fhir/us/cqfmeasures/StructureDefinition/cqfm-directReferenceCode"));
planToUse.getExtension().removeAll(planToUse.getExtensionsByUrl("http://hl7.org/fhir/us/cqfmeasures/StructureDefinition/cqfm-logicDefinition"));
planToUse.getExtension().removeAll(planToUse.getExtensionsByUrl("http://hl7.org/fhir/us/cqfmeasures/StructureDefinition/cqfm-effectiveDataRequirements"));

for (Extension extension : moduleDefinitionLibrary.getExtension()) {
org.hl7.fhir.dstu3.model.Extension ext = (org.hl7.fhir.dstu3.model.Extension) new VersionConvertor_30_50(new BaseAdvisor_30_50()).convertType(extension);
planToUse.addExtension(ext);
}

planToUse.getContained().removeIf(resource -> resource.getId().equalsIgnoreCase("effective-data-requirements"));
// planToUse.addContained(moduleDefinitionLibrary.setId("effective-data-requirements"));
// planToUse.addExtension().setUrl("http://hl7.org/fhir/us/cqfmeasures/StructureDefinition/cqfm-effectiveDataRequirements").setValue(new Reference("#effective-data-requirements")).setId("effective-data-requirements");
return planToUse;
}

private void getExpressions(List<PlanDefinition.PlanDefinitionActionComponent> actions, Set<String> expressions) {
for (PlanDefinition.PlanDefinitionActionComponent action : actions) {
if (action.hasCondition()) {
for (PlanDefinition.PlanDefinitionActionConditionComponent condition : action.getCondition()) {
if (condition.hasKind() && condition.getKind() == PlanDefinition.ActionConditionKind.APPLICABILITY
&& condition.hasExpression() && isExpressionIdentifier(condition)) {
expressions.add(condition.getExpression());
}
}
}
if (action.hasDynamicValue()) {
for (PlanDefinition.PlanDefinitionActionDynamicValueComponent dynamicValue : action.getDynamicValue()) {
if (dynamicValue.hasExpression()) {
expressions.add(dynamicValue.getExpression());
}
}
}
if (action.hasAction()) {
getExpressions(action.getAction(), expressions);
}
}
}

private boolean isExpressionIdentifier(PlanDefinition.PlanDefinitionActionConditionComponent expression) {
return expression.hasLanguage() && expression.hasExpression() && (expression.getLanguage().equalsIgnoreCase("text/cql.identifier") || expression.getLanguage().equalsIgnoreCase("text/cql"));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package org.opencds.cqf.tooling.plandefinition.stu3;

import ca.uhn.fhir.context.FhirContext;
import org.cqframework.cql.cql2elm.*;
import org.cqframework.cql.cql2elm.model.TranslatedLibrary;
import org.hl7.elm.r1.VersionedIdentifier;
import org.hl7.fhir.dstu3.model.PlanDefinition;
import org.hl7.fhir.instance.model.api.IBaseResource;
import org.opencds.cqf.tooling.utilities.CanonicalUtils;
import org.opencds.cqf.tooling.utilities.IOUtils;
import org.testng.annotations.Test;

import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class PlanDefinitionRefreshProcessorTest {

public static final String separator = System.getProperty("file.separator");

@Test
public void refreshPlanDefinition() {
String libraryDirectoryPath = "/Users/christopherschuler/Documents/workspace/cqframework/cqf-tooling/src/test/resources/org/opencds/cqf/tooling/stu3/input/resources/library";
String plandefinitionDirectoryPath = "/Users/christopherschuler/Documents/workspace/cqframework/cqf-tooling/src/test/resources/org/opencds/cqf/tooling/stu3/input/resources/plandefinition";

List<IBaseResource> resources = IOUtils.readResources(
Arrays.asList(
plandefinitionDirectoryPath + separator + "plandefinition-OpioidCDSREC01.json",
plandefinitionDirectoryPath + separator + "plandefinition-OpioidCDSREC02.json",
plandefinitionDirectoryPath + separator + "plandefinition-OpioidCDSREC03.json",
plandefinitionDirectoryPath + separator + "plandefinition-OpioidCDSREC04.json",
plandefinitionDirectoryPath + separator + "plandefinition-OpioidCDSREC04PatientView.json",
plandefinitionDirectoryPath + separator + "plandefinition-OpioidCDSREC05.json",
plandefinitionDirectoryPath + separator + "plandefinition-OpioidCDSREC06.json",
plandefinitionDirectoryPath + separator + "plandefinition-OpioidCDSREC07.json",
plandefinitionDirectoryPath + separator + "plandefinition-OpioidCDSREC08.json",
plandefinitionDirectoryPath + separator + "plandefinition-OpioidCDSREC08OrderSign.json",
plandefinitionDirectoryPath + separator + "plandefinition-OpioidCDSREC09.json",
plandefinitionDirectoryPath + separator + "plandefinition-OpioidCDSREC10.json",
plandefinitionDirectoryPath + separator + "plandefinition-OpioidCDSREC10OrderSign.json",
plandefinitionDirectoryPath + separator + "plandefinition-OpioidCDSREC10PatientView.json",
plandefinitionDirectoryPath + separator + "plandefinition-OpioidCDSREC11.json",
plandefinitionDirectoryPath + separator + "plandefinition-OpioidCDSREC11PatientView.json",
plandefinitionDirectoryPath + separator + "plandefinition-OpioidCDSREC12PatientView.json"

), FhirContext.forDstu3()
);

LibrarySourceProvider sourceProvider = new DefaultLibrarySourceProvider(Path.of(libraryDirectoryPath));
LibraryManager libraryManager = new LibraryManager(new ModelManager());
libraryManager.getLibrarySourceLoader().registerProvider(sourceProvider);

CqlTranslatorOptions options = CqlTranslatorOptions.defaultOptions();
VersionedIdentifier identifier;
TranslatedLibrary translatedLibrary;
PlanDefinitionRefreshProcessor refreshProcessor = new PlanDefinitionRefreshProcessor();
for(IBaseResource plan : resources) {
PlanDefinition planDef = (PlanDefinition) plan;
identifier = new VersionedIdentifier().withId(CanonicalUtils.getId(planDef.getLibrary().get(0).getReference()));
translatedLibrary = libraryManager.resolveLibrary(identifier, options, new ArrayList<>());
PlanDefinition refreshedPlan = refreshProcessor.refreshPlanDefinition(planDef, libraryManager, translatedLibrary, options);
IOUtils.writeResource(refreshedPlan, "/Users/christopherschuler/Documents/scratch", IOUtils.Encoding.JSON, FhirContext.forDstu3());
}


// String resource = new XmlParser(FhirContext.forR5(), new LenientErrorHandler()).setPrettyPrint(true).encodeResourceToString(refreshedPlan);
// String resource = new JsonParser(FhirContext.forR5(), new LenientErrorHandler()).setPrettyPrint(true).encodeResourceToString(refreshedPlan);

String s = "";
}

}
Loading

0 comments on commit 4041700

Please sign in to comment.