Search in sources :

Example 1 with BaseRuntimeChildDefinition

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

the class ElementDefinition method getChildElement.

/**
 * Returns the child element of this element with the specified name.
 *
 * @param name The name of the child element
 * @return A new ElementDefinition describing the child
 */
@Nonnull
public Optional<ElementDefinition> getChildElement(@Nonnull final String name) {
    if (elementDefinition.isPresent() && elementDefinition.get() instanceof BaseRuntimeElementCompositeDefinition) {
        final BaseRuntimeElementCompositeDefinition compositeDefinition = (BaseRuntimeElementCompositeDefinition) elementDefinition.get();
        final BaseRuntimeChildDefinition newChild = compositeDefinition.getChildByName(name);
        if (newChild == null) {
            return Optional.empty();
        }
        return Optional.of(ElementDefinition.build(newChild, name));
    } else {
        return Optional.empty();
    }
}
Also used : BaseRuntimeElementCompositeDefinition(ca.uhn.fhir.context.BaseRuntimeElementCompositeDefinition) BaseRuntimeChildDefinition(ca.uhn.fhir.context.BaseRuntimeChildDefinition) Nonnull(javax.annotation.Nonnull)

Example 2 with BaseRuntimeChildDefinition

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

the class ReverseResolveFunctionTest method throwsErrorIfArgumentTypeDoesNotMatchInput.

@Test
void throwsErrorIfArgumentTypeDoesNotMatchInput() {
    final ResourcePath input = new ResourcePathBuilder(spark).resourceType(ResourceType.PATIENT).expression("Patient").build();
    final BaseRuntimeChildDefinition childDefinition = fhirContext.getResourceDefinition("Encounter").getChildByName("episodeOfCare");
    final ElementDefinition definition = ElementDefinition.build(childDefinition, "episodeOfCare");
    final ElementPath argument = new ElementPathBuilder(spark).expression("Encounter.episodeOfCare").fhirType(FHIRDefinedType.REFERENCE).definition(definition).buildDefined();
    final ParserContext parserContext = new ParserContextBuilder(spark, fhirContext).inputExpression("Patient").build();
    final NamedFunctionInput reverseResolveInput = new NamedFunctionInput(parserContext, input, Collections.singletonList(argument));
    final NamedFunction reverseResolveFunction = NamedFunction.getInstance("reverseResolve");
    final InvalidUserInputError error = assertThrows(InvalidUserInputError.class, () -> reverseResolveFunction.invoke(reverseResolveInput));
    assertEquals("Reference in argument to reverseResolve does not support input resource type: reverseResolve(Encounter.episodeOfCare)", error.getMessage());
}
Also used : InvalidUserInputError(au.csiro.pathling.errors.InvalidUserInputError) ResourcePath(au.csiro.pathling.fhirpath.ResourcePath) ElementPath(au.csiro.pathling.fhirpath.element.ElementPath) ParserContextBuilder(au.csiro.pathling.test.builders.ParserContextBuilder) ResourcePathBuilder(au.csiro.pathling.test.builders.ResourcePathBuilder) BaseRuntimeChildDefinition(ca.uhn.fhir.context.BaseRuntimeChildDefinition) ElementDefinition(au.csiro.pathling.fhirpath.element.ElementDefinition) 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 3 with BaseRuntimeChildDefinition

use of ca.uhn.fhir.context.BaseRuntimeChildDefinition in project bunsen by cerner.

the class HapiCompositeConverter method toHapiConverter.

@Override
public HapiFieldSetter toHapiConverter(BaseRuntimeElementDefinition... elementDefinitions) {
    BaseRuntimeElementDefinition elementDefinition = elementDefinitions[0];
    // explicitly from the root
    if (elementDefinition instanceof RuntimeElemContainedResourceList) {
        return NOOP_FIELD_SETTER;
    }
    if (!(elementDefinition instanceof BaseRuntimeElementCompositeDefinition)) {
        throw new IllegalArgumentException("Composite converter must be given a " + "single composite element, received: " + elementDefinition.getName());
    }
    BaseRuntimeElementCompositeDefinition compositeDefinition = (BaseRuntimeElementCompositeDefinition) elementDefinition;
    List<StructureField<HapiFieldSetter>> toHapiChildren = children.stream().map(child -> {
        HapiFieldSetter childConverter;
        if ("contained".equals(child.propertyName())) {
            // Handle contained resources.
            HapiFieldSetter containedFieldSetter = NOOP_FIELD_SETTER;
            if (elementDefinitions.length > 1) {
                BaseRuntimeElementDefinition containedDefinition = compositeDefinition.getChildByName("contained").getChildByName("contained");
                BaseRuntimeElementDefinition[] containedDefinitions = new BaseRuntimeElementDefinition[elementDefinitions.length];
                containedDefinitions[0] = containedDefinition;
                System.arraycopy(elementDefinitions, 1, containedDefinitions, 1, containedDefinitions.length - 1);
                containedFieldSetter = child.result().toHapiConverter(containedDefinitions);
            }
            return new StructureField<>("contained", "contained", null, false, false, containedFieldSetter);
        } else if (child.extensionUrl() != null) {
            // Handle extensions.
            BaseRuntimeChildDefinition childDefinition = compositeDefinition.getChildByName("extension");
            childConverter = child.result().toHapiConverter(childDefinition.getChildByName("extension"));
        } else {
            String propertyName = child.propertyName();
            // Append the [x] suffix for choice properties.
            if (child.isChoice()) {
                propertyName = propertyName + "[x]";
            }
            BaseRuntimeChildDefinition childDefinition = compositeDefinition.getChildByName(propertyName);
            BaseRuntimeElementDefinition[] childElementDefinitions;
            if (child.isChoice()) {
                int childCount = childDefinition.getValidChildNames().size();
                childElementDefinitions = new BaseRuntimeElementDefinition[childCount];
                int index = 0;
                for (String childName : childDefinition.getValidChildNames()) {
                    childDefinition.getChildByName(childName);
                    childElementDefinitions[index++] = childDefinition.getChildByName(childName);
                }
            } else {
                childElementDefinitions = new BaseRuntimeElementDefinition[] { childDefinition.getChildByName(propertyName) };
            }
            childConverter = child.result().toHapiConverter(childElementDefinitions);
        }
        return new StructureField<>(child.propertyName(), child.fieldName(), child.extensionUrl(), child.isModifier(), child.isChoice(), childConverter);
    }).collect(Collectors.toList());
    return new CompositeFieldSetter(compositeDefinition, toHapiChildren);
}
Also used : IBaseHasExtensions(org.hl7.fhir.instance.model.api.IBaseHasExtensions) IBaseHasModifierExtensions(org.hl7.fhir.instance.model.api.IBaseHasModifierExtensions) Iterator(java.util.Iterator) BaseRuntimeElementDefinition(ca.uhn.fhir.context.BaseRuntimeElementDefinition) IBase(org.hl7.fhir.instance.model.api.IBase) RuntimeElemContainedResourceList(ca.uhn.fhir.context.RuntimeElemContainedResourceList) IBaseExtension(org.hl7.fhir.instance.model.api.IBaseExtension) Collectors(java.util.stream.Collectors) IAnyResource(org.hl7.fhir.instance.model.api.IAnyResource) List(java.util.List) BaseRuntimeChildDefinition(ca.uhn.fhir.context.BaseRuntimeChildDefinition) Map(java.util.Map) BaseRuntimeElementCompositeDefinition(ca.uhn.fhir.context.BaseRuntimeElementCompositeDefinition) BaseRuntimeElementDefinition(ca.uhn.fhir.context.BaseRuntimeElementDefinition) RuntimeElemContainedResourceList(ca.uhn.fhir.context.RuntimeElemContainedResourceList) BaseRuntimeElementCompositeDefinition(ca.uhn.fhir.context.BaseRuntimeElementCompositeDefinition) BaseRuntimeChildDefinition(ca.uhn.fhir.context.BaseRuntimeChildDefinition)

Example 4 with BaseRuntimeChildDefinition

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

the class FindRecursiveTypesApp method traverseDefinition.

private void traverseDefinition(final BaseRuntimeElementDefinition<?> definition, final String name, final int level) {
    final String thisType = definition.getName();
    if (path.containsKey(thisType)) {
        final String parentPath = path.get(thisType);
        final boolean isIndirect = name.substring(parentPath.length() + 1).indexOf('.') > 0;
        recursive.add(definition.getName() + ": " + parentPath + "-> " + name + (isIndirect ? " (indirect)" : ""));
    } else {
        if (!("Reference".equals(definition.getName()) || "Extension".equals(definition.getName()))) {
            path.put(thisType, name);
            final List<BaseRuntimeChildDefinition> children = definition.getChildren();
            // for each child
            for (final BaseRuntimeChildDefinition child : children) {
                for (final String validChildName : child.getValidChildNames()) {
                    if (!validChildName.equals("modifierExtension")) {
                        traverseDefinition(child.getChildByName(validChildName), name.isBlank() ? validChildName : name + "." + validChildName, level + 1);
                    }
                }
            }
            path.remove(thisType);
        }
    }
}
Also used : BaseRuntimeChildDefinition(ca.uhn.fhir.context.BaseRuntimeChildDefinition)

Aggregations

BaseRuntimeChildDefinition (ca.uhn.fhir.context.BaseRuntimeChildDefinition)4 BaseRuntimeElementCompositeDefinition (ca.uhn.fhir.context.BaseRuntimeElementCompositeDefinition)2 InvalidUserInputError (au.csiro.pathling.errors.InvalidUserInputError)1 ResourcePath (au.csiro.pathling.fhirpath.ResourcePath)1 ElementDefinition (au.csiro.pathling.fhirpath.element.ElementDefinition)1 ElementPath (au.csiro.pathling.fhirpath.element.ElementPath)1 ParserContext (au.csiro.pathling.fhirpath.parser.ParserContext)1 ElementPathBuilder (au.csiro.pathling.test.builders.ElementPathBuilder)1 ParserContextBuilder (au.csiro.pathling.test.builders.ParserContextBuilder)1 ResourcePathBuilder (au.csiro.pathling.test.builders.ResourcePathBuilder)1 BaseRuntimeElementDefinition (ca.uhn.fhir.context.BaseRuntimeElementDefinition)1 RuntimeElemContainedResourceList (ca.uhn.fhir.context.RuntimeElemContainedResourceList)1 Iterator (java.util.Iterator)1 List (java.util.List)1 Map (java.util.Map)1 Collectors (java.util.stream.Collectors)1 Nonnull (javax.annotation.Nonnull)1 IAnyResource (org.hl7.fhir.instance.model.api.IAnyResource)1 IBase (org.hl7.fhir.instance.model.api.IBase)1 IBaseExtension (org.hl7.fhir.instance.model.api.IBaseExtension)1