Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for converting CQL Tuples to FHIR Parameters #1422

Merged
merged 10 commits into from
Oct 10, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.math.BigDecimal;
import java.util.Calendar;
import java.util.Collection;
import java.util.GregorianCalendar;
import java.util.stream.Collectors;
import org.apache.commons.lang3.NotImplementedException;
Expand Down Expand Up @@ -227,7 +228,35 @@ public IBase toFhirTuple(Tuple value) {
return null;
}

throw new NotImplementedException("can't convert Tuples");
var parameters = new Parameters();
if (value.getElements().isEmpty()) {
return parameters;
}

var param = parameters.addParameter();
for (String key : value.getElements().keySet()) {
var part = param.addPart();
part.setName(key);
var element = value.getElements().get(key);
if (element == null) {
part.addExtension()
JPercival marked this conversation as resolved.
Show resolved Hide resolved
.setUrl(DATA_ABSENT_REASON_EXT_URL)
.setValue(new CodeType(DATA_ABSENT_REASON_UNKNOWN_CODE));
} else if (element instanceof Collection && ((Collection<?>) element).isEmpty()) {
part.addExtension().setUrl(EMPTY_LIST_EXT_URL).setValue(new BooleanType(true));
JPercival marked this conversation as resolved.
Show resolved Hide resolved
} else {
var result = toFhirType(element);
JPercival marked this conversation as resolved.
Show resolved Hide resolved
if (result instanceof Resource) {
part.setResource((Resource) result);
} else if (result instanceof Type) {
part.setValue((Type) result);
} else {
throw new IllegalArgumentException("Tuple contains unsupported type");
}
}
}

return parameters;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.math.BigDecimal;
import java.time.format.DateTimeFormatter;
import java.util.Collection;
import java.util.stream.Collectors;
import org.apache.commons.lang3.NotImplementedException;
import org.hl7.fhir.dstu3.model.*;
Expand Down Expand Up @@ -226,7 +227,35 @@ public IBase toFhirTuple(Tuple value) {
return null;
}

throw new NotImplementedException("can't convert Tuples");
var parameters = new Parameters();
if (value.getElements().isEmpty()) {
return parameters;
}

var param = parameters.addParameter();
for (String key : value.getElements().keySet()) {
var part = param.addPart();
part.setName(key);
var element = value.getElements().get(key);
if (element == null) {
part.addExtension()
.setUrl(DATA_ABSENT_REASON_EXT_URL)
.setValue(new CodeType(DATA_ABSENT_REASON_UNKNOWN_CODE));
} else if (element instanceof Collection && ((Collection<?>) element).isEmpty()) {
part.addExtension().setUrl(EMPTY_LIST_EXT_URL).setValue(new BooleanType(true));
} else {
var result = toFhirType(element);
if (result instanceof Resource) {
part.setResource((Resource) result);
} else if (result instanceof Type) {
part.setValue((Type) result);
} else {
throw new IllegalArgumentException("Tuple contains unsupported type");
}
}
}

return parameters;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@
*/
public interface FhirTypeConverter {

static final String EMPTY_LIST_EXT_URL = "http://hl7.org/fhir/StructureDefinition/cqf-isEmptyList";
static final String DATA_ABSENT_REASON_EXT_URL = "http://hl7.org/fhir/StructureDefinition/data-absent-reason";
static final String DATA_ABSENT_REASON_UNKNOWN_CODE = "unknown";

// CQL-to-FHIR conversions

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.math.BigDecimal;
import java.time.format.DateTimeFormatter;
import java.util.Collection;
import java.util.stream.Collectors;
import org.apache.commons.lang3.NotImplementedException;
import org.hl7.fhir.instance.model.api.IBase;
Expand Down Expand Up @@ -218,7 +219,35 @@ public IBase toFhirTuple(Tuple value) {
return null;
}

throw new NotImplementedException("can't convert Tuples");
var parameters = new Parameters();
if (value.getElements().isEmpty()) {
return parameters;
}

var param = parameters.addParameter();
for (String key : value.getElements().keySet()) {
var part = param.addPart();
part.setName(key);
var element = value.getElements().get(key);
if (element == null) {
part.addExtension()
.setUrl(DATA_ABSENT_REASON_EXT_URL)
.setValue(new CodeType(DATA_ABSENT_REASON_UNKNOWN_CODE));
} else if (element instanceof Collection && ((Collection<?>) element).isEmpty()) {
part.addExtension().setUrl(EMPTY_LIST_EXT_URL).setValue(new BooleanType(true));
} else {
var result = toFhirType(element);
if (result instanceof Resource) {
part.setResource((Resource) result);
} else if (result instanceof Type) {
part.setValue((Type) result);
} else {
throw new IllegalArgumentException("Tuple contains unsupported type");
}
}
}

return parameters;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.math.BigDecimal;
import java.time.format.DateTimeFormatter;
import java.util.Collection;
import java.util.stream.Collectors;
import org.apache.commons.lang3.NotImplementedException;
import org.hl7.fhir.instance.model.api.IBase;
Expand Down Expand Up @@ -218,7 +219,35 @@ public IBase toFhirTuple(Tuple value) {
return null;
}

throw new NotImplementedException("can't convert Tuples");
var parameters = new Parameters();
if (value.getElements().isEmpty()) {
return parameters;
}

var param = parameters.addParameter();
for (String key : value.getElements().keySet()) {
var part = param.addPart();
part.setName(key);
var element = value.getElements().get(key);
if (element == null) {
part.addExtension()
.setUrl(DATA_ABSENT_REASON_EXT_URL)
.setValue(new CodeType(DATA_ABSENT_REASON_UNKNOWN_CODE));
} else if (element instanceof Collection && ((Collection<?>) element).isEmpty()) {
part.addExtension().setUrl(EMPTY_LIST_EXT_URL).setValue(new BooleanType(true));
} else {
var result = toFhirType(element);
if (result instanceof Resource) {
part.setResource((Resource) result);
} else if (result instanceof DataType) {
part.setValue((DataType) result);
} else {
throw new IllegalArgumentException("Tuple contains unsupported type");
}
}
}

return parameters;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.instanceOf;
import static org.junit.Assert.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNull;
Expand All @@ -27,9 +28,12 @@
import org.hl7.fhir.dstu2.model.DateTimeType;
import org.hl7.fhir.dstu2.model.DateType;
import org.hl7.fhir.dstu2.model.DecimalType;
import org.hl7.fhir.dstu2.model.Encounter;
import org.hl7.fhir.dstu2.model.IdType;
import org.hl7.fhir.dstu2.model.InstantType;
import org.hl7.fhir.dstu2.model.IntegerType;
import org.hl7.fhir.dstu2.model.Parameters;
import org.hl7.fhir.dstu2.model.Parameters.ParametersParameterComponent;
import org.hl7.fhir.dstu2.model.Patient;
import org.hl7.fhir.dstu2.model.Period;
import org.hl7.fhir.dstu2.model.Range;
Expand Down Expand Up @@ -67,7 +71,7 @@ protected Boolean compareIterables(Iterable<Object> left, Iterable<Object> right
return !leftIterator.hasNext() && !rightIterator.hasNext();
}

@SuppressWarnings("unchecked")
@SuppressWarnings({"unchecked", "null"})
protected Boolean compareObjects(Object left, Object right) {
if (left == null ^ right == null) {
return false;
Expand Down Expand Up @@ -522,15 +526,49 @@ void invalidIntervalToFhirInterval() {
});
}

private static ParametersParameterComponent getPartByName(ParametersParameterComponent ppc, String name) {
return ppc.getPart().stream()
.filter(p -> p.getName().equals(name))
.findFirst()
.get();
}

@Test
void tupleToFhirTuple() {
IBase expected = typeConverter.toFhirTuple(null);
assertNull(expected);
Parameters actual = (Parameters) typeConverter.toFhirTuple(null);
assertNull(actual);

var tuple = new Tuple();
assertThrows(NotImplementedException.class, () -> {
typeConverter.toFhirTuple(tuple);
});
actual = (Parameters) typeConverter.toFhirTuple(tuple);
assertNotNull(actual);
assertTrue(actual.isEmpty());

tuple.getElements().put("W", null);
tuple.getElements().put("X", 5);
tuple.getElements().put("Y", new Encounter().setId("123"));
tuple.getElements().put("Z", new ArrayList<>());

actual = (Parameters) typeConverter.toFhirTuple(tuple);
assertNotNull(actual);
assertEquals(1, actual.getParameter().size());

var first = actual.getParameter().get(0);
assertEquals(4, first.getPart().size());

var w = getPartByName(first, "W");
assertEquals(
FhirTypeConverter.DATA_ABSENT_REASON_EXT_URL,
w.getExtension().get(0).getUrl());

var x = getPartByName(first, "X");
assertEquals(5, ((IntegerType) x.getValue()).getValue());

var y = getPartByName(first, "Y");
assertEquals("123", y.getResource().getId());

var z = getPartByName(first, "Z");
assertEquals(
FhirTypeConverter.EMPTY_LIST_EXT_URL, z.getExtension().get(0).getUrl());
}

// FHIR-to-CQL
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import static org.hamcrest.Matchers.instanceOf;
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.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
Expand All @@ -27,9 +28,12 @@
import org.hl7.fhir.dstu3.model.DateTimeType;
import org.hl7.fhir.dstu3.model.DateType;
import org.hl7.fhir.dstu3.model.DecimalType;
import org.hl7.fhir.dstu3.model.Encounter;
import org.hl7.fhir.dstu3.model.IdType;
import org.hl7.fhir.dstu3.model.InstantType;
import org.hl7.fhir.dstu3.model.IntegerType;
import org.hl7.fhir.dstu3.model.Parameters;
import org.hl7.fhir.dstu3.model.Parameters.ParametersParameterComponent;
import org.hl7.fhir.dstu3.model.Patient;
import org.hl7.fhir.dstu3.model.Period;
import org.hl7.fhir.dstu3.model.Range;
Expand Down Expand Up @@ -67,7 +71,7 @@ protected Boolean compareIterables(Iterable<Object> left, Iterable<Object> right
return !leftIterator.hasNext() && !rightIterator.hasNext();
}

@SuppressWarnings("unchecked")
@SuppressWarnings({"unchecked", "null"})
protected Boolean compareObjects(Object left, Object right) {
if (left == null ^ right == null) {
return false;
Expand Down Expand Up @@ -526,15 +530,49 @@ void invalidIntervalToFhirInterval() {
});
}

private static ParametersParameterComponent getPartByName(ParametersParameterComponent ppc, String name) {
return ppc.getPart().stream()
.filter(p -> p.getName().equals(name))
.findFirst()
.get();
}

@Test
void tupleToFhirTuple() {
IBase expected = typeConverter.toFhirTuple(null);
assertNull(expected);
Parameters actual = (Parameters) typeConverter.toFhirTuple(null);
assertNull(actual);

var tuple = new Tuple();
assertThrows(NotImplementedException.class, () -> {
typeConverter.toFhirTuple(tuple);
});
actual = (Parameters) typeConverter.toFhirTuple(tuple);
assertNotNull(actual);
assertTrue(actual.isEmpty());

tuple.getElements().put("W", null);
tuple.getElements().put("X", 5);
tuple.getElements().put("Y", new Encounter().setId("123"));
tuple.getElements().put("Z", new ArrayList<>());

actual = (Parameters) typeConverter.toFhirTuple(tuple);
assertNotNull(actual);
assertEquals(1, actual.getParameter().size());

var first = actual.getParameterFirstRep();
assertEquals(4, first.getPart().size());

var w = getPartByName(first, "W");
assertEquals(
FhirTypeConverter.DATA_ABSENT_REASON_EXT_URL,
w.getExtension().get(0).getUrl());

var x = getPartByName(first, "X");
assertEquals(5, ((IntegerType) x.getValue()).getValue());

var y = getPartByName(first, "Y");
assertEquals("123", y.getResource().getId());

var z = getPartByName(first, "Z");
assertEquals(
FhirTypeConverter.EMPTY_LIST_EXT_URL, z.getExtension().get(0).getUrl());
}

// FHIR-to-CQL
Expand Down
Loading
Loading