org.hl7.fhir.instance.model.api.IBase Java Examples

The following examples show how to use org.hl7.fhir.instance.model.api.IBase. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: MeasureOperationsProvider.java    From cqf-ruler with Apache License 2.0 6 votes vote down vote up
private void resolveReferences(Resource resource, Parameters parameters, Map<String, Resource> resourceMap) {
    List<IBase> values;
    for (BaseRuntimeChildDefinition child : this.measureResourceProvider.getContext().getResourceDefinition(resource).getChildren()) {
        values = child.getAccessor().getValues(resource);
        if (values == null || values.isEmpty()) {
            continue;
        }

        else if (values.get(0) instanceof Reference
                && ((Reference) values.get(0)).getReferenceElement().hasResourceType()
                && ((Reference) values.get(0)).getReferenceElement().hasIdPart()) {
            Resource fetchedResource = (Resource) registry
                    .getResourceDao(((Reference) values.get(0)).getReferenceElement().getResourceType())
                    .read(new IdType(((Reference) values.get(0)).getReferenceElement().getIdPart()));

            if (!resourceMap.containsKey(fetchedResource.getIdElement().getValue())) {
                parameters.addParameter(new Parameters.ParametersParameterComponent().setName("resource")
                        .setResource(fetchedResource));

                resourceMap.put(fetchedResource.getIdElement().getValue(), fetchedResource);
            }
        }
    }
}
 
Example #2
Source File: MeasureOperationsProvider.java    From cqf-ruler with Apache License 2.0 6 votes vote down vote up
private void resolveReferences(Resource resource, Parameters parameters, Map<String, Resource> resourceMap) {
    List<IBase> values;
    for (BaseRuntimeChildDefinition child : this.measureResourceProvider.getContext().getResourceDefinition(resource).getChildren()) {
        values = child.getAccessor().getValues(resource);
        if (values == null || values.isEmpty()) {
            continue;
        }

        else if (values.get(0) instanceof Reference
                && ((Reference) values.get(0)).getReferenceElement().hasResourceType()
                && ((Reference) values.get(0)).getReferenceElement().hasIdPart()) {
            Resource fetchedResource = (Resource) registry
                    .getResourceDao(((Reference) values.get(0)).getReferenceElement().getResourceType())
                    .read(new IdType(((Reference) values.get(0)).getReferenceElement().getIdPart()));

            if (!resourceMap.containsKey(fetchedResource.getIdElement().getValue())) {
                parameters.addParameter(new Parameters.ParametersParameterComponent().setName("resource")
                        .setResource(fetchedResource));

                resourceMap.put(fetchedResource.getIdElement().getValue(), fetchedResource);
            }
        }
    }
}
 
Example #3
Source File: Stu3FhirConversionSupport.java    From bunsen with Apache License 2.0 6 votes vote down vote up
@Override
public Map<String, List> compositeValues(IBase composite) {

  List<Property> children = ((Base) composite).children();

  if (children == null) {

    return null;
  } else {

    // Some FHIR resources produce duplicate properties in the children,
    // so just use the first when converting to a map.
    return children.stream()
        .filter(property -> property.hasValues())
        .collect(
            Collectors.toMap(Property::getName,
                property -> property.getValues(),
                (first, second) -> first));
  }
}
 
Example #4
Source File: FhirModelResolver.java    From cql_engine with Apache License 2.0 6 votes vote down vote up
public Object getContextPath(String contextType, String targetType) {
    if (targetType == null || contextType == null ) {
        return null;
    }

    if (contextType != null && !(contextType.equals("Unspecified") || contextType.equals("Population"))) {
        if (targetType != null && contextType.equals(targetType)) {
            return "id";
        }

        RuntimeResourceDefinition resourceDefinition = this.fhirContext.getResourceDefinition(targetType);
        Object theValue = this.createInstance(contextType);
        Class<? extends IBase> type = (Class<? extends IBase>)theValue.getClass();

        List<BaseRuntimeChildDefinition> children = resourceDefinition.getChildren();
        for (BaseRuntimeChildDefinition child : children) {

            String path = this.innerGetContextPath(child, type);
            if (path != null) {
                return path;
            }
        }
    }

    return null;
}
 
Example #5
Source File: HapiChoiceConverter.java    From bunsen with Apache License 2.0 6 votes vote down vote up
@Override
public void setField(IBase parentObject,
    BaseRuntimeChildDefinition fieldToSet,
    Object composite) {

  Iterator<Entry<String, HapiFieldSetter>> setterIterator =
      choiceFieldSetters.entrySet().iterator();

  // Co-iterate with an index so we place the correct values into the corresponding locations.
  for (int valueIndex = 0; valueIndex < choiceTypes.size(); ++valueIndex) {

    Map.Entry<String, HapiFieldSetter> setterEntry = setterIterator.next();

    if (getChild(composite, valueIndex) != null) {

      HapiFieldSetter setter = setterEntry.getValue();

      setter.setField(parentObject, fieldToSet, getChild(composite, valueIndex));

      // We set a non-null field for the choice type, so stop looking.
      break;
    }
  }
}
 
Example #6
Source File: FHIRPathEvaluatorBenchmark.java    From FHIR with Apache License 2.0 5 votes vote down vote up
public static void testRun(String exampleName, String expression) throws Exception {
    String specExample = BenchmarkUtil.getSpecExample(Format.JSON, exampleName);

    FhirContext context = FhirContext.forR4();
    IBaseResource baseResource = context.newJsonParser().parseResource(new StringReader(specExample));
    IFluentPath fluentPath = context.newFluentPath();
    System.out.println(fluentPath.evaluate(baseResource, expression, IBase.class));

    Resource resource = FHIRParser.parser(Format.JSON).parse(new StringReader(specExample));
    FHIRPathEvaluator evaluator = FHIRPathEvaluator.evaluator();
    EvaluationContext evaluationContext = new EvaluationContext(resource);
    System.out.println(evaluator.evaluate(evaluationContext, expression, singleton(evaluationContext.getTree().getRoot())));
}
 
Example #7
Source File: FhirModelResolver.java    From cql_engine with Apache License 2.0 5 votes vote down vote up
protected BaseRuntimeElementCompositeDefinition<?> resolveRuntimeDefinition(IBase base) {
    if (base instanceof IAnyResource) {
        return getFhirContext().getResourceDefinition((IAnyResource) base);
    }

    else if (base instanceof IBaseBackboneElement || base instanceof IBaseElement) {
        return (BaseRuntimeElementCompositeDefinition<?>) getFhirContext().getElementDefinition(base.getClass());
    }

    else if (base instanceof ICompositeType) {
        return (BaseRuntimeElementCompositeDefinition<ICompositeType>) getFhirContext().getElementDefinition(base.getClass());
    }

    throw new UnknownType(String.format("Unable to resolve the runtime definition for %s", base.getClass().getName()));
}
 
Example #8
Source File: DefinitionToAvroVisitor.java    From bunsen with Apache License 2.0 5 votes vote down vote up
@Override
public void setField(IBase parentObject,
    BaseRuntimeChildDefinition fieldToSet,
    Object element) {

  for (Object value: (Iterable) element) {

    Object hapiObject = elementToHapiConverter.toHapi(value);

    fieldToSet.getMutator().addValue(parentObject, (IBase) hapiObject);
  }
}
 
Example #9
Source File: HapiContainedConverter.java    From bunsen with Apache License 2.0 5 votes vote down vote up
@Override
public void setField(IBase parentObject,
    BaseRuntimeChildDefinition fieldToSet,
    Object object) {

  List<ContainerEntry> containedEntries = getContained(object);

  for (ContainerEntry containedEntry: containedEntries) {

    String containedElementType = containedEntry.getElementType();
    IBase resource = contained.get(containedElementType).toHapi(containedEntry.getElement());

    fieldToSet.getMutator().addValue(parentObject, resource);
  }
}
 
Example #10
Source File: PrimitiveConverter.java    From bunsen with Apache License 2.0 5 votes vote down vote up
@Override
public void setField(IBase parentObject,
    BaseRuntimeChildDefinition fieldToSet,
    Object object) {

  IPrimitiveType element = (IPrimitiveType) elementDefinition
      .newInstance(fieldToSet.getInstanceConstructorArguments());

  PrimitiveConverter.this.toHapi(object, element);

  fieldToSet.getMutator().setValue(parentObject, element);
}
 
Example #11
Source File: HapiCompositeConverter.java    From bunsen with Apache License 2.0 5 votes vote down vote up
@Override
public void setField(IBase parentObject,
    BaseRuntimeChildDefinition fieldToSet,
    Object sourceObject) {

  IBase fhirObject = toHapi(sourceObject);

  if (extensionUrl != null) {

    fieldToSet.getMutator().addValue(parentObject, fhirObject);

  } else {
    fieldToSet.getMutator().setValue(parentObject, fhirObject);
  }
}
 
Example #12
Source File: DefinitionToSparkVisitor.java    From bunsen with Apache License 2.0 4 votes vote down vote up
@Override
public IBase toHapi(Object input) {
  return null;
}
 
Example #13
Source File: FHIRPathEvaluatorBenchmark.java    From FHIR with Apache License 2.0 4 votes vote down vote up
@Benchmark
public void benchmarkHAPIEvaluator(FHIRPathEvaluatorState state) throws Exception {
    state.fluentPath.evaluate(state.baseResource, FHIRPathEvaluatorState.EXPRESSION, IBase.class);
}
 
Example #14
Source File: Stu3FhirConversionSupport.java    From bunsen with Apache License 2.0 4 votes vote down vote up
@Override
public String fhirType(IBase base) {

  return ((Base) base).fhirType();
}
 
Example #15
Source File: HapiCompositeConverter.java    From bunsen with Apache License 2.0 4 votes vote down vote up
@Override
public void setField(IBase parentObject, BaseRuntimeChildDefinition fieldToSet,
    Object sourceObject) {}
 
Example #16
Source File: FhirModelResolver.java    From cql_engine with Apache License 2.0 4 votes vote down vote up
protected Object resolveProperty(Object target, String path) {
    if (target == null) {
        return null;
    }

    if (target instanceof IBaseEnumeration && path.equals("value")) {
        return ((IBaseEnumeration) target).getValueAsString();
    }


    // TODO: Consider using getResourceType everywhere?
    if (target instanceof IAnyResource && this.getResourceType((ResourceType) target).equals(path)) {
        return target;
    }

    IBase base = (IBase) target;
    BaseRuntimeElementCompositeDefinition<?> definition;
    if (base instanceof IPrimitiveType) {
        return toJavaPrimitive(path.equals("value") ? ((IPrimitiveType<?>) target).getValue() : target, base);
    }
    else {
        definition = resolveRuntimeDefinition(base);
    }

    BaseRuntimeChildDefinition child = definition.getChildByName(path);
    if (child == null) {
        child = resolveChoiceProperty(definition, path);
    }

    if (child == null) {
        return null;
    }

    List<IBase> values = child.getAccessor().getValues(base);

    if (values == null || values.isEmpty()) {
        return null;
    }

    if (child instanceof RuntimeChildChoiceDefinition && !child.getElementName().equalsIgnoreCase(path)) {
        if (!values.get(0).getClass().getSimpleName().equalsIgnoreCase(child.getChildByName(path).getImplementingClass().getSimpleName()))
        {
            return null;
        }
    }

    return toJavaPrimitive(child.getMax() < 1 ? values : values.get(0), base);
}
 
Example #17
Source File: FhirModelResolver.java    From cql_engine with Apache License 2.0 4 votes vote down vote up
@Override
public void setValue(Object target, String path, Object value) {
    if (target == null) {
        return;
    }

    if (target instanceof IBaseEnumeration && path.equals("value")) {
        ((IBaseEnumeration)target).setValueAsString((String)value);
        return;
    }

    IBase base = (IBase) target;
    BaseRuntimeElementCompositeDefinition<?> definition;
    if (base instanceof IPrimitiveType) {
        ((IPrimitiveType) target).setValue(fromJavaPrimitive(value, base));
        return;
    }
    else {
        definition = resolveRuntimeDefinition(base);
    }

    BaseRuntimeChildDefinition child = definition.getChildByName(path);
    if (child == null) {
        child = resolveChoiceProperty(definition, path);
    }

    if (child == null) {
        throw new DataProviderException(String.format("Unable to resolve path %s.", path));
    }

    try {
        if (value instanceof Iterable) {
            for (Object val : (Iterable<?>) value) {
                child.getMutator().addValue(base, (IBase) fromJavaPrimitive(val, base));
            }
        }
        else {
            child.getMutator().setValue(base, (IBase) fromJavaPrimitive(value, base));
        }
    } catch (IllegalArgumentException le) {
        if (value.getClass().getSimpleName().equals("Quantity")) {
            try {
                value = this.castToSimpleQuantity((BaseType) value);
            } catch (FHIRException e) {
                throw new InvalidCast("Unable to cast Quantity to SimpleQuantity");
            }
            child.getMutator().setValue(base, (IBase) fromJavaPrimitive(value, base));
        }
        else {
            throw new DataProviderException(String.format("Configuration error encountered: %s", le.getMessage()));
        }
    }
}
 
Example #18
Source File: HapiCompositeConverter.java    From bunsen with Apache License 2.0 4 votes vote down vote up
@Override
public IBase toHapi(Object input) {
  return null;
}
 
Example #19
Source File: NoOpConverter.java    From bunsen with Apache License 2.0 4 votes vote down vote up
@Override
public IBase toHapi(Object input) {
  return null;
}
 
Example #20
Source File: DefinitionToAvroVisitor.java    From bunsen with Apache License 2.0 4 votes vote down vote up
@Override
public IBase toHapi(Object input) {
  return null;
}
 
Example #21
Source File: DefinitionToAvroVisitor.java    From bunsen with Apache License 2.0 4 votes vote down vote up
@Override
public void setField(IBase parentObject, BaseRuntimeChildDefinition fieldToSet,
    Object sparkObject) {

}
 
Example #22
Source File: DefinitionToSparkVisitor.java    From bunsen with Apache License 2.0 4 votes vote down vote up
@Override
public void setField(IBase parentObject, BaseRuntimeChildDefinition fieldToSet,
    Object sparkObject) {}
 
Example #23
Source File: StringToHapiSetter.java    From bunsen with Apache License 2.0 4 votes vote down vote up
@Override
public void setField(IBase parentObject,
    BaseRuntimeChildDefinition fieldToSet,
    Object sparkObject) {
  fieldToSet.getMutator().setValue(parentObject, toHapi(sparkObject));
}
 
Example #24
Source File: LeafExtensionConverter.java    From bunsen with Apache License 2.0 3 votes vote down vote up
/**
 * Converts an object from a different data model to a HAPI object.
 *
 * @param input the object to convert
 * @return the HAPI equivalent.
 */
@Override
public IBase toHapi(Object input) {

  IBase hapiObject = valuetoHapiConverter.toHapi(input);

  IBaseExtension extension = (IBaseExtension) elementDefinition.newInstance(extensionUrl);

  extension.setValue((IBaseDatatype) hapiObject);

  return extension;
}
 
Example #25
Source File: HapiChoiceConverter.java    From bunsen with Apache License 2.0 3 votes vote down vote up
@Override
public Object fromHapi(Object input) {

  String fhirType = fhirSupport.fhirType((IBase) input);

  Object[] values = new Object[choiceTypes.size()];

  Iterator<Map.Entry<String, HapiConverter<T>>> schemaIterator =
      choiceTypes.entrySet().iterator();

  // Co-iterate with an index so we place the correct values into the corresponding locations.
  for (int valueIndex = 0; valueIndex < choiceTypes.size(); ++valueIndex) {

    Map.Entry<String, HapiConverter<T>> choiceEntry = schemaIterator.next();

    // Set the nested field that matches the choice type.
    if (choiceEntry.getKey().equals(fhirType)) {

      HapiConverter converter = choiceEntry.getValue();

      values[valueIndex] = converter.fromHapi(input);
    }

  }

  return createComposite(values);
}
 
Example #26
Source File: StringToHapiSetter.java    From bunsen with Apache License 2.0 3 votes vote down vote up
@Override
public IBase toHapi(Object sparkObject) {

  IPrimitiveType element = (IPrimitiveType) elementDefinition.newInstance();

  element.setValueAsString((String) sparkObject);

  return element;
}
 
Example #27
Source File: PrimitiveConverter.java    From bunsen with Apache License 2.0 3 votes vote down vote up
@Override
public IBase toHapi(Object input) {

  IPrimitiveType element = (IPrimitiveType) elementDefinition.newInstance();

  PrimitiveConverter.this.toHapi(input, element);

  return element;
}
 
Example #28
Source File: LeafExtensionConverter.java    From bunsen with Apache License 2.0 3 votes vote down vote up
@Override
public void setField(IBase parentObject,
    BaseRuntimeChildDefinition fieldToSet,
    Object sparkObject) {

  IBase hapiObject = valuetoHapiConverter.toHapi(sparkObject);

  IBaseExtension extension = (IBaseExtension) elementDefinition.newInstance(extensionUrl);

  extension.setValue((IBaseDatatype) hapiObject);

  fieldToSet.getMutator().addValue(parentObject, extension);
}
 
Example #29
Source File: HapiConverter.java    From bunsen with Apache License 2.0 2 votes vote down vote up
/**
 * Converts an object from a different data model to a HAPI object.
 *
 * @param input the object to convert
 * @return the HAPI equivalent.
 */
IBase toHapi(Object input);
 
Example #30
Source File: NoOpConverter.java    From bunsen with Apache License 2.0 2 votes vote down vote up
@Override
public void setField(IBase parentObject, BaseRuntimeChildDefinition fieldToSet, Object value) {

}