Java Code Examples for org.hl7.fhir.dstu3.model.ElementDefinition#getPath()

The following examples show how to use org.hl7.fhir.dstu3.model.ElementDefinition#getPath() . 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: ShExGenerator.java    From org.hl7.fhir.core with Apache License 2.0 6 votes vote down vote up
/**
 * Generate a definition for a referenced element
 * @param sd Containing structure definition
 * @param ed Inner element
 * @return ShEx representation of element reference
 */
private String genInnerTypeDef(StructureDefinition sd, ElementDefinition ed) {
  String path = ed.hasBase() ? ed.getBase().getPath() : ed.getPath();;
  ST element_reference = tmplt(SHAPE_DEFINITION_TEMPLATE);
  element_reference.add("resourceDecl", "");  // Not a resource
  element_reference.add("id", path);
  String comment = ed.getShort();
  element_reference.add("comment", comment == null? " " : "# " + comment);

  List<String> elements = new ArrayList<String>();
  for (ElementDefinition child: ProfileUtilities.getChildList(sd, path, null))
    elements.add(genElementDefinition(sd, child));

  element_reference.add("elements", StringUtils.join(elements, "\n"));
  return element_reference.render();
}
 
Example 2
Source File: ProfileUtilities.java    From org.hl7.fhir.core with Apache License 2.0 6 votes vote down vote up
public static List<ElementDefinition> getSliceList(StructureDefinition profile, ElementDefinition element) throws DefinitionException {
  if (!element.hasSlicing())
    throw new Error("getSliceList should only be called when the element has slicing");

  List<ElementDefinition> res = new ArrayList<ElementDefinition>();
  List<ElementDefinition> elements = profile.getSnapshot().getElement();
  String path = element.getPath();
  for (int index = elements.indexOf(element) + 1; index < elements.size(); index++) {
    ElementDefinition e = elements.get(index);
    if (e.getPath().startsWith(path + ".") || e.getPath().equals(path)) {
      // We want elements with the same path (until we hit an element that doesn't start with the same path)
      if (e.getPath().equals(element.getPath()))
        res.add(e);
    } else
      break;
  }
  return res;
}
 
Example 3
Source File: ProfileUtilities.java    From org.hl7.fhir.core with Apache License 2.0 6 votes vote down vote up
private void determineSlicing(ElementDefinition slicer, List<ElementDefinition> slices) {
  // first, name them
  int i = 0;
  for (ElementDefinition ed : slices) {
    if (ed.hasUserData("slice-name")) {
      ed.setSliceName(ed.getUserString("slice-name"));
    } else {
      i++;
      ed.setSliceName("slice-"+Integer.toString(i));
    }
  }
  // now, the hard bit, how are they differentiated? 
  // right now, we hard code this...
  if (slicer.getPath().endsWith(".extension") || slicer.getPath().endsWith(".modifierExtension"))
    slicer.getSlicing().addDiscriminator().setType(DiscriminatorType.VALUE).setPath("url");
  else if (slicer.getPath().equals("DiagnosticReport.result"))
    slicer.getSlicing().addDiscriminator().setType(DiscriminatorType.VALUE).setPath("reference.code");
  else if (slicer.getPath().equals("Observation.related"))
    slicer.getSlicing().addDiscriminator().setType(DiscriminatorType.VALUE).setPath("target.reference.code");
  else if (slicer.getPath().equals("Bundle.entry"))
    slicer.getSlicing().addDiscriminator().setType(DiscriminatorType.VALUE).setPath("resource.@profile");
  else  
    throw new Error("No slicing for "+slicer.getPath()); 
}
 
Example 4
Source File: ProfileUtilities.java    From org.hl7.fhir.core with Apache License 2.0 6 votes vote down vote up
private org.hl7.fhir.dstu3.elementmodel.Element generateExample(StructureDefinition profile, ExampleValueAccessor accessor) throws FHIRException {
  ElementDefinition ed = profile.getSnapshot().getElementFirstRep();
  org.hl7.fhir.dstu3.elementmodel.Element r = new org.hl7.fhir.dstu3.elementmodel.Element(ed.getPath(), new Property(context, ed, profile));
  List<ElementDefinition> children = getChildMap(profile, ed);
  for (ElementDefinition child : children) {
    if (child.getPath().endsWith(".id")) {
      org.hl7.fhir.dstu3.elementmodel.Element id = new org.hl7.fhir.dstu3.elementmodel.Element("id", new Property(context, child, profile));
      id.setValue(profile.getId()+accessor.getId());
      r.getChildren().add(id);
    } else { 
      org.hl7.fhir.dstu3.elementmodel.Element e = createExampleElement(profile, child, accessor);
      if (e != null)
        r.getChildren().add(e);
    }
  }
  return r;
}
 
Example 5
Source File: ProfileUtilities.java    From org.hl7.fhir.core with Apache License 2.0 6 votes vote down vote up
private boolean hasInnerDiffMatches(StructureDefinitionDifferentialComponent context, String path, int start, int end, List<ElementDefinition> base) throws DefinitionException {
  for (int i = start; i <= end; i++) {
    String statedPath = context.getElement().get(i).getPath();
    if (statedPath.startsWith(path+".") && !statedPath.substring(path.length()+1).contains(".")) {
      boolean found = false;
      for (ElementDefinition ed : base) {
        String ep = ed.getPath();
        if (ep.equals(statedPath) || (ep.endsWith("[x]") && statedPath.length() > ep.length() - 2 && statedPath.substring(0, ep.length()-3).equals(ep.substring(0, ep.length()-3)) && !statedPath.substring(ep.length()).contains(".")))
          found = true;
      }
      if (!found)
        return true;
    }
  }
  return false;
}
 
Example 6
Source File: ProfileUtilities.java    From org.hl7.fhir.core with Apache License 2.0 6 votes vote down vote up
private void closeChildren(StructureDefinition base, ElementDefinition edb, StructureDefinition derived, ElementDefinition edm) {
  String path = edb.getPath()+".";
  int baseStart = base.getSnapshot().getElement().indexOf(edb);
  int baseEnd = findEnd(base.getSnapshot().getElement(), edb, baseStart+1);
  int diffStart = derived.getDifferential().getElement().indexOf(edm);
  int diffEnd = findEnd(derived.getDifferential().getElement(), edm, diffStart+1);
  
  for (int cBase = baseStart; cBase < baseEnd; cBase++) {
    ElementDefinition edBase = base.getSnapshot().getElement().get(cBase);
    if (isImmediateChild(edBase, edb)) {
      ElementDefinition edMatch = getMatchInDerived(edBase, derived.getDifferential().getElement(), diffStart, diffEnd);
      if (edMatch == null) {
        ElementDefinition edd = derived.getDifferential().addElement();
        edd.setPath(edBase.getPath());
        edd.setMax("0");
      } else {
        closeChildren(base, edBase, derived, edMatch);
      }        
    }
  }
}
 
Example 7
Source File: Property.java    From org.hl7.fhir.core with Apache License 2.0 5 votes vote down vote up
protected List<Property> getChildProperties(TypeDetails type) throws DefinitionException {
  ElementDefinition ed = definition;
  StructureDefinition sd = structure;
  List<ElementDefinition> children = ProfileUtilities.getChildMap(sd, ed);
  if (children.isEmpty()) {
    // ok, find the right definitions
    String t = null;
    if (ed.getType().size() == 1)
      t = ed.getType().get(0).getCode();
    else if (ed.getType().size() == 0)
      throw new Error("types == 0, and no children found");
    else {
      t = ed.getType().get(0).getCode();
      boolean all = true;
      for (TypeRefComponent tr : ed.getType()) {
        if (!tr.getCode().equals(t)) {
          all = false;
          break;
        }
      }
      if (!all) {
        // ok, it's polymorphic
        t = type.getType();
      }
    }
    if (!"xhtml".equals(t)) {
      sd = context.fetchResource(StructureDefinition.class, t);
      if (sd == null)
        throw new DefinitionException("Unable to find class '"+t+"' for name '"+ed.getPath()+"' on property "+definition.getPath());
      children = ProfileUtilities.getChildMap(sd, sd.getSnapshot().getElement().get(0));
    }
  }
  List<Property> properties = new ArrayList<Property>();
  for (ElementDefinition child : children) {
    properties.add(new Property(context, child, sd));
  }
  return properties;
}
 
Example 8
Source File: ProfileUtilities.java    From org.hl7.fhir.core with Apache License 2.0 5 votes vote down vote up
private static String normalizePath(ElementDefinition e) {
  if (!e.hasPath()) return "";
  String path = e.getPath();
  // if sorting element names make sure onset[x] appears before onsetAge, onsetDate, etc.
  // so strip off the [x] suffix when comparing the path names.
  if (path.endsWith("[x]")) {
    path = path.substring(0, path.length()-3);
  }
  return path;
}
 
Example 9
Source File: ProfileUtilities.java    From org.hl7.fhir.core with Apache License 2.0 5 votes vote down vote up
private boolean isImmediateChild(ElementDefinition candidate, ElementDefinition base) {
  String p = candidate.getPath();
  if (!p.contains("."))
    return false;
  if (!p.startsWith(base.getPath()+"."))
    return false;
  p = p.substring(base.getPath().length()+1);
  return !p.contains(".");
}
 
Example 10
Source File: ProfileUtilities.java    From org.hl7.fhir.core with Apache License 2.0 5 votes vote down vote up
private boolean isImmediateChild(ElementDefinition ed) {
  String p = ed.getPath();
  if (!p.contains("."))
    return false;
  p = p.substring(p.indexOf(".")+1);
  return !p.contains(".");
}
 
Example 11
Source File: ProfileUtilities.java    From org.hl7.fhir.core with Apache License 2.0 5 votes vote down vote up
private List<ElementDefinition> getSiblings(List<ElementDefinition> list, ElementDefinition current) {
  List<ElementDefinition> result = new ArrayList<ElementDefinition>();
  String path = current.getPath();
  int cursor = list.indexOf(current)+1;
  while (cursor < list.size() && list.get(cursor).getPath().length() >= path.length()) {
    if (pathMatches(list.get(cursor).getPath(), path))
      result.add(list.get(cursor));
    cursor++;
  }
  return result;
}
 
Example 12
Source File: ProfileUtilities.java    From org.hl7.fhir.core with Apache License 2.0 5 votes vote down vote up
/**
 * Given a Structure, navigate to the element given by the path and return the direct children of that element
 *
 * @param structure The structure to navigate into
 * @param path The path of the element within the structure to get the children for
 * @return A List containing the element children (all of them are Elements)
 */
public static List<ElementDefinition> getChildList(StructureDefinition profile, String path, String id) {
  List<ElementDefinition> res = new ArrayList<ElementDefinition>();

  boolean capturing = id==null;
  if (id==null && !path.contains("."))
    capturing = true;
  
  for (ElementDefinition e : profile.getSnapshot().getElement()) {
    if (!capturing && id!=null && e.getId().equals(id)) {
      capturing = true;
    }
    
    // If our element is a slice, stop capturing children as soon as we see the next slice
    if (capturing && e.hasId() && id!= null && !e.getId().equals(id) && e.getPath().equals(path))
      break;
    
    if (capturing) {
      String p = e.getPath();

      if (!Utilities.noString(e.getContentReference()) && path.startsWith(p)) {
        if (path.length() > p.length())
          return getChildList(profile, e.getContentReference()+"."+path.substring(p.length()+1), null);
        else
          return getChildList(profile, e.getContentReference(), null);
        
      } else if (p.startsWith(path+".") && !p.equals(path)) {
        String tail = p.substring(path.length()+1);
        if (!tail.contains(".")) {
          res.add(e);
        }
      }
    }
  }

  return res;
}
 
Example 13
Source File: Property.java    From org.hl7.fhir.core with Apache License 2.0 5 votes vote down vote up
public String getType(String elementName) {
   if (!definition.getPath().contains("."))
     return definition.getPath();
   ElementDefinition ed = definition;
   if (definition.hasContentReference()) {
     if (!definition.getContentReference().startsWith("#"))
       throw new Error("not handled yet");
     boolean found = false;
     for (ElementDefinition d : structure.getSnapshot().getElement()) {
       if (d.hasId() && d.getId().equals(definition.getContentReference().substring(1))) {
         found = true;
         ed = d;
       }
     }
     if (!found)
       throw new Error("Unable to resolve "+definition.getContentReference()+" at "+definition.getPath()+" on "+structure.getUrl());
   }
   if (ed.getType().size() == 0)
		return null;
   else if (ed.getType().size() > 1) {
     String t = ed.getType().get(0).getCode();
		boolean all = true;
     for (TypeRefComponent tr : ed.getType()) {
			if (!t.equals(tr.getCode()))
				all = false;
		}
		if (all)
			return t;
     String tail = ed.getPath().substring(ed.getPath().lastIndexOf(".")+1);
     if (tail.endsWith("[x]") && elementName != null && elementName.startsWith(tail.substring(0, tail.length()-3))) {
			String name = elementName.substring(tail.length()-3);
       return isPrimitive(lowFirst(name)) ? lowFirst(name) : name;        
		} else
       throw new Error("logic error, gettype when types > 1, name mismatch for "+elementName+" on at "+ed.getPath());
   } else if (ed.getType().get(0).getCode() == null) {
     return structure.getId();
	} else
     return ed.getType().get(0).getCode();
}
 
Example 14
Source File: ProfileUtilities.java    From org.hl7.fhir.core with Apache License 2.0 4 votes vote down vote up
private int findEnd(List<ElementDefinition> list, ElementDefinition ed, int cursor) {
  String path = ed.getPath()+".";
  while (cursor < list.size() && list.get(cursor).getPath().startsWith(path))
    cursor++;
  return cursor;
}
 
Example 15
Source File: ProfileUtilities.java    From org.hl7.fhir.core with Apache License 2.0 4 votes vote down vote up
public ElementDefinitionHolder(ElementDefinition self) {
  super();
  this.self = self;
  this.name = self.getPath();
  children = new ArrayList<ElementDefinitionHolder>();
}
 
Example 16
Source File: ProfileUtilities.java    From org.hl7.fhir.core with Apache License 2.0 4 votes vote down vote up
private String pathTail(List<ElementDefinition> diffMatches, int i) {
  
  ElementDefinition d = diffMatches.get(i);
  String s = d.getPath().contains(".") ? d.getPath().substring(d.getPath().lastIndexOf(".")+1) : d.getPath();
  return "."+s + (d.hasType() && d.getType().get(0).hasProfile() ? "["+d.getType().get(0).getProfile()+"]" : "");
}
 
Example 17
Source File: ShExGenerator.java    From org.hl7.fhir.core with Apache License 2.0 4 votes vote down vote up
/**
 * Generate a ShEx element definition
 * @param sd Containing structure definition
 * @param ed Containing element definition
 * @return ShEx definition
 */
private String genElementDefinition(StructureDefinition sd, ElementDefinition ed) {
  String id = ed.hasBase() ? ed.getBase().getPath() : ed.getPath();
  String shortId = id.substring(id.lastIndexOf(".") + 1);
  String defn;
  ST element_def;
  String card = ("*".equals(ed.getMax()) ? (ed.getMin() == 0 ? "*" : "+") : (ed.getMin() == 0 ? "?" : "")) + ";";

  if(id.endsWith("[x]")) {
    element_def = ed.getType().size() > 1? tmplt(INNER_SHAPE_TEMPLATE) : tmplt(ELEMENT_TEMPLATE);
    element_def.add("id", "");
  } else {
    element_def = tmplt(ELEMENT_TEMPLATE);
    element_def.add("id", "fhir:" + (id.charAt(0) == id.toLowerCase().charAt(0)? shortId : id) + " ");
  }

  List<ElementDefinition> children = ProfileUtilities.getChildList(sd, ed);
  if (children.size() > 0) {
    innerTypes.add(new ImmutablePair<StructureDefinition, ElementDefinition>(sd, ed));
    defn = simpleElement(sd, ed, id);
  } else if(id.endsWith("[x]")) {
    defn = genChoiceTypes(sd, ed, id);
  }
  else if (ed.getType().size() == 1) {
    // Single entry
    defn = genTypeRef(sd, ed, id, ed.getType().get(0));
  } else if (ed.getContentReference() != null) {
    // Reference to another element
    String ref = ed.getContentReference();
    if(!ref.startsWith("#"))
      throw new AssertionError("Not equipped to deal with absolute path references: " + ref);
    String refPath = null;
    for(ElementDefinition ed1: sd.getSnapshot().getElement()) {
      if(ed1.getId() != null && ed1.getId().equals(ref.substring(1))) {
        refPath = ed1.getPath();
        break;
      }
    }
    if(refPath == null)
      throw new AssertionError("Reference path not found: " + ref);
    // String typ = id.substring(0, id.indexOf(".") + 1) + ed.getContentReference().substring(1);
    defn = simpleElement(sd, ed, refPath);
  } else if(id.endsWith("[x]")) {
    defn = genChoiceTypes(sd, ed, id);
  } else {
    // TODO: Refactoring required here
    element_def = genAlternativeTypes(ed, id, shortId);
    element_def.add("id", id.charAt(0) == id.toLowerCase().charAt(0)? shortId : id);
    element_def.add("card", card);
    addComment(element_def, ed);
    return element_def.render();
  }
  element_def.add("defn", defn);
  element_def.add("card", card);
  addComment(element_def, ed);
  return element_def.render();
}
 
Example 18
Source File: QuestionnaireBuilder.java    From org.hl7.fhir.core with Apache License 2.0 4 votes vote down vote up
private void processDataType(StructureDefinition profile, QuestionnaireItemComponent group, ElementDefinition element, String path, TypeRefComponent t, List<QuestionnaireResponse.QuestionnaireResponseItemComponent> answerGroups) throws FHIRException {
  if (t.getCode().equals("code"))
    addCodeQuestions(group, element, path, answerGroups);
  else if (t.getCode().equals("string") || t.getCode().equals("id") || t.getCode().equals("oid") || t.getCode().equals("markdown"))
    addStringQuestions(group, element, path, answerGroups);
  else if (t.getCode().equals("uri"))
    addUriQuestions(group, element, path, answerGroups);
  else if (t.getCode().equals("boolean"))
    addBooleanQuestions(group, element, path, answerGroups);
  else if (t.getCode().equals("decimal"))
    addDecimalQuestions(group, element, path, answerGroups);
  else if (t.getCode().equals("dateTime") || t.getCode().equals("date"))
      addDateTimeQuestions(group, element, path, answerGroups);
  else if (t.getCode().equals("instant"))
    addInstantQuestions(group, element, path, answerGroups);
  else if (t.getCode().equals("time"))
    addTimeQuestions(group, element, path, answerGroups);
  else if (t.getCode().equals("CodeableConcept"))
    addCodeableConceptQuestions(group, element, path, answerGroups);
  else if (t.getCode().equals("Period"))
    addPeriodQuestions(group, element, path, answerGroups);
  else if (t.getCode().equals("Ratio"))
    addRatioQuestions(group, element, path, answerGroups);
  else if (t.getCode().equals("HumanName"))
    addHumanNameQuestions(group, element, path, answerGroups);
  else if (t.getCode().equals("Address"))
    addAddressQuestions(group, element, path, answerGroups);
  else if (t.getCode().equals("ContactPoint"))
    addContactPointQuestions(group, element, path, answerGroups);
  else if (t.getCode().equals("Identifier"))
    addIdentifierQuestions(group, element, path, answerGroups);
  else if (t.getCode().equals("integer") || t.getCode().equals("positiveInt") || t.getCode().equals("unsignedInt") )
    addIntegerQuestions(group, element, path, answerGroups);
  else if (t.getCode().equals("Coding"))
    addCodingQuestions(group, element, path, answerGroups);
  else if (Utilities.existsInList(t.getCode(), "Quantity", "Count", "Age", "Duration", "Distance", "Money"))
    addQuantityQuestions(group, element, path, answerGroups);
  else if (t.getCode().equals("Money"))
    addMoneyQuestions(group, element, path, answerGroups);
  else if (t.getCode().equals("Reference"))
    addReferenceQuestions(group, element, path, t.hasTargetProfile() ? t.getTargetProfile() : null, answerGroups);
  else if (t.getCode().equals("Duration"))
    addDurationQuestions(group, element, path, answerGroups);
  else if (t.getCode().equals("base64Binary"))
    addBinaryQuestions(group, element, path, answerGroups);
  else if (t.getCode().equals("Attachment"))
    addAttachmentQuestions(group, element, path, answerGroups);
  else if (t.getCode().equals("Age"))
    addAgeQuestions(group, element, path, answerGroups);
  else if (t.getCode().equals("Range"))
    addRangeQuestions(group, element, path, answerGroups);
  else if (t.getCode().equals("Timing"))
    addTimingQuestions(group, element, path, answerGroups);
  else if (t.getCode().equals("Annotation"))
    addAnnotationQuestions(group, element, path, answerGroups);
  else if (t.getCode().equals("SampledData"))
    addSampledDataQuestions(group, element, path, answerGroups);
  else if (t.getCode().equals("Extension")) {
    if (t.hasProfile())
      addExtensionQuestions(profile, group, element, path, t.getProfile(), answerGroups);
  } else if (t.getCode().equals("SampledData"))
    addSampledDataQuestions(group, element, path, answerGroups);
  else if (!t.getCode().equals("Narrative") && !t.getCode().equals("Resource") && !t.getCode().equals("Meta") && !t.getCode().equals("Signature"))
    throw new NotImplementedException("Unhandled Data Type: "+t.getCode()+" on element "+element.getPath());
}
 
Example 19
Source File: Stu3StructureDefinitions.java    From bunsen with Apache License 2.0 3 votes vote down vote up
private <T> List<StructureField<T>> transformChildren(DefinitionVisitor<T> visitor,
    StructureDefinition rootDefinition,
    List<ElementDefinition> definitions,
    Deque<QualifiedPath> stack,
    ElementDefinition element) {

  QualifiedPath qualifiedPath = new QualifiedPath(rootDefinition.getUrl(),  element.getPath());

  if (shouldTerminateRecursive(visitor, qualifiedPath, stack)) {

    return Collections.emptyList();

  } else {
    stack.push(qualifiedPath);

    // Handle composite type
    List<StructureField<T>> childElements = new ArrayList<>();

    for (ElementDefinition child: getChildren(element, definitions)) {

      List<StructureField<T>> childFields = elementToFields(visitor, rootDefinition,
          child, definitions, stack);

      childElements.addAll(childFields);
    }

    stack.pop();

    return childElements;
  }
}