Search in sources :

Example 1 with SimpleCodingsDecoders

use of au.csiro.pathling.fhirpath.encoding.SimpleCodingsDecoders in project pathling by aehrc.

the class SubsumesFunction method invoke.

@Nonnull
@Override
public FhirPath invoke(@Nonnull final NamedFunctionInput input) {
    validateInput(input);
    final NonLiteralPath inputFhirPath = input.getInput();
    final Dataset<Row> idAndCodingSet = createJoinedDataset(input.getInput(), input.getArguments().get(0));
    // Process the subsumption operation per partition, adding a result column to the dataset.
    final Column codingPairCol = struct(idAndCodingSet.col(COL_INPUT_CODINGS), idAndCodingSet.col(COL_ARG_CODINGS));
    @SuppressWarnings({ "OptionalGetWithoutIsPresent", "TypeMayBeWeakened" }) final SubsumptionMapperWithPreview mapper = new SubsumptionMapperWithPreview(MDC.get("requestId"), input.getContext().getTerminologyServiceFactory().get(), inverted);
    final Dataset<Row> resultDataset = SqlExtensions.mapWithPartitionPreview(idAndCodingSet, codingPairCol, SimpleCodingsDecoders::decodeListPair, mapper, StructField.apply("result", DataTypes.BooleanType, true, Metadata.empty()));
    final Column resultColumn = col("result");
    // Construct a new result expression.
    final String expression = expressionFromInput(input, functionName);
    return ElementPath.build(expression, resultDataset, inputFhirPath.getIdColumn(), inputFhirPath.getEidColumn(), resultColumn, inputFhirPath.isSingular(), inputFhirPath.getCurrentResource(), inputFhirPath.getThisColumn(), FHIRDefinedType.BOOLEAN);
}
Also used : Column(org.apache.spark.sql.Column) SimpleCodingsDecoders(au.csiro.pathling.fhirpath.encoding.SimpleCodingsDecoders) Row(org.apache.spark.sql.Row) NonLiteralPath(au.csiro.pathling.fhirpath.NonLiteralPath) Nonnull(javax.annotation.Nonnull)

Example 2 with SimpleCodingsDecoders

use of au.csiro.pathling.fhirpath.encoding.SimpleCodingsDecoders 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 3 with SimpleCodingsDecoders

use of au.csiro.pathling.fhirpath.encoding.SimpleCodingsDecoders 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

SimpleCodingsDecoders (au.csiro.pathling.fhirpath.encoding.SimpleCodingsDecoders)3 Nonnull (javax.annotation.Nonnull)3 Column (org.apache.spark.sql.Column)3 Row (org.apache.spark.sql.Row)3 TerminologyServiceFactory (au.csiro.pathling.fhir.TerminologyServiceFactory)2 ElementPath (au.csiro.pathling.fhirpath.element.ElementPath)2 ParserContext (au.csiro.pathling.fhirpath.parser.ParserContext)2 List (java.util.List)2 NonLiteralPath (au.csiro.pathling.fhirpath.NonLiteralPath)1 ElementDefinition (au.csiro.pathling.fhirpath.element.ElementDefinition)1 StringLiteralPath (au.csiro.pathling.fhirpath.literal.StringLiteralPath)1 ConceptTranslator (au.csiro.pathling.terminology.ConceptTranslator)1 Set (java.util.Set)1 MutablePair (org.apache.commons.lang3.tuple.MutablePair)1 ConceptMapEquivalence (org.hl7.fhir.r4.model.Enumerations.ConceptMapEquivalence)1