Search in sources :

Example 11 with FhirPath

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

the class EmptyFunction method invoke.

@Nonnull
@Override
public FhirPath invoke(@Nonnull final NamedFunctionInput input) {
    checkNoArguments(NAME, input);
    final NonLiteralPath inputPath = input.getInput();
    final String expression = expressionFromInput(input, NAME);
    // We use the count function to determine whether there are zero items in the input collection.
    final FhirPath countResult = new CountFunction().invoke(input);
    final Dataset<Row> dataset = countResult.getDataset();
    final Column valueColumn = countResult.getValueColumn().equalTo(0);
    return ElementPath.build(expression, dataset, inputPath.getIdColumn(), Optional.empty(), valueColumn, true, Optional.empty(), inputPath.getThisColumn(), FHIRDefinedType.BOOLEAN);
}
Also used : FhirPath(au.csiro.pathling.fhirpath.FhirPath) Column(org.apache.spark.sql.Column) Row(org.apache.spark.sql.Row) NonLiteralPath(au.csiro.pathling.fhirpath.NonLiteralPath) Nonnull(javax.annotation.Nonnull)

Example 12 with FhirPath

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

the class ExtensionFunction method invoke.

@Nonnull
@Override
public FhirPath invoke(@Nonnull final NamedFunctionInput input) {
    final String expression = NamedFunction.expressionFromInput(input, NAME);
    checkUserInput(input.getArguments().size() == 1, "extension function must have one argument: " + expression);
    final FhirPath urlArgument = input.getArguments().get(0);
    checkUserInput(urlArgument instanceof StringLiteralPath, "extension function must have argument of type String literal: " + expression);
    final NonLiteralPath inputPath = input.getInput();
    final ElementPath extensionPath = new PathTraversalOperator().invoke(new PathTraversalInput(input.getContext(), inputPath, ExtensionSupport.EXTENSION_ELEMENT_NAME()));
    // Now we need to create a correct argument context for the `where` call.
    final ParserContext argumentContext = input.getContext();
    final FhirPath extensionUrlPath = new PathTraversalOperator().invoke(new PathTraversalInput(argumentContext, extensionPath.toThisPath(), "url"));
    final FhirPath extensionUrCondition = new ComparisonOperator(ComparisonOperation.EQUALS).invoke(new OperatorInput(argumentContext, extensionUrlPath, urlArgument));
    // Override the expression in the function input.
    return new WhereFunction().invoke(new NamedFunctionInput(input.getContext(), extensionPath, Collections.singletonList(extensionUrCondition), expression));
}
Also used : PathTraversalInput(au.csiro.pathling.fhirpath.operator.PathTraversalInput) ComparisonOperator(au.csiro.pathling.fhirpath.operator.ComparisonOperator) FhirPath(au.csiro.pathling.fhirpath.FhirPath) StringLiteralPath(au.csiro.pathling.fhirpath.literal.StringLiteralPath) ElementPath(au.csiro.pathling.fhirpath.element.ElementPath) OperatorInput(au.csiro.pathling.fhirpath.operator.OperatorInput) PathTraversalOperator(au.csiro.pathling.fhirpath.operator.PathTraversalOperator) ParserContext(au.csiro.pathling.fhirpath.parser.ParserContext) NonLiteralPath(au.csiro.pathling.fhirpath.NonLiteralPath) Nonnull(javax.annotation.Nonnull)

Example 13 with FhirPath

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

the class CountFunctionTest method doesNotCountNullElements.

@Test
void doesNotCountNullElements() {
    final Dataset<Row> dataset = new DatasetBuilder(spark).withIdColumn().withColumn("gender", DataTypes.StringType).withRow("patient-1", "female").withRow("patient-2", null).withRow("patient-3", "male").build();
    final ElementPath inputPath = new ElementPathBuilder(spark).expression("gender").fhirType(FHIRDefinedType.CODE).dataset(dataset).idAndValueColumns().build();
    final ParserContext parserContext = new ParserContextBuilder(spark, fhirContext).idColumn(inputPath.getIdColumn()).groupingColumns(Collections.emptyList()).build();
    final NamedFunctionInput countInput = new NamedFunctionInput(parserContext, inputPath, Collections.emptyList());
    final NamedFunction count = NamedFunction.getInstance("count");
    final FhirPath result = count.invoke(countInput);
    final Dataset<Row> expectedDataset = new DatasetBuilder(spark).withIdColumn().withColumn(DataTypes.LongType).withRow("patient-1", 2L).build();
    assertThat(result).hasExpression("gender.count()").isSingular().isElementPath(IntegerPath.class).hasFhirType(FHIRDefinedType.UNSIGNEDINT).selectOrderedResult().hasRows(expectedDataset);
}
Also used : ElementPath(au.csiro.pathling.fhirpath.element.ElementPath) FhirPath(au.csiro.pathling.fhirpath.FhirPath) IntegerPath(au.csiro.pathling.fhirpath.element.IntegerPath) 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) ElementPathBuilder(au.csiro.pathling.test.builders.ElementPathBuilder) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 14 with FhirPath

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

the class CountFunctionTest method countsByResourceIdentity.

@Test
void countsByResourceIdentity() {
    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", false);
    final ParserContext parserContext = new ParserContextBuilder(spark, fhirContext).idColumn(inputPath.getIdColumn()).groupingColumns(Collections.singletonList(inputPath.getIdColumn())).inputExpression("Patient").build();
    final NamedFunctionInput countInput = new NamedFunctionInput(parserContext, inputPath, Collections.emptyList());
    final NamedFunction count = NamedFunction.getInstance("count");
    final FhirPath result = count.invoke(countInput);
    final Dataset<Row> expectedDataset = new DatasetBuilder(spark).withIdColumn().withColumn(DataTypes.LongType).withRow("patient-1", 1L).withRow("patient-2", 1L).withRow("patient-3", 1L).build();
    assertThat(result).hasExpression("count()").isSingular().isElementPath(IntegerPath.class).hasFhirType(FHIRDefinedType.UNSIGNEDINT).selectOrderedResult().hasRows(expectedDataset);
}
Also used : ResourcePath(au.csiro.pathling.fhirpath.ResourcePath) FhirPath(au.csiro.pathling.fhirpath.FhirPath) ResourceDatasetBuilder(au.csiro.pathling.test.builders.ResourceDatasetBuilder) IntegerPath(au.csiro.pathling.fhirpath.element.IntegerPath) 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 15 with FhirPath

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

the class CountFunctionTest method countsByGrouping.

@Test
void countsByGrouping() {
    final Dataset<Row> inputDataset = new ResourceDatasetBuilder(spark).withIdColumn().withColumn("gender", DataTypes.StringType).withColumn("active", DataTypes.BooleanType).withRow("patient-1", "female", true).withRow("patient-2", "female", false).withRow("patient-2", "male", true).build();
    when(database.read(ResourceType.PATIENT)).thenReturn(inputDataset);
    final ResourcePath inputPath = new ResourcePathBuilder(spark).database(database).resourceType(ResourceType.PATIENT).expression("Patient").build();
    final Column groupingColumn = inputPath.getElementColumn("gender");
    final ParserContext parserContext = new ParserContextBuilder(spark, fhirContext).groupingColumns(Collections.singletonList(groupingColumn)).inputExpression("Patient").build();
    final NamedFunctionInput countInput = new NamedFunctionInput(parserContext, inputPath, Collections.emptyList());
    final NamedFunction count = NamedFunction.getInstance("count");
    final FhirPath result = count.invoke(countInput);
    final Dataset<Row> expectedDataset = new DatasetBuilder(spark).withColumn(DataTypes.StringType).withColumn(DataTypes.LongType).withRow("female", 2L).withRow("male", 1L).build();
    assertThat(result).hasExpression("count()").isSingular().isElementPath(IntegerPath.class).hasFhirType(FHIRDefinedType.UNSIGNEDINT).selectGroupingResult(Collections.singletonList(groupingColumn)).hasRows(expectedDataset);
}
Also used : FhirPath(au.csiro.pathling.fhirpath.FhirPath) IntegerPath(au.csiro.pathling.fhirpath.element.IntegerPath) ParserContextBuilder(au.csiro.pathling.test.builders.ParserContextBuilder) ResourcePathBuilder(au.csiro.pathling.test.builders.ResourcePathBuilder) ResourcePath(au.csiro.pathling.fhirpath.ResourcePath) ResourceDatasetBuilder(au.csiro.pathling.test.builders.ResourceDatasetBuilder) Column(org.apache.spark.sql.Column) 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)

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