Merge branch 'master' into 2515_MDM_survivorship_rules

This commit is contained in:
Nick Goupinets 2021-04-01 09:39:05 -04:00
commit fdef49d1aa
6 changed files with 188 additions and 223 deletions

View File

@ -32,13 +32,12 @@ import java.util.Date;
public class DateHelper {
/**
*
* @param date A date String in the format YYYY-MM-DD.
* @return A {@link java.util.Date} object representing the String data that was passed in.
*/
public static Date resolveRequestDate(String date) {
public static Date resolveRequestDate(String paramName, String date) {
if (StringUtils.isBlank(date)) {
throw new IllegalArgumentException("date parameter cannot be blank!");
throw new IllegalArgumentException(paramName + " parameter cannot be blank!");
}
return new DateTimeType(date).getValue();
}

View File

@ -43,11 +43,11 @@ public class MeasureEvaluationSeed {
private Measure measure;
private Context context;
private Interval measurementPeriod;
private LibraryLoader libraryLoader;
private LibraryResolutionProvider<org.hl7.fhir.dstu3.model.Library> libraryResourceProvider;
private EvaluationProviderFactory providerFactory;
private final LibraryLoader libraryLoader;
private final LibraryResolutionProvider<org.hl7.fhir.dstu3.model.Library> libraryResourceProvider;
private final EvaluationProviderFactory providerFactory;
private DataProvider dataProvider;
private LibraryHelper libraryHelper;
private final LibraryHelper libraryHelper;
public MeasureEvaluationSeed(EvaluationProviderFactory providerFactory, LibraryLoader libraryLoader,
LibraryResolutionProvider<org.hl7.fhir.dstu3.model.Library> libraryResourceProvider, LibraryHelper libraryHelper) {
@ -113,8 +113,8 @@ public class MeasureEvaluationSeed {
}
// resolve the measurement period
measurementPeriod = new Interval(DateHelper.resolveRequestDate(periodStart), true,
DateHelper.resolveRequestDate(periodEnd), true);
measurementPeriod = new Interval(DateHelper.resolveRequestDate("periodStart", periodStart), true,
DateHelper.resolveRequestDate("periodEnd", periodEnd), true);
context.setParameter(null, "Measurement Period",
new Interval(DateTime.fromJavaDate((Date) measurementPeriod.getStart()), true,

View File

@ -23,6 +23,7 @@ package ca.uhn.fhir.cql.dstu3.helper;
import ca.uhn.fhir.cql.common.evaluation.LibraryLoader;
import ca.uhn.fhir.cql.common.provider.LibraryResolutionProvider;
import ca.uhn.fhir.cql.common.provider.LibrarySourceProvider;
import org.apache.commons.lang3.StringUtils;
import org.cqframework.cql.cql2elm.LibraryManager;
import org.cqframework.cql.cql2elm.ModelManager;
import org.cqframework.cql.cql2elm.model.Model;
@ -31,20 +32,22 @@ import org.cqframework.cql.elm.execution.VersionedIdentifier;
import org.hl7.fhir.dstu3.model.Attachment;
import org.hl7.fhir.dstu3.model.Coding;
import org.hl7.fhir.dstu3.model.Measure;
import org.hl7.fhir.dstu3.model.PlanDefinition;
import org.hl7.fhir.dstu3.model.Reference;
import org.hl7.fhir.dstu3.model.RelatedArtifact;
import org.hl7.fhir.dstu3.model.Resource;
import org.opencds.cqf.cql.evaluator.cql2elm.model.CacheAwareModelManager;
import org.opencds.cqf.cql.evaluator.engine.execution.PrivateCachingLibraryLoaderDecorator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
public class LibraryHelper {
private static final Logger ourLog = LoggerFactory.getLogger(LibraryHelper.class);
private Map<org.hl7.elm.r1.VersionedIdentifier, Model> modelCache;
private final Map<org.hl7.elm.r1.VersionedIdentifier, Model> modelCache;
public LibraryHelper(Map<org.hl7.elm.r1.VersionedIdentifier, Model> modelCache) {
this.modelCache = modelCache;
@ -67,6 +70,7 @@ public class LibraryHelper {
org.opencds.cqf.cql.engine.execution.LibraryLoader libraryLoader,
LibraryResolutionProvider<org.hl7.fhir.dstu3.model.Library> libraryResourceProvider) {
List<org.cqframework.cql.elm.execution.Library> libraries = new ArrayList<Library>();
List<String> messages = new ArrayList<>();
// load libraries
//TODO: if there's a bad measure argument, this blows up for an obscure error
@ -88,15 +92,21 @@ public class LibraryHelper {
}
org.hl7.fhir.dstu3.model.Library library = libraryResourceProvider.resolveLibraryById(id);
if (library != null && isLogicLibrary(library)) {
if (library != null) {
if (isLogicLibrary(library)) {
libraries.add(libraryLoader
.load(new VersionedIdentifier().withId(library.getName()).withVersion(library.getVersion())));
} else {
String message = "Skipping library " + library.getId() + " is not a logic library. Probably missing type.coding.system=\"http://hl7.org/fhir/library-type\"";
messages.add(message);
ourLog.warn(message);
}
}
}
if (libraries.isEmpty()) {
throw new IllegalArgumentException(String
.format("Could not load library source for libraries referenced in Measure %s.", measure.getId()));
.format("Could not load library source for libraries referenced in %s:\n%s", measure.getId(), StringUtils.join("\n", messages)));
}
VersionedIdentifier primaryLibraryId = libraries.get(0).getIdentifier();
@ -159,22 +169,6 @@ public class LibraryHelper {
org.hl7.fhir.dstu3.model.Library fhirLibrary = libraryResourceProvider.resolveLibraryById(libraryId);
return libraryLoader
.load(new VersionedIdentifier().withId(fhirLibrary.getName()).withVersion(fhirLibrary.getVersion()));
// for (Library l : libraryLoader.getLibraries()) {
// VersionedIdentifier vid = l.getIdentifier();
// if (vid.getId().equals(fhirLibrary.getName()) &&
// LibraryResourceHelper.compareVersions(fhirLibrary.getVersion(),
// vid.getVersion()) == 0) {
// library = l;
// break;
// }
// }
// if (library == null) {
// }
// return library;
}
public Library resolvePrimaryLibrary(Measure measure,
@ -192,18 +186,4 @@ public class LibraryHelper {
return library;
}
public Library resolvePrimaryLibrary(PlanDefinition planDefinition, org.opencds.cqf.cql.engine.execution.LibraryLoader libraryLoader,
LibraryResolutionProvider<org.hl7.fhir.dstu3.model.Library> libraryResourceProvider) {
String id = planDefinition.getLibraryFirstRep().getReferenceElement().getIdPart();
Library library = resolveLibraryById(id, libraryLoader, libraryResourceProvider);
if (library == null) {
throw new IllegalArgumentException(String.format("Could not resolve primary library for PlanDefinition/%s",
planDefinition.getIdElement().getIdPart()));
}
return library;
}
}

View File

@ -43,11 +43,11 @@ public class MeasureEvaluationSeed {
private Measure measure;
private Context context;
private Interval measurementPeriod;
private LibraryLoader libraryLoader;
private LibraryResolutionProvider<org.hl7.fhir.r4.model.Library> libraryResourceProvider;
private EvaluationProviderFactory providerFactory;
private final LibraryLoader libraryLoader;
private final LibraryResolutionProvider<org.hl7.fhir.r4.model.Library> libraryResourceProvider;
private final EvaluationProviderFactory providerFactory;
private DataProvider dataProvider;
private LibraryHelper libraryHelper;
private final LibraryHelper libraryHelper;
public MeasureEvaluationSeed(EvaluationProviderFactory providerFactory, LibraryLoader libraryLoader,
LibraryResolutionProvider<org.hl7.fhir.r4.model.Library> libraryResourceProvider, LibraryHelper libraryHelper) {
@ -115,8 +115,8 @@ public class MeasureEvaluationSeed {
if (periodStart != null && periodEnd != null) {
// resolve the measurement period
measurementPeriod = new Interval(DateHelper.resolveRequestDate(periodStart), true,
DateHelper.resolveRequestDate(periodEnd), true);
measurementPeriod = new Interval(DateHelper.resolveRequestDate("periodStart", periodStart), true,
DateHelper.resolveRequestDate("periodEnd", periodEnd), true);
context.setParameter(null, "Measurement Period",
new Interval(DateTime.fromJavaDate((Date) measurementPeriod.getStart()), true,

View File

@ -23,6 +23,7 @@ package ca.uhn.fhir.cql.r4.helper;
import ca.uhn.fhir.cql.common.evaluation.LibraryLoader;
import ca.uhn.fhir.cql.common.provider.LibraryResolutionProvider;
import ca.uhn.fhir.cql.common.provider.LibrarySourceProvider;
import org.apache.commons.lang3.StringUtils;
import org.cqframework.cql.cql2elm.LibraryManager;
import org.cqframework.cql.cql2elm.ModelManager;
import org.cqframework.cql.cql2elm.model.Model;
@ -32,11 +33,12 @@ import org.hl7.fhir.r4.model.Attachment;
import org.hl7.fhir.r4.model.CanonicalType;
import org.hl7.fhir.r4.model.Coding;
import org.hl7.fhir.r4.model.Measure;
import org.hl7.fhir.r4.model.PlanDefinition;
import org.hl7.fhir.r4.model.RelatedArtifact;
import org.hl7.fhir.r4.model.Resource;
import org.opencds.cqf.cql.evaluator.cql2elm.model.CacheAwareModelManager;
import org.opencds.cqf.cql.evaluator.engine.execution.PrivateCachingLibraryLoaderDecorator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.ArrayList;
import java.util.List;
@ -46,8 +48,9 @@ import java.util.Map;
* Created by Christopher on 1/11/2017.
*/
public class LibraryHelper {
private static final Logger ourLog = LoggerFactory.getLogger(LibraryHelper.class);
private Map<org.hl7.elm.r1.VersionedIdentifier, Model> modelCache;
private final Map<org.hl7.elm.r1.VersionedIdentifier, Model> modelCache;
public LibraryHelper(Map<org.hl7.elm.r1.VersionedIdentifier, Model> modelCache) {
this.modelCache = modelCache;
@ -93,10 +96,19 @@ public class LibraryHelper {
LibraryResolutionProvider<org.hl7.fhir.r4.model.Library> libraryResourceProvider) {
List<org.cqframework.cql.elm.execution.Library> libraries = new ArrayList<org.cqframework.cql.elm.execution.Library>();
List<String> messages = new ArrayList<>();
// load libraries
//TODO: if there's a bad measure argument, this blows up for an obscure error
org.hl7.fhir.r4.model.Library primaryLibrary = null;
for (CanonicalType ref : measure.getLibrary()) {
List<CanonicalType> measureLibraries = measure.getLibrary();
if (measureLibraries.isEmpty()) {
String message = "No libraries found on " + measure.getId() + ". Did you perhaps load a DSTU3 Measure onto an R4 server?";
messages.add(message);
ourLog.warn(message);
}
for (CanonicalType ref : measureLibraries) {
// if library is contained in measure, load it into server
String id = ref.getValue(); //CanonicalHelper.getId(ref);
if (id.startsWith("#")) {
@ -115,20 +127,25 @@ public class LibraryHelper {
primaryLibrary = library;
}
if (library != null && isLogicLibrary(library)) {
if (library != null) {
if (isLogicLibrary(library)) {
libraries.add(
libraryLoader.load(new VersionedIdentifier().withId(library.getName()).withVersion(library.getVersion()))
);
} else {
String message = "Skipping library " + library.getId() + " is not a logic library. Probably missing type.coding.system=\"http://terminology.hl7.org/CodeSystem/library-type\"";
messages.add(message);
ourLog.warn(message);
}
}
}
if (libraries.isEmpty()) {
throw new IllegalArgumentException(String
.format("Could not load library source for libraries referenced in %s.", measure.getId()));
.format("Could not load library source for libraries referenced in %s:\n%s", measure.getId(), StringUtils.join("\n", messages)));
}
//VersionedIdentifier primaryLibraryId = libraries.get(0).getIdentifier();
//org.hl7.fhir.r4.model.Library primaryLibrary = libraryResourceProvider.resolveLibraryByName(primaryLibraryId.getId(), primaryLibraryId.getVersion());
for (RelatedArtifact artifact : primaryLibrary.getRelatedArtifact()) {
if (artifact.hasType() && artifact.getType().equals(RelatedArtifact.RelatedArtifactType.DEPENDSON) && artifact.hasResource()) {
org.hl7.fhir.r4.model.Library library = null;
@ -186,22 +203,6 @@ public class LibraryHelper {
org.hl7.fhir.r4.model.Library fhirLibrary = libraryResourceProvider.resolveLibraryById(libraryId);
return libraryLoader
.load(new VersionedIdentifier().withId(fhirLibrary.getName()).withVersion(fhirLibrary.getVersion()));
// for (Library l : libraryLoader.getLibraries()) {
// VersionedIdentifier vid = l.getIdentifier();
// if (vid.getId().equals(fhirLibrary.getName()) &&
// LibraryResourceHelper.compareVersions(fhirLibrary.getVersion(),
// vid.getVersion()) == 0) {
// library = l;
// break;
// }
// }
// if (library == null) {
// }
// return library;
}
public Library resolvePrimaryLibrary(Measure measure,
@ -219,19 +220,4 @@ public class LibraryHelper {
return library;
}
public Library resolvePrimaryLibrary(PlanDefinition planDefinition,
org.opencds.cqf.cql.engine.execution.LibraryLoader libraryLoader,
LibraryResolutionProvider<org.hl7.fhir.r4.model.Library> libraryResourceProvider) {
String id = CanonicalHelper.getId(planDefinition.getLibrary().get(0));
Library library = resolveLibraryById(id, libraryLoader, libraryResourceProvider);
if (library == null) {
throw new IllegalArgumentException(String.format("Could not resolve primary library for PlanDefinition/%s",
planDefinition.getIdElement().getIdPart()));
}
return library;
}
}

View File

@ -17,7 +17,7 @@ public class DateHelperTest {
@Test
public void testDateHelperProperlyResolvesValidDate() {
DateHelper dateHelper = new DateHelper();
Date result = dateHelper.resolveRequestDate("2001-01-29");
Date result = DateHelper.resolveRequestDate("param", "2001-01-29");
assertNotNull("result should not be NULL!", result);
GregorianCalendar cal = new GregorianCalendar();
cal.setTimeInMillis(result.getTime());
@ -28,7 +28,7 @@ public class DateHelperTest {
@Test
public void testDateHelperProperlyResolvesValidDateWithYearOnly() {
DateHelper dateHelper = new DateHelper();
Date result = dateHelper.resolveRequestDate("2001");
Date result = DateHelper.resolveRequestDate("param", "2001");
assertNotNull("result should not be NULL!", result);
}
@ -37,7 +37,7 @@ public class DateHelperTest {
DateHelper dateHelper = new DateHelper();
Date result = null;
try {
result = dateHelper.resolveRequestDate(null);
result = DateHelper.resolveRequestDate("param", null);
fail();
} catch (IllegalArgumentException e) {
assertNull("result should be NULL!", result);
@ -49,7 +49,7 @@ public class DateHelperTest {
DateHelper dateHelper = new DateHelper();
Date result = null;
try {
result = dateHelper.resolveRequestDate("aaa-bbb-ccc");
result = DateHelper.resolveRequestDate("param", "aaa-bbb-ccc");
fail();
} catch (DataFormatException e) {
assertNull("result should be NULL!", result);