Search in sources :

Example 16 with ResourcePath

use of au.csiro.pathling.fhirpath.ResourcePath in project pathling by aehrc.

the class ReverseResolveFunctionTest method reverseResolve.

@Test
void reverseResolve() {
    final Dataset<Row> patientDataset = new ResourceDatasetBuilder(spark).withIdColumn().withColumn("gender", DataTypes.StringType).withColumn("active", DataTypes.BooleanType).withRow("patient-1", "female", true).withRow("patient-2", "female", false).withRow("patient-3", "male", true).withRow("patient-4", "male", true).build();
    when(database.read(ResourceType.PATIENT)).thenReturn(patientDataset);
    final ResourcePath inputPath = ResourcePath.build(fhirContext, database, ResourceType.PATIENT, "Patient", true);
    final DatasetBuilder encounterDatasetBuilder = new ResourceDatasetBuilder(spark).withIdColumn().withColumn("status", DataTypes.StringType).withRow("encounter-1", "planned").withRow("encounter-2", "arrived").withRow("encounter-3", "triaged").withRow("encounter-4", "in-progress").withRow("encounter-5", "onleave");
    final Dataset<Row> encounterDataset = encounterDatasetBuilder.build();
    when(database.read(ResourceType.ENCOUNTER)).thenReturn(encounterDataset);
    final ResourcePath originPath = ResourcePath.build(fhirContext, database, ResourceType.ENCOUNTER, "Encounter", false);
    final Optional<ElementDefinition> optionalDefinition = FhirHelpers.getChildOfResource(fhirContext, "Encounter", "subject");
    assertTrue(optionalDefinition.isPresent());
    final ElementDefinition definition = optionalDefinition.get();
    final Dataset<Row> argumentDatasetPreJoin = new DatasetBuilder(spark).withIdColumn().withStructTypeColumns(referenceStructType()).withRow("encounter-1", RowFactory.create(null, "Patient/patient-1", null)).withRow("encounter-2", RowFactory.create(null, "Patient/patient-3", null)).withRow("encounter-3", RowFactory.create(null, "Patient/patient-2", null)).withRow("encounter-4", RowFactory.create(null, "Patient/patient-2", null)).withRow("encounter-5", RowFactory.create(null, "Group/group-1", null)).buildWithStructValue();
    final IdAndValueColumns idAndValueColumns = getIdAndValueColumns(argumentDatasetPreJoin);
    final Column idColumn = idAndValueColumns.getId();
    final Column valueColumn = idAndValueColumns.getValues().get(0);
    final Dataset<Row> argumentDataset = join(originPath.getDataset(), originPath.getIdColumn(), argumentDatasetPreJoin, idColumn, JoinType.LEFT_OUTER);
    final FhirPath argumentPath = new ElementPathBuilder(spark).dataset(argumentDataset).idColumn(originPath.getIdColumn()).valueColumn(valueColumn).expression("Encounter.subject").singular(false).currentResource(originPath).definition(definition).buildDefined();
    final ParserContext parserContext = new ParserContextBuilder(spark, fhirContext).idColumn(inputPath.getIdColumn()).database(database).inputExpression("Patient").build();
    final NamedFunctionInput reverseResolveInput = new NamedFunctionInput(parserContext, inputPath, Collections.singletonList(argumentPath));
    final NamedFunction reverseResolve = NamedFunction.getInstance("reverseResolve");
    final FhirPath result = reverseResolve.invoke(reverseResolveInput);
    assertTrue(result instanceof ResourcePath);
    assertThat((ResourcePath) result).hasExpression("reverseResolve(Encounter.subject)").isNotSingular().hasResourceType(ResourceType.ENCOUNTER);
    final Dataset<Row> expectedDataset = new DatasetBuilder(spark).withIdColumn().withIdColumn().withRow("patient-1", "encounter-1").withRow("patient-2", "encounter-3").withRow("patient-2", "encounter-4").withRow("patient-3", "encounter-2").withRow("patient-4", null).build();
    assertThat(result).selectOrderedResult().hasRows(expectedDataset);
}
Also used : FhirPath(au.csiro.pathling.fhirpath.FhirPath) ParserContextBuilder(au.csiro.pathling.test.builders.ParserContextBuilder) ResourcePath(au.csiro.pathling.fhirpath.ResourcePath) ResourceDatasetBuilder(au.csiro.pathling.test.builders.ResourceDatasetBuilder) Column(org.apache.spark.sql.Column) SparkHelpers.getIdAndValueColumns(au.csiro.pathling.test.helpers.SparkHelpers.getIdAndValueColumns) IdAndValueColumns(au.csiro.pathling.test.helpers.SparkHelpers.IdAndValueColumns) Row(org.apache.spark.sql.Row) DatasetBuilder(au.csiro.pathling.test.builders.DatasetBuilder) ResourceDatasetBuilder(au.csiro.pathling.test.builders.ResourceDatasetBuilder) ElementDefinition(au.csiro.pathling.fhirpath.element.ElementDefinition) 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 17 with ResourcePath

use of au.csiro.pathling.fhirpath.ResourcePath in project pathling by aehrc.

the class ReverseResolveFunctionTest method throwsErrorIfMoreThanOneArgument.

@Test
void throwsErrorIfMoreThanOneArgument() {
    final ResourcePath input = new ResourcePathBuilder(spark).expression("Patient").build();
    final ElementPath argument1 = new ElementPathBuilder(spark).expression("Encounter.subject").fhirType(FHIRDefinedType.REFERENCE).build();
    final ElementPath argument2 = new ElementPathBuilder(spark).expression("Encounter.participant.individual").fhirType(FHIRDefinedType.REFERENCE).build();
    final ParserContext parserContext = new ParserContextBuilder(spark, fhirContext).inputExpression("Patient").build();
    final NamedFunctionInput reverseResolveInput = new NamedFunctionInput(parserContext, input, Arrays.asList(argument1, argument2));
    final NamedFunction reverseResolveFunction = NamedFunction.getInstance("reverseResolve");
    final InvalidUserInputError error = assertThrows(InvalidUserInputError.class, () -> reverseResolveFunction.invoke(reverseResolveInput));
    assertEquals("reverseResolve function accepts a single argument: reverseResolve(Encounter.subject, Encounter.participant.individual)", error.getMessage());
}
Also used : InvalidUserInputError(au.csiro.pathling.errors.InvalidUserInputError) ResourcePath(au.csiro.pathling.fhirpath.ResourcePath) ElementPath(au.csiro.pathling.fhirpath.element.ElementPath) ParserContextBuilder(au.csiro.pathling.test.builders.ParserContextBuilder) ResourcePathBuilder(au.csiro.pathling.test.builders.ResourcePathBuilder) 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 18 with ResourcePath

use of au.csiro.pathling.fhirpath.ResourcePath in project pathling by aehrc.

the class WhereFunctionTest method throwsErrorIfArgumentNotBoolean.

@Test
void throwsErrorIfArgumentNotBoolean() {
    final ResourcePath input = new ResourcePathBuilder(spark).build();
    final ElementPath argument = new ElementPathBuilder(spark).expression("$this.gender").fhirType(FHIRDefinedType.STRING).build();
    final ParserContext parserContext = new ParserContextBuilder(spark, fhirContext).build();
    final NamedFunctionInput whereInput = new NamedFunctionInput(parserContext, input, Collections.singletonList(argument));
    final NamedFunction whereFunction = NamedFunction.getInstance("where");
    final InvalidUserInputError error = assertThrows(InvalidUserInputError.class, () -> whereFunction.invoke(whereInput));
    assertEquals("Argument to where function must be a singular Boolean: $this.gender", error.getMessage());
}
Also used : InvalidUserInputError(au.csiro.pathling.errors.InvalidUserInputError) ResourcePath(au.csiro.pathling.fhirpath.ResourcePath) ElementPath(au.csiro.pathling.fhirpath.element.ElementPath) ParserContextBuilder(au.csiro.pathling.test.builders.ParserContextBuilder) ResourcePathBuilder(au.csiro.pathling.test.builders.ResourcePathBuilder) 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 19 with ResourcePath

use of au.csiro.pathling.fhirpath.ResourcePath 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 20 with ResourcePath

use of au.csiro.pathling.fhirpath.ResourcePath in project pathling by aehrc.

the class WhereFunctionTest method throwsErrorIfArgumentNotSingular.

@Test
void throwsErrorIfArgumentNotSingular() {
    final ResourcePath input = new ResourcePathBuilder(spark).build();
    final ElementPath argument = new ElementPathBuilder(spark).expression("$this.communication.preferred").fhirType(FHIRDefinedType.BOOLEAN).singular(false).build();
    final ParserContext parserContext = new ParserContextBuilder(spark, fhirContext).build();
    final NamedFunctionInput whereInput = new NamedFunctionInput(parserContext, input, Collections.singletonList(argument));
    final NamedFunction whereFunction = NamedFunction.getInstance("where");
    final InvalidUserInputError error = assertThrows(InvalidUserInputError.class, () -> whereFunction.invoke(whereInput));
    assertEquals("Argument to where function must be a singular Boolean: $this.communication.preferred", error.getMessage());
}
Also used : InvalidUserInputError(au.csiro.pathling.errors.InvalidUserInputError) ResourcePath(au.csiro.pathling.fhirpath.ResourcePath) ElementPath(au.csiro.pathling.fhirpath.element.ElementPath) ParserContextBuilder(au.csiro.pathling.test.builders.ParserContextBuilder) ResourcePathBuilder(au.csiro.pathling.test.builders.ResourcePathBuilder) 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)

Aggregations

ResourcePath (au.csiro.pathling.fhirpath.ResourcePath)46 Test (org.junit.jupiter.api.Test)32 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)30 ParserContextBuilder (au.csiro.pathling.test.builders.ParserContextBuilder)28 Row (org.apache.spark.sql.Row)26 ParserContext (au.csiro.pathling.fhirpath.parser.ParserContext)25 FhirPath (au.csiro.pathling.fhirpath.FhirPath)23 ResourcePathBuilder (au.csiro.pathling.test.builders.ResourcePathBuilder)23 ElementPath (au.csiro.pathling.fhirpath.element.ElementPath)16 ElementPathBuilder (au.csiro.pathling.test.builders.ElementPathBuilder)16 ResourceDatasetBuilder (au.csiro.pathling.test.builders.ResourceDatasetBuilder)16 DatasetBuilder (au.csiro.pathling.test.builders.DatasetBuilder)15 InvalidUserInputError (au.csiro.pathling.errors.InvalidUserInputError)14 Column (org.apache.spark.sql.Column)14 UntypedResourcePath (au.csiro.pathling.fhirpath.UntypedResourcePath)11 Nonnull (javax.annotation.Nonnull)11 NonLiteralPath (au.csiro.pathling.fhirpath.NonLiteralPath)8 UntypedResourcePathBuilder (au.csiro.pathling.test.builders.UntypedResourcePathBuilder)8 ElementDefinition (au.csiro.pathling.fhirpath.element.ElementDefinition)5 StringLiteralPath (au.csiro.pathling.fhirpath.literal.StringLiteralPath)4