Search in sources :

Example 1 with TerminologyServiceFactory

use of au.csiro.pathling.fhir.TerminologyServiceFactory in project pathling by aehrc.

the class TranslateFunction method invoke.

@Nonnull
@Override
public FhirPath invoke(@Nonnull final NamedFunctionInput input) {
    validateInput(input);
    final ElementPath inputPath = (ElementPath) input.getInput();
    final ParserContext inputContext = input.getContext();
    final Column idColumn = inputPath.getIdColumn();
    final Column conceptColumn = inputPath.getValueColumn();
    final boolean isCodeableConcept = isCodeableConcept(inputPath);
    final Column codingArrayCol = isCodeableConcept ? conceptColumn.getField("coding") : when(conceptColumn.isNotNull(), array(conceptColumn)).otherwise(lit(null));
    // The definition of the result is always the Coding element.
    @SuppressWarnings("OptionalGetWithoutIsPresent") final ElementDefinition resultDefinition = isCodeableConcept ? inputPath.getChildElement("coding").get() : inputPath.getDefinition().get();
    // Prepare the data which will be used within the map operation. All of these things must be
    // Serializable.
    @SuppressWarnings("OptionalGetWithoutIsPresent") final TerminologyServiceFactory terminologyServiceFactory = inputContext.getTerminologyServiceFactory().get();
    final Arguments arguments = Arguments.of(input);
    final String conceptMapUrl = arguments.getValue(0, String.class);
    final boolean reverse = arguments.getValueOr(1, DEFAULT_REVERSE);
    final String equivalence = arguments.getValueOr(2, DEFAULT_EQUIVALENCE);
    final Dataset<Row> dataset = inputPath.getDataset();
    final MapperWithPreview<List<SimpleCoding>, Row[], ConceptTranslator> mapper = new TranslateMapperWithPreview(MDC.get("requestId"), terminologyServiceFactory, conceptMapUrl, reverse, Strings.parseCsvList(equivalence, wrapInUserInputError(ConceptMapEquivalence::fromCode)));
    final Dataset<Row> translatedDataset = SqlExtensions.mapWithPartitionPreview(dataset, codingArrayCol, SimpleCodingsDecoders::decodeList, mapper, StructField.apply("result", DataTypes.createArrayType(CodingEncoding.DATA_TYPE), true, Metadata.empty()));
    // The result is an array of translations per each input element, which we now
    // need to explode in the same way as for path traversal, creating unique element ids.
    final MutablePair<Column, Column> valueAndEidColumns = new MutablePair<>();
    final Dataset<Row> resultDataset = inputPath.explodeArray(translatedDataset, translatedDataset.col("result"), valueAndEidColumns);
    // Construct a new result expression.
    final String expression = expressionFromInput(input, NAME);
    return ElementPath.build(expression, resultDataset, idColumn, Optional.of(valueAndEidColumns.getRight()), valueAndEidColumns.getLeft(), false, inputPath.getCurrentResource(), inputPath.getThisColumn(), resultDefinition);
}
Also used : TerminologyServiceFactory(au.csiro.pathling.fhir.TerminologyServiceFactory) MutablePair(org.apache.commons.lang3.tuple.MutablePair) ElementPath(au.csiro.pathling.fhirpath.element.ElementPath) Column(org.apache.spark.sql.Column) ConceptTranslator(au.csiro.pathling.terminology.ConceptTranslator) SimpleCodingsDecoders(au.csiro.pathling.fhirpath.encoding.SimpleCodingsDecoders) List(java.util.List) ConceptMapEquivalence(org.hl7.fhir.r4.model.Enumerations.ConceptMapEquivalence) ElementDefinition(au.csiro.pathling.fhirpath.element.ElementDefinition) Row(org.apache.spark.sql.Row) ParserContext(au.csiro.pathling.fhirpath.parser.ParserContext) Nonnull(javax.annotation.Nonnull)

Example 2 with TerminologyServiceFactory

use of au.csiro.pathling.fhir.TerminologyServiceFactory in project pathling by aehrc.

the class AggregateExecutorTest method runFirstGroupingThroughSearch.

/**
 * Test that the drill-down expression from the first grouping from each aggregate result can be
 * successfully executed using the FHIRPath search.
 */
@AfterEach
void runFirstGroupingThroughSearch() {
    if (response != null) {
        final Optional<Grouping> firstGroupingOptional = response.getGroupings().stream().filter(grouping -> grouping.getDrillDown().isPresent()).findFirst();
        if (firstGroupingOptional.isPresent()) {
            final Grouping firstGrouping = firstGroupingOptional.get();
            assertTrue(firstGrouping.getDrillDown().isPresent());
            final String drillDown = firstGrouping.getDrillDown().get();
            final StringAndListParam filters = new StringAndListParam();
            filters.addAnd(new StringParam(drillDown));
            final IBundleProvider searchExecutor = new SearchExecutor(configuration, fhirContext, spark, database, Optional.of(terminologyServiceFactory), fhirEncoders, subjectResource, Optional.of(filters));
            final List<IBaseResource> resources = searchExecutor.getResources(0, 100);
            assertTrue(resources.size() > 0);
        }
    }
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) IBundleProvider(ca.uhn.fhir.rest.api.server.IBundleProvider) TestHelpers(au.csiro.pathling.test.helpers.TestHelpers) Autowired(org.springframework.beans.factory.annotation.Autowired) ResourceType(org.hl7.fhir.r4.model.Enumerations.ResourceType) StringAndListParam(ca.uhn.fhir.rest.param.StringAndListParam) FhirContext(ca.uhn.fhir.context.FhirContext) Grouping(au.csiro.pathling.aggregate.AggregateResponse.Grouping) FhirEncoders(au.csiro.pathling.encoders.FhirEncoders) IBaseResource(org.hl7.fhir.instance.model.api.IBaseResource) TerminologyServiceFactory(au.csiro.pathling.fhir.TerminologyServiceFactory) Assertions.assertJson(au.csiro.pathling.test.assertions.Assertions.assertJson) Tag(org.junit.jupiter.api.Tag) Nonnull(javax.annotation.Nonnull) TerminologyService(au.csiro.pathling.terminology.TerminologyService) IParser(ca.uhn.fhir.parser.IParser) SparkSession(org.apache.spark.sql.SparkSession) Configuration(au.csiro.pathling.Configuration) List(java.util.List) AfterEach(org.junit.jupiter.api.AfterEach) SpringBootTest(org.springframework.boot.test.context.SpringBootTest) Database(au.csiro.pathling.io.Database) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) SharedMocks(au.csiro.pathling.test.SharedMocks) StringParam(ca.uhn.fhir.rest.param.StringParam) SearchExecutor(au.csiro.pathling.search.SearchExecutor) Optional(java.util.Optional) Parameters(org.hl7.fhir.r4.model.Parameters) Mockito.mock(org.mockito.Mockito.mock) StringAndListParam(ca.uhn.fhir.rest.param.StringAndListParam) Grouping(au.csiro.pathling.aggregate.AggregateResponse.Grouping) IBundleProvider(ca.uhn.fhir.rest.api.server.IBundleProvider) StringParam(ca.uhn.fhir.rest.param.StringParam) IBaseResource(org.hl7.fhir.instance.model.api.IBaseResource) SearchExecutor(au.csiro.pathling.search.SearchExecutor) AfterEach(org.junit.jupiter.api.AfterEach)

Example 3 with TerminologyServiceFactory

use of au.csiro.pathling.fhir.TerminologyServiceFactory in project pathling by aehrc.

the class MemberOfFunction method invoke.

@Nonnull
@Override
public FhirPath invoke(@Nonnull final NamedFunctionInput input) {
    validateInput(input);
    final ElementPath inputPath = (ElementPath) input.getInput();
    final StringLiteralPath argument = (StringLiteralPath) input.getArguments().get(0);
    final ParserContext inputContext = input.getContext();
    final Column idColumn = inputPath.getIdColumn();
    final Column conceptColumn = inputPath.getValueColumn();
    final Column codingArrayCol = (isCodeableConcept(inputPath)) ? conceptColumn.getField("coding") : when(conceptColumn.isNotNull(), array(conceptColumn)).otherwise(lit(null));
    // Prepare the data which will be used within the map operation. All of these things must be
    // Serializable.
    @SuppressWarnings("OptionalGetWithoutIsPresent") final TerminologyServiceFactory terminologyServiceFactory = inputContext.getTerminologyServiceFactory().get();
    final String valueSetUri = argument.getJavaValue();
    final Dataset<Row> dataset = inputPath.getDataset();
    // Perform a validate code operation on each Coding or CodeableConcept in the input dataset,
    // then create a new dataset with the boolean results.
    final MapperWithPreview<List<SimpleCoding>, Boolean, Set<SimpleCoding>> mapper = new MemberOfMapperWithPreview(MDC.get("requestId"), terminologyServiceFactory, valueSetUri);
    // This de-duplicates the Codings to be validated, then performs the validation on a
    // per-partition basis.
    final Dataset<Row> resultDataset = SqlExtensions.mapWithPartitionPreview(dataset, codingArrayCol, SimpleCodingsDecoders::decodeList, mapper, StructField.apply("result", DataTypes.BooleanType, true, Metadata.empty()));
    final Column resultColumn = col("result");
    // Construct a new result expression.
    final String expression = expressionFromInput(input, NAME);
    return ElementPath.build(expression, resultDataset, idColumn, inputPath.getEidColumn(), resultColumn, inputPath.isSingular(), inputPath.getCurrentResource(), inputPath.getThisColumn(), FHIRDefinedType.BOOLEAN);
}
Also used : StringLiteralPath(au.csiro.pathling.fhirpath.literal.StringLiteralPath) Set(java.util.Set) TerminologyServiceFactory(au.csiro.pathling.fhir.TerminologyServiceFactory) ElementPath(au.csiro.pathling.fhirpath.element.ElementPath) Column(org.apache.spark.sql.Column) SimpleCodingsDecoders(au.csiro.pathling.fhirpath.encoding.SimpleCodingsDecoders) List(java.util.List) Row(org.apache.spark.sql.Row) ParserContext(au.csiro.pathling.fhirpath.parser.ParserContext) Nonnull(javax.annotation.Nonnull)

Aggregations

TerminologyServiceFactory (au.csiro.pathling.fhir.TerminologyServiceFactory)3 List (java.util.List)3 Nonnull (javax.annotation.Nonnull)3 ElementPath (au.csiro.pathling.fhirpath.element.ElementPath)2 SimpleCodingsDecoders (au.csiro.pathling.fhirpath.encoding.SimpleCodingsDecoders)2 ParserContext (au.csiro.pathling.fhirpath.parser.ParserContext)2 Column (org.apache.spark.sql.Column)2 Row (org.apache.spark.sql.Row)2 Configuration (au.csiro.pathling.Configuration)1 Grouping (au.csiro.pathling.aggregate.AggregateResponse.Grouping)1 FhirEncoders (au.csiro.pathling.encoders.FhirEncoders)1 ElementDefinition (au.csiro.pathling.fhirpath.element.ElementDefinition)1 StringLiteralPath (au.csiro.pathling.fhirpath.literal.StringLiteralPath)1 Database (au.csiro.pathling.io.Database)1 SearchExecutor (au.csiro.pathling.search.SearchExecutor)1 ConceptTranslator (au.csiro.pathling.terminology.ConceptTranslator)1 TerminologyService (au.csiro.pathling.terminology.TerminologyService)1 SharedMocks (au.csiro.pathling.test.SharedMocks)1 Assertions.assertJson (au.csiro.pathling.test.assertions.Assertions.assertJson)1 TestHelpers (au.csiro.pathling.test.helpers.TestHelpers)1