Search in sources :

Example 1 with RuntimeResourceDefinition

use of ca.uhn.fhir.context.RuntimeResourceDefinition in project cqf-ruler by DBCG.

the class Reflections method getAccessor.

/**
 * Gets the IAccessor for the given BaseType and child
 *
 * @param <BaseType>       an IBase type
 * @param theBaseTypeClass the class of a the IBase type
 * @param theChildName     the name of the child property of the
 *                         BaseType to generate an accessor for
 * @return an IAccessor for the given child and the BaseType
 */
public static <BaseType extends IBase> IAccessor getAccessor(final Class<? extends BaseType> theBaseTypeClass, String theChildName) {
    checkNotNull(theBaseTypeClass);
    checkNotNull(theChildName);
    FhirContext fhirContext = FhirContext.forCached(FhirVersions.forClass(theBaseTypeClass));
    if (theBaseTypeClass.isInstance(IBaseResource.class)) {
        @SuppressWarnings("unchecked") RuntimeResourceDefinition resourceDefinition = fhirContext.getResourceDefinition((Class<? extends IBaseResource>) theBaseTypeClass);
        return resourceDefinition.getChildByName(theChildName).getAccessor();
    } else {
        BaseRuntimeElementDefinition<?> elementDefinition = fhirContext.getElementDefinition(theBaseTypeClass);
        return elementDefinition.getChildByName(theChildName).getAccessor();
    }
}
Also used : FhirContext(ca.uhn.fhir.context.FhirContext) RuntimeResourceDefinition(ca.uhn.fhir.context.RuntimeResourceDefinition)

Example 2 with RuntimeResourceDefinition

use of ca.uhn.fhir.context.RuntimeResourceDefinition in project pathling by aehrc.

the class ManifestConverter method populateScope.

void populateScope(@Nonnull final PassportScope passportScope, @Nonnull final VisaManifest manifest) {
    // Create a filter for the Patient resource.
    final String patientIdCollection = manifest.getPatientIds().stream().map(id -> "'" + id + "'").collect(Collectors.joining(" combine "));
    final String patientIdFilter = "identifier.where(system = '" + StringLiteralPath.escapeFhirPathString(patientIdSystem) + "').where(value in (" + patientIdCollection + "))" + ".empty().not()";
    final Set<String> patientFilters = passportScope.get(ResourceType.PATIENT);
    if (patientFilters == null) {
        passportScope.put(ResourceType.PATIENT, new HashSet<>(List.of(patientIdFilter)));
    } else {
        patientFilters.add(patientIdFilter);
    }
    // See: https://www.hl7.org/fhir/r4/compartmentdefinition-patient.html
    for (final ResourceType resourceType : ResourceType.values()) {
        if (resourceType.equals(ResourceType.DOMAINRESOURCE) || resourceType.equals(ResourceType.RESOURCE) || resourceType.equals(ResourceType.NULL)) {
            continue;
        }
        final RuntimeResourceDefinition definition = fhirContext.getResourceDefinition(resourceType.toCode());
        final List<RuntimeSearchParam> searchParams = definition.getSearchParamsForCompartmentName("Patient");
        for (final RuntimeSearchParam searchParam : searchParams) {
            final String path = searchParam.getPath();
            // Remove the leading "[resource type]." from the path.
            final String pathTrimmed = path.replaceFirst("^" + resourceType.toCode() + "\\.", "");
            // Paths that end with this resolve pattern are polymorphic references, and will need
            // to be resolved using `ofType()` within our implementation.
            final String resolvePattern = ".where(resolve() is Patient)";
            final String filter;
            if (pathTrimmed.endsWith(resolvePattern)) {
                filter = pathTrimmed.replace(resolvePattern, ".resolve().ofType(Patient)." + patientIdFilter);
            } else {
                final Set<String> targets = searchParam.getTargets();
                if (targets.size() > 0 && !targets.contains("Patient")) {
                    // Patient, we need to skip it altogether.
                    continue;
                } else if (targets.size() == 1) {
                    // If the search parameter is monomorphic, we can resolve it without `ofType`.
                    filter = pathTrimmed + ".resolve()." + patientIdFilter;
                } else {
                    // If the search parameter is polymorphic, we also need to resolve it to Patient. Note
                    // that polymorphic references with an "Any" type have zero targets.
                    filter = pathTrimmed + ".resolve().ofType(Patient)." + patientIdFilter;
                }
            }
            // Add the filter to the map.
            final Set<String> filters = passportScope.get(resourceType);
            if (filters == null) {
                passportScope.put(resourceType, new HashSet<>(List.of(filter)));
            } else {
                filters.add(filter);
            }
        }
    }
}
Also used : RuntimeSearchParam(ca.uhn.fhir.context.RuntimeSearchParam) Configuration(au.csiro.pathling.Configuration) Set(java.util.Set) ResourceType(org.hl7.fhir.r4.model.Enumerations.ResourceType) Collectors(java.util.stream.Collectors) Profile(org.springframework.context.annotation.Profile) HashSet(java.util.HashSet) FhirContext(ca.uhn.fhir.context.FhirContext) List(java.util.List) Component(org.springframework.stereotype.Component) StringLiteralPath(au.csiro.pathling.fhirpath.literal.StringLiteralPath) RuntimeResourceDefinition(ca.uhn.fhir.context.RuntimeResourceDefinition) Nonnull(javax.annotation.Nonnull) RuntimeResourceDefinition(ca.uhn.fhir.context.RuntimeResourceDefinition) ResourceType(org.hl7.fhir.r4.model.Enumerations.ResourceType) RuntimeSearchParam(ca.uhn.fhir.context.RuntimeSearchParam)

Example 3 with RuntimeResourceDefinition

use of ca.uhn.fhir.context.RuntimeResourceDefinition in project pathling by aehrc.

the class FindRecursiveTypesApp method findRecursiveTypes.

private void findRecursiveTypes() {
    System.out.println(">>> Listing recursive types:");
    for (final String resourceType : fhirContext.getResourceTypes()) {
        final RuntimeResourceDefinition rd = fhirContext.getResourceDefinition(resourceType);
        recursive.clear();
        path.clear();
        traverseDefinition(rd, "", 0);
        if (!recursive.isEmpty()) {
            System.out.println(resourceType);
            recursive.forEach(s -> System.out.println("  " + s));
        }
    }
}
Also used : RuntimeResourceDefinition(ca.uhn.fhir.context.RuntimeResourceDefinition)

Example 4 with RuntimeResourceDefinition

use of ca.uhn.fhir.context.RuntimeResourceDefinition in project pathling by aehrc.

the class FhirEncoders method of.

/**
 * Returns an encoder for the given FHIR resource.
 *
 * @param type the type of the resource to encode.
 * @param <T> the type of the resource to be encoded.
 * @return an encoder for the resource.
 */
public final <T extends IBaseResource> ExpressionEncoder<T> of(final Class<T> type) {
    final RuntimeResourceDefinition definition = context.getResourceDefinition(type);
    final int key = type.getName().hashCode();
    synchronized (encoderCache) {
        // noinspection unchecked
        return (ExpressionEncoder<T>) encoderCache.computeIfAbsent(key, k -> EncoderBuilder.of(definition, context, mappings, maxNestingLevel, JavaConverters.asScalaSet(openTypes).toSet(), enableExtensions));
    }
}
Also used : ExpressionEncoder(org.apache.spark.sql.catalyst.encoders.ExpressionEncoder) FhirContext(ca.uhn.fhir.context.FhirContext) FhirVersionEnum(ca.uhn.fhir.context.FhirVersionEnum) IBaseResource(org.hl7.fhir.instance.model.api.IBaseResource) Map(java.util.Map) JavaConverters(scala.collection.JavaConverters) Set(java.util.Set) DataTypeMappings(au.csiro.pathling.encoders.datatypes.DataTypeMappings) HashMap(java.util.HashMap) RuntimeResourceDefinition(ca.uhn.fhir.context.RuntimeResourceDefinition) Collections(java.util.Collections) Value(lombok.Value) RuntimeResourceDefinition(ca.uhn.fhir.context.RuntimeResourceDefinition) ExpressionEncoder(org.apache.spark.sql.catalyst.encoders.ExpressionEncoder)

Example 5 with RuntimeResourceDefinition

use of ca.uhn.fhir.context.RuntimeResourceDefinition in project pathling by aehrc.

the class ResourcePath method build.

/**
 * Build a new ResourcePath using the supplied {@link FhirContext} and {@link Database}.
 *
 * @param fhirContext the {@link FhirContext} to use for sourcing the resource definition
 * @param database the {@link Database} to use for retrieving the Dataset
 * @param resourceType the type of the resource
 * @param expression the expression to use in the resulting path
 * @param singular whether the resulting path should be flagged as a single item collection
 * @param skipAliasing set to true to skip column aliasing
 * @return A shiny new ResourcePath
 */
@Nonnull
public static ResourcePath build(@Nonnull final FhirContext fhirContext, @Nonnull final Database database, @Nonnull final ResourceType resourceType, @Nonnull final String expression, final boolean singular, final boolean skipAliasing) {
    // Get the resource definition from HAPI.
    final String resourceCode = resourceType.toCode();
    final RuntimeResourceDefinition hapiDefinition = fhirContext.getResourceDefinition(resourceCode);
    final ResourceDefinition definition = new ResourceDefinition(resourceType, hapiDefinition);
    // Retrieve the dataset for the resource type using the supplied resource reader.
    final Dataset<Row> dataset = database.read(resourceType);
    final Column idColumn = col("id");
    final Column finalIdColumn;
    final Dataset<Row> finalDataset;
    final Map<String, Column> elementsToColumns;
    if (skipAliasing) {
        // If aliasing is disabled, the dataset will contain columns with the original element names.
        // This is used for contexts where we need the original column names for encoding (e.g.
        // search).
        finalDataset = dataset;
        finalIdColumn = idColumn;
        elementsToColumns = Stream.of(dataset.columns()).collect(Collectors.toMap(Function.identity(), functions::col, (a, b) -> null));
    } else {
        // If aliasing is enabled, all columns in the dataset will be aliased, and the original
        // columns will be dropped. This is to avoid column name clashes when doing joins.
        final DatasetWithColumnMap datasetWithColumnMap = aliasAllColumns(dataset);
        finalDataset = datasetWithColumnMap.getDataset();
        final Map<Column, Column> columnMap = datasetWithColumnMap.getColumnMap();
        elementsToColumns = columnMap.keySet().stream().collect(Collectors.toMap(Column::toString, columnMap::get, (a, b) -> null));
        finalIdColumn = elementsToColumns.get(idColumn.toString());
    }
    // We use the ID column as the value column for a ResourcePath.
    return new ResourcePath(expression, finalDataset, finalIdColumn, Optional.empty(), finalIdColumn, singular, Optional.empty(), definition, elementsToColumns);
}
Also used : org.apache.spark.sql.functions(org.apache.spark.sql.functions) DatasetWithColumnMap(au.csiro.pathling.QueryHelpers.DatasetWithColumnMap) RuntimeResourceDefinition(ca.uhn.fhir.context.RuntimeResourceDefinition) Column(org.apache.spark.sql.Column) RuntimeResourceDefinition(ca.uhn.fhir.context.RuntimeResourceDefinition) Row(org.apache.spark.sql.Row) Nonnull(javax.annotation.Nonnull)

Aggregations

RuntimeResourceDefinition (ca.uhn.fhir.context.RuntimeResourceDefinition)12 Nonnull (javax.annotation.Nonnull)4 FhirContext (ca.uhn.fhir.context.FhirContext)3 FhirVersionEnum (ca.uhn.fhir.context.FhirVersionEnum)3 HashMap (java.util.HashMap)3 ResourceDefinition (au.csiro.pathling.fhirpath.ResourceDefinition)2 Set (java.util.Set)2 Column (org.apache.spark.sql.Column)2 Test (org.junit.Test)2 Configuration (au.csiro.pathling.Configuration)1 DatasetWithColumn (au.csiro.pathling.QueryHelpers.DatasetWithColumn)1 DatasetWithColumnMap (au.csiro.pathling.QueryHelpers.DatasetWithColumnMap)1 QueryHelpers.createColumn (au.csiro.pathling.QueryHelpers.createColumn)1 DataTypeMappings (au.csiro.pathling.encoders.datatypes.DataTypeMappings)1 ResourcePath (au.csiro.pathling.fhirpath.ResourcePath)1 StringLiteralPath (au.csiro.pathling.fhirpath.literal.StringLiteralPath)1 RuntimeSearchParam (ca.uhn.fhir.context.RuntimeSearchParam)1 DefinitionToAvroVisitor (com.cerner.bunsen.avro.converters.DefinitionToAvroVisitor)1 HapiConverter (com.cerner.bunsen.definitions.HapiConverter)1 StructureDefinitions (com.cerner.bunsen.definitions.StructureDefinitions)1