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);
}
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);
}
}
}
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);
}
Aggregations