Search in sources :

Example 16 with FhirPath

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

the class ExtensionFunctionTest method testExtensionOnElements.

@Test
public void testExtensionOnElements() {
    final ParserContext parserContext = new ParserContextBuilder(spark, fhirContext).build();
    // Construct element dataset from the resource dataset so that the resource path
    // can be used as the current resource for this element path
    // Note: this resource path is not singular as this will be a base for elements.
    final Dataset<Row> resourceLikeDataset = new ResourceDatasetBuilder(spark).withIdColumn().withEidColumn().withStructColumn("name", DataTypes.StringType).withStructColumn("_fid", DataTypes.IntegerType).withStructValueColumn().withExtensionColumn().withRow("observation-1", makeEid(0), RowFactory.create("name1", 0), oneEntryMap(0, MANY_MY_EXTENSIONS)).withRow("observation-2", makeEid(0), RowFactory.create("name2", 1), oneEntryMap(1, ONE_MY_EXTENSION)).withRow("observation-3", makeEid(0), RowFactory.create("name3", 2), oneEntryMap(2, NO_MY_EXTENSIONS)).withRow("observation-4", makeEid(0), RowFactory.create("name4", 3), oneEntryMap(3, ONE_MY_EXTENSION)).withRow("observation-4", makeEid(1), RowFactory.create("name5", 4), oneEntryMap(3, ONE_MY_EXTENSION)).withRow("observation-5", makeEid(0), null, null).withRow("observation-5", makeEid(1), null, null).build();
    when(database.read(ResourceType.OBSERVATION)).thenReturn(resourceLikeDataset);
    final ResourcePath baseResourcePath = ResourcePath.build(fhirContext, database, ResourceType.OBSERVATION, "Observation", false);
    final Dataset<Row> elementDataset = toElementDataset(resourceLikeDataset, baseResourcePath);
    final ElementDefinition codeDefinition = checkPresent(FhirHelpers.getChildOfResource(fhirContext, "Observation", "code"));
    final ElementPath inputPath = new ElementPathBuilder(spark).fhirType(FHIRDefinedType.CODEABLECONCEPT).definition(codeDefinition).dataset(elementDataset).idAndEidAndValueColumns().expression("code").singular(false).currentResource(baseResourcePath).buildDefined();
    final StringLiteralPath argumentExpression = StringLiteralPath.fromString("'" + "uuid:myExtension" + "'", inputPath);
    final NamedFunctionInput extensionInput = new NamedFunctionInput(parserContext, inputPath, Collections.singletonList(argumentExpression));
    final NamedFunction extension = NamedFunction.getInstance("extension");
    final FhirPath result = extension.invoke(extensionInput);
    final Dataset<Row> expectedResult = new DatasetBuilder(spark).withIdColumn().withEidColumn().withStructTypeColumns(DatasetBuilder.SIMPLE_EXTENSION_TYPE).withRow("observation-1", makeEid(0, 0), null).withRow("observation-1", makeEid(0, 1), null).withRow("observation-1", makeEid(0, 2), MANY_EXT_ROW_1).withRow("observation-1", makeEid(0, 3), MANY_EXT_ROW_2).withRow("observation-2", makeEid(0, 0), null).withRow("observation-2", makeEid(0, 1), ONE_EXT_ROW_1).withRow("observation-3", makeEid(0, 0), null).withRow("observation-3", makeEid(0, 1), null).withRow("observation-4", makeEid(0, 0), null).withRow("observation-4", makeEid(0, 1), ONE_EXT_ROW_1).withRow("observation-4", makeEid(1, 0), null).withRow("observation-5", makeEid(0, 0), null).withRow("observation-5", makeEid(1, 0), null).buildWithStructValue();
    assertThat(result).hasExpression("code.extension('uuid:myExtension')").isNotSingular().isElementPath(ElementPath.class).hasFhirType(FHIRDefinedType.EXTENSION).selectOrderedResultWithEid().hasRows(expectedResult);
}
Also used : StringLiteralPath(au.csiro.pathling.fhirpath.literal.StringLiteralPath) FhirPath(au.csiro.pathling.fhirpath.FhirPath) ParserContextBuilder(au.csiro.pathling.test.builders.ParserContextBuilder) ResourcePath(au.csiro.pathling.fhirpath.ResourcePath) ElementPath(au.csiro.pathling.fhirpath.element.ElementPath) ResourceDatasetBuilder(au.csiro.pathling.test.builders.ResourceDatasetBuilder) Row(org.apache.spark.sql.Row) ElementDefinition(au.csiro.pathling.fhirpath.element.ElementDefinition) DatasetBuilder(au.csiro.pathling.test.builders.DatasetBuilder) ResourceDatasetBuilder(au.csiro.pathling.test.builders.ResourceDatasetBuilder) 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 FhirPath

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

the class FirstFunctionTest method firstOfRootResources.

@Test
void firstOfRootResources() {
    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).build();
    when(database.read(ResourceType.PATIENT)).thenReturn(patientDataset);
    final ResourcePath inputPath = ResourcePath.build(fhirContext, database, ResourceType.PATIENT, "Patient", true);
    final ParserContext parserContext = new ParserContextBuilder(spark, fhirContext).groupingColumns(Collections.singletonList(inputPath.getIdColumn())).build();
    final NamedFunctionInput firstInput = new NamedFunctionInput(parserContext, inputPath, Collections.emptyList());
    final NamedFunction firstFunction = NamedFunction.getInstance("first");
    final FhirPath result = firstFunction.invoke(firstInput);
    assertTrue(result instanceof ResourcePath);
    assertThat((ResourcePath) result).hasExpression("Patient.first()").isSingular().hasResourceType(ResourceType.PATIENT);
    final Dataset<Row> expectedDataset = new DatasetBuilder(spark).withIdColumn().withColumn(DataTypes.StringType).withRow("patient-1", "patient-1").withRow("patient-2", "patient-2").withRow("patient-3", "patient-3").build();
    assertThat(result).selectOrderedResult().hasRows(expectedDataset);
}
Also used : ResourcePath(au.csiro.pathling.fhirpath.ResourcePath) FhirPath(au.csiro.pathling.fhirpath.FhirPath) ResourceDatasetBuilder(au.csiro.pathling.test.builders.ResourceDatasetBuilder) ParserContextBuilder(au.csiro.pathling.test.builders.ParserContextBuilder) Row(org.apache.spark.sql.Row) DatasetBuilder(au.csiro.pathling.test.builders.DatasetBuilder) ResourceDatasetBuilder(au.csiro.pathling.test.builders.ResourceDatasetBuilder) ParserContext(au.csiro.pathling.fhirpath.parser.ParserContext) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 18 with FhirPath

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

the class FirstFunctionTest method firstOfUngroupedSubResources.

@Test
void firstOfUngroupedSubResources() {
    final String subresourceId = randomAlias();
    final String statusColumn = randomAlias();
    final Dataset<Row> inputDataset = new DatasetBuilder(spark).withIdColumn().withEidColumn().withColumn(subresourceId, DataTypes.StringType).withColumn(statusColumn, DataTypes.StringType).withRow("patient-1", makeEid(2), "Encounter/5", "in-progress").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", null, null, null).build();
    final ResourcePath inputPath = new ResourcePathBuilder(spark).expression("reverseResolve(Encounter.subject)").dataset(inputDataset).idEidAndValueColumns().valueColumn(inputDataset.col(subresourceId)).resourceType(ResourceType.ENCOUNTER).buildCustom();
    final ParserContext parserContext = new ParserContextBuilder(spark, fhirContext).groupingColumns(Collections.singletonList(inputPath.getIdColumn())).build();
    final NamedFunctionInput firstInput = new NamedFunctionInput(parserContext, inputPath, Collections.emptyList());
    final NamedFunction firstFunction = NamedFunction.getInstance("first");
    final FhirPath result = firstFunction.invoke(firstInput);
    final Dataset<Row> expectedDataset = new DatasetBuilder(spark).withIdColumn().withEidColumn().withIdColumn().withRow("patient-1", null, "Encounter/2").withRow("patient-2", null, "Encounter/3").withRow("patient-3", null, null).build();
    assertThat(result).isResourcePath().hasExpression("reverseResolve(Encounter.subject).first()").isSingular().hasResourceType(ResourceType.ENCOUNTER).selectOrderedResultWithEid().hasRows(expectedDataset);
}
Also used : ResourcePath(au.csiro.pathling.fhirpath.ResourcePath) FhirPath(au.csiro.pathling.fhirpath.FhirPath) ParserContextBuilder(au.csiro.pathling.test.builders.ParserContextBuilder) ResourcePathBuilder(au.csiro.pathling.test.builders.ResourcePathBuilder) Row(org.apache.spark.sql.Row) DatasetBuilder(au.csiro.pathling.test.builders.DatasetBuilder) ResourceDatasetBuilder(au.csiro.pathling.test.builders.ResourceDatasetBuilder) ParserContext(au.csiro.pathling.fhirpath.parser.ParserContext) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 19 with FhirPath

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

the class IifFunctionTest method returnsCorrectResultsForLiteralAndNonLiteral.

@Test
void returnsCorrectResultsForLiteralAndNonLiteral() {
    final Dataset<Row> inputContextDataset = new ResourceDatasetBuilder(spark).withIdColumn().withRow("observation-1").withRow("observation-2").withRow("observation-3").withRow("observation-4").withRow("observation-5").build();
    when(database.read(ResourceType.OBSERVATION)).thenReturn(inputContextDataset);
    final ResourcePath inputContext = new ResourcePathBuilder(spark).expression("Observation").resourceType(ResourceType.OBSERVATION).database(database).singular(true).build();
    final Dataset<Row> inputDataset = new DatasetBuilder(spark).withIdColumn(ID_ALIAS).withEidColumn().withColumn(DataTypes.BooleanType).withRow("observation-1", makeEid(0), false).withRow("observation-2", makeEid(0), true).withRow("observation-3", makeEid(0), null).withRow("observation-4", makeEid(0), true).withRow("observation-4", makeEid(1), false).withRow("observation-5", makeEid(0), null).withRow("observation-5", makeEid(1), null).build();
    final ElementPath inputPath = new ElementPathBuilder(spark).fhirType(FHIRDefinedType.BOOLEAN).dataset(inputDataset).idAndEidAndValueColumns().expression("valueBoolean").singular(false).build();
    parserContext = new ParserContextBuilder(spark, fhirContext).groupingColumns(Collections.singletonList(inputPath.getIdColumn())).build();
    final NonLiteralPath condition = inputPath.toThisPath();
    final Dataset<Row> ifTrueDataset = new DatasetBuilder(spark).withIdColumn(ID_ALIAS).withEidColumn().withColumn(DataTypes.IntegerType).withRow("observation-1", makeEid(0), 1).withRow("observation-2", makeEid(0), 2).withRow("observation-3", makeEid(0), 3).withRow("observation-4", makeEid(0), 4).withRow("observation-5", makeEid(0), 5).build();
    final ElementPath ifTrue = new ElementPathBuilder(spark).fhirType(FHIRDefinedType.INTEGER).dataset(ifTrueDataset).idAndEidAndValueColumns().expression("someInteger").singular(true).build();
    final IntegerLiteralPath otherwise = IntegerLiteralPath.fromString("99", inputContext);
    final NamedFunctionInput iifInput1 = new NamedFunctionInput(parserContext, inputPath, Arrays.asList(condition, ifTrue, otherwise));
    final FhirPath result1 = NamedFunction.getInstance("iif").invoke(iifInput1);
    final Dataset<Row> expectedDataset1 = new DatasetBuilder(spark).withIdColumn(ID_ALIAS).withEidColumn().withColumn(DataTypes.IntegerType).withRow("observation-1", makeEid(0), 99).withRow("observation-2", makeEid(0), 2).withRow("observation-3", makeEid(0), 99).withRow("observation-4", makeEid(0), 4).withRow("observation-4", makeEid(1), 99).withRow("observation-5", makeEid(0), 99).withRow("observation-5", makeEid(1), 99).build();
    assertThat(result1).hasExpression("valueBoolean.iif($this, someInteger, 99)").isNotSingular().isElementPath(IntegerPath.class).selectOrderedResultWithEid().hasRows(expectedDataset1);
    final NamedFunctionInput iifInput2 = new NamedFunctionInput(parserContext, inputPath, Arrays.asList(condition, otherwise, ifTrue));
    final FhirPath result2 = NamedFunction.getInstance("iif").invoke(iifInput2);
    final Dataset<Row> expectedDataset2 = new DatasetBuilder(spark).withIdColumn(ID_ALIAS).withEidColumn().withColumn(DataTypes.IntegerType).withRow("observation-1", makeEid(0), 1).withRow("observation-2", makeEid(0), 99).withRow("observation-3", makeEid(0), 3).withRow("observation-4", makeEid(0), 99).withRow("observation-4", makeEid(1), 4).withRow("observation-5", makeEid(0), 5).withRow("observation-5", makeEid(1), 5).build();
    assertThat(result2).hasExpression("valueBoolean.iif($this, 99, someInteger)").isNotSingular().isElementPath(IntegerPath.class).selectOrderedResultWithEid().hasRows(expectedDataset2);
}
Also used : FhirPath(au.csiro.pathling.fhirpath.FhirPath) ParserContextBuilder(au.csiro.pathling.test.builders.ParserContextBuilder) UntypedResourcePathBuilder(au.csiro.pathling.test.builders.UntypedResourcePathBuilder) ResourcePathBuilder(au.csiro.pathling.test.builders.ResourcePathBuilder) ResourcePath(au.csiro.pathling.fhirpath.ResourcePath) UntypedResourcePath(au.csiro.pathling.fhirpath.UntypedResourcePath) ElementPath(au.csiro.pathling.fhirpath.element.ElementPath) ResourceDatasetBuilder(au.csiro.pathling.test.builders.ResourceDatasetBuilder) Row(org.apache.spark.sql.Row) DatasetBuilder(au.csiro.pathling.test.builders.DatasetBuilder) ResourceDatasetBuilder(au.csiro.pathling.test.builders.ResourceDatasetBuilder) NonLiteralPath(au.csiro.pathling.fhirpath.NonLiteralPath) IntegerLiteralPath(au.csiro.pathling.fhirpath.literal.IntegerLiteralPath) ElementPathBuilder(au.csiro.pathling.test.builders.ElementPathBuilder) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 20 with FhirPath

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

the class OfTypeFunctionTest method resolvesPolymorphicReference.

@Test
void resolvesPolymorphicReference() {
    final Dataset<Row> inputDataset = new DatasetBuilder(spark).withIdColumn().withEidColumn().withTypeColumn().withStructTypeColumns(referenceStructType()).withRow("encounter-1", makeEid(1), "Patient", RowFactory.create(null, "Patient/patient-1", null)).withRow("encounter-1", makeEid(0), "Patient", RowFactory.create(null, "Patient/patient-2", null)).withRow("encounter-2", makeEid(0), "Patient", RowFactory.create(null, "Patient/patient-3", null)).withRow("encounter-2", makeEid(1), "Group", RowFactory.create(null, "Group/group-1", null)).withRow("encounter-3", makeEid(0), "Patient", RowFactory.create(null, "Patient/patient-2", null)).withRow("encounter-4", makeEid(0), "Patient", RowFactory.create(null, "Patient/patient-2", null)).withRow("encounter-5", makeEid(0), "Group", RowFactory.create(null, "Group/group-1", null)).withRow("encounter-6", null, null, null).buildWithStructValue();
    final UntypedResourcePath inputPath = new UntypedResourcePathBuilder(spark).expression("subject.resolve()").dataset(inputDataset).idEidTypeAndValueColumns().singular(false).build();
    final Dataset<Row> argumentDataset = new ResourceDatasetBuilder(spark).withIdColumn().withColumn(DataTypes.StringType).withColumn(DataTypes.BooleanType).withRow("patient-1", "female", true).withRow("patient-2", "female", false).withRow("patient-3", "male", true).build();
    when(database.read(ResourceType.PATIENT)).thenReturn(argumentDataset);
    final ResourcePath argumentPath = new ResourcePathBuilder(spark).database(database).resourceType(ResourceType.PATIENT).expression("Patient").build();
    final ParserContext parserContext = new ParserContextBuilder(spark, fhirContext).idColumn(inputPath.getIdColumn()).build();
    final NamedFunctionInput ofTypeInput = new NamedFunctionInput(parserContext, inputPath, Collections.singletonList(argumentPath));
    final NamedFunction ofType = NamedFunction.getInstance("ofType");
    final FhirPath result = ofType.invoke(ofTypeInput);
    assertTrue(result instanceof ResourcePath);
    assertThat((ResourcePath) result).hasExpression("subject.resolve().ofType(Patient)").isNotSingular().hasResourceType(ResourceType.PATIENT);
    final Dataset<Row> expectedDataset = new DatasetBuilder(spark).withIdColumn().withEidColumn().withIdColumn().withRow("encounter-1", makeEid(0), "patient-2").withRow("encounter-1", makeEid(1), "patient-1").withRow("encounter-2", makeEid(0), "patient-3").withRow("encounter-2", makeEid(1), null).withRow("encounter-3", makeEid(0), "patient-2").withRow("encounter-4", makeEid(0), "patient-2").withRow("encounter-5", makeEid(0), null).withRow("encounter-6", null, null).build();
    assertThat(result).selectOrderedResultWithEid().hasRows(expectedDataset);
}
Also used : FhirPath(au.csiro.pathling.fhirpath.FhirPath) ParserContextBuilder(au.csiro.pathling.test.builders.ParserContextBuilder) UntypedResourcePathBuilder(au.csiro.pathling.test.builders.UntypedResourcePathBuilder) ResourcePathBuilder(au.csiro.pathling.test.builders.ResourcePathBuilder) ResourcePath(au.csiro.pathling.fhirpath.ResourcePath) UntypedResourcePath(au.csiro.pathling.fhirpath.UntypedResourcePath) UntypedResourcePathBuilder(au.csiro.pathling.test.builders.UntypedResourcePathBuilder) ResourceDatasetBuilder(au.csiro.pathling.test.builders.ResourceDatasetBuilder) Row(org.apache.spark.sql.Row) DatasetBuilder(au.csiro.pathling.test.builders.DatasetBuilder) ResourceDatasetBuilder(au.csiro.pathling.test.builders.ResourceDatasetBuilder) ParserContext(au.csiro.pathling.fhirpath.parser.ParserContext) UntypedResourcePath(au.csiro.pathling.fhirpath.UntypedResourcePath) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

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