Search in sources :

Example 1 with UntypedResourcePath

use of au.csiro.pathling.fhirpath.UntypedResourcePath 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)

Example 2 with UntypedResourcePath

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

the class OfTypeFunctionTest method throwsErrorIfMoreThanOneArgument.

@Test
void throwsErrorIfMoreThanOneArgument() {
    final UntypedResourcePath input = new UntypedResourcePathBuilder(spark).expression("subject").build();
    final ResourcePath argument1 = new ResourcePathBuilder(spark).expression("Patient").build();
    final ResourcePath argument2 = new ResourcePathBuilder(spark).expression("Condition").build();
    final ParserContext parserContext = new ParserContextBuilder(spark, fhirContext).build();
    final NamedFunctionInput ofTypeInput = new NamedFunctionInput(parserContext, input, Arrays.asList(argument1, argument2));
    final NamedFunction ofTypeFunction = NamedFunction.getInstance("ofType");
    final InvalidUserInputError error = assertThrows(InvalidUserInputError.class, () -> ofTypeFunction.invoke(ofTypeInput));
    assertEquals("ofType function must have one argument: subject.ofType(Patient, Condition)", error.getMessage());
}
Also used : InvalidUserInputError(au.csiro.pathling.errors.InvalidUserInputError) ResourcePath(au.csiro.pathling.fhirpath.ResourcePath) UntypedResourcePath(au.csiro.pathling.fhirpath.UntypedResourcePath) UntypedResourcePathBuilder(au.csiro.pathling.test.builders.UntypedResourcePathBuilder) ParserContextBuilder(au.csiro.pathling.test.builders.ParserContextBuilder) UntypedResourcePathBuilder(au.csiro.pathling.test.builders.UntypedResourcePathBuilder) ResourcePathBuilder(au.csiro.pathling.test.builders.ResourcePathBuilder) 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)

Example 3 with UntypedResourcePath

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

the class ResolveFunctionTest method polymorphicResolve.

@Test
void polymorphicResolve() {
    final Optional<ElementDefinition> optionalDefinition = FhirHelpers.getChildOfResource(fhirContext, "Encounter", "subject");
    assertTrue(optionalDefinition.isPresent());
    final ElementDefinition definition = optionalDefinition.get();
    final Dataset<Row> referenceDataset = 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 ElementPath referencePath = new ElementPathBuilder(spark).expression("Encounter.subject").dataset(referenceDataset).idAndValueColumns().singular(true).definition(definition).buildDefined();
    final Dataset<Row> patientDataset = 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(patientDataset);
    final Dataset<Row> groupDataset = new ResourceDatasetBuilder(spark).withIdColumn().withColumn(DataTypes.StringType).withColumn(DataTypes.BooleanType).withRow("group-1", "Some group", true).build();
    when(database.read(ResourceType.GROUP)).thenReturn(groupDataset);
    final NamedFunctionInput resolveInput = buildFunctionInput(referencePath);
    final FhirPath result = invokeResolve(resolveInput);
    assertTrue(result instanceof UntypedResourcePath);
    assertThat((UntypedResourcePath) result).hasExpression("Encounter.subject.resolve()").isSingular();
    final Dataset<Row> expectedDataset = new DatasetBuilder(spark).withIdColumn().withTypeColumn().withStructTypeColumns(referenceStructType()).withRow("encounter-1", "Patient", RowFactory.create(null, "Patient/patient-1", null)).withRow("encounter-2", "Patient", RowFactory.create(null, "Patient/patient-3", null)).withRow("encounter-3", "Patient", RowFactory.create(null, "Patient/patient-2", null)).withRow("encounter-4", "Patient", RowFactory.create(null, "Patient/patient-2", null)).withRow("encounter-5", "Group", RowFactory.create(null, "Group/group-1", null)).buildWithStructValue();
    assertThat((UntypedResourcePath) result).selectUntypedResourceResult().hasRows(expectedDataset);
}
Also used : ElementPath(au.csiro.pathling.fhirpath.element.ElementPath) FhirPath(au.csiro.pathling.fhirpath.FhirPath) ResourceDatasetBuilder(au.csiro.pathling.test.builders.ResourceDatasetBuilder) ElementDefinition(au.csiro.pathling.fhirpath.element.ElementDefinition) Row(org.apache.spark.sql.Row) DatasetBuilder(au.csiro.pathling.test.builders.DatasetBuilder) ResourceDatasetBuilder(au.csiro.pathling.test.builders.ResourceDatasetBuilder) UntypedResourcePath(au.csiro.pathling.fhirpath.UntypedResourcePath) ElementPathBuilder(au.csiro.pathling.test.builders.ElementPathBuilder) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 4 with UntypedResourcePath

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

the class ResolveFunctionTest method polymorphicResolveAnyType.

@Test
void polymorphicResolveAnyType() {
    final Optional<ElementDefinition> optionalDefinition = FhirHelpers.getChildOfResource(fhirContext, "Condition", "evidence").flatMap(child -> child.getChildElement("detail"));
    assertTrue(optionalDefinition.isPresent());
    final ElementDefinition definition = optionalDefinition.get();
    TestHelpers.mockAllEmptyResources(database, spark, fhirEncoders);
    final Dataset<Row> referenceDataset = new DatasetBuilder(spark).withIdColumn().withEidColumn().withStructTypeColumns(referenceStructType()).withRow("condition-1", makeEid(0), RowFactory.create(null, "Observation/observation-1", null)).withRow("condition-2", makeEid(0), RowFactory.create(null, "ClinicalImpression/clinicalimpression-1", null)).buildWithStructValue();
    final ElementPath referencePath = new ElementPathBuilder(spark).expression("Condition.evidence.detail").dataset(referenceDataset).idAndEidAndValueColumns().singular(false).definition(definition).buildDefined();
    final Dataset<Row> observationDataset = new ResourceDatasetBuilder(spark).withIdColumn().withColumn(DataTypes.StringType).withRow("observation-1", "registered").build();
    when(database.read(ResourceType.OBSERVATION)).thenReturn(observationDataset);
    final Dataset<Row> clinicalImpressionDataset = new ResourceDatasetBuilder(spark).withIdColumn().withColumn(DataTypes.StringType).withRow("clinicalimpression-1", "in-progress").build();
    when(database.read(ResourceType.CLINICALIMPRESSION)).thenReturn(clinicalImpressionDataset);
    final NamedFunctionInput resolveInput = buildFunctionInput(referencePath);
    final FhirPath result = invokeResolve(resolveInput);
    assertTrue(result instanceof UntypedResourcePath);
    assertThat((UntypedResourcePath) result).hasExpression("Condition.evidence.detail.resolve()").isNotSingular();
    final Dataset<Row> expectedDataset = new DatasetBuilder(spark).withIdColumn().withTypeColumn().withStructTypeColumns(referenceStructType()).withRow("condition-1", "Observation", RowFactory.create(null, "Observation/observation-1", null)).withRow("condition-2", "ClinicalImpression", RowFactory.create(null, "ClinicalImpression/clinicalimpression-1", null)).buildWithStructValue();
    assertThat((UntypedResourcePath) result).selectUntypedResourceResult().hasRows(expectedDataset);
}
Also used : ElementPath(au.csiro.pathling.fhirpath.element.ElementPath) FhirPath(au.csiro.pathling.fhirpath.FhirPath) ResourceDatasetBuilder(au.csiro.pathling.test.builders.ResourceDatasetBuilder) ElementDefinition(au.csiro.pathling.fhirpath.element.ElementDefinition) Row(org.apache.spark.sql.Row) DatasetBuilder(au.csiro.pathling.test.builders.DatasetBuilder) ResourceDatasetBuilder(au.csiro.pathling.test.builders.ResourceDatasetBuilder) UntypedResourcePath(au.csiro.pathling.fhirpath.UntypedResourcePath) ElementPathBuilder(au.csiro.pathling.test.builders.ElementPathBuilder) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 5 with UntypedResourcePath

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

the class IifFunctionTest method throwsErrorWithUntypedResourceAndLiteralResults.

@Test
void throwsErrorWithUntypedResourceAndLiteralResults() {
    final NonLiteralPath condition = new ElementPathBuilder(spark).fhirType(FHIRDefinedType.BOOLEAN).expression("valueBoolean").singular(true).build().toThisPath();
    final UntypedResourcePath ifTrue = new UntypedResourcePathBuilder(spark).expression("someUntypedResource").build();
    final StringLiteralPath otherwise = StringLiteralPath.fromString("foo", condition);
    final NamedFunctionInput iifInput = new NamedFunctionInput(parserContext, condition, Arrays.asList(condition, ifTrue, otherwise));
    final NamedFunction notFunction = NamedFunction.getInstance("iif");
    final InvalidUserInputError error = assertThrows(InvalidUserInputError.class, () -> notFunction.invoke(iifInput));
    assertEquals("Paths cannot be merged into a collection together: someUntypedResource, 'foo'", error.getMessage());
}
Also used : InvalidUserInputError(au.csiro.pathling.errors.InvalidUserInputError) StringLiteralPath(au.csiro.pathling.fhirpath.literal.StringLiteralPath) UntypedResourcePathBuilder(au.csiro.pathling.test.builders.UntypedResourcePathBuilder) NonLiteralPath(au.csiro.pathling.fhirpath.NonLiteralPath) UntypedResourcePath(au.csiro.pathling.fhirpath.UntypedResourcePath) ElementPathBuilder(au.csiro.pathling.test.builders.ElementPathBuilder) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Aggregations

UntypedResourcePath (au.csiro.pathling.fhirpath.UntypedResourcePath)7 Test (org.junit.jupiter.api.Test)6 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)6 FhirPath (au.csiro.pathling.fhirpath.FhirPath)4 UntypedResourcePathBuilder (au.csiro.pathling.test.builders.UntypedResourcePathBuilder)4 Row (org.apache.spark.sql.Row)4 InvalidUserInputError (au.csiro.pathling.errors.InvalidUserInputError)3 ResourcePath (au.csiro.pathling.fhirpath.ResourcePath)3 ParserContext (au.csiro.pathling.fhirpath.parser.ParserContext)3 DatasetBuilder (au.csiro.pathling.test.builders.DatasetBuilder)3 ElementPathBuilder (au.csiro.pathling.test.builders.ElementPathBuilder)3 ParserContextBuilder (au.csiro.pathling.test.builders.ParserContextBuilder)3 ResourceDatasetBuilder (au.csiro.pathling.test.builders.ResourceDatasetBuilder)3 ElementDefinition (au.csiro.pathling.fhirpath.element.ElementDefinition)2 ElementPath (au.csiro.pathling.fhirpath.element.ElementPath)2 StringLiteralPath (au.csiro.pathling.fhirpath.literal.StringLiteralPath)2 ResourcePathBuilder (au.csiro.pathling.test.builders.ResourcePathBuilder)2 NonLiteralPath (au.csiro.pathling.fhirpath.NonLiteralPath)1 Nonnull (javax.annotation.Nonnull)1 Column (org.apache.spark.sql.Column)1