use of au.csiro.pathling.fhirpath.FhirPath in project pathling by aehrc.
the class WhereFunctionTest method whereOnResource.
// This test simulates the execution of the where function on the path
// `Patient.reverseResolve(Encounter.subject).where($this.status = 'in-progress')`.
@Test
void whereOnResource() {
final String statusColumn = randomAlias();
final Dataset<Row> inputDataset = new DatasetBuilder(spark).withIdColumn().withEidColumn().withIdColumn().withColumn(statusColumn, DataTypes.StringType).withRow("patient-1", makeEid(1), "encounter-1", "in-progress").withRow("patient-1", makeEid(0), "encounter-2", "finished").withRow("patient-2", makeEid(0), "encounter-3", "in-progress").withRow("patient-3", makeEid(1), "encounter-4", "in-progress").withRow("patient-3", makeEid(0), "encounter-5", "finished").withRow("patient-4", makeEid(1), "encounter-6", "finished").withRow("patient-4", makeEid(0), "encounter-7", "finished").withRow("patient-5", makeEid(1), "encounter-8", "in-progress").withRow("patient-5", makeEid(0), "encounter-9", "in-progress").withRow("patient-6", null, null, null).build();
final ResourcePath inputPath = new ResourcePathBuilder(spark).expression("reverseResolve(Encounter.subject)").dataset(inputDataset).idEidAndValueColumns().buildCustom();
// Build an expression which represents the argument to the function. We assume that the value
// column from the input dataset is also present within the argument dataset.
final NonLiteralPath thisPath = inputPath.toThisPath();
final Dataset<Row> argumentDataset = thisPath.getDataset().withColumn("value", thisPath.getDataset().col(statusColumn).equalTo("in-progress"));
assertTrue(thisPath.getThisColumn().isPresent());
final ElementPath argumentPath = new ElementPathBuilder(spark).fhirType(FHIRDefinedType.BOOLEAN).dataset(argumentDataset).idColumn(inputPath.getIdColumn()).valueColumn(argumentDataset.col("value")).thisColumn(thisPath.getThisColumn().get()).singular(true).build();
// Prepare the input to the function.
final ParserContext parserContext = new ParserContextBuilder(spark, fhirContext).build();
final NamedFunctionInput whereInput = new NamedFunctionInput(parserContext, inputPath, Collections.singletonList(argumentPath));
// Execute the function.
final NamedFunction whereFunction = NamedFunction.getInstance("where");
final FhirPath result = whereFunction.invoke(whereInput);
// Check the result dataset.
final Dataset<Row> expectedDataset = new DatasetBuilder(spark).withIdColumn().withEidColumn().withIdColumn().withRow("patient-1", makeEid(0), null).withRow("patient-1", makeEid(1), "patient-1").withRow("patient-2", makeEid(0), "patient-2").withRow("patient-3", makeEid(0), null).withRow("patient-3", makeEid(1), "patient-3").withRow("patient-4", makeEid(0), null).withRow("patient-4", makeEid(1), null).withRow("patient-5", makeEid(0), "patient-5").withRow("patient-5", makeEid(1), "patient-5").withRow("patient-6", null, null).build();
assertThat(result).selectOrderedResultWithEid().hasRows(expectedDataset);
}
use of au.csiro.pathling.fhirpath.FhirPath in project pathling by aehrc.
the class WhereFunctionTest method whereOnElement.
@Test
void whereOnElement() {
// Build an expression which represents the input to the function.
final Dataset<Row> dataset = new DatasetBuilder(spark).withIdColumn().withEidColumn().withColumn(DataTypes.StringType).withRow("patient-1", makeEid(1), "en").withRow("patient-1", makeEid(0), "es").withRow("patient-2", makeEid(0), "de").withRow("patient-3", makeEid(2), "en").withRow("patient-3", makeEid(1), "en").withRow("patient-3", makeEid(0), "zh").withRow("patient-4", makeEid(1), "fr").withRow("patient-4", makeEid(0), "fr").withRow("patient-5", null, null).build();
final ElementPath inputPath = new ElementPathBuilder(spark).fhirType(FHIRDefinedType.STRING).dataset(dataset).idAndEidAndValueColumns().singular(false).build();
final NonLiteralPath thisPath = inputPath.toThisPath();
// Build an expression which represents the argument to the function.
final Dataset<Row> argumentDataset = thisPath.getDataset().withColumn("value", inputPath.getValueColumn().equalTo("en"));
assertTrue(thisPath.getThisColumn().isPresent());
final ElementPath argumentExpression = new ElementPathBuilder(spark).fhirType(FHIRDefinedType.BOOLEAN).dataset(argumentDataset).idColumn(inputPath.getIdColumn()).valueColumn(argumentDataset.col("value")).thisColumn(thisPath.getThisColumn().get()).singular(true).build();
// Prepare the input to the function.
final ParserContext parserContext = new ParserContextBuilder(spark, fhirContext).build();
final NamedFunctionInput whereInput = new NamedFunctionInput(parserContext, inputPath, Collections.singletonList(argumentExpression));
// Execute the function.
final NamedFunction whereFunction = NamedFunction.getInstance("where");
final FhirPath result = whereFunction.invoke(whereInput);
// Check the result dataset.
final Dataset<Row> expectedDataset = new DatasetBuilder(spark).withIdColumn().withEidColumn().withColumn(DataTypes.StringType).withRow("patient-1", makeEid(0), null).withRow("patient-1", makeEid(1), "en").withRow("patient-2", makeEid(0), null).withRow("patient-3", makeEid(0), null).withRow("patient-3", makeEid(1), "en").withRow("patient-3", makeEid(2), "en").withRow("patient-4", makeEid(0), null).withRow("patient-4", makeEid(1), null).withRow("patient-5", null, null).build();
assertThat(result).selectOrderedResultWithEid().hasRows(expectedDataset);
}
use of au.csiro.pathling.fhirpath.FhirPath in project pathling by aehrc.
the class MemberOfFunctionTest method throwsErrorIfTerminologyServiceNotConfigured.
@Test
void throwsErrorIfTerminologyServiceNotConfigured() {
final ElementPath input = new ElementPathBuilder(spark).fhirType(FHIRDefinedType.CODEABLECONCEPT).build();
final FhirPath argument = StringLiteralPath.fromString("some string", input);
final ParserContext context = new ParserContextBuilder(spark, fhirContext).build();
final NamedFunctionInput memberOfInput = new NamedFunctionInput(context, input, Collections.singletonList(argument));
final InvalidUserInputError error = assertThrows(InvalidUserInputError.class, () -> new MemberOfFunction().invoke(memberOfInput));
assertEquals("Attempt to call terminology function memberOf when terminology service has not been configured", error.getMessage());
}
use of au.csiro.pathling.fhirpath.FhirPath in project pathling by aehrc.
the class MemberOfFunctionTest method memberOfEmptyCodingDatasetDoesNotCallTerminology.
@Test
void memberOfEmptyCodingDatasetDoesNotCallTerminology() {
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()).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);
// 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).build();
// Check the result.
assertThat(result).hasExpression("Encounter.class.memberOf('" + MY_VALUE_SET_URL + "')").isElementPath(BooleanPath.class).hasFhirType(FHIRDefinedType.BOOLEAN).isNotSingular().selectOrderedResultWithEid().hasRows(expectedResult);
verifyNoMoreInteractions(terminologyService);
}
use of au.csiro.pathling.fhirpath.FhirPath in project pathling by aehrc.
the class SubsumesFunctionTest method assertCallSuccess.
ElementPathAssertion assertCallSuccess(final NamedFunction function, final NonLiteralPath inputExpression, final FhirPath argumentExpression) {
final ParserContext parserContext = new ParserContextBuilder(spark, fhirContext).terminologyClientFactory(terminologyServiceFactory).build();
final NamedFunctionInput functionInput = new NamedFunctionInput(parserContext, inputExpression, Collections.singletonList(argumentExpression));
final FhirPath result = function.invoke(functionInput);
return assertThat(result).isElementPath(BooleanPath.class).preservesCardinalityOf(inputExpression);
}
Aggregations