Search in sources :

Example 26 with FhirPath

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);
}
Also used : FhirPath(au.csiro.pathling.fhirpath.FhirPath) ParserContextBuilder(au.csiro.pathling.test.builders.ParserContextBuilder) ResourcePathBuilder(au.csiro.pathling.test.builders.ResourcePathBuilder) ResourcePath(au.csiro.pathling.fhirpath.ResourcePath) ElementPath(au.csiro.pathling.fhirpath.element.ElementPath) Row(org.apache.spark.sql.Row) DatasetBuilder(au.csiro.pathling.test.builders.DatasetBuilder) ParserContext(au.csiro.pathling.fhirpath.parser.ParserContext) NonLiteralPath(au.csiro.pathling.fhirpath.NonLiteralPath) ElementPathBuilder(au.csiro.pathling.test.builders.ElementPathBuilder) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 27 with FhirPath

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);
}
Also used : ElementPath(au.csiro.pathling.fhirpath.element.ElementPath) FhirPath(au.csiro.pathling.fhirpath.FhirPath) ParserContextBuilder(au.csiro.pathling.test.builders.ParserContextBuilder) Row(org.apache.spark.sql.Row) DatasetBuilder(au.csiro.pathling.test.builders.DatasetBuilder) ParserContext(au.csiro.pathling.fhirpath.parser.ParserContext) NonLiteralPath(au.csiro.pathling.fhirpath.NonLiteralPath) ElementPathBuilder(au.csiro.pathling.test.builders.ElementPathBuilder) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 28 with FhirPath

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());
}
Also used : InvalidUserInputError(au.csiro.pathling.errors.InvalidUserInputError) ElementPath(au.csiro.pathling.fhirpath.element.ElementPath) FhirPath(au.csiro.pathling.fhirpath.FhirPath) NamedFunctionInput(au.csiro.pathling.fhirpath.function.NamedFunctionInput) ParserContextBuilder(au.csiro.pathling.test.builders.ParserContextBuilder) ParserContext(au.csiro.pathling.fhirpath.parser.ParserContext) ElementPathBuilder(au.csiro.pathling.test.builders.ElementPathBuilder) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 29 with FhirPath

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);
}
Also used : CodingPath(au.csiro.pathling.fhirpath.element.CodingPath) StringLiteralPath(au.csiro.pathling.fhirpath.literal.StringLiteralPath) FhirPath(au.csiro.pathling.fhirpath.FhirPath) ParserContextBuilder(au.csiro.pathling.test.builders.ParserContextBuilder) NamedFunctionInput(au.csiro.pathling.fhirpath.function.NamedFunctionInput) ElementDefinition(au.csiro.pathling.fhirpath.element.ElementDefinition) Row(org.apache.spark.sql.Row) DatasetBuilder(au.csiro.pathling.test.builders.DatasetBuilder) ParserContext(au.csiro.pathling.fhirpath.parser.ParserContext) ElementPathBuilder(au.csiro.pathling.test.builders.ElementPathBuilder) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 30 with FhirPath

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);
}
Also used : FhirPath(au.csiro.pathling.fhirpath.FhirPath) NamedFunctionInput(au.csiro.pathling.fhirpath.function.NamedFunctionInput) BooleanPath(au.csiro.pathling.fhirpath.element.BooleanPath) ParserContextBuilder(au.csiro.pathling.test.builders.ParserContextBuilder) ParserContext(au.csiro.pathling.fhirpath.parser.ParserContext)

Aggregations

FhirPath (au.csiro.pathling.fhirpath.FhirPath)105 Row (org.apache.spark.sql.Row)56 Test (org.junit.jupiter.api.Test)54 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)54 ElementPath (au.csiro.pathling.fhirpath.element.ElementPath)41 ParserContext (au.csiro.pathling.fhirpath.parser.ParserContext)40 ParserContextBuilder (au.csiro.pathling.test.builders.ParserContextBuilder)40 ElementPathBuilder (au.csiro.pathling.test.builders.ElementPathBuilder)39 DatasetBuilder (au.csiro.pathling.test.builders.DatasetBuilder)38 ResourcePath (au.csiro.pathling.fhirpath.ResourcePath)23 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)23 MethodSource (org.junit.jupiter.params.provider.MethodSource)23 ResourceDatasetBuilder (au.csiro.pathling.test.builders.ResourceDatasetBuilder)22 Nonnull (javax.annotation.Nonnull)21 Column (org.apache.spark.sql.Column)19 NonLiteralPath (au.csiro.pathling.fhirpath.NonLiteralPath)15 ElementDefinition (au.csiro.pathling.fhirpath.element.ElementDefinition)14 StringLiteralPath (au.csiro.pathling.fhirpath.literal.StringLiteralPath)13 NamedFunctionInput (au.csiro.pathling.fhirpath.function.NamedFunctionInput)11 BooleanPath (au.csiro.pathling.fhirpath.element.BooleanPath)9