Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.opencds.cqf.fhir.cr.measure.r4;

import com.google.common.annotations.VisibleForTesting;
import java.util.Map;
import org.hl7.fhir.r4.model.MeasureReport;
import org.opencds.cqf.cql.engine.execution.EvaluationResult;
Expand All @@ -20,7 +19,6 @@
* @param measureReport The scored R4 MeasureReport FHIR resource
* @param evaluationResults The raw CQL evaluation results per subject ID
*/
@VisibleForTesting
public record MeasureDefAndR4MeasureReport(
MeasureDef measureDef, MeasureReport measureReport, Map<String, CqlEvaluationResult> evaluationResults) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,52 @@ public MeasureReport evaluate(
.measureReport();
}

/**

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment, no action required: It would be good for cdr-cr and cqis-spark to eventually leverage this.

* Evaluates a single measure for a single subject and returns the full evaluation result — the
* {@link MeasureReport}, the captured {@link MeasureDef}, and the per-subject CQL
* {@link CqlEvaluationResult} map — rather than only the MeasureReport that {@link #evaluate}
* returns.
*
* <p>This is the supported, public way to obtain the MeasureDef (population definitions,
* evaluation errors, url/version) and the raw CQL evaluation results (expression results, and,
* when debug logging is enabled on the engine, debug results and traces) alongside the report.
* The evaluation runs against the {@link IRepository} this service was constructed with.
*
* @param measure the measure to evaluate
* @param periodStart start of the measurement period (nullable)
* @param periodEnd end of the measurement period (nullable)
* @param reportType the MeasureReport type (e.g. "subject")
* @param subjectId the subject to evaluate (e.g. "Patient/123")
* @param lastReceivedOn optional last-received-on timestamp
* @param parameters CQL parameters
* @param productLine product line
* @param practitioner practitioner id, when scoping the evaluation to a practitioner
* @return the MeasureDef, MeasureReport, and per-subject CQL evaluation results
*/
public MeasureDefAndR4MeasureReport evaluateWithDef(
MeasureReference measure,
@Nullable ZonedDateTime periodStart,
@Nullable ZonedDateTime periodEnd,
String reportType,
String subjectId,
String lastReceivedOn,
Parameters parameters,
String productLine,
String practitioner) {

return evaluateSingleMeasureCaptureDef(
measure,
periodStart,
periodEnd,
reportType,
subjectId,
lastReceivedOn,
repository,
parameters,
productLine,
practitioner);
}

@VisibleForTesting
MeasureDefAndR4MeasureReport evaluateSingleMeasureCaptureDef(
MeasureReference measure,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package org.opencds.cqf.fhir.cr.measure.r4;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.opencds.cqf.fhir.test.Resources.getResourcePath;

import ca.uhn.fhir.context.FhirContext;
import java.nio.file.Path;
import org.hl7.fhir.r4.model.IdType;
import org.hl7.fhir.r4.model.Parameters;
import org.junit.jupiter.api.Test;
import org.opencds.cqf.fhir.cql.engine.retrieve.RetrieveSettings.SEARCH_FILTER_MODE;
import org.opencds.cqf.fhir.cql.engine.retrieve.RetrieveSettings.TERMINOLOGY_FILTER_MODE;
import org.opencds.cqf.fhir.cql.engine.terminology.TerminologySettings.VALUESET_EXPANSION_MODE;
import org.opencds.cqf.fhir.cr.measure.MeasureEvaluationOptions;
import org.opencds.cqf.fhir.cr.measure.common.MeasurePeriodValidator;
import org.opencds.cqf.fhir.cr.measure.common.MeasureReference;
import org.opencds.cqf.fhir.utility.repository.ig.IgRepository;

/**
* Verifies the public {@link R4MultiMeasureService#evaluateWithDef} API returns the full evaluation
* result — a populated {@code MeasureDef} and the per-subject CQL results — not just the
* MeasureReport. This is the supported replacement for the split-package reflection workaround Link
* uses to reach the previously package-private capture-def method.
*/
@SuppressWarnings("squid:S2699")
class R4MultiMeasureServiceEvaluateWithDefTest {

private static final String REPOSITORY = "MeasureTest";
private static final String MEASURE_ID = "ProportionResourceAllPopulations";
private static final String SUBJECT = "Patient/patient-9";

private static R4MultiMeasureService service() {
var repository = new IgRepository(
FhirContext.forR4Cached(),
Path.of(getResourcePath(R4MultiMeasureServiceEvaluateWithDefTest.class) + "/" + Measure.CLASS_PATH + "/"
+ REPOSITORY));
var options = MeasureEvaluationOptions.defaultOptions();
options.getEvaluationSettings()
.getRetrieveSettings()
.setSearchParameterMode(SEARCH_FILTER_MODE.FILTER_IN_MEMORY)
.setTerminologyParameterMode(TERMINOLOGY_FILTER_MODE.FILTER_IN_MEMORY);
options.getEvaluationSettings()
.getTerminologySettings()
.setValuesetExpansionMode(VALUESET_EXPANSION_MODE.PERFORM_NAIVE_EXPANSION);
return new R4MultiMeasureService(repository, options, "http://localhost", new MeasurePeriodValidator());
}

@Test
void evaluateWithDefReturnsMeasureDefMeasureReportAndCqlResults() {
var result = service()
.evaluateWithDef(
new MeasureReference.ById(new IdType("Measure", MEASURE_ID)),
null,
null,
"subject",
SUBJECT,
null,
new Parameters(),
null,
null);

// MeasureReport present — parity with the public evaluate() return.
assertNotNull(result.measureReport(), "MeasureReport should be returned");

// MeasureDef populated — the pre-scoring model Link reads (groups, url, errors).
assertNotNull(result.measureDef(), "MeasureDef should be captured");
assertFalse(result.measureDef().groups().isEmpty(), "MeasureDef should have populated groups");

// Per-subject CQL results populated — what Link formats for debug output.
assertFalse(result.evaluationResults().isEmpty(), "per-subject CQL evaluation results should be populated");
assertEquals(1, result.evaluationResults().size(), "single-subject evaluation should yield one CQL result");
assertNotNull(
result.evaluationResults().values().iterator().next(),
"the per-subject CqlEvaluationResult should be non-null");
}
}
Loading