use of org.hl7.fhir.r5.model.ExpressionNode.Function in project pathling by aehrc.
the class MemberOfFunctionTest method memberOfCodeableConcept.
@Test
void memberOfCodeableConcept() {
final Coding coding1 = new Coding(LOINC_URL, "10337-4", "Procollagen type I [Mass/volume] in Serum");
final Coding coding2 = new Coding(LOINC_URL, "10428-1", "Varicella zoster virus immune globulin given [Volume]");
final Coding coding3 = new Coding(LOINC_URL, "10555-1", null);
final Coding coding4 = new Coding(LOINC_URL, "10665-8", "Fungus colony count [#/volume] in Unspecified specimen by Environmental culture");
final Coding coding5 = new Coding(SNOMED_URL, "416399002", "Procollagen type I amino-terminal propeptide level");
final CodeableConcept codeableConcept1 = new CodeableConcept(coding1);
codeableConcept1.addCoding(coding5);
final CodeableConcept codeableConcept2 = new CodeableConcept(coding2);
final CodeableConcept codeableConcept3 = new CodeableConcept(coding3);
final CodeableConcept codeableConcept4 = new CodeableConcept(coding3);
final CodeableConcept codeableConcept5 = new CodeableConcept(coding4);
final CodeableConcept codeableConcept6 = new CodeableConcept(coding1);
final Optional<ElementDefinition> optionalDefinition = FhirHelpers.getChildOfResource(fhirContext, "DiagnosticReport", "code");
assertTrue(optionalDefinition.isPresent());
final ElementDefinition definition = optionalDefinition.get();
final Dataset<Row> inputDataset = new DatasetBuilder(spark).withIdColumn().withStructTypeColumns(codeableConceptStructType()).withRow("diagnosticreport-1", rowFromCodeableConcept(codeableConcept1)).withRow("diagnosticreport-2", rowFromCodeableConcept(codeableConcept2)).withRow("diagnosticreport-3", rowFromCodeableConcept(codeableConcept3)).withRow("diagnosticreport-4", rowFromCodeableConcept(codeableConcept4)).withRow("diagnosticreport-5", rowFromCodeableConcept(codeableConcept5)).withRow("diagnosticreport-6", rowFromCodeableConcept(codeableConcept6)).withRow("diagnosticreport-7", null).buildWithStructValue();
final ElementPath inputExpression = new ElementPathBuilder(spark).dataset(inputDataset).idAndValueColumns().expression("DiagnosticReport.code").singular(true).definition(definition).buildDefined();
final StringLiteralPath argumentExpression = StringLiteralPath.fromString("'" + MY_VALUE_SET_URL + "'", inputExpression);
// Setup mocks
when(terminologyService.intersect(any(), any())).thenReturn(setOfSimpleFrom(codeableConcept1, codeableConcept3, codeableConcept4));
// Prepare the inputs to the function.
final ParserContext parserContext = new ParserContextBuilder(spark, fhirContext).terminologyClientFactory(terminologyServiceFactory).build();
final NamedFunctionInput memberOfInput = new NamedFunctionInput(parserContext, inputExpression, Collections.singletonList(argumentExpression));
// Invoke the function.
final FhirPath result = new MemberOfFunction().invoke(memberOfInput);
final Dataset<Row> expectedResult = new DatasetBuilder(spark).withIdColumn().withColumn(DataTypes.BooleanType).withRow("diagnosticreport-1", true).withRow("diagnosticreport-2", false).withRow("diagnosticreport-3", true).withRow("diagnosticreport-4", true).withRow("diagnosticreport-5", false).withRow("diagnosticreport-6", true).withRow("diagnosticreport-7", null).build();
// Check the result.
assertTrue(result instanceof BooleanPath);
assertThat((BooleanPath) result).hasExpression("DiagnosticReport.code.memberOf('" + MY_VALUE_SET_URL + "')").isSingular().hasFhirType(FHIRDefinedType.BOOLEAN).isElementPath(BooleanPath.class).selectOrderedResult().hasRows(expectedResult);
verify(terminologyService).intersect(eq(MY_VALUE_SET_URL), eq(setOfSimpleFrom(coding1, coding2, coding3, coding4, coding5)));
verifyNoMoreInteractions(terminologyService);
}
use of org.hl7.fhir.r5.model.ExpressionNode.Function in project pathling by aehrc.
the class MemberOfFunctionTest method memberOfCoding.
@Test
void memberOfCoding() {
final Coding coding1 = new Coding(MY_VALUE_SET_URL, "AMB", "ambulatory");
final Coding coding2 = new Coding(MY_VALUE_SET_URL, "EMER", null);
final Coding coding3 = new Coding(MY_VALUE_SET_URL, "IMP", "inpatient encounter");
final Coding coding4 = new Coding(MY_VALUE_SET_URL, "IMP", null);
final Coding coding5 = new Coding(MY_VALUE_SET_URL, "ACUTE", "inpatient acute");
final Optional<ElementDefinition> optionalDefinition = FhirHelpers.getChildOfResource(fhirContext, "Encounter", "class");
assertTrue(optionalDefinition.isPresent());
final ElementDefinition definition = optionalDefinition.get();
final Dataset<Row> inputDataset = new DatasetBuilder(spark).withIdColumn().withEidColumn().withStructTypeColumns(codingStructType()).withRow("encounter-1", makeEid(1), rowFromCoding(coding1)).withRow("encounter-1", makeEid(0), rowFromCoding(coding5)).withRow("encounter-2", makeEid(0), rowFromCoding(coding2)).withRow("encounter-3", makeEid(0), rowFromCoding(coding3)).withRow("encounter-4", makeEid(0), rowFromCoding(coding4)).withRow("encounter-5", makeEid(0), rowFromCoding(coding5)).withRow("encounter-6", null, null).buildWithStructValue();
final CodingPath inputExpression = (CodingPath) new ElementPathBuilder(spark).dataset(inputDataset).idAndEidAndValueColumns().expression("Encounter.class").singular(false).definition(definition).buildDefined();
final StringLiteralPath argumentExpression = StringLiteralPath.fromString("'" + MY_VALUE_SET_URL + "'", inputExpression);
// Setup mocks
when(terminologyService.intersect(any(), any())).thenReturn(setOfSimpleFrom(coding2, coding5));
// Prepare the inputs to the function.
final ParserContext parserContext = new ParserContextBuilder(spark, fhirContext).idColumn(inputExpression.getIdColumn()).terminologyClientFactory(terminologyServiceFactory).build();
final NamedFunctionInput memberOfInput = new NamedFunctionInput(parserContext, inputExpression, Collections.singletonList(argumentExpression));
// Invoke the function.
final FhirPath result = new MemberOfFunction().invoke(memberOfInput);
// The outcome is somehow random with regard to the sequence passed to MemberOfMapperAnswerer.
final Dataset<Row> expectedResult = new DatasetBuilder(spark).withIdColumn().withEidColumn().withColumn(DataTypes.BooleanType).withRow("encounter-1", makeEid(0), true).withRow("encounter-1", makeEid(1), false).withRow("encounter-2", makeEid(0), true).withRow("encounter-3", makeEid(0), false).withRow("encounter-4", makeEid(0), false).withRow("encounter-5", makeEid(0), true).withRow("encounter-6", null, null).build();
// Check the result.
assertThat(result).hasExpression("Encounter.class.memberOf('" + MY_VALUE_SET_URL + "')").isElementPath(BooleanPath.class).hasFhirType(FHIRDefinedType.BOOLEAN).isNotSingular().selectOrderedResultWithEid().hasRows(expectedResult);
verify(terminologyService).intersect(eq(MY_VALUE_SET_URL), eq(setOfSimpleFrom(coding1, coding2, coding3, coding4, coding5)));
verifyNoMoreInteractions(terminologyService);
}
use of org.hl7.fhir.r5.model.ExpressionNode.Function in project pathling by aehrc.
the class TranslateFunctionTest method translateCodeableConceptWithNonDefaultArguments.
@Test
void translateCodeableConceptWithNonDefaultArguments() {
final Optional<ElementDefinition> optionalDefinition = FhirHelpers.getChildOfResource(fhirContext, "Encounter", "type");
assertTrue(optionalDefinition.isPresent());
final ElementDefinition definition = optionalDefinition.get();
// The translations are
// {
// coding1 -> [translated1],
// coding2 -> [translated1, translated2]
// coding4 -> [translated2]
// }
// Use cases:
// 1. [ {C2,C3,C1}, {C3}, {C4} ] -> [ [T1, T2],[], [T2]]
// 2. [ {C3, C5}, {C3} ] -> [ [], [] ]
// 3. [ {C2} ] -> [[T1, T2]]
// 4. [ {C3}] -> [[]]
// 5. [ ]-> []
// 6. null -> null
final Dataset<Row> inputDataset = new DatasetBuilder(spark).withIdColumn().withEidColumn().withStructTypeColumns(codeableConceptStructType()).withRow("encounter-1", makeEid(0), rowFromCodeableConcept(new CodeableConcept(CODING_2).addCoding(CODING_3).addCoding(CODING_1))).withRow("encounter-1", makeEid(1), rowFromCodeableConcept(new CodeableConcept(CODING_3).addCoding(CODING_5))).withRow("encounter-1", makeEid(2), rowFromCodeableConcept(new CodeableConcept(CODING_4))).withRow("encounter-2", makeEid(0), rowFromCodeableConcept(new CodeableConcept(CODING_3).addCoding(CODING_5))).withRow("encounter-2", makeEid(1), rowFromCodeableConcept(new CodeableConcept(CODING_3))).withRow("encounter-3", makeEid(0), rowFromCodeableConcept(new CodeableConcept(CODING_2))).withRow("encounter-4", makeEid(0), rowFromCodeableConcept(new CodeableConcept(CODING_3))).withRow("encounter-5", makeEid(0), null).withRow("encounter-6", null, null).buildWithStructValue();
final ElementPath inputExpression = new ElementPathBuilder(spark).dataset(inputDataset).idAndEidAndValueColumns().expression("Encounter.type").singular(false).definition(definition).buildDefined();
final ConceptTranslator returnedConceptTranslator = ConceptTranslatorBuilder.toSystem(DEST_SYSTEM_URI).put(new SimpleCoding(CODING_1), TRANSLATED_1).put(new SimpleCoding(CODING_2), TRANSLATED_1, TRANSLATED_2).put(new SimpleCoding(CODING_4), TRANSLATED_2).build();
// Create a mock terminology client.
when(terminologyService.translate(any(), any(), anyBoolean(), any())).thenReturn(returnedConceptTranslator);
// Prepare the inputs to the function.
final ParserContext parserContext = new ParserContextBuilder(spark, fhirContext).idColumn(inputExpression.getIdColumn()).terminologyClientFactory(terminologyServiceFactory).build();
final StringLiteralPath conceptMapUrlArgument = StringLiteralPath.fromString("'" + CONCEPT_MAP2_URI + "'", inputExpression);
final BooleanLiteralPath reverseArgument = BooleanLiteralPath.fromString("true", inputExpression);
final StringLiteralPath equivalenceArgument = StringLiteralPath.fromString("narrower,equivalent", inputExpression);
final NamedFunctionInput translateInput = new NamedFunctionInput(parserContext, inputExpression, Arrays.asList(conceptMapUrlArgument, reverseArgument, equivalenceArgument));
// Invoke the function.
final FhirPath result = new TranslateFunction().invoke(translateInput);
final Dataset<Row> expectedResult = new DatasetBuilder(spark).withIdColumn().withEidColumn().withStructTypeColumns(codingStructType()).withRow("encounter-1", makeEid(0, 0), rowFromCoding(TRANSLATED_1)).withRow("encounter-1", makeEid(0, 1), rowFromCoding(TRANSLATED_2)).withRow("encounter-1", makeEid(1, 0), null).withRow("encounter-1", makeEid(2, 0), rowFromCoding(TRANSLATED_2)).withRow("encounter-2", makeEid(0, 0), null).withRow("encounter-2", makeEid(1, 0), null).withRow("encounter-3", makeEid(0, 0), rowFromCoding(TRANSLATED_1)).withRow("encounter-3", makeEid(0, 1), rowFromCoding(TRANSLATED_2)).withRow("encounter-4", makeEid(0, 0), null).withRow("encounter-5", makeEid(0, 0), null).withRow("encounter-6", null, null).buildWithStructValue();
// Check the result.
assertThat(result).hasExpression("Encounter.type.translate('" + CONCEPT_MAP2_URI + "', true, 'narrower,equivalent')").isElementPath(CodingPath.class).hasFhirType(FHIRDefinedType.CODING).isNotSingular().selectOrderedResultWithEid().hasRows(expectedResult);
// Verify mocks
final Set<SimpleCoding> expectedSourceCodings = ImmutableSet.of(new SimpleCoding(CODING_1), new SimpleCoding(CODING_2), new SimpleCoding(CODING_3), new SimpleCoding(CODING_4), new SimpleCoding(CODING_5));
final List<ConceptMapEquivalence> expectedEquivalences = Arrays.asList(ConceptMapEquivalence.NARROWER, ConceptMapEquivalence.EQUIVALENT);
verify(terminologyService).translate(eq(expectedSourceCodings), eq(CONCEPT_MAP2_URI), eq(true), eq(expectedEquivalences));
verifyNoMoreInteractions(terminologyService);
}
use of org.hl7.fhir.r5.model.ExpressionNode.Function in project pathling by aehrc.
the class TranslateFunctionTest method translateCodingWithDefaultArguments.
@Test
void translateCodingWithDefaultArguments() {
final Optional<ElementDefinition> optionalDefinition = FhirHelpers.getChildOfResource(fhirContext, "Encounter", "class");
assertTrue(optionalDefinition.isPresent());
final ElementDefinition definition = optionalDefinition.get();
// The translations are
// {
// coding1 -> [translated1],
// coding2 -> [translated1, translated2]
// }
// Use cases:
// 1. [ C2,C3,C1 ] -> [ [T1, T2],[], [T1]]
// 2. [ C3, C5 ] -> [[],[]]
// 3. [ C2 ] -> [ [T1, T2] ]
// 4. [] -> []
// 5. null -> null
final Dataset<Row> inputDataset = new DatasetBuilder(spark).withIdColumn().withEidColumn().withStructTypeColumns(codingStructType()).withRow("encounter-1", makeEid(0), rowFromCoding(CODING_2)).withRow("encounter-1", makeEid(1), rowFromCoding(CODING_3)).withRow("encounter-1", makeEid(2), rowFromCoding(CODING_1)).withRow("encounter-2", makeEid(0), rowFromCoding(CODING_3)).withRow("encounter-2", makeEid(1), rowFromCoding(CODING_5)).withRow("encounter-3", makeEid(0), rowFromCoding(CODING_2)).withRow("encounter-4", makeEid(0), null).withRow("encounter-5", null, null).buildWithStructValue();
final CodingPath inputExpression = (CodingPath) new ElementPathBuilder(spark).dataset(inputDataset).idAndEidAndValueColumns().expression("Encounter.class").singular(false).definition(definition).buildDefined();
final ConceptTranslator returnedConceptTranslator = ConceptTranslatorBuilder.toSystem(DEST_SYSTEM_URI).put(new SimpleCoding(CODING_1), TRANSLATED_1).put(new SimpleCoding(CODING_2), TRANSLATED_1, TRANSLATED_2).build();
// Create a mock terminology client.
when(terminologyService.translate(any(), any(), anyBoolean(), any())).thenReturn(returnedConceptTranslator);
// Prepare the inputs to the function.
final ParserContext parserContext = new ParserContextBuilder(spark, fhirContext).idColumn(inputExpression.getIdColumn()).terminologyClientFactory(terminologyServiceFactory).build();
final StringLiteralPath conceptMapUrlArgument = StringLiteralPath.fromString("'" + CONCEPT_MAP1_URI + "'", inputExpression);
final NamedFunctionInput translateInput = new NamedFunctionInput(parserContext, inputExpression, Collections.singletonList(conceptMapUrlArgument));
// Invoke the function.
final FhirPath result = new TranslateFunction().invoke(translateInput);
final Dataset<Row> expectedResult = new DatasetBuilder(spark).withIdColumn().withEidColumn().withStructTypeColumns(codingStructType()).withRow("encounter-1", makeEid(0, 0), rowFromCoding(TRANSLATED_1)).withRow("encounter-1", makeEid(0, 1), rowFromCoding(TRANSLATED_2)).withRow("encounter-1", makeEid(1, 0), null).withRow("encounter-1", makeEid(2, 0), rowFromCoding(TRANSLATED_1)).withRow("encounter-2", makeEid(0, 0), null).withRow("encounter-2", makeEid(1, 0), null).withRow("encounter-3", makeEid(0, 0), rowFromCoding(TRANSLATED_1)).withRow("encounter-3", makeEid(0, 1), rowFromCoding(TRANSLATED_2)).withRow("encounter-4", makeEid(0, 0), null).withRow("encounter-5", null, null).buildWithStructValue();
// Check the result.
assertThat(result).hasExpression("Encounter.class.translate('" + CONCEPT_MAP1_URI + "')").isElementPath(CodingPath.class).hasFhirType(FHIRDefinedType.CODING).isNotSingular().selectOrderedResultWithEid().hasRows(expectedResult);
// Verify mocks
final Set<SimpleCoding> expectedSourceCodings = ImmutableSet.of(new SimpleCoding(CODING_1), new SimpleCoding(CODING_2), new SimpleCoding(CODING_3), new SimpleCoding(CODING_5));
final List<ConceptMapEquivalence> expectedEquivalences = Collections.singletonList(ConceptMapEquivalence.EQUIVALENT);
verify(terminologyService).translate(eq(expectedSourceCodings), eq(CONCEPT_MAP1_URI), eq(false), eq(expectedEquivalences));
verifyNoMoreInteractions(terminologyService);
}
use of org.hl7.fhir.r5.model.ExpressionNode.Function in project pathling by aehrc.
the class EmptyFunctionTest method returnsCorrectResults.
@Test
void returnsCorrectResults() {
final Coding coding1 = new Coding(TestHelpers.SNOMED_URL, "840546002", "Exposure to COVID-19");
final CodeableConcept concept1 = new CodeableConcept(coding1);
final Coding coding2 = new Coding(TestHelpers.SNOMED_URL, "248427009", "Fever symptoms");
final CodeableConcept concept2 = new CodeableConcept(coding2);
final Dataset<Row> dataset = new DatasetBuilder(spark).withIdColumn().withColumn(codeableConceptStructType()).withRow("observation-1", null).withRow("observation-2", null).withRow("observation-2", null).withRow("observation-3", rowFromCodeableConcept(concept1)).withRow("observation-4", rowFromCodeableConcept(concept1)).withRow("observation-4", null).withRow("observation-5", rowFromCodeableConcept(concept1)).withRow("observation-5", rowFromCodeableConcept(concept2)).build();
final ElementPath input = new ElementPathBuilder(spark).fhirType(FHIRDefinedType.CODEABLECONCEPT).dataset(dataset).idAndValueColumns().expression("code").build();
final ParserContext parserContext = new ParserContextBuilder(spark, fhirContext).groupingColumns(Collections.singletonList(input.getIdColumn())).build();
// Set up the function input.
final NamedFunctionInput emptyInput = new NamedFunctionInput(parserContext, input, Collections.emptyList());
// Invoke the function.
final NamedFunction emptyFunction = NamedFunction.getInstance("empty");
final FhirPath result = emptyFunction.invoke(emptyInput);
// Check the result.
final Dataset<Row> expectedDataset = new DatasetBuilder(spark).withIdColumn().withColumn(DataTypes.BooleanType).withRow("observation-1", true).withRow("observation-2", true).withRow("observation-3", false).withRow("observation-4", false).withRow("observation-5", false).build();
assertThat(result).hasExpression("code.empty()").isSingular().isElementPath(BooleanPath.class).selectOrderedResult().hasRows(expectedDataset);
}
Aggregations